@astral/validations 4.23.0 → 4.24.0

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.
@@ -1,2 +1,3 @@
1
1
  import { type ErrorInfo } from '../core';
2
2
  export declare const EMAIL_OR_PHONE_ERROR_INFO: ErrorInfo;
3
+ export declare const EMAIL_OR_PHONE_REQUIRED_ERROR_INFO: ErrorInfo;
@@ -1,5 +1,9 @@
1
- import { createErrorCode } from '../core';
1
+ import { REQUIRED_ERROR_INFO, createErrorCode } from '../core';
2
2
  export const EMAIL_OR_PHONE_ERROR_INFO = {
3
3
  code: createErrorCode('mobileOrPhone'),
4
4
  message: 'Некорректный формат',
5
5
  };
6
+ export const EMAIL_OR_PHONE_REQUIRED_ERROR_INFO = {
7
+ code: createErrorCode('mobileOrPhone-required'),
8
+ message: REQUIRED_ERROR_INFO.message,
9
+ };
@@ -1,3 +1,4 @@
1
+ import { type ValidationContext } from '../core';
1
2
  /**
2
3
  * Guard для поля "Email или телефон". Проверяет валидность email или мобильного телефона.
3
4
  * - Если строка содержит кириллицу, латиницу или символ @ - проверяется как email
@@ -9,4 +10,4 @@
9
10
  * validate({ value: '+7 (999) 999-99-99', normalizedValue: '79999999999', type: 'phone' });
10
11
  * ```
11
12
  */
12
- export declare const emailOrPhone: <TLastSchemaValues extends Record<string, unknown>>() => import("../core").Guard<TLastSchemaValues, {}>;
13
+ export declare const emailOrPhone: <TLastSchemaValues extends Record<string, unknown>>() => (value: unknown, ctx?: ValidationContext<TLastSchemaValues>) => import("../core").ValidationResult;
@@ -1,7 +1,7 @@
1
1
  import { createGuard } from '../core';
2
2
  import { email } from '../email';
3
3
  import { mobilePhone } from '../mobilePhone';
4
- import { EMAIL_OR_PHONE_ERROR_INFO } from './constants';
4
+ import { EMAIL_OR_PHONE_ERROR_INFO, EMAIL_OR_PHONE_REQUIRED_ERROR_INFO, } from './constants';
5
5
  /**
6
6
  * Guard для поля "Email или телефон". Проверяет валидность email или мобильного телефона.
7
7
  * - Если строка содержит кириллицу, латиницу или символ @ - проверяется как email
@@ -13,26 +13,41 @@ import { EMAIL_OR_PHONE_ERROR_INFO } from './constants';
13
13
  * validate({ value: '+7 (999) 999-99-99', normalizedValue: '79999999999', type: 'phone' });
14
14
  * ```
15
15
  */
