@astral/validations 4.11.0 → 4.13.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.
package/README.md CHANGED
@@ -36,6 +36,8 @@
36
36
  - [mobilePhone](#mobilePhone)
37
37
  - [innUL](#innUL)
38
38
  - [innIP](#innIP)
39
+ - [innFL](#innFL)
40
+ - [innTwelveSymbols](#innTwelveSymbols)
39
41
  - [kpp](#kpp)
40
42
  - [ogrnIP](#ogrnIP)
41
43
  - [ogrnUL](#ogrnUL)
@@ -45,6 +47,7 @@
45
47
  - [passportSeries](#passportSeries)
46
48
  - [passportNumber](#passportNumber)
47
49
  - [passportCode](#passportCode)
50
+ - [textField](#textField)
48
51
  - [date](#date)
49
52
  - [min](#min-date)
50
53
  - [max](#max-date)
@@ -549,6 +552,27 @@ validate('156-573-259 92')
549
552
 
550
553
  ---
551
554
 
555
+ ### textField
556
+
557
+ Проверяет валидно ли текстовое поле
558
+
559
+ ```ts
560
+ import { string, textField } from '@astral/validations';
561
+
562
+ const validate = string(textField());
563
+
564
+ // undefined
565
+ validate('!@#$%^&*()-_=+|[]{};:",.<>/?')
566
+ validate('абвАБВ abcABC')
567
+
568
+ // { message: 'Содержит запрещённые символы' }
569
+ validate('😀')
570
+ validate('∑')
571
+ validate('٩(◕‿◕。)۶')
572
+ ```
573
+
574
+ ---
575
+
552
576
  ### mobilePhone
553
577
 
554
578
  - Проверяет валиден ли мобильный телефон
@@ -616,6 +640,51 @@ validate('+384212952720')
616
640
 
617
641
  ---
618
642
 
643
+ ### innFL
644
+
645
+ Проверяет валиден ли ИНН ФЛ
646
+
647
+ ```ts
648
+ import { string, innFL } from '@astral/validations';
649
+
650
+ const validate = string(innFL());
651
+
652
+ // undefined
653
+ validate('384212952720')
654
+ validate('000000000000')
655
+
656
+ // { message: 'Некорректный ИНН ФЛ' }
657
+ validate('3842129527')
658
+ validate('384212952a20')
659
+ validate('+384212952720')
660
+ ```
661
+
662
+ :information_source: Поддерживает [exclude](#exclusion-managing)
663
+
664
+ ---
665
+
666
+ ### innTwelveSymbols
667
+
668
+ Проверяет валиден ли ИНН из 12 символов
669
+
670
+ ```ts
671
+ import { string, innTwelveSymbols } from '@astral/validations';
672
+
673
+ const validate = string(innTwelveSymbols());
674
+
675
+ // undefined
676
+ validate('384212952720')
677
+
678
+ // { message: 'Некорректный ИНН' }
679
+ validate('3842129527')
680
+ validate('384212952a20')
681
+ validate('+384212952720')
682
+ ```
683
+
684
+ :information_source: Поддерживает [exclude](#exclusion-managing)
685
+
686
+ ---
687
+
619
688
  ### kpp
620
689
 
621
690
  Проверяет валиден ли КПП
package/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  export { object, objectAsync, OBJECT_TYPE_ERROR_INFO, type Schema, type SchemaValue, type ObjectGuard, type ObjectAsyncGuard, } from './object';
2
2
  export { optional, optionalAsync } from './optional';
3
3
  export { string, STRING_TYPE_ERROR_INFO, stringAsync } from './string';
4
+ export { textField, INVALID_TEXT_FIELD_ERROR_INFO, LENGTH_TEXT_FIELD_ERROR_INFO, } from './textField';
4
5
  export { date, INVALID_DATE_ERROR_INFO, DATE_TYPE_ERROR_INFO } from './date';
5
6
  export { number, NAN_NUMBER_ERROR_INFO, NUMBER_TYPE_ERROR_INFO, INFINITY_NUMBER_ERROR_INFO, } from './number';
6
7
  export { boolean, BOOLEAN_TYPE_ERROR_INFO } from './boolean';
@@ -24,7 +25,9 @@ export { guid, INVALID_GUID_ERROR_INFO } from './guid';
24
25
  export { length, STRING_LENGTH_ERROR_CODE } from './length';
25
26
  export { mobilePhone, MOBILE_PHONE_ERROR_INFO } from './mobilePhone';
26
27
  export { innUL, INN_UL_ERROR_INFO } from './innUL';
28
+ export { innTwelveSymbols, INN_12_SYMBOLS_ERROR_INFO, } from './innTwelveSymbols';
27
29
  export { innIP, INN_IP_ERROR_INFO } from './innIP';
30
+ export { innFL, INN_FL_ERROR_INFO } from './innFL';
28
31
  export { kpp, INVALID_KPP_ERROR_INFO, KPP_DOUBLE_ZERO_START_ERROR_INFO, KPP_ZEROS_ONLY_ERROR_INFO, } from './kpp';
29
32
  export { snils, SNILS_ERROR_INFO } from './snils';
30
33
  export { createRule, REQUIRED_ERROR_INFO, type ValidationRule } from './core';
package/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  export { object, objectAsync, OBJECT_TYPE_ERROR_INFO, } from './object';
2
2
  export { optional, optionalAsync } from './optional';
3
3
  export { string, STRING_TYPE_ERROR_INFO, stringAsync } from './string';
4
+ export { textField, INVALID_TEXT_FIELD_ERROR_INFO, LENGTH_TEXT_FIELD_ERROR_INFO, } from './textField';
4
5
  export { date, INVALID_DATE_ERROR_INFO, DATE_TYPE_ERROR_INFO } from './date';
5
6
  export { number, NAN_NUMBER_ERROR_INFO, NUMBER_TYPE_ERROR_INFO, INFINITY_NUMBER_ERROR_INFO, } from './number';
6
7
  export { boolean, BOOLEAN_TYPE_ERROR_INFO } from './boolean';
@@ -24,7 +25,9 @@ export { guid, INVALID_GUID_ERROR_INFO } from './guid';
24
25
  export { length, STRING_LENGTH_ERROR_CODE } from './length';
25
26
  export { mobilePhone, MOBILE_PHONE_ERROR_INFO } from './mobilePhone';
26
27
  export { innUL, INN_UL_ERROR_INFO } from './innUL';
28
+ export { innTwelveSymbols, INN_12_SYMBOLS_ERROR_INFO, } from './innTwelveSymbols';
27
29
  export { innIP, INN_IP_ERROR_INFO } from './innIP';
30
+ export { innFL, INN_FL_ERROR_INFO } from './innFL';
28
31
  export { kpp, INVALID_KPP_ERROR_INFO, KPP_DOUBLE_ZERO_START_ERROR_INFO, KPP_ZEROS_ONLY_ERROR_INFO, } from './kpp';
29
32
  export { snils, SNILS_ERROR_INFO } from './snils';
30
33
  export { createRule, REQUIRED_ERROR_INFO } from './core';
@@ -0,0 +1,2 @@
1
+ import { ErrorInfo } from '../core';
2
+ export declare const INN_FL_ERROR_INFO: ErrorInfo;
@@ -0,0 +1,5 @@
1
+ import { createErrorCode } from '../core';
2
+ export const INN_FL_ERROR_INFO = {
3
+ code: createErrorCode('innFL'),
4
+ message: 'Некорректный ИНН ФЛ',
5
+ };
@@ -0,0 +1,2 @@
1
+ export * from './innFL';
2
+ export * from './constants';
package/innFL/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from './innFL';
2
+ export * from './constants';
@@ -0,0 +1,17 @@
1
+ import { CommonRuleParams } from '../core';
2
+ type InnFLParams = CommonRuleParams<string> & {
3
+ /**
4
+ * @description Замена стандартного сообщения ошибки.
5
+ */
6
+ message?: string;
7
+ };
8
+ /**
9
+ * @description Проверяет валиден ли ИНН ФЛ
10
+ * @example
11
+ * ```ts
12
+ * const validate = string(innFL());
13
+ * validate('7728168971');
14
+ * ```
15
+ */
16
+ export declare const innFL: <TLastSchemaValues extends Record<string, unknown>>(params?: InnFLParams) => (value: string, prevCtx?: import("../core").ValidationContext<TLastSchemaValues> | undefined) => import("../core").ValidationResult;
17
+ export {};
package/innFL/innFL.js ADDED
@@ -0,0 +1,21 @@
1
+ import { createRule } from '../core';
2
+ import { innTwelveSymbols } from '../innTwelveSymbols';
3
+ import { INN_FL_ERROR_INFO } from './constants';
4
+ /**
5
+ * @description Проверяет валиден ли ИНН ФЛ
6
+ * @example
7
+ * ```ts
8
+ * const validate = string(innFL());
9
+ * validate('7728168971');
10
+ * ```
11
+ */
12
+ export const innFL = (params) => createRule((value, ctx) => {
13
+ const createInnFLError = () => ctx.createError({
14
+ message: (params === null || params === void 0 ? void 0 : params.message) || INN_FL_ERROR_INFO.message,
15
+ code: INN_FL_ERROR_INFO.code,
16
+ });
17
+ if (innTwelveSymbols()(value) !== undefined) {
18
+ return createInnFLError();
19
+ }
20
+ return undefined;
21
+ }, { exclude: params === null || params === void 0 ? void 0 : params.exclude });
@@ -1,5 +1,2 @@
1
1
  import { ErrorInfo } from '../core';
2
2
  export declare const INN_IP_ERROR_INFO: ErrorInfo;
3
- export declare const INN_IP_LENGTH = 12;
4
- export declare const FIRST_INN_IP_DECODING: number[];
5
- export declare const SECOND_INN_IP_DECODING: number[];
@@ -3,6 +3,3 @@ export const INN_IP_ERROR_INFO = {
3
3
  code: createErrorCode('innIP'),
4
4
  message: 'Некорректный ИНН ИП',
5
5
  };
6
- export const INN_IP_LENGTH = 12;
7
- export const FIRST_INN_IP_DECODING = [7, 2, 4, 10, 3, 5, 9, 4, 6, 8];
8
- export const SECOND_INN_IP_DECODING = [3, 7, 2, 4, 10, 3, 5, 9, 4, 6, 8];
package/innIP/innIP.js CHANGED
@@ -1,15 +1,6 @@
1
1
  import { createRule, isNoDoubleZeroStart, isStringOfZeros, } from '../core';
2
- import { FIRST_INN_IP_DECODING, INN_IP_ERROR_INFO, INN_IP_LENGTH, SECOND_INN_IP_DECODING, } from './constants';
3
- const calcFirstCheckSumForInnIP = (arrSymbols) => (arrSymbols
4
- .slice(0, -2)
5
- .reduce((sum, symbol, index) => FIRST_INN_IP_DECODING[index] * Number(symbol) + sum, 0) %
6
- 11) %
7
- 10;
8
- const calcSecondCheckSumForInnIP = (arrSymbols) => (arrSymbols
9
- .slice(0, -1)
10
- .reduce((sum, symbol, index) => SECOND_INN_IP_DECODING[index] * Number(symbol) + sum, 0) %
11
- 11) %
12
- 10;
2
+ import { innTwelveSymbols } from '../innTwelveSymbols';
3
+ import { INN_IP_ERROR_INFO } from './constants';
13
4
  /**
14
5
  * @description Проверяет валиден ли ИНН ИП
15
6
  * @example
@@ -29,14 +20,7 @@ export const innIP = (params) => createRule((value, ctx) => {
29
20
  if (!isNoDoubleZeroStart(value)) {
30
21
  return createInnIPError();
31
22
  }
32
- if (value.length !== INN_IP_LENGTH) {
33
- return createInnIPError();
34
- }
35
- const arrSymbols = value.split('');
36
- const firstChecksum = calcFirstCheckSumForInnIP(arrSymbols);
37
- const secondChecksum = calcSecondCheckSumForInnIP(arrSymbols);
38
- if (Number(value[10]) !== firstChecksum &&
39
- Number(value[11]) !== secondChecksum) {
23
+ if (innTwelveSymbols()(value) !== undefined) {
40
24
  return createInnIPError();
41
25
  }
42
26
  return undefined;
@@ -0,0 +1,5 @@
1
+ import { ErrorInfo } from '../core';
2
+ export declare const INN_12_SYMBOLS_ERROR_INFO: ErrorInfo;
3
+ export declare const INN_12_SYMBOLS_LENGTH = 12;
4
+ export declare const FIRST_INN_12_SYMBOLS_DECODING: number[];
5
+ export declare const SECOND_INN_12_SYMBOLS_DECODING: number[];
@@ -0,0 +1,10 @@
1
+ import { createErrorCode } from '../core';
2
+ export const INN_12_SYMBOLS_ERROR_INFO = {
3
+ code: createErrorCode('innTwelveSymbols'),
4
+ message: 'Некорректный ИНН',
5
+ };
6
+ export const INN_12_SYMBOLS_LENGTH = 12;
7
+ export const FIRST_INN_12_SYMBOLS_DECODING = [7, 2, 4, 10, 3, 5, 9, 4, 6, 8];
8
+ export const SECOND_INN_12_SYMBOLS_DECODING = [
9
+ 3, 7, 2, 4, 10, 3, 5, 9, 4, 6, 8,
10
+ ];
@@ -0,0 +1,2 @@
1
+ export * from './innTwelveSymbols';
2
+ export * from './constants';
@@ -0,0 +1,2 @@
1
+ export * from './innTwelveSymbols';
2
+ export * from './constants';
@@ -0,0 +1,17 @@
1
+ import { CommonRuleParams } from '../core';
2
+ type InnTwelveSymbolsParams = CommonRuleParams<string> & {
3
+ /**
4
+ * @description Замена стандартного сообщения ошибки.
5
+ */
6
+ message?: string;
7
+ };
8
+ /**
9
+ * @description Проверяет, валиден ли ИНН из 12 символов
10
+ * @example
11
+ * ```ts
12
+ * const validate = string(innTwelveSymbols());
13
+ * validate('7728168971');
14
+ * ```
15
+ */
16
+ export declare const innTwelveSymbols: <TLastSchemaValues extends Record<string, unknown>>(params?: InnTwelveSymbolsParams) => (value: string, prevCtx?: import("../core").ValidationContext<TLastSchemaValues> | undefined) => import("../core").ValidationResult;
17
+ export {};
@@ -0,0 +1,37 @@
1
+ import { createRule } from '../core';
2
+ import { FIRST_INN_12_SYMBOLS_DECODING, INN_12_SYMBOLS_ERROR_INFO, INN_12_SYMBOLS_LENGTH, SECOND_INN_12_SYMBOLS_DECODING, } from './constants';
3
+ const firstCheckSumForInnTwelveSymbols = (arrSymbols) => (arrSymbols
4
+ .slice(0, -2)
5
+ .reduce((sum, symbol, index) => FIRST_INN_12_SYMBOLS_DECODING[index] * Number(symbol) + sum, 0) %
6
+ 11) %
7
+ 10;
8
+ const secondCheckSumForInnTwelveSymbols = (arrSymbols) => (arrSymbols
9
+ .slice(0, -1)
10
+ .reduce((sum, symbol, index) => SECOND_INN_12_SYMBOLS_DECODING[index] * Number(symbol) + sum, 0) %
11
+ 11) %
12
+ 10;
13
+ /**
14
+ * @description Проверяет, валиден ли ИНН из 12 символов
15
+ * @example
16
+ * ```ts
17
+ * const validate = string(innTwelveSymbols());
18
+ * validate('7728168971');
19
+ * ```
20
+ */
21
+ export const innTwelveSymbols = (params) => createRule((value, ctx) => {
22
+ const createInnTwelveSymbolsError = () => ctx.createError({
23
+ message: (params === null || params === void 0 ? void 0 : params.message) || INN_12_SYMBOLS_ERROR_INFO.message,
24
+ code: INN_12_SYMBOLS_ERROR_INFO.code,
25
+ });
26
+ if (value.length !== INN_12_SYMBOLS_LENGTH) {
27
+ return createInnTwelveSymbolsError();
28
+ }
29
+ const arrSymbols = value.split('');
30
+ const firstChecksum = firstCheckSumForInnTwelveSymbols(arrSymbols);
31
+ const secondChecksum = secondCheckSumForInnTwelveSymbols(arrSymbols);
32
+ if (Number(value[10]) !== firstChecksum ||
33
+ Number(value[11]) !== secondChecksum) {
34
+ return createInnTwelveSymbolsError();
35
+ }
36
+ return undefined;
37
+ }, { exclude: params === null || params === void 0 ? void 0 : params.exclude });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astral/validations",
3
- "version": "4.11.0",
3
+ "version": "4.13.0",
4
4
  "browser": "./index.js",
5
5
  "main": "./index.js",
6
6
  "dependencies": {
@@ -0,0 +1,5 @@
1
+ import { ErrorInfo } from '../core';
2
+ export declare const TEXT_FIELD_REGEXP: RegExp;
3
+ export declare const TEXT_FIELD_MAX_LENGTH = 256;
4
+ export declare const INVALID_TEXT_FIELD_ERROR_INFO: ErrorInfo;
5
+ export declare const LENGTH_TEXT_FIELD_ERROR_INFO: ErrorInfo;
@@ -0,0 +1,11 @@
1
+ import { createErrorCode } from '../core';
2
+ export const TEXT_FIELD_REGEXP = /^[a-zA-Zа-яА-Я0-9\s!@#$%^&*()-_=+\\|[\]{};:'",.<>/?]+$/;
3
+ export const TEXT_FIELD_MAX_LENGTH = 256;
4
+ export const INVALID_TEXT_FIELD_ERROR_INFO = {
5
+ code: createErrorCode('textField-invalid'),
6
+ message: 'Содержит запрещённые символы',
7
+ };
8
+ export const LENGTH_TEXT_FIELD_ERROR_INFO = {
9
+ code: createErrorCode('textField-length'),
10
+ message: 'Превышено максимальное кол-во символов',
11
+ };
@@ -0,0 +1,2 @@
1
+ export * from './constants';
2
+ export * from './textField';
@@ -0,0 +1,2 @@
1
+ export * from './constants';
2
+ export * from './textField';
@@ -0,0 +1,24 @@
1
+ type TextFieldParams = {
2
+ /**
3
+ * @description Замена стандартного сообщения ошибки.
4
+ */
5
+ customLength?: number;
6
+ /**
7
+ * @description Замена стандартного сообщения ошибки.
8
+ */
9
+ message?: string;
10
+ /**
11
+ * @description Замена стандартного сообщения ошибки при превышении допустимого количества символов.
12
+ */
13
+ invalidLengthMessage?: string;
14
+ };
15
+ /**
16
+ * @description Проверяет валидность текстового поля
17
+ * @example
18
+ * ```ts
19
+ * const validate = string(textField());
20
+ * validate('Авада кедавра...');
21
+ * ```
22
+ */
23
+ export declare const textField: <TLastSchemaValues extends Record<string, unknown>>(params?: TextFieldParams) => (value: string, prevCtx?: import("../core").ValidationContext<TLastSchemaValues> | undefined) => import("../core").ValidationResult;
24
+ export {};
@@ -0,0 +1,19 @@
1
+ import { createRule } from '../core';
2
+ import { INVALID_TEXT_FIELD_ERROR_INFO, LENGTH_TEXT_FIELD_ERROR_INFO, TEXT_FIELD_MAX_LENGTH, TEXT_FIELD_REGEXP, } from './constants';
3
+ /**
4
+ * @description Проверяет валидность текстового поля
5
+ * @example
6
+ * ```ts
7
+ * const validate = string(textField());
8
+ * validate('Авада кедавра...');
9
+ * ```
10
+ */
11
+ export const textField = (params) => createRule((value, ctx) => {
12
+ if (value.length > ((params === null || params === void 0 ? void 0 : params.customLength) || TEXT_FIELD_MAX_LENGTH)) {
13
+ return ctx.createError(Object.assign(Object.assign({}, LENGTH_TEXT_FIELD_ERROR_INFO), { message: (params === null || params === void 0 ? void 0 : params.invalidLengthMessage) || LENGTH_TEXT_FIELD_ERROR_INFO.message }));
14
+ }
15
+ if (!TEXT_FIELD_REGEXP.test(value)) {
16
+ return ctx.createError(Object.assign(Object.assign({}, INVALID_TEXT_FIELD_ERROR_INFO), { message: (params === null || params === void 0 ? void 0 : params.message) || INVALID_TEXT_FIELD_ERROR_INFO.message }));
17
+ }
18
+ return undefined;
19
+ });