ajv-validator / ajv-validator/ajv

`coerceTypes: "array"` when combined with `oneOf` may produce invalid result

Aperta
#1,294 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub
limitation
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?**

v6.12.5

**Ajv options object**

```javascript
{ coerceTypes: "array" }
```

**JSON Schema**

```json
{
"type": "object",
"properties": {
"foo": {
"oneOf": [
{ "const": "*" },
{ "type": "array", "items": { "type": "string", "pattern": "^[A_Z]+$" } }
]
}
}
}
```

**Sample data**

```json
{ "foo": "*" }
```

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

```
{ foo: [ '*' ] }
```

**What results did you expect?**

I didn't expect coercion to be made on value that doesn't validate against `items` schema, and buggy side effect of that is that input value was changed to one which no longer matches the schema.

I believe by design validation should not produce results which no longer validates against same schema.

Additionally I observed that coercion to array is forced even if singular value is accepted

e.g. _string_ value will be put into array with schema as `oneOf: [{ type: "string" }, { type: "array", items: { type: "string" } }]`, and interesting quirk is that it'll impose a validation error, as internals will assume that both variants were matched.

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.