You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
is this issue currently blocking your project? (yes/no): no
is this issue affecting a production system? (yes/no): no
Context
node version: 12.15.0
module version with issue: 17.4.2
last module version without issue: n/a
environment (e.g. node, browser, native): node
used with (e.g. hapi application, another framework, standalone, ...): standalone
any other relevant information:
What are you trying to achieve or the steps to reproduce?
I'm trying to validate form submissions and API requests (separate components) in which some values may be either explicitly empty or may not be present in the submission format at all. For example, if an input has a disabled attribute, it will not be submitted at all, while the one with an empty value will be submitted as such. In other words,
, will yield this query, where abc is not present:
/?xyz=
Same for API clients - some fields may be missing and some may be explicitly empty strings.
I would like to be able to distinguish missing fields from empty fields using optinal() and min(0), just as described in the docs. That is, given this validation object:
min(5) applied in the string context is the string length, so it stands to reason that min(0) would allow empty strings. The docs don't call out that min(0) isn't allowed to be used for strings, which wouldn't make much sense, anyway.
Also, note that allow("") isn't the same as min(0), even though it's commonly advised as a workaround, which is what it is. Yes, I can compare against an empty string, which is the only option now, but comparing two strings and comparing their lengths are two different logical operations and if I could set up validation rules with min(0), I would prefer to do just that to align with min(5) in this case.
I see quite often confusion between empty string arguments in form/API submissions and non-existing arguments, as is evident from the discussion in issue #482. I'm happy that Joi distinguishes the two with optional/required, similar to how Mongo DB uses the $exists operator, but string().min(0) is not following this logic and forces one having to compare values instead of comparing just lengths.
Here's a small example with both validation cases having correctly formatted objects being validated, but while the first one correctly allows abc to be omitted, the second one fails to apply min(0) against a zero-length string and incorrectly reports abc as an invalid empty string.
letJoi=require("joi");letvalidateOptions={abortEarly: false,allowUnknown: false,presence: "required"};//// Expected://// * `abc` may be missing or present as a 0-5 character string// * `xyz` must be present, may be equal to "" of have length// up to 5 characters//letvalidationSchema=Joi.object({abc: Joi.string().optional().min(0).max(5),xyz: Joi.string().allow("").max(5)});//// Correctly allows field `abc` to be missing// Correctly allows field `xyz` to be present and empty//letvalidated=validationSchema.validate({xyz: ""},validateOptions);if(validated.error)console.error("FAIL: %s",validated.error.message);elseconsole.log("PASS: %s",JSON.stringify(validated.value));//// Correctly allows `xyz` to be empty// Fails to validate `abc` as a present zero-length string//validated=validationSchema.validate({xyz: "",abc: ""},validateOptions);if(validated.error)console.error("FAIL: %s",validated.error.message);elseconsole.log("PASS: %s",JSON.stringify(validated.value));
What was the result you got?
string().min(0) fails to accept a zero length string.
What result did you expect?
I expect string().min(0) to work the same way as string().min(5) does in enforcing the string length being 5 characters and allow a zero-length string to be considered valid..
The text was updated successfully, but these errors were encountered:
Support plan
Context
What are you trying to achieve or the steps to reproduce?
I'm trying to validate form submissions and API requests (separate components) in which some values may be either explicitly empty or may not be present in the submission format at all. For example, if an
input
has adisabled
attribute, it will not be submitted at all, while the one with an empty value will be submitted as such. In other words,, will yield this query, where
abc
is not present:Same for API clients - some fields may be missing and some may be explicitly empty strings.
I would like to be able to distinguish missing fields from empty fields using
optinal()
andmin(0)
, just as described in the docs. That is, given this validation object:, following objects should be valid. Note that
xyz
is just for contrast andabc
is the example.min(5)
applied in the string context is the string length, so it stands to reason thatmin(0)
would allow empty strings. The docs don't call out thatmin(0)
isn't allowed to be used for strings, which wouldn't make much sense, anyway.Also, note that
allow("")
isn't the same asmin(0)
, even though it's commonly advised as a workaround, which is what it is. Yes, I can compare against an empty string, which is the only option now, but comparing two strings and comparing their lengths are two different logical operations and if I could set up validation rules withmin(0)
, I would prefer to do just that to align withmin(5)
in this case.I see quite often confusion between empty string arguments in form/API submissions and non-existing arguments, as is evident from the discussion in issue #482. I'm happy that Joi distinguishes the two with
optional
/required
, similar to how Mongo DB uses the$exists
operator, butstring().min(0)
is not following this logic and forces one having to compare values instead of comparing just lengths.Here's a small example with both validation cases having correctly formatted objects being validated, but while the first one correctly allows
abc
to be omitted, the second one fails to applymin(0)
against a zero-length string and incorrectly reportsabc
as an invalid empty string.What was the result you got?
string().min(0)
fails to accept a zero length string.What result did you expect?
I expect
string().min(0)
to work the same way asstring().min(5)
does in enforcing the string length being 5 characters and allow a zero-length string to be considered valid..The text was updated successfully, but these errors were encountered: