@astral/validations 4.10.1 → 4.12.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
@@ -26,6 +26,7 @@
26
26
  - [max](#max-string)
27
27
  - [email](#email)
28
28
  - [guid](#guid)
29
+ - [length](#length)
29
30
  - [pattern](#pattern)
30
31
  - [onlyNumber](#onlyNumber)
31
32
  - [containsNumbers](#containsNumbers)
@@ -35,6 +36,8 @@
35
36
  - [mobilePhone](#mobilePhone)
36
37
  - [innUL](#innUL)
37
38
  - [innIP](#innIP)
39
+ - [innFL](#innFL)
40
+ - [innTwelveSymbols](#innTwelveSymbols)
38
41
  - [kpp](#kpp)
39
42
  - [ogrnIP](#ogrnIP)
40
43
  - [ogrnUL](#ogrnUL)
@@ -412,6 +415,25 @@ validate('x56a4180-h5aa-42ec-a945-5fd21dec0538');
412
415
 
413
416
  ---
414
417
 
418
+ ### length
419
+
420
+ Проверяет значение на соответствие длине.
421
+
422
+ ```ts
423
+ import { string, length } from '@astral/validations';
424
+
425
+ const validate = string(length(5));
426
+
427
+ // undefined
428
+ validate('aaaaa');
429
+
430
+
431
+ // { message: 'Кол-во символов должно быть: 5' }
432
+ validate('abc');
433
+ ```
434
+
435
+ ---
436
+
415
437
  ### pattern
416
438
 
417
439
  Проверяет строку на соответствие регулярному выражению.
@@ -596,6 +618,51 @@ validate('+384212952720')
596
618
 
597
619
  ---
598
620
 
621
+ ### innFL
622
+
623
+ Проверяет валиден ли ИНН ФЛ
624
+
625
+ ```ts
626
+ import { string, innFL } from '@astral/validations';
627
+
628
+ const validate = string(innFL());
629
+
630
+ // undefined
631
+ validate('384212952720')
632
+ validate('000000000000')
633
+
634
+ // { message: 'Некорректный ИНН ФЛ' }
635
+ validate('3842129527')
636
+ validate('384212952a20')
637
+ validate('+384212952720')
638
+ ```
639
+
640
+ :information_source: Поддерживает [exclude](#exclusion-managing)
641
+
642
+ ---
643
+
644
+ ### innTwelveSymbols
645
+
646
+ Проверяет валиден ли ИНН из 12 символов
647
+
648
+ ```ts
649
+ import { string, innTwelveSymbols } from '@astral/validations';
650
+
651
+ const validate = string(innTwelveSymbols());
652
+
653
+ // undefined
654
+ validate('384212952720')
655
+
656
+ // { message: 'Некорректный ИНН' }
657
+ validate('3842129527')
658
+ validate('384212952a20')
659
+ validate('+384212952720')
660
+ ```
661
+
662
+ :information_source: Поддерживает [exclude](#exclusion-managing)
663
+
664
+ ---
665
+
599
666
  ### kpp
600
667
 
601
668
  Проверяет валиден ли КПП
package/index.d.ts CHANGED
@@ -21,9 +21,12 @@ export { containsDifferentCases, CONTAINS_DIFFERENT_CASES_ERROR_CODE, } from './
21
21
  export { toPlainError } from './toPlainError';
22
22
  export { email, LENGTH_EMAIL_ERROR_INFO, INVALID_EMAIL_ERROR_INFO, } from './email';
23
23
  export { guid, INVALID_GUID_ERROR_INFO } from './guid';
24
+ export { length, STRING_LENGTH_ERROR_CODE } from './length';
24
25
  export { mobilePhone, MOBILE_PHONE_ERROR_INFO } from './mobilePhone';
25
26
  export { innUL, INN_UL_ERROR_INFO } from './innUL';
27
+ export { innTwelveSymbols, INN_12_SYMBOLS_ERROR_INFO, } from './innTwelveSymbols';
26
28
  export { innIP, INN_IP_ERROR_INFO } from './innIP';
29
+ export { innFL, INN_FL_ERROR_INFO } from './innFL';
27
30
  export { kpp, INVALID_KPP_ERROR_INFO, KPP_DOUBLE_ZERO_START_ERROR_INFO, KPP_ZEROS_ONLY_ERROR_INFO, } from './kpp';
28
31
  export { snils, SNILS_ERROR_INFO } from './snils';
29
32
  export { createRule, REQUIRED_ERROR_INFO, type ValidationRule } from './core';
package/index.js CHANGED
@@ -21,9 +21,12 @@ export { containsDifferentCases, CONTAINS_DIFFERENT_CASES_ERROR_CODE, } from './
21
21
  export { toPlainError } from './toPlainError';
22
22
  export { email, LENGTH_EMAIL_ERROR_INFO, INVALID_EMAIL_ERROR_INFO, } from './email';
23
23
  export { guid, INVALID_GUID_ERROR_INFO } from './guid';
24
+ export { length, STRING_LENGTH_ERROR_CODE } from './length';
24
25
  export { mobilePhone, MOBILE_PHONE_ERROR_INFO } from './mobilePhone';
25
26
  export { innUL, INN_UL_ERROR_INFO } from './innUL';
27
+ export { innTwelveSymbols, INN_12_SYMBOLS_ERROR_INFO, } from './innTwelveSymbols';
26
28
  export { innIP, INN_IP_ERROR_INFO } from './innIP';
29
+ export { innFL, INN_FL_ERROR_INFO } from './innFL';
27
30
  export { kpp, INVALID_KPP_ERROR_INFO, KPP_DOUBLE_ZERO_START_ERROR_INFO, KPP_ZEROS_ONLY_ERROR_INFO, } from './kpp';
28
31
  export { snils, SNILS_ERROR_INFO } from './snils';
29
32
  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 });
@@ -0,0 +1,2 @@
1
+ import { ErrorCode } from '../core';
2
+ export declare const STRING_LENGTH_ERROR_CODE: ErrorCode;
@@ -0,0 +1,2 @@
1
+ import { createErrorCode } from '../core';
2
+ export const STRING_LENGTH_ERROR_CODE = createErrorCode('stringLength');
@@ -0,0 +1,2 @@
1
+ export * from './length';
2
+ export * from './constants';
@@ -0,0 +1,2 @@
1
+ export * from './length';
2
+ export * from './constants';
@@ -0,0 +1,22 @@
1
+ type LengthParams = {
2
+ /**
3
+ * @description Замена стандартного сообщения ошибки.
4
+ */
5
+ message?: string;
6
+ };
7
+ /**
8
+ * @description Проверяет значение на соответствие длине. Работает с: string
9
+ * @param len - целевой кол-во символов
10
+ * @example
11
+ * // Длина строки должна быть 5 символов
12
+ * const validate = string(length(5));
13
+ *
14
+ * // undefined
15
+ * validate('aaaaa')
16
+ *
17
+ * // error
18
+ * validate('va')
19
+ *
20
+ */
21
+ export declare const length: <TLastSchemaValues extends Record<string, unknown>>(len: number, params?: LengthParams) => (value: string, prevCtx?: import("../core").ValidationContext<TLastSchemaValues> | undefined) => import("../core").ValidationResult;
22
+ export {};
@@ -0,0 +1,26 @@
1
+ import { createRule } from '../core';
2
+ import { STRING_LENGTH_ERROR_CODE } from './constants';
3
+ /**
4
+ * @description Проверяет значение на соответствие длине. Работает с: string
5
+ * @param len - целевой кол-во символов
6
+ * @example
7
+ * // Длина строки должна быть 5 символов
8
+ * const validate = string(length(5));
9
+ *
10
+ * // undefined
11
+ * validate('aaaaa')
12
+ *
13
+ * // error
14
+ * validate('va')
15
+ *
16
+ */
17
+ export const length = (len, params) => createRule((value, ctx) => {
18
+ const currentLength = value.trim().length;
19
+ if (currentLength !== len) {
20
+ return ctx.createError({
21
+ code: STRING_LENGTH_ERROR_CODE,
22
+ message: (params === null || params === void 0 ? void 0 : params.message) || `Кол-во символов должно быть: ${len}`,
23
+ });
24
+ }
25
+ return undefined;
26
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astral/validations",
3
- "version": "4.10.1",
3
+ "version": "4.12.0",
4
4
  "browser": "./index.js",
5
5
  "main": "./index.js",
6
6
  "dependencies": {