Home
Schema Validation
Schema Validation Ajv Example
Schema Validation Ajv Example
Example of validating API request data against a JSON Schema using AJV in a Node.js middleware.
API Governance Contract Testing JSON Schema OpenAPI Schema Validation
Schema Validation Ajv Example is an example object payload from Schema Validation, with 9 top-level fields. It illustrates the shape of data this provider's APIs accept or return.
Top-level fields
title description language package schema valid_input valid_output invalid_input invalid_output
Example Payload
{
"title": "AJV JSON Schema Validation",
"description": "Example of validating API request data against a JSON Schema using AJV in a Node.js middleware.",
"language": "JavaScript",
"package": "ajv",
"schema": {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"required": ["name", "email", "plan"],
"properties": {
"name": { "type": "string", "minLength": 1, "maxLength": 100 },
"email": { "type": "string", "format": "email" },
"plan": { "type": "string", "enum": ["free", "starter", "pro", "enterprise"] },
"seats": { "type": "integer", "minimum": 1, "maximum": 500 }
}
},
"valid_input": {
"name": "Acme Corp",
"email": "admin@acmecorp.com",
"plan": "pro",
"seats": 25
},
"valid_output": {
"valid": true,
"errors": null
},
"invalid_input": {
"name": "",
"email": "not-an-email",
"plan": "gold",
"seats": -1
},
"invalid_output": {
"valid": false,
"errors": [
{
"instancePath": "/name",
"schemaPath": "#/properties/name/minLength",
"keyword": "minLength",
"message": "must NOT have fewer than 1 characters"
},
{
"instancePath": "/email",
"schemaPath": "#/properties/email/format",
"keyword": "format",
"message": "must match format \"email\""
},
{
"instancePath": "/plan",
"schemaPath": "#/properties/plan/enum",
"keyword": "enum",
"message": "must be equal to one of the allowed values"
},
{
"instancePath": "/seats",
"schemaPath": "#/properties/seats/minimum",
"keyword": "minimum",
"message": "must be >= 1"
}
]
}
}