ajv-validator / ajv-validator/ajv

Unexpected behavior when validating a schema with anyOf between two objects

オープン
#2,500 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る
bug report
主要言語
TypeScript
スター
14.8k
フォーク
1k
PR マージ指標
30日以内にマージされた PR はありません

説明

**What version of Ajv are you using? Does the issue happen if you use the latest version?** reproduces for me on latest version

I am seeing that for some reason when having a schema with an anyof between two objects the removeAdditionalProperties does not consider all the possible keys when removing the properties from the object and only looks at the first object in the anyOf

**jsfiddle with the issue reproduced:**
https://jsfiddle.net/x7s85b3t/1/

**Your code**
```javascript
const schema = {
"properties": {
"body": {
"type": "object",
"properties": {
"configuration": {
"anyOf": [
{
"type": "object",
"properties": {
"differentA": {
"type": "string"
},
"differentB": {
"type": "string"
}
},
"required": [
"differentA",
"differentB"
],
"additionalProperties": false
},
{
"type": "object",
"properties": {
"propA": {
"type": "string"
},
"propB": {
"type": "string"
}
},
"required": [
"propA",
"propB"
],
"additionalProperties": false
}
]
}
},
"required": [
"configuration"
],
"additionalProperties": false
}
},
"required": [
"body"
],
"additionalProperties": true,
"definitions": {}
}

var json = {
body: {
"configuration": {
"propA": "1",
"propB": "2"
}
}
};
var ajv = new Ajv({
allErrors: true,
removeAdditional: true
});

var result = ajv.validate(schema, json);

console.log('Result: ', result);
console.log('Errors: ', ajv.errors);
console.log('Final Configuration object json: ', JSON.stringify(json.body.configuration));

```

**Validation result, data AFTER validation, error messages**

```
"Errors: ", [{
dataPath: ".body.configuration",
keyword: "required",
message: "should have required property 'differentA'",
params: {
missingProperty: "differentA"
},
schemaPath: "#/properties/body/properties/configuration/anyOf/0/required"
}, {
dataPath: ".body.configuration",
keyword: "required",
message: "should have required property 'differentB'",
params: {
missingProperty: "differentB"
},
schemaPath: "#/properties/body/properties/configuration/anyOf/0/required"
}, {
dataPath: ".body.configuration",
keyword: "required",
message: "should have required property 'propA'",
params: {
missingProperty: "propA"
},
schemaPath: "#/properties/body/properties/configuration/anyOf/1/required"
}, {
dataPath: ".body.configuration",
keyword: "required",
message: "should have required property 'propB'",
params: {
missingProperty: "propB"
},
schemaPath: "#/properties/body/properties/configuration/anyOf/1/required"
}, {
dataPath: ".body.configuration",
keyword: "anyOf",
message: "should match some schema in anyOf",
params: { ... },
schemaPath: "#/properties/body/properties/configuration/anyOf"
}]
"Final Configuration object json: ", "{}"

```

**What results did you expect?**
I expected that the remove additional properties will take all the possible object keys into consideration and not only the first one, so I am able to have a schema that allows any of the objects inside the anyof where in reality it fails for the second object

コントリビューションガイド

コントリビューションガイドを開く

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。