Elb*_*sel 5 javascript node.js ecmascript-6 ajv
我正在尝试将 AJV 与以下代码一起使用,当我验证具有多个错误的对象时,AJV 一次只抛出一个错误。
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);
}Run Code Online (Sandbox Code Playgroud)
[ { keyword: 'required',
dataPath: '',
schemaPath: '#/required',
params: { missingProperty: 'name' },
message: 'should have required property \'name\'' } ]Run Code Online (Sandbox Code Playgroud)
您需要为此设置配置。
如果您一次收到所有错误,则必须在创建 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);
}
Run Code Online (Sandbox Code Playgroud)
另请查看此链接以获取更多配置参数。链接https://github.com/epoberezkin/ajv#options
| 归档时间: |
|
| 查看次数: |
1291 次 |
| 最近记录: |