Skip to content

Error if an enum field allows null and is invalid #224

@rflorence

Description

@rflorence

Ajv requires that nullable enum fields include null in the enum array with a schema like this:

{
  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

[
  {
    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 (<anonymous>)
    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 (<anonymous>)
    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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions