-
Notifications
You must be signed in to change notification settings - Fork 55
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
Laxer validation for properties and beforeValidate() as filter #150
Conversation
….beforeValidate() be used as a filter
I'm not totally opposed to this, but what you're wanting here could easily be achieved simply by using this.validatesWithFunction('password', function (val) {
return ((!val) || /^[^a-z]$/.test(val));
}); And of course if you were doing this a lot you'd make it a reusable function. It's not quite as declarative this way, but it's definitely clearer what exactly is going on. I do like the idea of passing the params to the And of course, it seems like another real problem here is the inability to set default values. This has been requested before, and something I think would be really beneficial to people. |
I agree, repurposing The reason why I'm in favor of dealing with
If this sounds like a doable plan, I'm happy to refactor as needed. |
Ah, I can see what you mean. Seems like allowing null (empty?) values is a common pattern. AR does indeed have both Default values are another whole can of worms, but definitely a feature we really want. Does that give you enough info for a rough implementation? |
This is now refactored to use |
Can we get some test and doc updates for this? Then I'll merge it ASAP! |
Will do. Could you take a look at lib/index.js? I'm thinking that the lines 919-930 should be moved to take place before beforeValidate calls and emits. Otherwise those may use data from non-camelized keys. Does it make sense, or I'm worrying over nothing? |
That's actually a good question. That code is there for conversion from balls of data passed from snake_case APIs. It's not entirely clear whether beforeValidate should get the raw params that were originally passed, or converted ones. Presumably the user knows what params have been passed, so they wouldn't be surprised to find snake_case keys in some cases. OTOH, it might be nice to allow writers of JS code to have the post-conversion camelCase keys to work with. Let's go ahead and move it, like you suggest. |
Where are we on this? Do you want to make those changes? Or do you want me to merge and make the change? |
I'm in a crunch mode at the moment, so can't get this finished for another few weeks. Would prefer to write a few tests before merging. |
…Null configuration
Looking forward to it! Lemme know what I can do to help. |
Assuming...
And:
...Current validation system will fail, because undefined property
password
will not validate against the regular expression validator I have created. However, typical database design allows default values for fields not mentioned in the insert query, not to mention use ofnull
.Laxer validation checks if the property's
required
parameter has been explicitly been set tofalse
. If that is the case, the validation test will not be run against the empty value, because we're signalling that empty/null is safe.Secondly, properties are passed to
beforeValidate()
callback as its first parameter. This is to allowbeforeValidate()
function to be used as a filter:Currently, validation would fail, because User has a validation tester which allows only lowercase letters through. In many cases -- particularly with data received from users -- we could safely enforce data formatting rules so that we won't have to worry about the format of the data we received.