API request validation: check inputs from parsing through business rules
Parse API requests on the server, validate their structure and business meaning, check authorization, and only then process them. Browser checks are a convenience for fast feedback; the server must validate every input at trust boundaries. A schema alone does not provide authorization, injection defenses, or every business rule.
Parse API requests on the server, validate their structure and business meaning, check authorization, and only then process them. Browser checks are a convenience for fast feedback; the server must validate every input at trust boundaries. A schema alone does not provide authorization, injection defenses, or every business rule.
Place checks in the request flow
**Bound size and parser resources first.** Limit request size before buffering or parsing, and cap resources such as nesting depth and array items. Oversized input can exhaust resources before schema validation begins.
**Parse safely according to the media type.** Check that the declared and actual formats agree and use a maintained parser. Do not pass malformed JSON/XML or decoding failures to business logic. Decode consistently once as required by the protocol; avoid interpreting the value differently later.
**Validate structure and field rules.** Specify required fields, types, allowed values, string lengths, numeric ranges, array limits, and nested object rules. In JSON Schema, declaring `properties` does not make them required: use `required`, and decide by contract whether unknown properties are allowed.
**Check relationships and business rules.** Two dates may each be valid while an end date still must follow a start date. Rules about inventory, state transitions, or quotas depend on current state and policy, so distinguish them from a simple type schema.
**Check authorization separately, then process only validated data.** A valid account ID does not prove that the caller may access it. Explicitly bind only fields the request is allowed to change; do not persist an object that was only partly validated.
For example, a booking API might parse start and end dates, check their ranges, and require the end date to follow the start. Passing those checks still does not prove the user may book the resource or that it is available at that moment; those are separate authorization and state checks. This example illustrates the process and is not a tested API.
Define rules for each field
| Input | Example contract rules |
|---|---|
| Fixed choices | Exact membership in the defined allowed set |
| Numbers and dates | Parseable format, minimum/maximum, time-zone interpretation |
| Strings | Length limits and format appropriate to the field’s meaning |
| Objects | Required/optional fields, missing versus null, unknown-property policy |
| Arrays | Item-count limits and validation of every item and nested object |
Do not try to remove every string that looks malicious with a denylist. Names and free-form text may need legitimate punctuation and Unicode. Define accepted values according to each field’s meaning. Allowlist validation still does not replace parameterized SQL, context-aware output encoding, file-upload inspection, or authorization. Validation checks shape; separate controls make each use safe.
Errors and validation boundaries
Client-side checks help users correct input quickly, but a handcrafted HTTP request or modified app can bypass them. The server remains authoritative. Validate data from internal APIs, queues, partners, or storage when it crosses another component’s trust boundary.
Reject invalid input clearly, reporting field locations and stable error codes according to the API contract. Do not copy full request bodies, passwords, tokens, internal paths, or stack traces into responses or logs. Use non-sensitive failure details and request identifiers for diagnosis. For user-authored HTML, use a maintained HTML sanitizer rather than regex or generic input validation.
How to verify validation
Check missing required values, malformed formats, range boundaries and excesses, and the behavior for unknown fields.
Check invalid nested values, empty arrays, array limits, body-size limits, and nesting-depth limits.
Check relationships such as start and end dates, and confirm that authorization failures and state conflicts are distinct from input errors.
Check that API documentation matches runtime rules and that errors reveal no secrets or implementation details.
Keep the flow as size limits → parsing → structural and field rules → business rules → authorization → processing. This sequence does not turn authorization into data validation, and successful validation does not prove the caller is authorized or that later use is safe.
References
[OWASP Input Validation Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Input_Validation_Cheat_Sheet.html)
[OpenAPI Specification 3.2.1: Request Body Object](https://spec.openapis.org/oas/latest.html#request-body-object)
[JSON Schema: Object reference](https://json-schema.org/understanding-json-schema/reference/object)