ajv-validator/ajv

report all duplicates with uniqueItems: "all" option

Aperta

#1092 aperta il 25 set 2019

 (6 commenti) (0 reazioni) (0 assegnatari)TypeScript (897 fork)batch import
enhancementhelp wanted

Metriche repository

Star
 (12.984 stelle)
Metriche merge PR
 (Metriche PR in attesa)

Descrizione

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.

Guida contributor