-
Notifications
You must be signed in to change notification settings - Fork 362
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
Validatable.validatesDateOf()
replacing exception thrown for invalid dates.
#718
Conversation
…emove throwing exception when parsing invalid date into model
@hotaru355 Thanks for the patch. Enforcing date value check as part of the validation is nice. What I'm wondering is what expectation a developer has against the following code: p1 = new Person({name: 'John', dob: 'X'}); Should we throw from the constructor as dob property has an invalid value? |
@hotaru355 Thanks for the PR. Sorry that it's been a while we last looked at it. It's good to include your changes. Could you please rebase? Thanks! |
Can one of the admins verify this patch? |
2 similar comments
Can one of the admins verify this patch? |
Can one of the admins verify this patch? |
@hotaru355, I actually rebase it for you. Could you please grant me access to your fork? Thanks! |
@hotaru355, your changes look good to us after rebasing and changing some test cases. Could you please grant me access to your fork (see instructions in my previous post)? Please respond by this Friday (April 21), otherwise we'd have to unfortunately close the PR. Thanks! |
I've created another PR for the changes suggested here and fixed the test cases in #1339. |
Please review this PR that fixes issue #592, but also takes a broader approach on how loopback handles invalid dates.
POST
request or instantiating a model with the following data throws an exception:I would like to propose changing this behavior since it allows any client to crash the server with invalid data. Unfortunately, trying to catch the error on the server is not possible by adding custom validation methods, since parsing the data into a model (and throwing the exception) happens before any validation method is called. The only feasible solution I see for this problem is to implement a
before save
hook on every model that contains a date field or maybe implement some middleware to handle date field errors (? not tested).validatesDateOf()
method which is added to any date field by default could be the cleaner solution. Making a request to the REST API with an invalid date field triggers a proper validation error as a client would expect it. Instantiating a model with an invalid date would not throw an exception, but callingisValid()
on it would return false, without having to add the validation method explicitly. Removing the default validation method can easily be done if so desired.