Validate JSON data with schemas, types, required fields, formats, composition, and API contracts.
Lessons
Extend validation with your own keywords.
Custom Keywords is easiest to learn by reading the example, changing it, and observing the result.
import Ajv from 'ajv';
const ajv = new Ajv();
// Add a custom "even" keyword
ajv.addKeyword({
keyword: 'even',
type: 'number',
validate: (schema, data) => schema === (data % 2 === 0)
});
const validate = ajv.compile({ type: 'number', even: true });
console.log(validate(4), validate(5)); // true falsePractice the Custom Keywords example in a small scratch file, then explain what changed and why.