Add optional validator
eugene-p opened this issue · comments
Would be cool to have ability to mark value as optional.
I see 3 options
Have a second parameter for validation e.g:
value = nil -- or value = ""
valua:new().type("string").len(3,5)(value) -- returns false
valua:new().type("string").len(3,5)(value, true) -- returns true
Have a parameter on creation e.g:
value = nil -- or value = ""
valua:new().type("string").len(3,5)(value) -- returns false
valua:new(true).type("string").len(3,5)(value) -- returns true
Have stackable function e.g:
value = nil -- or value = ""
valua:new().type("string").len(3,5)(value) -- returns false
valua:new().optional().type("string").len(3,5)(value) -- returns true
You mean it is only validated if it's different than nil or empty string? Yea :) We could arrange that :)
Exactly. Not sure though if 0 should act the same
0 definitely shouldn't act the same way! I think it would go against with some very basic Lua principles. Normally I wouldn't even be sure about empty strings either but the thing is thay I use valua mainly for sailor which means form web input so it's never nil and rather an empty string. What do you think about that?
Same here. That is why I mentioned empty string.
So have some time this week and can take a crack at this issue. What would be the better way to implement optional parameter?
I also found commit e999377
Would it make more sense to revert those to original way and handle empty through optional flag/parameter/function though i do understand that it would brake compatibility.
It must be optional()
method for validator declaration.
Have stackable function e.g:
value = nil valua:new().optional().type("string").len(3,5)(value) -- returns true
For example database table may have default values for some fields of any type. optional()
method is definitely the most readable solution.
It's sad that this feature isn't implemented yet :c