atlassian / atlassian/better-ajv-errors

Error if an enum field allows null and is invalid

Open
#224 0 comments 0 reactions 1 assignee Claimed by @dmiller113 View on GitHub
bug
Dominant language
JavaScript
Stars
245
Forks
50
PR merge metrics
No merged PRs in 30d

Description

Ajv requires that nullable enum fields include `null` in the enum array with a schema like this:
```js
{
type: 'object',
properties: {
test: {
type: 'string',
enum: ['a', 'b', null],
nullable: true
},
metadata: reviewMetadataSchema
},
}
```

If you pass in `{ test: 'c' }` then `ajv.errors` will look like this
```js
[
{
instancePath: '/test',
schemaPath: '#/properties/test/enum',
keyword: 'enum',
params: {
allowedValues: ['a', 'b', null]
},
message: 'must be equal to one of the allowed values'
}
]
```

and better-ajv-errors will throw an error like this
```
TypeError: Cannot read properties of null (reading 'length')
at leven (/node_modules/leven/index.js:14:11)
at /node_modules/better-ajv-errors/src/validation-errors/enum.js:63:17
at Array.map ()
at EnumValidationError.findBestMatch (/node_modules/better-ajv-errors/src/validation-errors/enum.js:61:8)
at EnumValidationError.getError (/node_modules/better-ajv-errors/src/validation-errors/enum.js:30:28)
at customErrorToStructure (/node_modules/better-ajv-errors/src/index.js:11:49)
at Array.map ()
at src_default (/node_modules/better-ajv-errors/src/index.js:22:25)
```

Without the `null` in the enum, better-ajv-errors will work as expected, but then you can't make the property nullable.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.