最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AJV return only one error although there is multiple - Stack Overflow

programmeradmin6浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 18

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./epoberezkin/ajv#options

发布评论

评论列表(0)

  1. 暂无评论