const Ajv = require('ajv'); const ajvErrors = require('ajv-errors'); const ajv = new Ajv({allErrors: true}); ajvErrors(ajv);
const schema = { type: 'object', properties: { name: { type: 'string' }, address: { type: 'object', properties: { city: { type: 'string', errorMessage: { type: 'should be a string', $data: { dataPath: '.address.city', }, }, }, state: { type: 'string', errorMessage: { type: 'should be a string', $data: { dataPath: '.address.state', }, }, }, }, }, }, };
ajv.compile(schema)({name: 'John', address: {city: 1, state: 2}}); // Output: // [ // { // keyword: 'type', // dataPath: '.address.city', // schemaPath: '#/properties/address/properties/city/type', // params: {type: 'string'}, // message: 'should be a string', // }, // { // keyword: 'type', // dataPath: '.address.state', // schemaPath: '#/properties/address/properties/state/type', // params: {type: 'string'}, // message: 'should be a string', // }, // ]