-
Notifications
You must be signed in to change notification settings - Fork 336
Bug report: Poor empty value handling for default format validator? #99
Comments
This is by design, you have to use both presence and format to achieve what you want. |
Nope. Both of them does not work for me By code, https://github.com/ansman/validate.js/blob/master/validate.js#L759 For option Code example, var a = ['', ' ', ' '];
a.forEach(function (v) {
console.log('value |%s|', v);
// Presence on
console.log('presesence on' , validate.single(v, {
presence: true,
format: {
pattern: "^.{1,64}$",
message: 'haha',
},
}));
// presence off
console.log('presence off', validate.single(v, {
presence: false,
format: {
pattern: "^.{1,64}$",
message: 'haha',
},
}));
}); Output
Fiddler https://jsfiddle.net/8okbtbuz/1/ Expected result
|
Ah, I see your dilemma. I'll draft a solution. |
@mondwan, @jpbufe3, @sckogi I've drafted a solution in ecde117, how does it look? The other is a server which should have strict parsing in which a whitespace string is very much defined. |
Hello @ansman , glad to see this patch. In my opinions, changes on However, I have not checked out the changes on others as there are no real usages on my side. I cannot judge whether they are good or not 2 suggestions at last:
Thanks for your patch :D |
@sckogi Datetime was changed here: I've made the changes here: 75ec791 |
Hello @ansman , may I know the date for releasing this patch? |
Released in 0.11.0 |
Hello @ansman , after using the 0.11.0 in my production environment, I believe value In line 275
Here is a JSfiddler to illustrate the reasons: Goal of my program
Current result
SummaryVersion before In version Below is the expected result I hope to get. Expected result
|
I am trying to figure out if this should now return error or not:
I am expecting to NOT return error since |
Before 0.11.1, it will not raise error from length as value `''` is treated
as `empty`. No further validations will be made if value is `empty`
At 0.11.1, value `''` is treated as `defined`, validations will always be
made it value is `defined`.
That's explain the result you got.
So, I suggest to define `''` to be a kind of `not defined` value which same
as `null` and `undefined` to fix the problem
|
@mondwan , thanks for the answers for starters. Redefining My case for the validator is a form which has optional fields but if the user does enter a value, it should be validated (specifically it's an optional "New Password" field) I guess i need to re-define |
Yes. I know. It is just my suggestion.
Actually, I need the similar mechanism you mentioned.
I haven't studied usage of `isUndefined`. However, if you are trying to
solve this by regex, I guess regex like this `^.{0,5}$` should play well on
existing mechanism.
Regex is still possible to achieve what I need, but I don't know how to do
if using rules like `numericality` with avoiding empty value as discussed.
|
That's what I did, but I really think it should be fixed via the |
agree with alolis (I have a similar scenario) it would be nice to specify value:false. For the time being I'll downgrade validatejs. I didn't expect a breaking change in a minor release either... |
Hello @ansman , any comment for this issue? |
easiest way to handle this is by using the regexp before:
after
|
Description
Given I need to validate value is not
empty
, for example''
: Invalid' '
: ValidProblems
Option
presence
explicitly stateswhitespaces only
is not a valid string. So I head toformat
.However, default format validator always allows empty value (empty defined above) at the beginner which ignores my regex object.
Reproduce the problem
Here is the fiddle to demonstrate the problem: https://jsfiddle.net/8okbtbuz/
Given I have a regex
^.{1,64}$'
which ensures there is at least one character,validator.single()
always treat theempty
value is a valid one.Problematic codes spotted
https://github.com/ansman/validate.js/blob/master/validate.js#L948
It always return success for empty value. This is not good.....
The text was updated successfully, but these errors were encountered: