Schema validation checks each API request and response against the schemas that you define in API builder. When validation is on, a request that doesn't match its schema gets an error and your API doesn't run. A response that doesn't match its schema also gets an error, so callers never receive data in a shape you didn't intend.
Validation is one setting for the whole API, and it is off by default, including for new APIs. While it is off, API builder doesn't reject any request or response. To turn it on, see Configure advanced API settings.
What validation checks
| Item | What validation checks |
|---|---|
| Request body | The body matches the schema in the API request. |
| Query parameters | Each query parameter marked Required is present and not empty. A value of only spaces counts as empty. |
| Response body | The body matches the schema in the API response that runs. |
Validation doesn't check headers, path parameters, or query parameters that you haven't added to the API request. For a GET request, validation skips the body but still checks query parameters.
Set rules on a field
Each field in a request or response body has settings that depend on its type. To open them, select Additional settings on the field's row. To add fields, see Configure the API request and Configure the API response.
| Field type | Settings |
|---|---|
| String | Description, Regex, Enum values, Min length, Max length, Required, Allow null |
| Number | Description, Enum values, Min value, Max value, Exclusive min, Exclusive max, Multiple of, Required, Allow null |
| Boolean | Description, Required, Allow null |
| Object | Description, Required, Allow null, and whether the object can contain fields you haven't defined |
| Array | The settings for the item type, plus Min items and Max items. Rules for the item type apply to each item. |
Rules behave as follows:
- Required means the field is present. An empty string, 0, false, an empty array, and an empty object all pass. To reject an empty string, set Min length to 1. To reject an empty array, set Min items to 1.
- A nested required field is required only inside its parent.
- Min length and Max length count characters, including spaces.
- Min value, Max value, Min length, Max length, Min items, and Max items include the number you enter.
- Exclusive min and Exclusive max exclude the number you enter. If you set both Min value and Exclusive min, a number must pass both, so the stricter one applies. The same is true for Max value and Exclusive max.
- Multiple of must be greater than 0.
- To allow one exact value, use Enum values with a single value.
API builder rejects combinations that can't work, such as a minimum above the maximum, a negative length, or an invalid regular expression. The field keeps its last saved settings. When you change a field's type, settings that no longer apply are cleared. When you remove a field, its settings are removed too.
What callers receive
| Result | When it happens | What the caller gets |
|---|---|---|
| HTTP 400 | The request body doesn't match its schema, or a required query parameter is missing or empty. | An error that names the field and the rule it broke. Your API doesn't run. |
| HTTP 500 | The response doesn't match its schema. | A generic message. API builder doesn't send the response headers. |
| HTTP 501 | A saved schema has a rule that API builder can't run. Some regular expressions save but can't be run. | An error response. In a test run, the error names the schema location. |
Requests that validation rejects count toward your monthly API usage. If you exceed your usage limit, the caller gets HTTP 422 before validation runs.
For the messages callers see and how to fix them, see Troubleshoot APIs. For the full list of error codes, see Return custom errors and error codes.
Preview and test runs
Preview in the API request and the API response uses the saved validation setting. If the mock request fails validation, Parsed output lists the errors. If the response fails validation, Preview shows status 500, keeps your headers, and lists the errors in the body.
A test run doesn't report schema failures while validation is off. See Test and debug your API using API builder.
Limitations
- Validation applies to APIs built in API builder, not JavaScript APIs.
- Validation checks query parameters for presence only, not for type.
- Switching to Visual removes rules that Visual can't show. See Switch between Visual and JSON.
- There is no Format setting. A format that API builder infers from a sample file, such as date-time, is kept and enforced.
- If a step has already run when response validation fails, the caller gets HTTP 500 and the earlier steps aren't undone.
Best practices
- Turn validation on before you test.
- Mark only the query parameters that callers must send as Required.
- If your API writes data, check how callers retry. A caller that gets HTTP 500 can send the request again.