I'm trying to use AJV with the below code, when I validate an object with multiple errors, AJV throws only one error at a time.
const schema = {
type: 'object',
properties: {
name: {type: 'string', minLength: 1, maxLength: 1},
sku: { type: 'string', minLength: 1, maxLength: 200},
},
required: ['name', 'sku']
}
const ajv = require('ajv');
const validator = new ajv();
const valid = validator.validate(schema, {});
if (!valid) {
console.log(validator.errors);
}
[ { keyword: 'required',
dataPath: '',
schemaPath: '#/required',
params: { missingProperty: 'name' },
message: 'should have required property \'name\'' } ]
You need to set the configuration for that.
If you have get all the errors in once then you have to set this object param when creating an object of ajv {allErrors: true}
here is updated the code.
const schema = {
type: 'object',
properties: {
name: {type: 'string', minLength: 1, maxLength: 1},
sku: { type: 'string', minLength: 1, maxLength: 200},
},
required: ['name', 'sku']
}
const ajv = require('ajv');
const validator = new ajv({allErrors:true});
const valid = validator.validate(schema, {});
if (!valid) {
console.log(validator.errors);
}
Please also check this link for more configuration params. Link https://github.com/epoberezkin/ajv#options
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With