You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If an email is invalid for example foobar i'm expecting to get error.common.email as error message but i get error.common.required.
This is due to the fact that checkRequired does not only check for the mandatory value but also for the email validation:
My current alternative is to override this function by setting back the default mongoose behavior:
mongoose.SchemaTypes.Email.prototype.checkRequired = function (val) {
return val != null;
};
That way, requirement is handled as before and will throw error.common.required only if value is missing. In case of incorrect email, checkRequired will be true, passing the requirement validation and will fail later in the plugin lifecycle providing error.common.email error as expected.
Beware that my alternative only care about non null value for requirement. It might be interesting to take care of allowBlank options here as well.
The text was updated successfully, but these errors were encountered:
Hmm, I see what you mean, but I am not sure how it should work instead. At least at the time I made this, I think it just had 1 validation function. The code is very simple, and I haven't actually used it (or even mongoose) in several years, so I am bit out of touch with how it should work. Would you be interested in making a PR? The actual code is smaller than a description of the problem, so it seems like it might be just as easy to just change it to work however you think it should. I will accept whatever you think is a reasonable solution, and publish it for npm.
As you can see i've been away from this topic as well from a long time 😢
I'm not working with mongoose either. Not sure i'll be able to come back to this in the future.
Hi,
Following 2bd35bb and its origin issue #24, there might be some change of behavior in the way error message are defined.
Considering this schema:
If an email is invalid for example
foobar
i'm expecting to geterror.common.email
as error message but i geterror.common.required
.This is due to the fact that
checkRequired
does not only check for the mandatory value but also for the email validation:mongoose-type-email/index.js
Lines 35 to 37 in 07ca26a
From my POV,
checkRequired
should focus only on requirement, leaving the email validation to the plugin later lifecycle.Current code for checkRequired is :
My current alternative is to override this function by setting back the default mongoose behavior:
That way, requirement is handled as before and will throw
error.common.required
only if value is missing. In case of incorrect email,checkRequired
will be true, passing the requirement validation and will fail later in the plugin lifecycle providingerror.common.email
error as expected.Beware that my alternative only care about non null value for requirement. It might be interesting to take care of
allowBlank
options here as well.The text was updated successfully, but these errors were encountered: