Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

required() does not catch specific empty values #945

Closed
steelsojka opened this issue Jul 13, 2016 · 2 comments
Closed

required() does not catch specific empty values #945

steelsojka opened this issue Jul 13, 2016 · 2 comments
Assignees
Labels
bug Bug or defect
Milestone

Comments

@steelsojka
Copy link

steelsojka commented Jul 13, 2016

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'

@Marsup Marsup added the bug Bug or defect label Jul 13, 2016
@Marsup Marsup self-assigned this Jul 13, 2016
@Marsup
Copy link
Collaborator

Marsup commented Jul 13, 2016

I initially flagged it as a bug but this schema is just wrong, if you want to make null invalid just do so.

@steelsojka
Copy link
Author

I don't think that's quite the same solution though. Here's my scenario.

I have a list of empty resources where any key that does not have a value is preset to null. So I want to treat null as if it was undefined, so I used the extend method to overwrite a specific type like this:

Joi.extend([{
  name: 'string',
  base: Joi.string().empty(null)  
}]);

So this should work according the documentation for empty.

"Considers anything that matches the schema to be empty (undefined)"

If I were to make null invalid I would have to do this everywhere I wanted to make a key required.

Joi.string().invalid(null).required()

I think anything that qualifies as empty should be treated the exact same way as if it was undefined as per the documentation. If not then the documentation should be updated to reflect this scenario.

@Marsup Marsup added this to the 9.0.3 milestone Jul 24, 2016
@Marsup Marsup closed this as completed in b252ea9 Jul 24, 2016
@lock lock bot locked as resolved and limited conversation to collaborators Jan 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Bug or defect
Projects
None yet
Development

No branches or pull requests

2 participants