16
- export const emailOrPhone = () => createGuard((value, ctx) => {
17
- if (!value ||
16
+ // Отдельная функциональная обертка необходима для работы с guard optional
17
+ export const emailOrPhone = () => (value, ctx) => createGuard((_, innerCtx) => {
18
+ const requiredValue = value &&
19
+ typeof value === 'object' &&
20
+ 'value' in value &&
21
+ !Boolean(value.value);
22
+ const checkValidObject = !value ||
18
23
  typeof value !== 'object' ||
19
24
  !('value' in value) ||
20
25
  !('normalizedValue' in value) ||
21
- !('type' in value)) {
22
- return ctx.createError({
26
+ !('type' in value);
27
+ if (requiredValue && (ctx === null || ctx === void 0 ? void 0 : ctx.isOptional)) {
28
+ return undefined;
29
+ }
30
+ if (requiredValue) {
31
+ return innerCtx.createError({
32
+ code: EMAIL_OR_PHONE_REQUIRED_ERROR_INFO.code,
33
+ message: EMAIL_OR_PHONE_REQUIRED_ERROR_INFO.message,
34
+ });
35
+ }
36
+ if (checkValidObject) {
37
+ return innerCtx.createError({
23
38
  code: EMAIL_OR_PHONE_ERROR_INFO.code,
24
39
  message: EMAIL_OR_PHONE_ERROR_INFO.message,
25
40
  });
26
41
  }
27
42
  const { value: formattedValue = '', normalizedValue = '', type, } = value;
28
43
  if (type === 'email') {
29
- return email()(formattedValue, ctx);
44
+ return email()(formattedValue, innerCtx);
30
45
  }
31
46
  if (type === 'phone') {
32
- return mobilePhone()(normalizedValue, ctx);
47
+ return mobilePhone()(normalizedValue, innerCtx);
33
48
  }
34
- return ctx.createError({
49
+ return innerCtx.createError({
35
50
  message: EMAIL_OR_PHONE_ERROR_INFO.message,
36
51
  code: EMAIL_OR_PHONE_ERROR_INFO.code,
37
52
  });
38
- });
53
+ })(value, ctx);
@@ -36,7 +36,7 @@ export function minYearsOld(age, params) {
36
36
  if (value < BIRTH_DATE_MIN) {
37
37
  return ctx.createError({
38
38
  code: BIRTH_DATE_MIN_ERROR_CODE,
39
- message: getMessage(`Не ранее ${BIRTH_DATE_MIN}`),
39
+ message: getMessage(`Не ранее ${BIRTH_DATE_MIN.toLocaleDateString('ru-RU')}`),
40
40
  });
41
41
  }
42
42
  return undefined;
@@ -1,2 +1,3 @@
1
1
  import { type ErrorInfo } from '../core';
2
2
  export declare const EMAIL_OR_PHONE_ERROR_INFO: ErrorInfo;
3
+ export declare const EMAIL_OR_PHONE_REQUIRED_ERROR_INFO: ErrorInfo;
@@ -1,8 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.EMAIL_OR_PHONE_ERROR_INFO = void 0;
3
+ exports.EMAIL_OR_PHONE_REQUIRED_ERROR_INFO = exports.EMAIL_OR_PHONE_ERROR_INFO = void 0;
4
4
  const core_1 = require("../core");
5
5
  exports.EMAIL_OR_PHONE_ERROR_INFO = {
6
6
  code: (0, core_1.createErrorCode)('mobileOrPhone'),
7
7
  message: 'Некорректный формат',
8
8
  };
9
+ exports.EMAIL_OR_PHONE_REQUIRED_ERROR_INFO = {
10
+ code: (0, core_1.createErrorCode)('mobileOrPhone-required'),
11
+ message: core_1.REQUIRED_ERROR_INFO.message,
12
+ };
@@ -1,3 +1,4 @@
1
+ import { type ValidationContext } from '../core';
1
2
  /**
2
3
  * Guard для поля "Email или телефон". Проверяет валидность email или мобильного телефона.
3
4
  * - Если строка содержит кириллицу, латиницу или символ @ - проверяется как email
@@ -9,4 +10,4 @@
9
10
  * validate({ value: '+7 (999) 999-99-99', normalizedValue: '79999999999', type: 'phone' });
10
11
  * ```
11
12
  */
12
- export declare const emailOrPhone: <TLastSchemaValues extends Record<string, unknown>>() => import("../core").Guard<TLastSchemaValues, {}>;
13
+ export declare const emailOrPhone: <TLastSchemaValues extends Record<string, unknown>>() => (value: unknown, ctx?: ValidationContext<TLastSchemaValues>) => import("../core").ValidationResult;
@@ -16,27 +16,42 @@ const constants_1 = require("./constants");
16
16
  * validate({ value: '+7 (999) 999-99-99', normalizedValue: '79999999999', type: 'phone' });
17
17
  * ```
18
18
  */
19
- const emailOrPhone = () => (0, core_1.createGuard)((value, ctx) => {
20
- if (!value ||
19
+ // Отдельная функциональная обертка необходима для работы с guard optional
20
+ const emailOrPhone = () => (value, ctx) => (0, core_1.createGuard)((_, innerCtx) => {
21
+ const requiredValue = value &&
22
+ typeof value === 'object' &&
23
+ 'value' in value &&
24
+ !Boolean(value.value);
25
+ const checkValidObject = !value ||
21
26
  typeof value !== 'object' ||
22
27
  !('value' in value) ||
23
28
  !('normalizedValue' in value) ||
24
- !('type' in value)) {
25
- return ctx.createError({
29
+ !('type' in value);
30
+ if (requiredValue && (ctx === null || ctx === void 0 ? void 0 : ctx.isOptional)) {
31
+ return undefined;
32
+ }
33
+ if (requiredValue) {
34
+ return innerCtx.createError({
35
+ code: constants_1.EMAIL_OR_PHONE_REQUIRED_ERROR_INFO.code,
36
+ message: constants_1.EMAIL_OR_PHONE_REQUIRED_ERROR_INFO.message,
37
+ });
38
+ }
39
+ if (checkValidObject) {
40
+ return innerCtx.createError({
26
41
  code: constants_1.EMAIL_OR_PHONE_ERROR_INFO.code,
27
42
  message: constants_1.EMAIL_OR_PHONE_ERROR_INFO.message,
28
43
  });
29
44
  }
30
45
  const { value: formattedValue = '', normalizedValue = '', type, } = value;
31
46
  if (type === 'email') {
32
- return (0, email_1.email)()(formattedValue, ctx);
47
+ return (0, email_1.email)()(formattedValue, innerCtx);
33
48
  }
34
49
  if (type === 'phone') {
35
- return (0, mobilePhone_1.mobilePhone)()(normalizedValue, ctx);
50
+ return (0, mobilePhone_1.mobilePhone)()(normalizedValue, innerCtx);
36
51
  }
37
- return ctx.createError({
52
+ return innerCtx.createError({
38
53
  message: constants_1.EMAIL_OR_PHONE_ERROR_INFO.message,
39
54
  code: constants_1.EMAIL_OR_PHONE_ERROR_INFO.code,
40
55
  });
41
- });
56
+ })(value, ctx);
42
57
  exports.emailOrPhone = emailOrPhone;
@@ -39,7 +39,7 @@ function minYearsOld(age, params) {
39
39
  if (value < constants_1.BIRTH_DATE_MIN) {
40
40
  return ctx.createError({
41
41
  code: constants_1.BIRTH_DATE_MIN_ERROR_CODE,
42
- message: getMessage(`Не ранее ${constants_1.BIRTH_DATE_MIN}`),
42
+ message: getMessage(`Не ранее ${constants_1.BIRTH_DATE_MIN.toLocaleDateString('ru-RU')}`),
43
43
  });
44
44
  }
45
45
  return undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astral/validations",
3
- "version": "4.23.0",
3
+ "version": "4.24.0",
4
4
  "browser": "./index.js",
5
5
  "main": "./mode/index.js",
6
6
  "dependencies": {