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

trim + empty confuses me #1039

Closed
awilkins opened this issue Nov 18, 2016 · 5 comments
Closed

trim + empty confuses me #1039

awilkins opened this issue Nov 18, 2016 · 5 comments
Assignees
Labels
feature New functionality or improvement
Milestone

Comments

@awilkins
Copy link

Context

  • node version: 6.9.1
  • joi version: 9.2.0
  • environment (node, browser): node
  • used with (hapi, standalone, ...): just node in a REPL
  • any other relevant information:

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

Describe your issue here, include schemas and inputs you are validating if needed.

const schema = Joi.string().trim().empty('');
schema.validate(' '); 

Which result you had ?

"value" is not allowed to be empty

What did you expect ?

Successful validation.

trim() states in it's documentation that it edits the field. Intuitively I expected that edit to be applied before the next step in the schema, such that the .empty('') would receive an empty string since the input consisted only of whitespace.

The correct way to do this ... seems to be to pass /\s+/ to .empty()

const schema = Joi.string().empty(/\s+/).trim();
schema.validate(' '); // passes (and value is undefined)

Most of the documentation uses .empty('') though..

Would it break everyone's world if .trim() was applied before other validation.....? Possibly.

At a min. I'd like to see the docs improved to state that

  • Conversions are applied after validations (or whatever the actual behaviour is)
  • Maybe an example or 2 that uses that whitespace regex with .empty() in that context
@awilkins
Copy link
Author

Hah, I do of course mean /^\s*$/ because the other one is wayyyyy to general

@awilkins
Copy link
Author

But ...

const schema = Joi.string().empty(/^\s*$/);
schema.validate(''); // fails

Arrgh.

/^\s*$/.exec('') === 
[ '', index: 0, input: '' ]

The regex matches empty string. Why isn't it considered empty since empty has been told that anything that matches the regex is empty?

@Marsup
Copy link
Collaborator

Marsup commented Nov 18, 2016

Because your schema is strictly equivalent to Joi.string().empty(Joi.string().regex(/^\s*$/)), which will fail for the same reason a simple string would fail on an empty string. To make it work you'd need Joi.string().empty(Joi.string().allow('').regex(/^\s*$/)).

I'll check to see if the complexity of supporting this exceeds what I'm willing to support, will report back.

@awilkins
Copy link
Author

Thanks for the workaround anyway 👍

@Marsup Marsup added the request label Nov 18, 2016
@Marsup Marsup self-assigned this Nov 18, 2016
@Marsup Marsup added this to the 10.0.1 milestone Nov 18, 2016
@Marsup Marsup closed this as completed in 1a4a2f8 Nov 18, 2016
@hueniverse hueniverse added feature New functionality or improvement and removed request labels Sep 19, 2019
@lock
Copy link

lock bot commented Jan 9, 2020

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature New functionality or improvement
Projects
None yet
Development

No branches or pull requests

3 participants