ajv-validator / ajv-validator/ajv
`coerceTypes: "array"` when combined with `oneOf` may produce invalid result
- Dominant language
- TypeScript
- Stars
- 14.8k
- Forks
- 1k
- PR merge metrics
- No merged PRs in 30d
Description
**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.
Contributor guide
Assessment
This issue has not been assessed yet.