@astral/validations 4.14.1 → 4.14.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/email/constants.d.ts +2 -1
- package/email/constants.js +5 -1
- package/email/email.d.ts +4 -0
- package/email/email.js +17 -2
- package/package.json +1 -1
package/email/constants.d.ts
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
import { type ErrorInfo } from '../core';
|
2
2
|
export declare const EMAIL_REGEXP: RegExp;
|
3
|
-
export declare const EMAIL_MAX_LENGTH =
|
3
|
+
export declare const EMAIL_MAX_LENGTH = 254;
|
4
4
|
export declare const INVALID_EMAIL_ERROR_INFO: ErrorInfo;
|
5
5
|
export declare const LENGTH_EMAIL_ERROR_INFO: ErrorInfo;
|
6
|
+
export declare const DOUBLE_DOTS_EMAIL_ERROR_INFO: ErrorInfo;
|
package/email/constants.js
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
import { createErrorCode } from '../core';
|
2
2
|
export const EMAIL_REGEXP = /^[-\w.]+@([A-z0-9][-A-z0-9]+\.)+[A-z]{2,}$/;
|
3
|
-
export const EMAIL_MAX_LENGTH =
|
3
|
+
export const EMAIL_MAX_LENGTH = 254;
|
4
4
|
export const INVALID_EMAIL_ERROR_INFO = {
|
5
5
|
code: createErrorCode('email-invalid'),
|
6
6
|
message: 'Некорректный E-mail',
|
@@ -9,3 +9,7 @@ export const LENGTH_EMAIL_ERROR_INFO = {
|
|
9
9
|
code: createErrorCode('email-length'),
|
10
10
|
message: 'E-mail слишком длинный',
|
11
11
|
};
|
12
|
+
export const DOUBLE_DOTS_EMAIL_ERROR_INFO = {
|
13
|
+
code: createErrorCode('email-double-dots'),
|
14
|
+
message: 'Две точки подряд недопустимы',
|
15
|
+
};
|
package/email/email.d.ts
CHANGED
@@ -7,6 +7,10 @@ type EmailParams = {
|
|
7
7
|
* @description Замена стандартного сообщения ошибки при превышении допустимого количества символов.
|
8
8
|
*/
|
9
9
|
invalidLengthMessage?: string;
|
10
|
+
/**
|
11
|
+
* @description Замена стандартного сообщения ошибки для повторяющихся точек.
|
12
|
+
*/
|
13
|
+
doubleDotsErrorMessage?: string;
|
10
14
|
};
|
11
15
|
/**
|
12
16
|
* @description Проверяет валидность email. Не работает с русскими доменами
|
package/email/email.js
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
import { createRule } from '../core';
|
2
|
-
import { EMAIL_MAX_LENGTH, EMAIL_REGEXP, INVALID_EMAIL_ERROR_INFO, LENGTH_EMAIL_ERROR_INFO, } from './constants';
|
2
|
+
import { DOUBLE_DOTS_EMAIL_ERROR_INFO, EMAIL_MAX_LENGTH, EMAIL_REGEXP, INVALID_EMAIL_ERROR_INFO, LENGTH_EMAIL_ERROR_INFO, } from './constants';
|
3
3
|
/**
|
4
4
|
* @description Проверяет валидность email. Не работает с русскими доменами
|
5
5
|
* @example
|
@@ -9,11 +9,26 @@ import { EMAIL_MAX_LENGTH, EMAIL_REGEXP, INVALID_EMAIL_ERROR_INFO, LENGTH_EMAIL_
|
|
9
9
|
* ```
|
10
10
|
*/
|
11
11
|
export const email = (params) => createRule((value, ctx) => {
|
12
|
+
const invalidEmailError = ctx.createError(Object.assign(Object.assign({}, INVALID_EMAIL_ERROR_INFO), { message: (params === null || params === void 0 ? void 0 : params.message) || INVALID_EMAIL_ERROR_INFO.message }));
|
12
13
|
if (!EMAIL_REGEXP.test(value)) {
|
13
|
-
return
|
14
|
+
return invalidEmailError;
|
14
15
|
}
|
15
16
|
if (value.length > EMAIL_MAX_LENGTH) {
|
16
17
|
return ctx.createError(Object.assign(Object.assign({}, LENGTH_EMAIL_ERROR_INFO), { message: (params === null || params === void 0 ? void 0 : params.invalidLengthMessage) || LENGTH_EMAIL_ERROR_INFO.message }));
|
17
18
|
}
|
19
|
+
const [username, hostname] = value.split('@');
|
20
|
+
if (username.startsWith('.') || username.startsWith('-')) {
|
21
|
+
return invalidEmailError;
|
22
|
+
}
|
23
|
+
if (username.endsWith('.')) {
|
24
|
+
return invalidEmailError;
|
25
|
+
}
|
26
|
+
if (username.includes('..')) {
|
27
|
+
return ctx.createError(Object.assign(Object.assign({}, DOUBLE_DOTS_EMAIL_ERROR_INFO), { message: (params === null || params === void 0 ? void 0 : params.doubleDotsErrorMessage) ||
|
28
|
+
DOUBLE_DOTS_EMAIL_ERROR_INFO.message }));
|
29
|
+
}
|
30
|
+
if (hostname[2] === '-' && hostname[3] === '-') {
|
31
|
+
return invalidEmailError;
|
32
|
+
}
|
18
33
|
return undefined;
|
19
34
|
});
|