ajv-validator/ajv

report all duplicates with uniqueItems: "all" option

Open

#1,092 建立於 2019年9月25日

在 GitHub 查看
 (6 留言) (0 反應) (0 負責人)TypeScript (12,984 star) (897 fork)batch import
enhancementhelp wanted

描述

I have an array of simple scalar values and I want validation to fail when there is a duplicate.

This works, however I want to be able to see all duplicates at once and not resolve them one at a time.

So I added the allErrors option into the solution and I still only see a single error.

Code to reproduce:

var Ajv = require('ajv')

var ajv = new Ajv({ allErrors: true, uniqueItems: true });
var schema = {
  "type": "array",
  "items": [
    { "type": "number" }
  ],
  "uniqueItems": true
}

var data = [1, 1, 2, 3, 3];

var validate = ajv.compile(schema);

console.log(validate.errors);

Result:

[ { keyword: 'uniqueItems',
    dataPath: '',
    schemaPath: '#/uniqueItems',
    params: { i: 4, j: 3 },
    message:
     'should NOT have duplicate items (items ## 3 and 4 are identical)' } ]

From the docs: allErrors: check all rules collecting all errors. Default is to return after the first error.

貢獻者指南

report all duplicates with uniqueItems: "all" option · ajv-validator/ajv#1092 | Good First Issue