Javascript AJV只返回一个错误,尽管存在多个错误
我试图将AJV与下面的代码一起使用,当我验证一个有多个错误的对象时,AJV一次只抛出一个错误Javascript AJV只返回一个错误,尽管存在多个错误,javascript,node.js,ecmascript-6,ajv,Javascript,Node.js,Ecmascript 6,Ajv,我试图将AJV与下面的代码一起使用,当我验证一个有多个错误的对象时,AJV一次只抛出一个错误 const模式={ 类型:“对象”, 特性:{ 名称:{type:'string',minLength:1,maxLength:1}, sku:{type:'string',minLength:1,maxLength:200}, }, 必需:['name','sku'] } const ajv=需要(“ajv”); const validator=new ajv(); const valid=vali
const模式={
类型:“对象”,
特性:{
名称:{type:'string',minLength:1,maxLength:1},
sku:{type:'string',minLength:1,maxLength:200},
},
必需:['name','sku']
}
const ajv=需要(“ajv”);
const validator=new ajv();
const valid=validator.validate(模式,{});
如果(!有效){
console.log(validator.errors);
}
您需要为此设置配置
如果一次获得所有错误,那么在创建ajv{allErrors:true}
这里是更新的代码
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);
}
请检查此链接以了解更多配置参数。链接