This example demonstrates how to use the Request Validation policy with OpenAPI schemas stored in external files using $ref. This pattern keeps your route definitions clean and allows you to reuse schemas across multiple endpoints.
Prerequisites
- A Zuplo account. You can sign up for free.
Working with this Example
Locally
Working locally is the best way to explore and understand the code for this example. You can get a local version by using the Zuplo CLI:
Then, in the project directory run the following commands:
Deploy this example to Zuplo
It is also possible to deploy this example directly to your Zuplo account and work with it via the Zuplo Portal. You can do this by clicking the Deploy to Zuplo button anywhere on this page.
How It Works
External Schema References
Instead of defining schemas inline in your OpenAPI document, you can reference external JSON Schema files:
Request Validation Policy
The request-validation-inbound policy automatically validates incoming requests against the referenced schemas:
When validation fails, the policy returns a 400 Bad Request with details about what failed.
Project Structure
Schema Example
The insert-todo-object.json schema defines the required fields for creating a todo:
Testing the Example
Valid Request
Invalid Request (missing required field)
This returns a 400 error with validation details:
Validation Options
| Option | Values | Description |
|---|---|---|
validateBody | reject-and-log, log-only, none | How to handle body validation failures |
validatePathParameters | reject-and-log, log-only, none | How to handle path parameter validation |
validateQueryParameters | reject-and-log, log-only, none | How to handle query parameter validation |
Common Customizations
- Add more schemas: Create new
.jsonfiles in theschemas/directory and reference them with$ref - Reuse schemas: Reference the same schema file from multiple endpoints
- Nested schemas: Use
$refwithin schemas to reference other schemas - Change validation behavior: Set to
log-onlyduring development to see validation errors without rejecting requests