konsumer / konsumer/mongoose-type-email
checkRequired overrides email validation message.
- Dominant language
- JavaScript
- Stars
- 26
- Forks
- 13
- PR merge metrics
- No merged PRs in 30d
Description
Hi,
Following https://github.com/konsumer/mongoose-type-email/commit/2bd35bbdf4580551bc4d8fd2f90fae90c9e894e3 and its origin issue https://github.com/konsumer/mongoose-type-email/issues/24, there might be some change of behavior in the way error message are defined.
Considering this schema:
```js
const userSchema = mongoose.Schema({
email: {
type: mongoose.SchemaTypes.Email,
required: [true, "error.common.required"],
unique: true,
message: "error.common.email",
},
});
```
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: https://github.com/konsumer/mongoose-type-email/blob/07ca26a473124be5b717c3d44c7222f243ac409d/index.js#L35-L37
From my POV, `checkRequired` should focus only on requirement, leaving the email validation to the plugin later lifecycle.
Current code for checkRequired is :
```js
mongoose.SchemaTypes.Email.prototype.checkRequired = function (val) {
return typeof val === 'string' && validateEmail(val, this.options);
};
```
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.
Contributor guide
No contributing guide indexed for this repository
Research direction
Start in index.js around the Email.prototype.checkRequired implementation and read the linked origin issue and commit for context. Verify how required and invalid-email values are reported, including the allowBlank option mentioned in the issue. Done means missing values retain the required message while invalid non-empty emails receive the email-validation message.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100