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);
}
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);
}
That code should produce two errors, as name and SKU is required, but it returns only one error, check the below output:
[ { keyword: 'required',
dataPath: '',
schemaPath: '#/required',
params: { missingProperty: 'name' },
message: 'should have required property \'name\'' } ]
Share
Improve this question
asked May 29, 2019 at 16:25
ElbasselElbassel
4547 silver badges19 bronze badges
1 Answer
Reset to default 18You 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./epoberezkin/ajv#options