-
Notifications
You must be signed in to change notification settings - Fork 242
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
Streamline validations #362
Comments
Good proposal! Just a question: would you like to join our tiny team and implement it yourself or just put it to out loooong list of upcoming stuff. If you like to go for it I would be happy to provide you more information about process. Thank you, @yanickrochon! |
Sure, I'd be happy to put my shoulder to the wheel! I'll fork What do you think? |
Excellent! We're happy to have you on board. I think this is the best solution to build as an add-on or dependency and I added you to a team, so you have admin/push/pull access to Let me know if you have any other questions or I can help you somehow. On Fri, Jan 24, 2014 at 1:38 PM, Yanick Rochon notifications@github.comwrote:
|
Thanks! That's a good starting point. I will start working on this today. And I agree that, at least for the time being, all current tests will continue to pass, perhaps until the next major release. Glad to be on board! |
An update to the state of this project...The repository has been created here (thanks Anatoliy Chakkaev). All "standard" validators has been implemented, and should be compatible with the "legacy" JugglingDB validation functions.
The validators and validations are thoroughly tested, however there should be some more adjustments during the integration phase with the core project. Documentations will come as soon as the integration is done and a PR is submitted. |
@yanickrochon I'm finishing my promise version of jugglingdb, and all validations are async by default. I'm just waiting for your green light to port it over to my version |
@pocesar Is this promise version official? |
not yet, but intend to be, it's a dual API, work with old callbacks, but most of sync functions were turned into promise ones |
Why not let async validation callback accept error message as arguments? Instead of call |
Because errors are set directly unto the model. The callback receives an boolean indicating if the model has errors or not. That's all. If the boolean was not passed, then we'd need to manually check if |
Rationale
The first thing I noticed when reading the documentation (I'm actually evaluating this project as a suitable ORM for our own project) was the hard-coded base validators and it's lack of flexibility. Reading the issues, I see many tickets on the subject.
Having a non-negligible Zend Framework background, the way validators are handled has inspired many other projects, as it allows effortless flexibility and extensibility. An other such project is Backbone.Validation.
Suggestion
So, instead of
I would have something like
Then, adding new validators could be done like
Internationalisation
All error messages should support a
sprintf
-like syntax. With Node, this is supported via theutil.format
function. The default fallback message on failed validation (if the validation function returnsfalse
) could be"'%s' validation failed!"
, which will be formatted with the validatedfieldName
.To enable translation of these messages, simply provide a function to
Obviously, since all (failed) validation messages are formatted inside the validator functions, before being returned, the translations should also be performed before formatting the message. Therefore, and since we already have a reference to
Schema.Validation
, I would proposeIntegration and Backward compatibility
To support existing projects, all current implementations could remain unchanged and simply proxying to the new validation system, to be deprecated at the next minor release (version 0.3). Thus, the built-in current validators could still be shipped with the ORM, along with new ones provided by the community.
validatesPresenceOf
becomesrequired
Options
The model field must be specified, with any given value.
validatesLengthOf
becomesminLength
andmaxLength
Options
For strings, check it's
length
property. Ignored for any other type.validatesInclusionOf
becomesin
Options
The
values
array items' type should be compatible with the model's field type.validatesExclusionOf
becomesnotIn
Options
The
values
array items' type should be compatible with the model's field type.validatesNumericalityOf
becomesnumber
,integer
,decimal
, orfinite
Options
For
number
, the value must be any numeric value.For
integer
, the value must be an integer value.For
decimal
, the value must not be an integer value.For
finite
, the value must be finite.validatesUniquenessOf
becomesunique
Options
This validator checks in all the set for another model with the same field value. This validator is asynchronous.
Other proposed validations
From the sails.js framework.
The text was updated successfully, but these errors were encountered: