Skip to content

Email min/max property #213

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

Merged
merged 2 commits into from
Jan 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,8 @@ Property | Default | Description
`empty` | `false` | If `true`, the validator accepts an empty array `""`.
`mode` | `quick` | Checker method. Can be `quick` or `precise`.
`normalize` | `false` | Normalize the e-mail address (trim & lower-case). _It's a sanitizer, it will change the value in the original object._
`min` | `null` | Minimum value length.
`max` | `null` | Maximum value length.

## `enum`
This is an enum validator.
Expand Down Expand Up @@ -1333,7 +1335,10 @@ Name | Default text
`dateMin` | The '{field}' field must be greater than or equal to {expected}.
`dateMax` | The '{field}' field must be less than or equal to {expected}.
`forbidden` | The '{field}' field is forbidden.
`email` | The '{field}' field must be a valid e-mail.
‍‍`email` | The '{field}' field must be a valid e-mail.
`emailEmpty` | The '{field}' field must not be empty.
`emailMin` | The '{field}' field length must be greater than or equal to {expected} characters long.
`emailMax` | The '{field}' field length must be less than or equal to {expected} characters long.
`url` | The '{field}' field must be a valid URL.
`enumValue` | The '{field}' field value '{expected}' does not match any of the allowed values.
`equalValue` | The '{field}' field value must be equal to '{expected}'.
Expand Down
8 changes: 8 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@ declare module "fastest-validator" {
* Checker method. Can be quick or precise
*/
mode?: "quick" | "precise";
/**
* Minimum value length
*/
min?: number;
/**
* Maximum value length
*/
max?: number;

normalize?: boolean;
}
Expand Down
2 changes: 2 additions & 0 deletions lib/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ module.exports = {

email: "The '{field}' field must be a valid e-mail.",
emailEmpty: "The '{field}' field must not be empty.",
emailMin: "The '{field}' field length must be greater than or equal to {expected} characters long.",
emailMax: "The '{field}' field length must be less than or equal to {expected} characters long.",

luhn: "The '{field}' field must be a valid checksum luhn.",

Expand Down
16 changes: 16 additions & 0 deletions lib/rules/email.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ module.exports = function({ schema, messages }, path, context) {
`);
}

if (schema.min != null) {
src.push(`
if (value.length < ${schema.min}) {
${this.makeError({ type: "emailMin", expected: schema.min, actual: "value.length", messages })}
}
`);
}

if (schema.max != null) {
src.push(`
if (value.length > ${schema.max}) {
${this.makeError({ type: "emailMax", expected: schema.max, actual: "value.length", messages })}
}
`);
}

src.push(`
if (!${pattern.toString()}.test(value)) {
${this.makeError({ type: "email", actual: "value", messages })}
Expand Down
14 changes: 14 additions & 0 deletions test/rules/email.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,18 @@ describe("Test rule: email", () => {
});
});

it("check min length", () => {
const check = v.compile({ $$root: true, type: "email", min: 10 });

expect(check("[email protected]")).toEqual([{ type: "emailMin", expected: 10, actual: 7, message: "The '' field length must be greater than or equal to 10 characters long." }]);
expect(check("[email protected]")).toEqual(true);
});

it("check max length", () => {
const check = v.compile({ $$root: true, type: "email", max: 20 });

expect(check("[email protected]")).toEqual(true);
expect(check("[email protected]")).toEqual([{ type: "emailMax", expected: 20, actual: 45, message: "The '' field length must be less than or equal to 20 characters long." }]);
});

});