ajv-validator/ajv

report all duplicates with uniqueItems: "all" option

Open

#1092 aperta il 25 set 2019

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

Metriche repository

Star
 (12.984 star)
Metriche merge PR
 (Nessuna PR mergiata in 30 g)

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