@astral/validations 2.26.0 → 3.0.0-beta.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/LICENSE +1 -1
- package/README.md +1243 -1
- package/any/any.d.ts +21 -0
- package/any/any.js +14 -0
- package/any/index.d.ts +1 -0
- package/any/index.js +1 -0
- package/array/array.d.ts +15 -0
- package/array/array.js +21 -0
- package/array/constants.d.ts +2 -0
- package/array/constants.js +5 -0
- package/array/index.d.ts +2 -0
- package/array/index.js +2 -0
- package/arrayItem/arrayItem.d.ts +35 -0
- package/arrayItem/arrayItem.js +33 -0
- package/arrayItem/index.d.ts +1 -0
- package/arrayItem/index.js +1 -0
- package/boolean/boolean.d.ts +13 -0
- package/boolean/boolean.js +19 -0
- package/boolean/constants.d.ts +2 -0
- package/boolean/constants.js +5 -0
- package/boolean/index.d.ts +2 -0
- package/boolean/index.js +2 -0
- package/core/compose/compose.d.ts +6 -0
- package/{compose → core/compose}/compose.js +2 -3
- package/core/context/createContext/createContext.d.ts +8 -0
- package/core/context/createContext/createContext.js +15 -0
- package/core/context/createContext/index.d.ts +1 -0
- package/core/context/createContext/index.js +1 -0
- package/core/context/index.d.ts +2 -0
- package/core/context/index.js +2 -0
- package/core/context/types.d.ts +28 -0
- package/core/errors/ArrayError/ArrayError.d.ts +18 -0
- package/core/errors/ArrayError/ArrayError.js +10 -0
- package/core/errors/ArrayError/createArrayError/createArrayError.d.ts +5 -0
- package/core/errors/ArrayError/createArrayError/createArrayError.js +11 -0
- package/core/errors/ArrayError/createArrayError/index.d.ts +1 -0
- package/core/errors/ArrayError/createArrayError/index.js +1 -0
- package/core/errors/ArrayError/index.d.ts +2 -0
- package/core/errors/ArrayError/index.js +2 -0
- package/core/errors/ErrorMap/ErrorMap.d.ts +21 -0
- package/core/errors/ErrorMap/ErrorMap.js +10 -0
- package/core/errors/ErrorMap/createErrorMap/createErrorMap.d.ts +5 -0
- package/core/errors/ErrorMap/createErrorMap/createErrorMap.js +10 -0
- package/core/errors/ErrorMap/createErrorMap/index.d.ts +1 -0
- package/core/errors/ErrorMap/createErrorMap/index.js +1 -0
- package/core/errors/ErrorMap/index.d.ts +2 -0
- package/core/errors/ErrorMap/index.js +2 -0
- package/core/errors/SimpleError/SimpleError.d.ts +16 -0
- package/core/errors/SimpleError/SimpleError.js +9 -0
- package/core/errors/SimpleError/createSimpleError/createSimpleError.d.ts +6 -0
- package/core/errors/SimpleError/createSimpleError/createSimpleError.js +5 -0
- package/core/errors/SimpleError/createSimpleError/index.d.ts +1 -0
- package/core/errors/SimpleError/createSimpleError/index.js +1 -0
- package/core/errors/SimpleError/index.d.ts +2 -0
- package/core/errors/SimpleError/index.js +2 -0
- package/core/errors/createErrorCode/createErrorCode.d.ts +5 -0
- package/core/errors/createErrorCode/createErrorCode.js +4 -0
- package/core/errors/createErrorCode/index.d.ts +1 -0
- package/core/errors/createErrorCode/index.js +1 -0
- package/core/errors/index.d.ts +5 -0
- package/core/errors/index.js +5 -0
- package/core/errors/types.d.ts +18 -0
- package/core/errors/types.js +1 -0
- package/core/guard/createGuard/createGuard.d.ts +54 -0
- package/core/guard/createGuard/createGuard.js +41 -0
- package/core/guard/createGuard/index.d.ts +1 -0
- package/core/guard/createGuard/index.js +1 -0
- package/core/guard/index.d.ts +1 -0
- package/core/guard/index.js +1 -0
- package/core/index.d.ts +8 -0
- package/core/index.js +8 -0
- package/core/rule/createRule/createRule.d.ts +42 -0
- package/core/rule/createRule/createRule.js +27 -0
- package/core/rule/index.d.ts +3 -0
- package/core/rule/index.js +3 -0
- package/core/rule/required/constants.d.ts +2 -0
- package/core/rule/required/constants.js +5 -0
- package/core/rule/required/index.d.ts +2 -0
- package/core/rule/required/index.js +2 -0
- package/core/rule/required/required.d.ts +19 -0
- package/core/rule/required/required.js +25 -0
- package/core/rule/types.d.ts +15 -0
- package/core/rule/types.js +1 -0
- package/core/types.d.ts +9 -0
- package/core/types.js +1 -0
- package/{utils → core/utils}/index.d.ts +1 -1
- package/{utils → core/utils}/index.js +1 -1
- package/core/utils/isStringOfZeros/index.d.ts +1 -0
- package/core/utils/isStringOfZeros/index.js +1 -0
- package/{utils → core/utils/isStringOfZeros}/isStringOfZeros.d.ts +1 -1
- package/{utils → core/utils/isStringOfZeros}/isStringOfZeros.js +1 -1
- package/core/utils/resetTime/index.d.ts +1 -0
- package/core/utils/resetTime/index.js +1 -0
- package/core/utils/resetTime/resetTime.d.ts +5 -0
- package/core/utils/resetTime/resetTime.js +12 -0
- package/date/constants.d.ts +3 -0
- package/date/constants.js +9 -0
- package/date/date.d.ts +19 -0
- package/date/date.js +24 -0
- package/date/index.d.ts +1 -0
- package/date/index.js +1 -0
- package/deepPartial/deepPartial.d.ts +29 -0
- package/deepPartial/deepPartial.js +21 -0
- package/deepPartial/index.d.ts +1 -0
- package/deepPartial/index.js +1 -0
- package/email/constants.d.ts +5 -0
- package/email/constants.js +11 -0
- package/email/email.d.ts +28 -0
- package/email/email.js +19 -0
- package/email/index.d.ts +2 -0
- package/email/index.js +2 -0
- package/index.d.ts +24 -20
- package/index.js +24 -20
- package/innIP/constants.d.ts +5 -0
- package/innIP/constants.js +8 -0
- package/innIP/index.d.ts +2 -0
- package/innIP/index.js +2 -0
- package/innIP/innIP.d.ts +25 -0
- package/innIP/innIP.js +40 -0
- package/innUL/constants.d.ts +4 -0
- package/innUL/constants.js +7 -0
- package/innUL/index.d.ts +2 -0
- package/innUL/index.js +2 -0
- package/innUL/innUL.d.ts +25 -0
- package/innUL/innUL.js +36 -0
- package/kpp/constants.d.ts +3 -0
- package/kpp/constants.js +6 -0
- package/kpp/index.d.ts +2 -0
- package/kpp/index.js +2 -0
- package/kpp/kpp.d.ts +25 -0
- package/kpp/kpp.js +16 -0
- package/max/constants.d.ts +5 -0
- package/max/constants.js +5 -0
- package/max/index.d.ts +2 -0
- package/max/index.js +2 -0
- package/max/max.d.ts +26 -0
- package/max/max.js +42 -0
- package/min/constants.d.ts +5 -0
- package/min/constants.js +5 -0
- package/min/index.d.ts +2 -0
- package/min/index.js +2 -0
- package/min/min.d.ts +26 -0
- package/min/min.js +42 -0
- package/mobilePhone/constants.d.ts +3 -0
- package/mobilePhone/constants.js +6 -0
- package/mobilePhone/index.d.ts +2 -0
- package/mobilePhone/index.js +2 -0
- package/mobilePhone/mobilePhone.d.ts +27 -0
- package/mobilePhone/mobilePhone.js +21 -0
- package/number/constants.d.ts +4 -0
- package/number/constants.js +13 -0
- package/number/index.d.ts +2 -0
- package/number/index.js +2 -0
- package/number/number.d.ts +19 -0
- package/number/number.js +26 -0
- package/object/constants.d.ts +2 -0
- package/object/constants.js +5 -0
- package/object/index.d.ts +2 -0
- package/object/index.js +2 -0
- package/object/isEmptyErrors/index.d.ts +1 -0
- package/object/isEmptyErrors/index.js +1 -0
- package/object/isEmptyErrors/isEmptyErrors.d.ts +5 -0
- package/object/isEmptyErrors/isEmptyErrors.js +4 -0
- package/object/object.d.ts +52 -0
- package/object/object.js +50 -0
- package/ogrnIP/constants.d.ts +3 -0
- package/ogrnIP/constants.js +6 -0
- package/ogrnIP/index.d.ts +2 -0
- package/ogrnIP/index.js +2 -0
- package/ogrnIP/ogrnIP.d.ts +25 -0
- package/ogrnIP/ogrnIP.js +27 -0
- package/ogrnUL/constants.d.ts +3 -0
- package/ogrnUL/constants.js +6 -0
- package/ogrnUL/index.d.ts +2 -0
- package/ogrnUL/index.js +2 -0
- package/ogrnUL/ogrnUL.d.ts +25 -0
- package/ogrnUL/ogrnUL.js +27 -0
- package/onlyNumber/constants.d.ts +2 -0
- package/onlyNumber/constants.js +2 -0
- package/onlyNumber/index.d.ts +2 -0
- package/onlyNumber/index.js +2 -0
- package/onlyNumber/onlyNumber.d.ts +24 -0
- package/onlyNumber/onlyNumber.js +20 -0
- package/optional/index.d.ts +1 -0
- package/optional/index.js +1 -0
- package/optional/optional.d.ts +7 -0
- package/optional/optional.js +15 -0
- package/or/index.d.ts +1 -0
- package/or/index.js +1 -0
- package/or/or.d.ts +22 -0
- package/or/or.js +21 -0
- package/package.json +2 -2
- package/partial/index.d.ts +1 -0
- package/partial/index.js +1 -0
- package/partial/partial.d.ts +10 -0
- package/partial/partial.js +6 -0
- package/pattern/constants.d.ts +2 -0
- package/pattern/constants.js +2 -0
- package/pattern/index.d.ts +2 -0
- package/pattern/index.js +2 -0
- package/pattern/pattern.d.ts +26 -0
- package/pattern/pattern.js +22 -0
- package/snils/constants.d.ts +4 -0
- package/snils/constants.js +7 -0
- package/snils/index.d.ts +2 -0
- package/snils/index.js +2 -0
- package/snils/snils.d.ts +25 -0
- package/{isSNILS/isSNILS.js → snils/snils.js} +20 -21
- package/string/constants.d.ts +2 -0
- package/string/constants.js +5 -0
- package/string/index.d.ts +2 -0
- package/string/index.js +2 -0
- package/string/string.d.ts +2 -0
- package/string/string.js +8 -0
- package/toPlainError/index.d.ts +1 -0
- package/toPlainError/index.js +1 -0
- package/toPlainError/toPlainError.d.ts +35 -0
- package/toPlainError/toPlainError.js +49 -0
- package/toPlainError/utils/generateArrayPath/generateArrayPath.d.ts +4 -0
- package/toPlainError/utils/generateArrayPath/generateArrayPath.js +9 -0
- package/toPlainError/utils/generateArrayPath/index.d.ts +1 -0
- package/toPlainError/utils/generateArrayPath/index.js +1 -0
- package/toPlainError/utils/generateObjectPath/generateObjectPath.d.ts +4 -0
- package/toPlainError/utils/generateObjectPath/generateObjectPath.js +9 -0
- package/toPlainError/utils/generateObjectPath/index.d.ts +1 -0
- package/toPlainError/utils/generateObjectPath/index.js +1 -0
- package/toPlainError/utils/index.d.ts +2 -0
- package/toPlainError/utils/index.js +2 -0
- package/transform/index.d.ts +1 -0
- package/transform/index.js +1 -0
- package/transform/transform.d.ts +26 -0
- package/transform/transform.js +16 -0
- package/when/index.d.ts +1 -0
- package/when/index.js +1 -0
- package/when/when.d.ts +47 -0
- package/when/when.js +29 -0
- package/compose/compose.d.ts +0 -7
- package/composeSome/composeSome.d.ts +0 -8
- package/composeSome/composeSome.js +0 -25
- package/composeSome/index.d.ts +0 -1
- package/composeSome/index.js +0 -1
- package/constants.d.ts +0 -2
- package/constants.js +0 -2
- package/createRule/createRule.d.ts +0 -14
- package/createRule/createRule.js +0 -12
- package/isDate/index.d.ts +0 -1
- package/isDate/index.js +0 -1
- package/isDate/isDate.d.ts +0 -10
- package/isDate/isDate.js +0 -23
- package/isEmail/index.d.ts +0 -1
- package/isEmail/index.js +0 -1
- package/isEmail/isEmail.d.ts +0 -13
- package/isEmail/isEmail.js +0 -25
- package/isINNIP/index.d.ts +0 -1
- package/isINNIP/index.js +0 -1
- package/isINNIP/isINNIP.d.ts +0 -10
- package/isINNIP/isINNIP.js +0 -42
- package/isINNUL/index.d.ts +0 -1
- package/isINNUL/index.js +0 -1
- package/isINNUL/isINNUL.d.ts +0 -10
- package/isINNUL/isINNUL.js +0 -37
- package/isKPP/index.d.ts +0 -1
- package/isKPP/index.js +0 -1
- package/isKPP/isKPP.d.ts +0 -7
- package/isKPP/isKPP.js +0 -19
- package/isMaxLength/index.d.ts +0 -1
- package/isMaxLength/index.js +0 -1
- package/isMaxLength/isMaxLength.d.ts +0 -18
- package/isMaxLength/isMaxLength.js +0 -34
- package/isMaxValue/index.d.ts +0 -1
- package/isMaxValue/index.js +0 -1
- package/isMaxValue/isMaxValue.d.ts +0 -17
- package/isMaxValue/isMaxValue.js +0 -35
- package/isMaybeNumber/index.d.ts +0 -1
- package/isMaybeNumber/index.js +0 -1
- package/isMaybeNumber/isMaybeNumber.d.ts +0 -10
- package/isMaybeNumber/isMaybeNumber.js +0 -23
- package/isMinLength/index.d.ts +0 -1
- package/isMinLength/index.js +0 -1
- package/isMinLength/isMinLength.d.ts +0 -18
- package/isMinLength/isMinLength.js +0 -34
- package/isMinValue/index.d.ts +0 -1
- package/isMinValue/index.js +0 -1
- package/isMinValue/isMinValue.d.ts +0 -17
- package/isMinValue/isMinValue.js +0 -35
- package/isMobilePhone/index.d.ts +0 -1
- package/isMobilePhone/index.js +0 -1
- package/isMobilePhone/isMobilePhone.d.ts +0 -10
- package/isMobilePhone/isMobilePhone.js +0 -20
- package/isOGRNIP/index.d.ts +0 -1
- package/isOGRNIP/index.js +0 -1
- package/isOGRNIP/isOGRNIP.d.ts +0 -10
- package/isOGRNIP/isOGRNIP.js +0 -29
- package/isOGRNUL/index.d.ts +0 -1
- package/isOGRNUL/index.js +0 -1
- package/isOGRNUL/isOGRNUL.d.ts +0 -10
- package/isOGRNUL/isOGRNUL.js +0 -29
- package/isRequired/index.d.ts +0 -1
- package/isRequired/index.js +0 -1
- package/isRequired/isRequired.d.ts +0 -11
- package/isRequired/isRequired.js +0 -23
- package/isSNILS/index.d.ts +0 -1
- package/isSNILS/index.js +0 -1
- package/isSNILS/isSNILS.d.ts +0 -10
- package/types.d.ts +0 -4
- package/utils/isEmptyString.d.ts +0 -4
- package/utils/isEmptyString.js +0 -6
- package/yupAdapter/index.d.ts +0 -1
- package/yupAdapter/index.js +0 -1
- package/yupAdapter/yupAdapter.d.ts +0 -8
- package/yupAdapter/yupAdapter.js +0 -11
- /package/{compose → core/compose}/index.d.ts +0 -0
- /package/{compose → core/compose}/index.js +0 -0
- /package/{types.js → core/context/types.js} +0 -0
- /package/{createRule → core/rule/createRule}/index.d.ts +0 -0
- /package/{createRule → core/rule/createRule}/index.js +0 -0
@@ -0,0 +1,8 @@
|
|
1
|
+
import { createErrorCode } from '../core';
|
2
|
+
export const INN_IP_ERROR_INFO = {
|
3
|
+
code: createErrorCode('innIP'),
|
4
|
+
message: 'Некорректный ИНН ИП',
|
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/index.d.ts
ADDED
package/innIP/index.js
ADDED
package/innIP/innIP.d.ts
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
import { CommonRuleParams } from '../core';
|
2
|
+
type InnIPParams = CommonRuleParams<string> & {
|
3
|
+
/**
|
4
|
+
* @description Замена стандартного сообщения ошибки.
|
5
|
+
*/
|
6
|
+
message?: string;
|
7
|
+
};
|
8
|
+
/**
|
9
|
+
* @description Проверяет валиден ли ИНН ИП
|
10
|
+
* @example
|
11
|
+
* ```ts
|
12
|
+
* const validate = string(innIP());
|
13
|
+
* validate('384212952720');
|
14
|
+
* ```
|
15
|
+
*/
|
16
|
+
export declare const innIP: <TValues>(params?: InnIPParams) => (value: string, prevCtx?: Readonly<{
|
17
|
+
global: Readonly<{
|
18
|
+
values: TValues;
|
19
|
+
overrides: Readonly<{
|
20
|
+
objectIsPartial: boolean;
|
21
|
+
}>;
|
22
|
+
}>;
|
23
|
+
createError: ({ message, code }: import("../core").ErrorInfo) => import("../core").ValidationSimpleError<{}>;
|
24
|
+
}> | undefined) => import("../core").ValidationResult;
|
25
|
+
export {};
|
package/innIP/innIP.js
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
import { createRule, 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;
|
13
|
+
/**
|
14
|
+
* @description Проверяет валиден ли ИНН ИП
|
15
|
+
* @example
|
16
|
+
* ```ts
|
17
|
+
* const validate = string(innIP());
|
18
|
+
* validate('384212952720');
|
19
|
+
* ```
|
20
|
+
*/
|
21
|
+
export const innIP = (params) => createRule((value, ctx) => {
|
22
|
+
const createInnIPError = () => ctx.createError({
|
23
|
+
message: (params === null || params === void 0 ? void 0 : params.message) || INN_IP_ERROR_INFO.message,
|
24
|
+
code: INN_IP_ERROR_INFO.code,
|
25
|
+
});
|
26
|
+
if (isStringOfZeros(value)) {
|
27
|
+
return createInnIPError();
|
28
|
+
}
|
29
|
+
if (value.length !== INN_IP_LENGTH) {
|
30
|
+
return createInnIPError();
|
31
|
+
}
|
32
|
+
const arrSymbols = value.split('');
|
33
|
+
const firstChecksum = calcFirstCheckSumForInnIP(arrSymbols);
|
34
|
+
const secondChecksum = calcSecondCheckSumForInnIP(arrSymbols);
|
35
|
+
if (Number(value[10]) !== firstChecksum &&
|
36
|
+
Number(value[11]) !== secondChecksum) {
|
37
|
+
return createInnIPError();
|
38
|
+
}
|
39
|
+
return undefined;
|
40
|
+
}, { exclude: params === null || params === void 0 ? void 0 : params.exclude });
|
package/innUL/index.d.ts
ADDED
package/innUL/index.js
ADDED
package/innUL/innUL.d.ts
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
import { CommonRuleParams } from '../core';
|
2
|
+
type InnULParams = CommonRuleParams<string> & {
|
3
|
+
/**
|
4
|
+
* @description Замена стандартного сообщения ошибки.
|
5
|
+
*/
|
6
|
+
message?: string;
|
7
|
+
};
|
8
|
+
/**
|
9
|
+
* @description Проверяет валиден ли ИНН ЮЛ
|
10
|
+
* @example
|
11
|
+
* ```ts
|
12
|
+
* const validate = string(innUL());
|
13
|
+
* validate('7728168971');
|
14
|
+
* ```
|
15
|
+
*/
|
16
|
+
export declare const innUL: <TValues>(params?: InnULParams) => (value: string, prevCtx?: Readonly<{
|
17
|
+
global: Readonly<{
|
18
|
+
values: TValues;
|
19
|
+
overrides: Readonly<{
|
20
|
+
objectIsPartial: boolean;
|
21
|
+
}>;
|
22
|
+
}>;
|
23
|
+
createError: ({ message, code }: import("../core").ErrorInfo) => import("../core").ValidationSimpleError<{}>;
|
24
|
+
}> | undefined) => import("../core").ValidationResult;
|
25
|
+
export {};
|
package/innUL/innUL.js
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
import { createRule, isStringOfZeros } from '../core';
|
2
|
+
import { INN_UL_DECODING, INN_UL_ERROR_INFO, INN_UL_LENGTH } from './constants';
|
3
|
+
const calcCheckSumForInnUl = (arrSymbols) => (arrSymbols
|
4
|
+
.slice(0, -1)
|
5
|
+
.reduce((sum, symbol, index) => INN_UL_DECODING[index] * Number(symbol) + sum, 0) %
|
6
|
+
11) %
|
7
|
+
10;
|
8
|
+
/**
|
9
|
+
* @description Проверяет валиден ли ИНН ЮЛ
|
10
|
+
* @example
|
11
|
+
* ```ts
|
12
|
+
* const validate = string(innUL());
|
13
|
+
* validate('7728168971');
|
14
|
+
* ```
|
15
|
+
*/
|
16
|
+
export const innUL = (params) => createRule((value, ctx) => {
|
17
|
+
const createInnUlError = () => ctx.createError({
|
18
|
+
message: (params === null || params === void 0 ? void 0 : params.message) || INN_UL_ERROR_INFO.message,
|
19
|
+
code: INN_UL_ERROR_INFO.code,
|
20
|
+
});
|
21
|
+
if (isStringOfZeros(value)) {
|
22
|
+
return createInnUlError();
|
23
|
+
}
|
24
|
+
if (value.length !== INN_UL_LENGTH) {
|
25
|
+
return createInnUlError();
|
26
|
+
}
|
27
|
+
const arrSymbols = value.split('');
|
28
|
+
if (arrSymbols.some((symbol) => isNaN(Number(symbol)))) {
|
29
|
+
return createInnUlError();
|
30
|
+
}
|
31
|
+
const checksum = calcCheckSumForInnUl(arrSymbols);
|
32
|
+
if (Number(value[9]) !== checksum) {
|
33
|
+
return createInnUlError();
|
34
|
+
}
|
35
|
+
return undefined;
|
36
|
+
}, { exclude: params === null || params === void 0 ? void 0 : params.exclude });
|
package/kpp/constants.js
ADDED
package/kpp/index.d.ts
ADDED
package/kpp/index.js
ADDED
package/kpp/kpp.d.ts
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
import { CommonRuleParams } from '../core';
|
2
|
+
type KPPParams = CommonRuleParams<string> & {
|
3
|
+
/**
|
4
|
+
* @description Замена стандартного сообщения ошибки.
|
5
|
+
*/
|
6
|
+
message?: string;
|
7
|
+
};
|
8
|
+
/**
|
9
|
+
* @description Проверяет валидность кода КПП
|
10
|
+
* @example
|
11
|
+
* ```ts
|
12
|
+
* const validate = string(kpp());
|
13
|
+
* validate('770201001');
|
14
|
+
* ```
|
15
|
+
*/
|
16
|
+
export declare const kpp: <TValues>(params?: KPPParams) => (value: string, prevCtx?: Readonly<{
|
17
|
+
global: Readonly<{
|
18
|
+
values: TValues;
|
19
|
+
overrides: Readonly<{
|
20
|
+
objectIsPartial: boolean;
|
21
|
+
}>;
|
22
|
+
}>;
|
23
|
+
createError: ({ message, code }: import("../core").ErrorInfo) => import("../core").ValidationSimpleError<{}>;
|
24
|
+
}> | undefined) => import("../core").ValidationResult;
|
25
|
+
export {};
|
package/kpp/kpp.js
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
import { createRule, isStringOfZeros } from '../core';
|
2
|
+
import { INVALID_KPP_ERROR_INFO, KPP_REGEX } from './constants';
|
3
|
+
/**
|
4
|
+
* @description Проверяет валидность кода КПП
|
5
|
+
* @example
|
6
|
+
* ```ts
|
7
|
+
* const validate = string(kpp());
|
8
|
+
* validate('770201001');
|
9
|
+
* ```
|
10
|
+
*/
|
11
|
+
export const kpp = (params) => createRule((value, ctx) => {
|
12
|
+
if (isStringOfZeros(value) || !KPP_REGEX.test(value)) {
|
13
|
+
return ctx.createError(Object.assign(Object.assign({}, INVALID_KPP_ERROR_INFO), { message: (params === null || params === void 0 ? void 0 : params.message) || INVALID_KPP_ERROR_INFO.message }));
|
14
|
+
}
|
15
|
+
return undefined;
|
16
|
+
}, { exclude: params === null || params === void 0 ? void 0 : params.exclude });
|
package/max/constants.js
ADDED
@@ -0,0 +1,5 @@
|
|
1
|
+
import { createErrorCode } from '../core';
|
2
|
+
export const STRING_MAX_ERROR_CODE = createErrorCode('string-max');
|
3
|
+
export const NUMBER_MAX_ERROR_CODE = createErrorCode('number-max');
|
4
|
+
export const DATE_MAX_ERROR_CODE = createErrorCode('date-max');
|
5
|
+
export const ARRAY_MAX_ERROR_CODE = createErrorCode('array-max');
|
package/max/index.d.ts
ADDED
package/max/index.js
ADDED
package/max/max.d.ts
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
import { ValidationContext, createRule } from '../core';
|
2
|
+
type BaseMaxValidationTypes = number | string | Array<unknown>;
|
3
|
+
type CommonThreshold = number | Date;
|
4
|
+
type MaxParams<ValidationType> = {
|
5
|
+
/**
|
6
|
+
* @description Сообщение ошибки
|
7
|
+
*/
|
8
|
+
getMessage?: (threshold: CommonThreshold, value: ValidationType, ctx: ValidationContext<unknown>) => string;
|
9
|
+
};
|
10
|
+
/**
|
11
|
+
* @description Проверяет значение на соответствие максимуму. Работает с: string, array, Date, number
|
12
|
+
* @param threshold - верхний доступный порог value
|
13
|
+
* @example
|
14
|
+
* ```ts
|
15
|
+
* string(max(22));
|
16
|
+
*
|
17
|
+
* number(max(22));
|
18
|
+
*
|
19
|
+
* array(max(2)));
|
20
|
+
*
|
21
|
+
* date(max(new Date())));
|
22
|
+
* ```
|
23
|
+
*/
|
24
|
+
export declare function max<ValidationType extends Date>(threshold: Date, params?: MaxParams<ValidationType>): ReturnType<typeof createRule<Date, unknown>>;
|
25
|
+
export declare function max<ValidationType extends BaseMaxValidationTypes>(threshold: number, params?: MaxParams<ValidationType>): ReturnType<typeof createRule<BaseMaxValidationTypes, unknown>>;
|
26
|
+
export {};
|
package/max/max.js
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
import { createRule, resetTime } from '../core';
|
2
|
+
import { ARRAY_MAX_ERROR_CODE, DATE_MAX_ERROR_CODE, NUMBER_MAX_ERROR_CODE, STRING_MAX_ERROR_CODE, } from './constants';
|
3
|
+
export function max(threshold, params) {
|
4
|
+
return createRule((value, ctx) => {
|
5
|
+
const getMessage = (typeMessage) => (params === null || params === void 0 ? void 0 : params.getMessage)
|
6
|
+
? params.getMessage(threshold, value, ctx)
|
7
|
+
: typeMessage;
|
8
|
+
if (value instanceof Date && threshold instanceof Date) {
|
9
|
+
return resetTime(value) <= resetTime(threshold)
|
10
|
+
? undefined
|
11
|
+
: ctx.createError({
|
12
|
+
code: DATE_MAX_ERROR_CODE,
|
13
|
+
message: getMessage(`Не позднее ${threshold.toLocaleDateString('ru')}`),
|
14
|
+
});
|
15
|
+
}
|
16
|
+
if (threshold instanceof Date) {
|
17
|
+
return undefined;
|
18
|
+
}
|
19
|
+
if (typeof value === 'string') {
|
20
|
+
const isError = value.trim().length > threshold;
|
21
|
+
return isError
|
22
|
+
? ctx.createError({
|
23
|
+
code: STRING_MAX_ERROR_CODE,
|
24
|
+
message: getMessage(`Макс. символов: ${threshold}`),
|
25
|
+
})
|
26
|
+
: undefined;
|
27
|
+
}
|
28
|
+
if (Array.isArray(value) && value.length > threshold) {
|
29
|
+
return ctx.createError({
|
30
|
+
code: ARRAY_MAX_ERROR_CODE,
|
31
|
+
message: getMessage(`Не меньше ${threshold}`),
|
32
|
+
});
|
33
|
+
}
|
34
|
+
if (typeof value === 'number' && value > threshold) {
|
35
|
+
return ctx.createError({
|
36
|
+
code: NUMBER_MAX_ERROR_CODE,
|
37
|
+
message: getMessage(`Не меньше ${threshold}`),
|
38
|
+
});
|
39
|
+
}
|
40
|
+
return undefined;
|
41
|
+
});
|
42
|
+
}
|
package/min/constants.js
ADDED
@@ -0,0 +1,5 @@
|
|
1
|
+
import { createErrorCode } from '../core';
|
2
|
+
export const STRING_MIN_ERROR_CODE = createErrorCode('string-min');
|
3
|
+
export const NUMBER_MIN_ERROR_CODE = createErrorCode('number-min');
|
4
|
+
export const DATE_MIN_ERROR_CODE = createErrorCode('date-min');
|
5
|
+
export const ARRAY_MIN_ERROR_CODE = createErrorCode('array-min');
|
package/min/index.d.ts
ADDED
package/min/index.js
ADDED
package/min/min.d.ts
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
import { ValidationContext, createRule } from '../core';
|
2
|
+
type BaseMinValidationTypes = number | string | Array<unknown>;
|
3
|
+
type CommonThreshold = number | Date;
|
4
|
+
type MinParams<ValidationType> = {
|
5
|
+
/**
|
6
|
+
* @description Сообщение ошибки
|
7
|
+
*/
|
8
|
+
getMessage?: (threshold: CommonThreshold, value: ValidationType, ctx: ValidationContext<unknown>) => string;
|
9
|
+
};
|
10
|
+
/**
|
11
|
+
* @description Проверяет значение на соответствие минимуму. Работает с: string, array, Date, number
|
12
|
+
* @param threshold - нижний доступный порог value
|
13
|
+
* @example
|
14
|
+
* ```ts
|
15
|
+
* string(min(22));
|
16
|
+
*
|
17
|
+
* number(min(22));
|
18
|
+
*
|
19
|
+
* array(min(2)));
|
20
|
+
*
|
21
|
+
* date(min(new Date())));
|
22
|
+
* ```
|
23
|
+
*/
|
24
|
+
export declare function min<ValidationType extends Date>(threshold: Date, params?: MinParams<ValidationType>): ReturnType<typeof createRule<Date, unknown>>;
|
25
|
+
export declare function min<ValidationType extends BaseMinValidationTypes>(threshold: number, params?: MinParams<ValidationType>): ReturnType<typeof createRule<BaseMinValidationTypes, unknown>>;
|
26
|
+
export {};
|
package/min/min.js
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
import { createRule, resetTime } from '../core';
|
2
|
+
import { ARRAY_MIN_ERROR_CODE, DATE_MIN_ERROR_CODE, NUMBER_MIN_ERROR_CODE, STRING_MIN_ERROR_CODE, } from './constants';
|
3
|
+
export function min(threshold, params) {
|
4
|
+
return createRule((value, ctx) => {
|
5
|
+
const getMessage = (typeMessage) => (params === null || params === void 0 ? void 0 : params.getMessage)
|
6
|
+
? params.getMessage(threshold, value, ctx)
|
7
|
+
: typeMessage;
|
8
|
+
if (value instanceof Date && threshold instanceof Date) {
|
9
|
+
return resetTime(value) >= resetTime(threshold)
|
10
|
+
? undefined
|
11
|
+
: ctx.createError({
|
12
|
+
code: DATE_MIN_ERROR_CODE,
|
13
|
+
message: getMessage(`Не раньше ${threshold.toLocaleDateString('ru')}`),
|
14
|
+
});
|
15
|
+
}
|
16
|
+
if (threshold instanceof Date) {
|
17
|
+
return undefined;
|
18
|
+
}
|
19
|
+
if (typeof value === 'string') {
|
20
|
+
const isError = value.trim().length < threshold;
|
21
|
+
return isError
|
22
|
+
? ctx.createError({
|
23
|
+
code: STRING_MIN_ERROR_CODE,
|
24
|
+
message: getMessage(`Мин. символов: ${threshold}`),
|
25
|
+
})
|
26
|
+
: undefined;
|
27
|
+
}
|
28
|
+
if (Array.isArray(value) && value.length < threshold) {
|
29
|
+
return ctx.createError({
|
30
|
+
code: ARRAY_MIN_ERROR_CODE,
|
31
|
+
message: getMessage(`Не больше ${threshold}`),
|
32
|
+
});
|
33
|
+
}
|
34
|
+
if (typeof value === 'number' && value < threshold) {
|
35
|
+
return ctx.createError({
|
36
|
+
code: NUMBER_MIN_ERROR_CODE,
|
37
|
+
message: getMessage(`Не больше ${threshold}`),
|
38
|
+
});
|
39
|
+
}
|
40
|
+
return undefined;
|
41
|
+
});
|
42
|
+
}
|
@@ -0,0 +1,27 @@
|
|
1
|
+
import { CommonRuleParams } from '../core';
|
2
|
+
type MobilePhoneParams = CommonRuleParams<string> & {
|
3
|
+
/**
|
4
|
+
* @description Замена стандартного сообщения ошибки.
|
5
|
+
*/
|
6
|
+
message?: string;
|
7
|
+
};
|
8
|
+
/**
|
9
|
+
* @description
|
10
|
+
* Проверяет валиден ли мобильный телефон.
|
11
|
+
* Валидный телефон начинается с "79" и не содержит символов, кроме цифр.
|
12
|
+
* @example
|
13
|
+
* ```ts
|
14
|
+
* const validate = string(mobilePhone());
|
15
|
+
* validate('79999999999');
|
16
|
+
* ```
|
17
|
+
*/
|
18
|
+
export declare const mobilePhone: <TValues>(params?: MobilePhoneParams) => (value: string, prevCtx?: Readonly<{
|
19
|
+
global: Readonly<{
|
20
|
+
values: TValues;
|
21
|
+
overrides: Readonly<{
|
22
|
+
objectIsPartial: boolean;
|
23
|
+
}>;
|
24
|
+
}>;
|
25
|
+
createError: ({ message, code }: import("../core").ErrorInfo) => import("../core").ValidationSimpleError<{}>;
|
26
|
+
}> | undefined) => import("../core").ValidationResult;
|
27
|
+
export {};
|
@@ -0,0 +1,21 @@
|
|
1
|
+
import { createRule } from '../core';
|
2
|
+
import { MOBILE_PHONE_ERROR_INFO, MOBILE_PHONE_REGEX } from './constants';
|
3
|
+
/**
|
4
|
+
* @description
|
5
|
+
* Проверяет валиден ли мобильный телефон.
|
6
|
+
* Валидный телефон начинается с "79" и не содержит символов, кроме цифр.
|
7
|
+
* @example
|
8
|
+
* ```ts
|
9
|
+
* const validate = string(mobilePhone());
|
10
|
+
* validate('79999999999');
|
11
|
+
* ```
|
12
|
+
*/
|
13
|
+
export const mobilePhone = (params) => createRule((value, ctx) => {
|
14
|
+
if (!MOBILE_PHONE_REGEX.test(value)) {
|
15
|
+
return ctx.createError({
|
16
|
+
message: (params === null || params === void 0 ? void 0 : params.message) || MOBILE_PHONE_ERROR_INFO.message,
|
17
|
+
code: MOBILE_PHONE_ERROR_INFO.code,
|
18
|
+
});
|
19
|
+
}
|
20
|
+
return undefined;
|
21
|
+
}, { exclude: params === null || params === void 0 ? void 0 : params.exclude });
|
@@ -0,0 +1,13 @@
|
|
1
|
+
import { createErrorCode } from '../core';
|
2
|
+
export const NUMBER_TYPE_ERROR_INFO = {
|
3
|
+
code: createErrorCode('number-type'),
|
4
|
+
message: 'Не число',
|
5
|
+
};
|
6
|
+
export const NAN_NUMBER_ERROR_INFO = {
|
7
|
+
code: createErrorCode('number-nan'),
|
8
|
+
message: 'Некорректное число',
|
9
|
+
};
|
10
|
+
export const INFINITY_NUMBER_ERROR_INFO = {
|
11
|
+
code: createErrorCode('number-infinity'),
|
12
|
+
message: 'Бесконечное число',
|
13
|
+
};
|
package/number/index.js
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
import { ValidationRule } from '../core';
|
2
|
+
type AdditionalDefOptions = {
|
3
|
+
nanErrorMessage?: string;
|
4
|
+
infinityErrorMessage?: string;
|
5
|
+
};
|
6
|
+
/**
|
7
|
+
* @description Guard для number. Проверяет значение на тип number и NaN, Infinity
|
8
|
+
* Для NaN и Infinity возвращаются отдельные ошибки
|
9
|
+
* @param rules - правила, валидирующие number или unknown value
|
10
|
+
* @example
|
11
|
+
* ```ts
|
12
|
+
* const validate = number(min(22));
|
13
|
+
*
|
14
|
+
* // undefined
|
15
|
+
* validate(24);
|
16
|
+
* ```
|
17
|
+
*/
|
18
|
+
export declare const number: <TValues>(...rules: ValidationRule<number, TValues>[]) => import("../core").Guard<number, TValues, AdditionalDefOptions>;
|
19
|
+
export {};
|
package/number/number.js
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
import { compose, createGuard } from '../core';
|
2
|
+
import { INFINITY_NUMBER_ERROR_INFO, NAN_NUMBER_ERROR_INFO, NUMBER_TYPE_ERROR_INFO, } from './constants';
|
3
|
+
/**
|
4
|
+
* @description Guard для number. Проверяет значение на тип number и NaN, Infinity
|
5
|
+
* Для NaN и Infinity возвращаются отдельные ошибки
|
6
|
+
* @param rules - правила, валидирующие number или unknown value
|
7
|
+
* @example
|
8
|
+
* ```ts
|
9
|
+
* const validate = number(min(22));
|
10
|
+
*
|
11
|
+
* // undefined
|
12
|
+
* validate(24);
|
13
|
+
* ```
|
14
|
+
*/
|
15
|
+
export const number = (...rules) => createGuard((value, ctx, { typeErrorMessage, nanErrorMessage, infinityErrorMessage }) => {
|
16
|
+
if (typeof value !== 'number') {
|
17
|
+
return ctx.createError(Object.assign(Object.assign({}, NUMBER_TYPE_ERROR_INFO), { message: typeErrorMessage || NUMBER_TYPE_ERROR_INFO.message }));
|
18
|
+
}
|
19
|
+
if (Number.isNaN(value)) {
|
20
|
+
return ctx.createError(Object.assign(Object.assign({}, NAN_NUMBER_ERROR_INFO), { message: nanErrorMessage || NAN_NUMBER_ERROR_INFO.message }));
|
21
|
+
}
|
22
|
+
if (!Number.isFinite(value)) {
|
23
|
+
return ctx.createError(Object.assign(Object.assign({}, INFINITY_NUMBER_ERROR_INFO), { message: infinityErrorMessage || INFINITY_NUMBER_ERROR_INFO.message }));
|
24
|
+
}
|
25
|
+
return compose(...rules)(value, ctx);
|
26
|
+
});
|
package/object/index.js
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
export * from './isEmptyErrors';
|
@@ -0,0 +1 @@
|
|
1
|
+
export * from './isEmptyErrors';
|