Skip to content

Joi.string().optional() doesn't treat an empty string as unset #482

@ThisIsMissEm

Description

@ThisIsMissEm

I'm hitting into this when creating Joi schemas for forms, where I have a text input which can optionally be filled in, but if it's not, then no problems.

At the moment I have to use the work around of:

Joi.string().valid('').optional() // followed by any other validations

This is due to this line: https://github.com/hapijs/joi/blob/master/lib/string.js#L19

I'd be inclined to argue that if you've just said Joi.string() without any optional or required tags, then an empty string is actually a valid value. If you say it's required, then an empty string would be invalid.

Thoughts?

Metadata

Metadata

Assignees

Labels

supportQuestions, discussions, and general support

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions