ajv-validator / ajv-validator/ajv

useDefaults option produces incorrect result with different inputs

Aperta
#2,304 0 commenti 1 reazione 0 assegnatari Vedi su GitHub
Lingua principale
TypeScript
Stelle
14.8k
Fork
1k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

**What version of Ajv are you using? Does the issue happen if you use the latest version?**
Ajv Version:8.12.0
**Ajv options object**
const ajv = new Ajv({useDefaults: true, strict: false})

```javascript

```

**JSON Schema**

```json
{
"type": "object",
"properties": {
"foo": {
"type": "number",
"default": 5
},
"yyy": {
"type": "array",
"items": [
{
"type": "object",
"default": {
"xyz": "testValue",
"ssms": [
5
]
},
"properties": {
"ssms": {
"type": "array",
"items": [
{
"type": "array",
"default": [
2
]
},
{
"type": "number"
},
{
"type": "string",
"default": "abcdef"
}
]
}
}
}
]
},
"bar": {
"type": "object",
"default": {
"abc": "abc-outer"
},
"properties": {
"efg": {
"type": "number",
"default": 88
}
}
},
"prop1": {
"type": "array",
"default": [],
"items": [
{
"type": "array",
"default": [
5
],
"items": {
"type": "number"
}
}
]
}
}
}
```

**Sample data**

```json
//input for first test case
{
"foo": 6
}
```
```json
//input for second test case

{
"foo": 6,
"yyy":[]
}
```
**Your code**

```javascript
const ajv = new Ajv({useDefaults: true, strict: false})
const validate = ajv.compile(schema)
console.log(JSON.stringify(data))
```

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

```
{"foo":6,"bar":{"abc":"abc-outer","efg":88},"prop1":[[5]]}-- Output for first test case
{"foo":6,"yyy":[{"xyz":"testValue","ssms":[5,null,"abcdef"]}],"bar":{"abc":"abc-outer"},"prop1":[]}--output for second test case
```

**What results did you expect?**
{"foo":6,"bar":{"abc":"abc-outer","efg":88},"prop1":[[5]]}-- Output for first test case is fine and works as expected
{"foo":6,"yyy":[{"xyz":"testValue","ssms":[5,null,"abcdef"]}],"bar":{"abc":"abc-outer","efg":88},"prop1":[[5]]} -- This is what i expected for second test case.
The output for first test case is as expected but the output for second test case is not expected. I was expecting
{"foo":6,"yyy":[{"xyz":"testValue","ssms":[5,null,"abcdef"]}],"bar":{"abc":"abc-outer","efg":88},"prop1":[[5]]}
why does the output for "bar" and "prop1" change when an empty array is provided for a different field (yyy)
**Are you going to resolve the issue?**

Guida per i contributori

Apri la guida per i contributori

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.