ajv-validator/ajv

report all duplicates with uniqueItems: "all" option

Open

#1.092 geöffnet am 25. Sept. 2019

Auf GitHub ansehen
 (6 Kommentare) (0 Reaktionen) (0 zugewiesene Personen)TypeScript (12.984 Stars) (897 Forks)batch import
enhancementhelp wanted

Beschreibung

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.

Contributor Guide