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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.