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

Unexpected Errors with Strings #827

Closed
ghost opened this issue Feb 18, 2016 · 1 comment
Closed

Unexpected Errors with Strings #827

ghost opened this issue Feb 18, 2016 · 1 comment
Assignees
Labels
bug Bug or defect
Milestone

Comments

@ghost
Copy link

ghost commented Feb 18, 2016

When a string is purely whitespace (e.g. - " ") and uses string.trim, joi will return an any.invalid error instead of an any.empty error. If the value is just an empty string with no whitespace, it correctly returns any.empty.

var schema = joi.string().trim().required();

schema.validate("");          // any.empty
schema.validate("      ");    // any.invalid

I imagine it has to do with this line, which doesn't check for an empty string. In an earlier check (here), the empty string is accounted for.

Also, is an any.required error the expected behavior for the following case? I would expect an any.empty, since it should be defaulting to an empty string.

var schema = joi.string().default("").trim().required();

schema.validate();    // any.required

If you'd like, I could do a fix and send over a pull request, assuming this isn't the expected behavior for either case.

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

Marsup commented Feb 18, 2016

Agreed on the 1st report, I'll let you PR on this.

The 2nd case is a conflict between 2 conditions in your schema, it can't be required if you set a default. Fixing all those contradictions would bloat the lib so that's more of a won't fix.

@Marsup Marsup added this to the 8.0.2 milestone Feb 19, 2016
@Marsup Marsup closed this as completed in a21a966 Feb 19, 2016
Marsup added a commit that referenced this issue Feb 19, 2016
Return any.empty error for trimmed whitespace (fixes #827)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bug or defect
Projects
None yet
Development

No branches or pull requests

1 participant