koorchik / LIVR

Language Independent Validation Rules Specification
http://livr-spec.org
289 stars 22 forks source link

Allow empty validation for a field #24

Closed amakhrov closed 8 years ago

amakhrov commented 8 years ago

My form allows arbitrary string value for one of the fields. And this field is optional (not required). How can I configure a LIVR rule for that? I tried to pass 'fieldName' => NULL and 'fieldName' => [] - both trigger an error after calling validate(): Exception: Rule [] not registered and Undefined index:

koorchik commented 8 years ago

Pass any rule. For example min_length

http://webbylab.github.io/livr-playground/#%7B%22rules%22%3A%22%7B%5Cn%20%20%20%20name%3A%20%7B%20min_length%3A%201%20%7D%5Cn%7D%22%2C%22data%22%3A%22%7B%5Cn%20%20%20%20name%3A%20'John'%5Cn%7D%22%7D

koorchik commented 8 years ago

We want to add new rule "any" in future versions of LIVR

amakhrov commented 8 years ago

Thanks. Passing any rule probably works as a temporary workaround - but it kind breaks the semantics. Why not allow an empty list of validations ([]) for such cases?

koorchik commented 8 years ago

I am going to update the spec (not sure will it be empty array or "any" rule) but I am not sure that the update matches your need. For example, if you say {fieldName: []} it means that user can pass string or array or hash. But in most cases developers want any string but not array or hash. Can you describe your case?

amakhrov commented 8 years ago

@koorchik you're right, in my case it should be an arbitrary string (even a missing field) - but not e.g. an array.

Sandvich commented 8 years ago

Hi, similar issue here. I need a field that can never fail validation. Any thoughts on how to do that? The default value is an empty string, but there's potential for other things to be stored in there too.

DanielHreben commented 8 years ago

@Sandvich, lock at the discussion above, answer is here.

Sandvich commented 8 years ago

@SDSWanderer Yeah, I'm currently using min_length as suggested. That crashes when you try and put an integer in there, however, as integers have no length (at least not in python).

DanielHreben commented 8 years ago

@Sandvich, ok, you can register you own rule (any for example). Rule implementation is trivial, just always return empty function.

Sandvich commented 8 years ago

Great, thanks!

koorchik commented 8 years ago

"string" rule will be added to the LIVR 2.0 instead of "any". Will match any string.