ajv-validator / ajv-validator/ajv-errors

Default error message does not produce consistent output for `oneOf` nested in an array (expect 1 errors in output)

Đang mở
#83 0 bình luận 0 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
TypeScript
Star
289
Fork
21
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

`ajv@7.0.3`
`ajv-errors@2.0.0`

Greets,

I have included schema and simple test case below showing that a default error message does not produce consistent results. For some reason the "nested" oneOf statement w/ default error message produces two entries in the `errors` output. The oneOf statement at the top level which is exactly the same produces one entry in the `errors` output.

IE for the schema below the entry count with the default error message should be the same for both failing validation cases:

`{ system: [false] }` and `{ packs: [{ system: [false] }] }`

The latter is not picking up the child items type error in the default error message which is part of the oneOf block.

``` javascript
const Ajv = require("ajv").default;
const ajv = new Ajv({ allErrors: true });

// Ajv option allErrors is required
require("ajv-errors")(ajv);

const schema = {
$id: "test",
$schema: "http://json-schema.org/draft-07/schema#",
type: "object",
properties: {
packs: {
type: "array",
items: {
type: "object",
properties: {
system: {
oneOf: [
{ type: "string" },
{
items: { type: "string" },
type: "array"
}
],
errorMessage: "should be a string or array of strings"
}
}
}
},
system: {
oneOf: [
{ type: "string" },
{
items: { type: "string" },
type: "array"
}
],
errorMessage: "should be a string or array of strings"
}
}
};

const validate = ajv.compile(schema);

// Expect `errorMessage` to catch all errors at or below the oneOf level.
console.log(`{ system: [false] }:`);
console.log(JSON.stringify(validate.errors, null, 3));

// Expect `errorMessage` to catch all errors at or below the oneOf level.
// However it does not catch the further items type and there are two errors.
console.log('\n-------------------------------------------------------------\n');
console.log(`expect only one error in 'errors', but there are two for { packs: [{ system: [false] }] }:`);
console.log(JSON.stringify(validate.errors, null, 3));
```

#Output:

``` json
{ system: [false] }:
[
{
"keyword": "errorMessage",
"dataPath": "/system",
"schemaPath": "#/properties/system/errorMessage",
"params": {
"errors": [
{
"keyword": "type",
"dataPath": "/system",
"schemaPath": "#/properties/system/oneOf/0/type",
"params": {
"type": "string"
},
"message": "should be string",
"emUsed": true
},
{
"keyword": "type",
"dataPath": "/system/0",
"schemaPath": "#/properties/system/oneOf/1/items/type",
"params": {
"type": "string"
},
"message": "should be string",
"emUsed": true
},
{
"keyword": "oneOf",
"dataPath": "/system",
"schemaPath": "#/properties/system/oneOf",
"params": {
"passingSchemas": null
},
"message": "should match exactly one schema in oneOf",
"emUsed": true
}
]
},
"message": "should be a string or array of strings"
}
]

-------------------------------------------------------------

expect only one error in 'errors', but there are two for { packs: [{ system: [false] }] }:
[
{
"keyword": "type",
"dataPath": "/packs/0/system/0",
"schemaPath": "#/properties/packs/items/properties/system/oneOf/1/items/type",
"params": {
"type": "string"
},
"message": "should be string"
},
{
"keyword": "errorMessage",
"dataPath": "/packs/0/system",
"schemaPath": "#/properties/packs/items/properties/system/errorMessage",
"params": {
"errors": [
{
"keyword": "type",
"dataPath": "/packs/0/system",
"schemaPath": "#/properties/packs/items/properties/system/oneOf/0/type",
"params": {
"type": "string"
},
"message": "should be string",
"emUsed": true
},
{
"keyword": "oneOf",
"dataPath": "/packs/0/system",
"schemaPath": "#/properties/packs/items/properties/system/oneOf",
"params": {
"passingSchemas": null
},
"message": "should match exactly one schema in oneOf",
"emUsed": true
}
]
},
"message": "should be a string or array of strings"
}
]
```

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.