Skip to content

required() does not catch specific empty values #945

@steelsojka

Description

@steelsojka

Context

  • node version: 5.0.0
  • joi version: 9.0.0
  • environment (node, browser): Node
  • used with (hapi, standalone, ...): Standalone
  • any other relevant information:

What are you trying to achieve or the steps to reproduce ?

When setting a value to be treated as empty (such as null) and setting the required flag, the empty value is not caught by the required validator. The required check in 'Any' only checks explicitly for 'undefined' and does not check any set empty values.

The main goal is to treat null as if it was undefined.

const schema = Joi.object({
  myKey: Joi.string().empty(null).required()
});

schema.validate({ myKey: null });

Which result you had ?

Returns with no errors.

What did you expect ?

Expected 'myKey' to be treated as an empty value the same as 'undefined'

Metadata

Metadata

Assignees

Labels

bugBug or defect

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions