@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,52 @@
|
|
1
|
+
import { Guard, ValidationContext, ValidationRule } from '../core';
|
2
|
+
/**
|
3
|
+
* @description Специальный итерфейс Guard для object. В данном интерфейсе ctx required
|
4
|
+
* Переопределение необходимо для того, чтобы ts показывал, что ctx required в кастомных правилах
|
5
|
+
*/
|
6
|
+
interface ObjectPropGuard<TValues> {
|
7
|
+
(value: Parameters<Guard<unknown, TValues>>[0], ctx: ValidationContext<TValues>): ReturnType<Guard<unknown, TValues>>;
|
8
|
+
define: Guard<unknown, TValues>['define'];
|
9
|
+
}
|
10
|
+
type AdditionalDefOptions = {
|
11
|
+
/**
|
12
|
+
* @description Делает все свойства объекта partial
|
13
|
+
*/
|
14
|
+
isPartial?: boolean;
|
15
|
+
};
|
16
|
+
/**
|
17
|
+
* @description Тип, который необходим для того, чтобы object невозможно было использовать без использования generic
|
18
|
+
*/
|
19
|
+
type NeverSchema = Record<'__never', never>;
|
20
|
+
/**
|
21
|
+
* @description Возможные значения, принимаемые схемой
|
22
|
+
*/
|
23
|
+
export type SchemaValue<TValues> = ObjectPropGuard<TValues> | ValidationRule<unknown, TValues>;
|
24
|
+
/**
|
25
|
+
* @description Схема правил валдиации для объекта
|
26
|
+
*/
|
27
|
+
export type Schema<TValue extends Record<string, unknown>, TValues = unknown> = Record<keyof TValue, SchemaValue<TValues>>;
|
28
|
+
/**
|
29
|
+
* @description Guard для объекта
|
30
|
+
* @param schema - схема валидации объекта
|
31
|
+
* @example
|
32
|
+
* ```ts
|
33
|
+
* type Values = {
|
34
|
+
* name: string;
|
35
|
+
* age?: number;
|
36
|
+
* info: { surname: string };
|
37
|
+
* };
|
38
|
+
*
|
39
|
+
* const values: Values = { name: 'Vasya', info: { surname: 'Vasin' } };
|
40
|
+
*
|
41
|
+
* const validateObject = object<Values>({
|
42
|
+
* name: string(min(2)),
|
43
|
+
* age: optional(number()),
|
44
|
+
* info: object<Values['info']>({ surname: string(min(2)) }),
|
45
|
+
* customField: (value, ctx) => {
|
46
|
+
* return ctx.createError({ message: 'error', code: 'custom error' })
|
47
|
+
* }
|
48
|
+
* });
|
49
|
+
* ```
|
50
|
+
*/
|
51
|
+
export declare const object: <Value extends Record<string, unknown> = NeverSchema, TValues = unknown>(schema: Schema<Value, TValues>) => Guard<Value, TValues, AdditionalDefOptions>;
|
52
|
+
export {};
|
package/object/object.js
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
import isPlainObject from 'is-plain-obj';
|
2
|
+
import { createErrorMap, createGuard, } from '../core';
|
3
|
+
import { optional } from '../optional';
|
4
|
+
import { isEmptyErrors } from './isEmptyErrors';
|
5
|
+
import { OBJECT_TYPE_ERROR_INFO } from './constants';
|
6
|
+
/**
|
7
|
+
* @description Guard для объекта
|
8
|
+
* @param schema - схема валидации объекта
|
9
|
+
* @example
|
10
|
+
* ```ts
|
11
|
+
* type Values = {
|
12
|
+
* name: string;
|
13
|
+
* age?: number;
|
14
|
+
* info: { surname: string };
|
15
|
+
* };
|
16
|
+
*
|
17
|
+
* const values: Values = { name: 'Vasya', info: { surname: 'Vasin' } };
|
18
|
+
*
|
19
|
+
* const validateObject = object<Values>({
|
20
|
+
* name: string(min(2)),
|
21
|
+
* age: optional(number()),
|
22
|
+
* info: object<Values['info']>({ surname: string(min(2)) }),
|
23
|
+
* customField: (value, ctx) => {
|
24
|
+
* return ctx.createError({ message: 'error', code: 'custom error' })
|
25
|
+
* }
|
26
|
+
* });
|
27
|
+
* ```
|
28
|
+
*/
|
29
|
+
export const object = (schema) => createGuard((value, ctx, { typeErrorMessage, isPartial }) => {
|
30
|
+
if (!isPlainObject(value)) {
|
31
|
+
return ctx.createError(Object.assign(Object.assign({}, OBJECT_TYPE_ERROR_INFO), { message: typeErrorMessage || OBJECT_TYPE_ERROR_INFO.message }));
|
32
|
+
}
|
33
|
+
const generateErrorMap = () => {
|
34
|
+
const schemaEntries = Object.entries(schema);
|
35
|
+
const isOptional = ctx.global.overrides.objectIsPartial || isPartial;
|
36
|
+
return schemaEntries.reduce((errorMap, [key, rule]) => {
|
37
|
+
const isGuard = 'define' in rule;
|
38
|
+
const callRule = isGuard && isOptional
|
39
|
+
? optional(rule)
|
40
|
+
: rule;
|
41
|
+
errorMap[key] = callRule(value[key], ctx);
|
42
|
+
return errorMap;
|
43
|
+
}, {});
|
44
|
+
};
|
45
|
+
const errorMap = generateErrorMap();
|
46
|
+
if (!isEmptyErrors(errorMap)) {
|
47
|
+
return createErrorMap(errorMap);
|
48
|
+
}
|
49
|
+
return undefined;
|
50
|
+
});
|
package/ogrnIP/index.js
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
import { CommonRuleParams } from '../core';
|
2
|
+
type OgrnIPParams = CommonRuleParams<string> & {
|
3
|
+
/**
|
4
|
+
* @description Замена стандартного сообщения ошибки.
|
5
|
+
*/
|
6
|
+
message?: string;
|
7
|
+
};
|
8
|
+
/**
|
9
|
+
* @description Проверяет валиден ли ОГРН ИП
|
10
|
+
* @example
|
11
|
+
* ```ts
|
12
|
+
* const validate = string(ogrnIP());
|
13
|
+
* validate('7728168971');
|
14
|
+
* ```
|
15
|
+
*/
|
16
|
+
export declare const ogrnIP: <TValues>(params?: OgrnIPParams) => (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/ogrnIP/ogrnIP.js
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
import { createRule, isStringOfZeros } from '../core';
|
2
|
+
import { OGRN_IP_ERROR_INFO, OGRN_IP_LENGTH } from './constants';
|
3
|
+
/**
|
4
|
+
* @description Проверяет валиден ли ОГРН ИП
|
5
|
+
* @example
|
6
|
+
* ```ts
|
7
|
+
* const validate = string(ogrnIP());
|
8
|
+
* validate('7728168971');
|
9
|
+
* ```
|
10
|
+
*/
|
11
|
+
export const ogrnIP = (params) => createRule((value, ctx) => {
|
12
|
+
const createOgrnIPError = () => ctx.createError({
|
13
|
+
message: (params === null || params === void 0 ? void 0 : params.message) || OGRN_IP_ERROR_INFO.message,
|
14
|
+
code: OGRN_IP_ERROR_INFO.code,
|
15
|
+
});
|
16
|
+
if (isStringOfZeros(value)) {
|
17
|
+
return createOgrnIPError();
|
18
|
+
}
|
19
|
+
if (value.length !== OGRN_IP_LENGTH) {
|
20
|
+
return createOgrnIPError();
|
21
|
+
}
|
22
|
+
const checkSum = (parseInt(value.slice(0, -1)) % 13).toString().slice(-1);
|
23
|
+
if (value.slice(14, 15) !== checkSum) {
|
24
|
+
return createOgrnIPError();
|
25
|
+
}
|
26
|
+
return undefined;
|
27
|
+
}, { exclude: params === null || params === void 0 ? void 0 : params.exclude });
|
package/ogrnUL/index.js
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
import { CommonRuleParams } from '../core';
|
2
|
+
type OgrnULParams = CommonRuleParams<string> & {
|
3
|
+
/**
|
4
|
+
* @description Замена стандартного сообщения ошибки.
|
5
|
+
*/
|
6
|
+
message?: string;
|
7
|
+
};
|
8
|
+
/**
|
9
|
+
* @description Проверяет валиден ли ОГРН ЮЛ
|
10
|
+
* @example
|
11
|
+
* ```ts
|
12
|
+
* const validate = string(ogrnIP());
|
13
|
+
* validate('7728168971');
|
14
|
+
* ```
|
15
|
+
*/
|
16
|
+
export declare const ogrnUL: <TValues>(params?: OgrnULParams) => (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/ogrnUL/ogrnUL.js
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
import { createRule, isStringOfZeros } from '../core';
|
2
|
+
import { OGRN_UL_ERROR_INFO, OGRN_UL_LENGTH } from './constants';
|
3
|
+
/**
|
4
|
+
* @description Проверяет валиден ли ОГРН ЮЛ
|
5
|
+
* @example
|
6
|
+
* ```ts
|
7
|
+
* const validate = string(ogrnIP());
|
8
|
+
* validate('7728168971');
|
9
|
+
* ```
|
10
|
+
*/
|
11
|
+
export const ogrnUL = (params) => createRule((value, ctx) => {
|
12
|
+
const createOgrnUlError = () => ctx.createError({
|
13
|
+
message: (params === null || params === void 0 ? void 0 : params.message) || OGRN_UL_ERROR_INFO.message,
|
14
|
+
code: OGRN_UL_ERROR_INFO.code,
|
15
|
+
});
|
16
|
+
if (isStringOfZeros(value)) {
|
17
|
+
return createOgrnUlError();
|
18
|
+
}
|
19
|
+
if (value.length !== OGRN_UL_LENGTH) {
|
20
|
+
return createOgrnUlError();
|
21
|
+
}
|
22
|
+
const checkSum = (parseInt(value.slice(0, -1)) % 11).toString().slice(-1);
|
23
|
+
if (value.slice(12, 13) !== checkSum) {
|
24
|
+
return createOgrnUlError();
|
25
|
+
}
|
26
|
+
return undefined;
|
27
|
+
}, { exclude: params === null || params === void 0 ? void 0 : params.exclude });
|
@@ -0,0 +1,24 @@
|
|
1
|
+
type OnlyNumberParams = {
|
2
|
+
/**
|
3
|
+
* @description Замена стандартного сообщения ошибки.
|
4
|
+
*/
|
5
|
+
message?: string;
|
6
|
+
};
|
7
|
+
/**
|
8
|
+
* @description Проверяет на наличие только чисел в строке
|
9
|
+
* @example
|
10
|
+
* ```ts
|
11
|
+
* const validate = string(onlyNumber());
|
12
|
+
* validate('123');
|
13
|
+
* ```
|
14
|
+
*/
|
15
|
+
export declare const onlyNumber: <TValues>(params?: OnlyNumberParams) => (value: string, prevCtx?: Readonly<{
|
16
|
+
global: Readonly<{
|
17
|
+
values: TValues;
|
18
|
+
overrides: Readonly<{
|
19
|
+
objectIsPartial: boolean;
|
20
|
+
}>;
|
21
|
+
}>;
|
22
|
+
createError: ({ message, code }: import("../core").ErrorInfo) => import("../core").ValidationSimpleError<{}>;
|
23
|
+
}> | undefined) => import("../core").ValidationResult;
|
24
|
+
export {};
|
@@ -0,0 +1,20 @@
|
|
1
|
+
import { createRule } from '../core';
|
2
|
+
import { ONLY_NUMBER_ERROR_CODE } from './constants';
|
3
|
+
/**
|
4
|
+
* @description Проверяет на наличие только чисел в строке
|
5
|
+
* @example
|
6
|
+
* ```ts
|
7
|
+
* const validate = string(onlyNumber());
|
8
|
+
* validate('123');
|
9
|
+
* ```
|
10
|
+
*/
|
11
|
+
export const onlyNumber = (params) => createRule((value, ctx) => {
|
12
|
+
const isExtraValues = Boolean(value.replace(/^[0-9]+$/g, ''));
|
13
|
+
if (isExtraValues) {
|
14
|
+
return ctx.createError({
|
15
|
+
code: ONLY_NUMBER_ERROR_CODE,
|
16
|
+
message: (params === null || params === void 0 ? void 0 : params.message) || 'Строка должна содержать только числа',
|
17
|
+
});
|
18
|
+
}
|
19
|
+
return undefined;
|
20
|
+
});
|
@@ -0,0 +1 @@
|
|
1
|
+
export * from './optional';
|
@@ -0,0 +1 @@
|
|
1
|
+
export * from './optional';
|
@@ -0,0 +1,7 @@
|
|
1
|
+
import { Guard } from '../core';
|
2
|
+
/**
|
3
|
+
* @description Выключает проверку на required в guard
|
4
|
+
* @param guard - правило, проверяющее тип значения
|
5
|
+
* @example object({ name: optional(string(min(22))) })
|
6
|
+
*/
|
7
|
+
export declare const optional: <ValidationType extends unknown, TValues>(guard: Guard<ValidationType, TValues, {}>) => Guard<ValidationType, TValues, {}>;
|
@@ -0,0 +1,15 @@
|
|
1
|
+
import { string } from '../string';
|
2
|
+
/**
|
3
|
+
* @description Выключает проверку на required в guard
|
4
|
+
* @param guard - правило, проверяющее тип значения
|
5
|
+
* @example object({ name: optional(string(min(22))) })
|
6
|
+
*/
|
7
|
+
export const optional = (guard) => guard.define({ isOptional: true });
|
8
|
+
const validateCustomString = string().define({
|
9
|
+
typeErrorMessage: 'Только строка',
|
10
|
+
requiredErrorMessage: 'Не может быть пустым',
|
11
|
+
});
|
12
|
+
// { message: 'Не может быть пустым' }
|
13
|
+
validateCustomString(undefined);
|
14
|
+
// { message: 'Только строка' }
|
15
|
+
validateCustomString(20);
|
package/or/index.d.ts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
export * from './or';
|
package/or/index.js
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
export * from './or';
|
package/or/or.d.ts
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
import { ValidationResult, ValidationRule } from '../core';
|
2
|
+
/**
|
3
|
+
* @description Выполняет переданные правила аналогично оператору ||. Если одно из правил не завершилось ошибкой, то or вернет undefined
|
4
|
+
* Если все переданные правила завершились с ошибкой, то вернется ошибка из последнего правила
|
5
|
+
* @param rules - любые правила валдиаций
|
6
|
+
* @example
|
7
|
+
* ```ts
|
8
|
+
* const validate = or(number(), string(), array());
|
9
|
+
*
|
10
|
+
* // undefined
|
11
|
+
* const result = validate('string');
|
12
|
+
* ```
|
13
|
+
*/
|
14
|
+
export declare const or: <TValues>(...rules: ValidationRule<unknown, TValues>[]) => (value: unknown, prevCtx?: Readonly<{
|
15
|
+
global: Readonly<{
|
16
|
+
values: TValues;
|
17
|
+
overrides: Readonly<{
|
18
|
+
objectIsPartial: boolean;
|
19
|
+
}>;
|
20
|
+
}>;
|
21
|
+
createError: ({ message, code }: import("../core").ErrorInfo) => import("../core").ValidationSimpleError<{}>;
|
22
|
+
}> | undefined) => ValidationResult;
|
package/or/or.js
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
import { createRule } from '../core';
|
2
|
+
/**
|
3
|
+
* @description Выполняет переданные правила аналогично оператору ||. Если одно из правил не завершилось ошибкой, то or вернет undefined
|
4
|
+
* Если все переданные правила завершились с ошибкой, то вернется ошибка из последнего правила
|
5
|
+
* @param rules - любые правила валдиаций
|
6
|
+
* @example
|
7
|
+
* ```ts
|
8
|
+
* const validate = or(number(), string(), array());
|
9
|
+
*
|
10
|
+
* // undefined
|
11
|
+
* const result = validate('string');
|
12
|
+
* ```
|
13
|
+
*/
|
14
|
+
export const or = (...rules) => createRule((value, ctx) => {
|
15
|
+
let result;
|
16
|
+
rules.some((rule) => {
|
17
|
+
result = rule(value, ctx);
|
18
|
+
return result ? false : true;
|
19
|
+
});
|
20
|
+
return result;
|
21
|
+
});
|
package/package.json
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
{
|
2
2
|
"name": "@astral/validations",
|
3
|
-
"version": "
|
3
|
+
"version": "3.0.0-beta.0",
|
4
4
|
"browser": "./index.js",
|
5
5
|
"main": "./index.js",
|
6
6
|
"dependencies": {
|
7
|
-
"
|
7
|
+
"is-plain-obj": "^4.1.0"
|
8
8
|
},
|
9
9
|
"author": "Astral.Soft",
|
10
10
|
"license": "MIT",
|
@@ -0,0 +1 @@
|
|
1
|
+
export * from './partial';
|
package/partial/index.js
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
export * from './partial';
|
@@ -0,0 +1,10 @@
|
|
1
|
+
/**
|
2
|
+
* @description Выключает проверку на required для всех полей объекта
|
3
|
+
* @param objectGuard
|
4
|
+
* @example partial(object({ name: string() }))
|
5
|
+
*/
|
6
|
+
export declare const partial: (objectGuard: import("../core").Guard<Record<string, unknown>, unknown, {
|
7
|
+
isPartial?: boolean | undefined;
|
8
|
+
}>) => import("../core").Guard<Record<string, unknown>, unknown, {
|
9
|
+
isPartial?: boolean | undefined;
|
10
|
+
}>;
|
package/pattern/index.js
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
type PatternParams = {
|
2
|
+
/**
|
3
|
+
* @description Замена стандартного сообщения ошибки.
|
4
|
+
*/
|
5
|
+
message?: string;
|
6
|
+
};
|
7
|
+
/**
|
8
|
+
* @description Проверяет строку на соответствие регулярному выражению.
|
9
|
+
* @param regex - регулярное выражение
|
10
|
+
* @example
|
11
|
+
* ```ts
|
12
|
+
* string(pattern(/word/g))
|
13
|
+
*
|
14
|
+
* string(pattern(/[0-9]/))
|
15
|
+
* ```
|
16
|
+
*/
|
17
|
+
export declare const pattern: <TValues>(regex: RegExp, params?: PatternParams) => (value: string, prevCtx?: Readonly<{
|
18
|
+
global: Readonly<{
|
19
|
+
values: TValues;
|
20
|
+
overrides: Readonly<{
|
21
|
+
objectIsPartial: boolean;
|
22
|
+
}>;
|
23
|
+
}>;
|
24
|
+
createError: ({ message, code }: import("../core").ErrorInfo) => import("../core").ValidationSimpleError<{}>;
|
25
|
+
}> | undefined) => import("../core").ValidationResult;
|
26
|
+
export {};
|
@@ -0,0 +1,22 @@
|
|
1
|
+
import { createRule } from '../core';
|
2
|
+
import { PATTERN_ERROR_CODE } from './constants';
|
3
|
+
/**
|
4
|
+
* @description Проверяет строку на соответствие регулярному выражению.
|
5
|
+
* @param regex - регулярное выражение
|
6
|
+
* @example
|
7
|
+
* ```ts
|
8
|
+
* string(pattern(/word/g))
|
9
|
+
*
|
10
|
+
* string(pattern(/[0-9]/))
|
11
|
+
* ```
|
12
|
+
*/
|
13
|
+
export const pattern = (regex, params) => createRule((value, ctx) => {
|
14
|
+
if (!regex.test(value)) {
|
15
|
+
return ctx.createError({
|
16
|
+
code: PATTERN_ERROR_CODE,
|
17
|
+
message: (params === null || params === void 0 ? void 0 : params.message) ||
|
18
|
+
`Должно подходить под регулярное выражение: "${regex}"`,
|
19
|
+
});
|
20
|
+
}
|
21
|
+
return undefined;
|
22
|
+
});
|
package/snils/index.d.ts
ADDED
package/snils/index.js
ADDED
package/snils/snils.d.ts
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
import { CommonRuleParams } from '../core';
|
2
|
+
type SnilsParams = CommonRuleParams<string> & {
|
3
|
+
/**
|
4
|
+
* @description Замена стандартного сообщения ошибки.
|
5
|
+
*/
|
6
|
+
message?: string;
|
7
|
+
};
|
8
|
+
/**
|
9
|
+
* @description Проверяет валиден ли СНИЛС
|
10
|
+
* @example
|
11
|
+
* ```ts
|
12
|
+
* const validate = string(snils());
|
13
|
+
* validate('15657325992');
|
14
|
+
* ```
|
15
|
+
*/
|
16
|
+
export declare const snils: <TValues>(params?: SnilsParams) => (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 {};
|
@@ -1,8 +1,5 @@
|
|
1
|
-
import { createRule } from '../
|
2
|
-
import {
|
3
|
-
export const IS_SNILS_DEFAULT_MESSAGE = 'Некорректный СНИЛС';
|
4
|
-
const RESTRICTED_VALUES = ['00000000000'];
|
5
|
-
const DEFAULT_CHECKED_SUM = [0, 100, 101];
|
1
|
+
import { createRule, isStringOfZeros } from '../core';
|
2
|
+
import { DEFAULT_CHECKED_SUM, RESTRICTED_VALUES, SNILS_ERROR_INFO, } from './constants';
|
6
3
|
const removeSpecialCharacters = (value) => {
|
7
4
|
return value.replace(/\D/g, '');
|
8
5
|
};
|
@@ -18,27 +15,29 @@ const compareCheckSum = (calculatedCheckSum, checkSum) => {
|
|
18
15
|
};
|
19
16
|
/**
|
20
17
|
* @description Проверяет валиден ли СНИЛС
|
21
|
-
* @example
|
18
|
+
* @example
|
19
|
+
* ```ts
|
20
|
+
* const validate = string(snils());
|
21
|
+
* validate('15657325992');
|
22
|
+
* ```
|
22
23
|
*/
|
23
|
-
export const
|
24
|
-
|
25
|
-
|
26
|
-
|
24
|
+
export const snils = (params) => createRule((value, ctx) => {
|
25
|
+
const createSnilsError = () => ctx.createError({
|
26
|
+
message: (params === null || params === void 0 ? void 0 : params.message) || SNILS_ERROR_INFO.message,
|
27
|
+
code: SNILS_ERROR_INFO.code,
|
28
|
+
});
|
27
29
|
if (isStringOfZeros(value)) {
|
28
|
-
return
|
29
|
-
}
|
30
|
-
if (typeof value !== 'string') {
|
31
|
-
return message;
|
30
|
+
return createSnilsError();
|
32
31
|
}
|
33
32
|
const formattedValue = removeSpecialCharacters(value);
|
34
33
|
if (formattedValue.length !== value.length) {
|
35
|
-
return
|
34
|
+
return createSnilsError();
|
36
35
|
}
|
37
36
|
if (!/^(\d{11})$/.test(formattedValue)) {
|
38
|
-
return
|
37
|
+
return createSnilsError();
|
39
38
|
}
|
40
39
|
if (RESTRICTED_VALUES.includes(formattedValue)) {
|
41
|
-
return
|
40
|
+
return createSnilsError();
|
42
41
|
}
|
43
42
|
const checkSum = Number(formattedValue.slice(9, 11));
|
44
43
|
const calculatedCheckSum = calcCheckSumForSNILS(formattedValue);
|
@@ -46,20 +45,20 @@ export const isSNILS = createRule(({ message = IS_SNILS_DEFAULT_MESSAGE } = {})
|
|
46
45
|
if (calculatedCheckSum === checkSum) {
|
47
46
|
return undefined;
|
48
47
|
}
|
49
|
-
return
|
48
|
+
return createSnilsError();
|
50
49
|
}
|
51
50
|
if (calculatedCheckSum === DEFAULT_CHECKED_SUM[1] ||
|
52
51
|
calculatedCheckSum === DEFAULT_CHECKED_SUM[2]) {
|
53
52
|
if (checkSum === DEFAULT_CHECKED_SUM[0]) {
|
54
53
|
return undefined;
|
55
54
|
}
|
56
|
-
return
|
55
|
+
return createSnilsError();
|
57
56
|
}
|
58
57
|
if (calculatedCheckSum > DEFAULT_CHECKED_SUM[2]) {
|
59
58
|
if (compareCheckSum(calculatedCheckSum, checkSum)) {
|
60
59
|
return undefined;
|
61
60
|
}
|
62
|
-
return
|
61
|
+
return createSnilsError();
|
63
62
|
}
|
64
63
|
return undefined;
|
65
|
-
});
|
64
|
+
}, { exclude: params === null || params === void 0 ? void 0 : params.exclude });
|