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

Allowing empty strings by default for string types #73

Closed
maldimirov opened this issue Apr 18, 2019 · 6 comments · Fixed by #74
Closed

Allowing empty strings by default for string types #73

maldimirov opened this issue Apr 18, 2019 · 6 comments · Fixed by #74

Comments

@maldimirov
Copy link
Contributor

The regularString method makes, by default, an empty string a valid value for almost all string formats. This is a bug, since an empty string is not a valid value for e.g. an ipv4 string (which by the JSON Schema spec should be in "dotted quad" format as per RFC 2673).

Seems like the original reasoning behind this is that by default joi marks an empty string as an invalid value for any string type schema. But as I pointed out this breaks the logic for most string formats.

@tlivings
Copy link
Owner

Yeah I think there was a bug around the way joi handled this.

@maldimirov
Copy link
Contributor Author

It isn't an issue with joi itself. It's this line in the resolver code. Some string formats don't return in the switch, but break and go to the regularString function, which sets '' as a valid value.

@yosheeck
Copy link
Contributor

This is not fixed yet. Empty string still passes regex validation.
So empty string of '' passes validation against regex of /theText/ which shouldn't be the case I think ?
I proposed a PR to fix it...

yosheeck added a commit to yosheeck/enjoi that referenced this issue Jan 23, 2020
tlivings pushed a commit that referenced this issue Jan 23, 2020
* Fix for #73: Empty string ignores regex string validation

* Fix for #73: linting and code style adjustment
@svrnwnsch
Copy link

Hey, how is it possible to get the old behavior back which also allows empty strings?

@svrnwnsch
Copy link

Ah I see setting minLength: 0 in the json schema works.

@iudex-
Copy link

iudex- commented Apr 20, 2020

Although @svrnwnsch is correct that setting minLength: 0 fixes the issue, not accepting empty string for the string type directly contradicts the JSON schema specification (as well as the openapi spec).

I can see the point of not accepting empty strings if a format (optional part of the JSON schema spec) is set, but could the behavior of v6.0.0 of accepting empty string be restored for strings without formats?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants