Skip to content

[BUG] Cannot use enum on nullable field #27

@LonelyPrincess

Description

@LonelyPrincess

Describe the bug
There seems to be a problem when using the enum keyword on a nullable field. In those cases, it seems enum takes precedence over the nullable condition, causing the validation to fail even if we've added null to the list of supported types for the property.

To Reproduce
The problem can be triggered with a schema where we have a field that can either contain a subset of values (defined with the enum annotation) or be null.

/**
 * @typedef {object} MySchema
 * @property {string|null} value - Nullable field that can only contain certain values - enum:val1,val2
 */

Expected behavior
What I'd expect on this scenario is that validation succeeds if any of these conditions are met:

  • value contains any of the supported values defined in the enum annotation
  • value is null

Screenshots
Below there is a subset of the schema where the bug was detected:

/**
 * @typedef {object} GeneratedTestData
 * @property {number} id.required - Test id
 * @property {string} title.required - Name of the test
 * @property {string|null} format - Test format - enum:multiple-choice,reading,other
 * @property {string} createdOn - Creation date
 */

The library returned the following error when validating the response for an endpoint returning the forementioned schema:

{
  name: 'OpenAPIUtilsError',
  type: 'OpenAPIUtilsError:response',
  message: 'Error in response: Schema GeneratedTestData/properties/format must be equal to one of the allowed values: multiple-choice, reading, other. You provide "{"id":208,"title":"Toets 1","format":null,"createdOn":"2012-07-19T10:58:11.000Z","updatedOn":"2022-01-31T17:00:30.659Z"}"',
  extra: [
    {
      instancePath: '/0/format',
      schemaPath: 'defs.json#/definitions/components/schemas/GeneratedTestData/properties/format/enum',
      keyword: 'enum',
      params: [Object],
      message: 'must be equal to one of the allowed values'
    }
  ],
  status: 500,
  statusCode: 500
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions