ajv-validator / ajv-validator/ajv-keywords

uniqueItemProperties on non-required keys fails with multiple items with undefined keys

未關閉
#115 10 則留言 3 個 reaction 已指派 0 人 在 GitHub 檢視
question
主要語言
TypeScript
星號
257
分支
51
PR 合併指標
30 天內沒有已合併 PR

描述

Using `uniqueItemProperties: ['id']` where `id` is not required should (in my opinion, maybe it should be a separate function) allow multiple elements that have undefined `id` values. e.g.

`[{id: 'a', val: 1}]` - should pass

`[{id: 'a', val: 1}, {id: 'b', val: 2}]` - should pass

`[{id: 'a', val: 1}, {id: 'a', val: 2}]` - should fail

`[{id: 'a', val: 1}, {val: 2}]` - should pass

`[{val: 1}, {val: 2}]` - should pass (but doesn't under current `uniqueItemProperties`)

I have created a runkit here (code below as well):
https://runkit.com/jamesjansson/ajv-uniqueitemproperties-on-non-required-keys

```
const Ajv = require("ajv");
const ajvKeywords = require("ajv-keywords")

function validateJson(json, schema) {
const ajv = new Ajv({
verbose: true,
allErrors: true,
useDefaults: true,
format: 'full',
$data: true,
});
ajvKeywords(ajv);
const validate = ajv.compile(schema);
const isValid = validate(json);
return {
isValid,
errors: validate.errors,
};
}

const jsonSchema = {
type: 'array',
uniqueItemProperties: ['id'],
items: {
type: 'object',
properties: {
id: { type: 'string', format: 'uuid' },
otherData: { type: 'string' },
},
required: ['otherData'],
additionalProperties: false,
},
};

console.log('it should succeed with a single item with no id')
const testData = [
{
otherData: 'stuff',
},
];
const validation1 = validateJson(testData, jsonSchema);
console.log(validation1)

console.log('it should succeed with a two items with no id')
const testData2 = [
{
otherData: 'stuff0',
},
{
otherData: 'stuff1',
},
];
const validation2 = validateJson(testData2, jsonSchema);
console.log(validation2)
```

貢獻指南

這個儲存庫沒有索引到貢獻指南

評估

這個 Issue 還沒有評估資料。

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。