-
Notifications
You must be signed in to change notification settings - Fork 30
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
Static validate method does not apply defaults #44
Comments
Hi Jose, Ah yes, this one is technically not a bug but I can understand that it does not seem intuitive. You may have noticed that TL;DR: The reason I exposed this I will improve the API documentation to make things clearer |
better ? |
Got you! Yep, I think that is clear enough. The reason why we were using So our practical case is we get an external input that we want to validate as the first step and it could have more than one failed validation, so we want to return the complete list of errors. Are we missing another exposed method to only set defaults to the argument? Something like Thanks for your time! Is really appreciated your quick responses! |
Error collectors should collect all errors, not just the first one. Each error is displayed in a new line, maybe you missed the others ? See this example: See Custom error collectors section for more info on how to collect errors as a list and do whatever you want with them. To get what you want (cast + validation), calling the model constructor is indeed the best way to do it. About Therefore setting the defaults means assigning the prototype. A way to do this and only apply defaults would be |
OK! you put me in the right path! Before we weren't using the So, this should be the best way to do it: /**
* Validates the input against a Model.
* It returns an instance if valid. Or a list of errors if invalid.
* @param Model
* @param input
*/
static validate(Model, input) {
return new Promise((resolve, reject) => {
if (!(Model instanceof objectmodel)) {
return reject([new Error('Model should be an objectmodel class')]);
}
// before
// if (Model.test(input)) return resolve(new Model(input));
// return Model.validate(input, errors => reject(errors));
// after
const errorCollector = _.clone(Model.errorCollector); // backup
Model.errorCollector = errors => reject(errors); // catch errors
const instance = new Model(input);
if (instance) {
instance.errorCollector = errorCollector; // restore
return resolve(instance);
}
return null;
});
} Does it sounds right to you? Thanks! |
You made a mistake here: Note that object model validation is synchronous but I guess you want to return a Promise for your convenience. So I would do it this way:
or with a try/catch block:
Of course if you're planning to always validate your model instances this way, it would be easier to globally change the errorCollector on Model.prototype. I assumed you made this for a specific reason such as form input validation. |
Yes, you are right. We want to change the behavior to initially get the list, but we want to roll back to the initial The promise interface keeps our code more predictable as it's unified with the rest of validation methods (most of them asynchronous). Your first example fits 100% what we are looking for, thanks so much! |
Hi!
We found another issue. You can create an object with defaults and those will apply to the object when you create the new instance and then it will validate the object. But using the static validate method does not preserve the same behavior.
Example:
We think validate should be aware of the defaults values before it perform the assertions. So both cases static and new instance should return the same result (valid or invalid).
Thanks!
The text was updated successfully, but these errors were encountered: