@astral/validations 3.0.0-beta.0 → 3.0.0-beta.3
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 +30 -6
- package/any/any.d.ts +1 -9
- package/array/array.d.ts +8 -1
- package/arrayItem/arrayItem.d.ts +1 -9
- package/boolean/boolean.d.ts +8 -1
- package/core/context/types.d.ts +7 -6
- package/core/guard/createGuard/createGuard.d.ts +9 -6
- package/core/rule/createRule/createRule.d.ts +1 -9
- package/core/rule/required/required.d.ts +1 -9
- package/date/date.d.ts +8 -1
- package/date/index.d.ts +1 -0
- package/date/index.js +1 -0
- package/deepPartial/deepPartial.d.ts +1 -9
- package/email/email.d.ts +1 -9
- package/index.d.ts +3 -0
- package/index.js +3 -0
- package/innIP/innIP.d.ts +1 -9
- package/innUL/innUL.d.ts +1 -9
- package/integer/constants.d.ts +2 -0
- package/integer/constants.js +5 -0
- package/integer/index.d.ts +2 -0
- package/integer/index.js +2 -0
- package/integer/integer.d.ts +25 -0
- package/integer/integer.js +28 -0
- package/kpp/kpp.d.ts +1 -9
- package/mobilePhone/mobilePhone.d.ts +1 -9
- package/number/number.d.ts +8 -1
- package/object/object.d.ts +10 -7
- package/object/object.js +1 -3
- package/ogrnIP/ogrnIP.d.ts +1 -9
- package/ogrnUL/ogrnUL.d.ts +1 -9
- package/onlyNumber/onlyNumber.d.ts +1 -9
- package/optional/optional.d.ts +1 -1
- package/optional/optional.js +0 -9
- package/or/or.d.ts +1 -9
- package/package.json +3 -2
- package/partial/partial.d.ts +11 -5
- package/pattern/pattern.d.ts +1 -9
- package/snils/snils.d.ts +1 -9
- package/string/string.d.ts +8 -1
- package/transform/transform.d.ts +1 -9
- package/when/when.d.ts +1 -9
package/README.md
CHANGED
@@ -19,6 +19,7 @@
|
|
19
19
|
- [number](#number)
|
20
20
|
- [min](#min-number)
|
21
21
|
- [max](#max-number)
|
22
|
+
- [integer](#integer)
|
22
23
|
- [string](#string)
|
23
24
|
- [min](#min-string)
|
24
25
|
- [max](#max-string)
|
@@ -233,6 +234,27 @@ validate(10)
|
|
233
234
|
|
234
235
|
---
|
235
236
|
|
237
|
+
### integer
|
238
|
+
|
239
|
+
Проверяет является ли значение целым числом.
|
240
|
+
|
241
|
+
```ts
|
242
|
+
import { number, integer } from '@astral/validations';
|
243
|
+
|
244
|
+
const validate = number(integer(5));
|
245
|
+
|
246
|
+
// undefined
|
247
|
+
validate(5)
|
248
|
+
|
249
|
+
// undefined
|
250
|
+
validate(7)
|
251
|
+
|
252
|
+
// { message: 'Только целые числа' }
|
253
|
+
validate(3.14)
|
254
|
+
```
|
255
|
+
|
256
|
+
---
|
257
|
+
|
236
258
|
## string
|
237
259
|
|
238
260
|
- Возвращает ошибку если:
|
@@ -759,7 +781,9 @@ const validate = deepPartial(
|
|
759
781
|
|
760
782
|
// undefined
|
761
783
|
validate({
|
762
|
-
info:
|
784
|
+
info: {
|
785
|
+
permissions: [{}]
|
786
|
+
},
|
763
787
|
});
|
764
788
|
```
|
765
789
|
|
@@ -888,7 +912,7 @@ type Values = { name: string; isAgree: boolean };
|
|
888
912
|
|
889
913
|
const validate = object<Values, Values>({
|
890
914
|
name: when({
|
891
|
-
is: (_, ctx) => ctx.global.values.isAgree,
|
915
|
+
is: (_, ctx) => Boolean(ctx.global.values.isAgree),
|
892
916
|
then: string(),
|
893
917
|
otherwise: any(),
|
894
918
|
}),
|
@@ -942,7 +966,7 @@ type Values = {
|
|
942
966
|
nickname: string;
|
943
967
|
};
|
944
968
|
|
945
|
-
const validate = object<Values>({
|
969
|
+
const validate = object<Values, Values>({
|
946
970
|
name: string(),
|
947
971
|
nickname: string((value, ctx) => {
|
948
972
|
if (value.includes('_')) {
|
@@ -974,7 +998,7 @@ type Values = {
|
|
974
998
|
|
975
999
|
const validate = object<Values, Values>({
|
976
1000
|
password: string(min(9)),
|
977
|
-
repeatPassword: string
|
1001
|
+
repeatPassword: string(min(9), (value, ctx) => {
|
978
1002
|
if (value !== ctx.global.values.password) {
|
979
1003
|
return ctx.createError({
|
980
1004
|
message: 'Пароли не совпадают',
|
@@ -1067,7 +1091,7 @@ type Values = { name: string; isAgree: boolean };
|
|
1067
1091
|
|
1068
1092
|
const validate = object<Values, Values>({
|
1069
1093
|
name: when({
|
1070
|
-
is: (_, ctx) => ctx.global.values.isAgree,
|
1094
|
+
is: (_, ctx) => Boolean(ctx.global.values.isAgree),
|
1071
1095
|
then: string(),
|
1072
1096
|
otherwise: any(),
|
1073
1097
|
}),
|
@@ -1092,7 +1116,7 @@ type Values = {
|
|
1092
1116
|
|
1093
1117
|
const validate = object<Values, Values>({
|
1094
1118
|
name: string(),
|
1095
|
-
info: when
|
1119
|
+
info: when({
|
1096
1120
|
is: (_, ctx) => ctx.global.values.name === 'Vasya',
|
1097
1121
|
then: object<ValuesInfo>({ surname: string() }),
|
1098
1122
|
otherwise: any(),
|
package/any/any.d.ts
CHANGED
@@ -10,12 +10,4 @@
|
|
10
10
|
* validate({});
|
11
11
|
* ```
|
12
12
|
*/
|
13
|
-
export declare const any: <TValues>() => (value: unknown, prevCtx?:
|
14
|
-
global: Readonly<{
|
15
|
-
values: TValues;
|
16
|
-
overrides: Readonly<{
|
17
|
-
objectIsPartial: boolean;
|
18
|
-
}>;
|
19
|
-
}>;
|
20
|
-
createError: ({ message, code }: import("../core").ErrorInfo) => import("../core").ValidationSimpleError<{}>;
|
21
|
-
}> | undefined) => import("../core").ValidationResult;
|
13
|
+
export declare const any: <TValues>() => (value: unknown, prevCtx?: import("../core").ValidationContext<TValues> | undefined) => import("../core").ValidationResult;
|
package/array/array.d.ts
CHANGED
@@ -12,4 +12,11 @@ import { ValidationRule } from '../core';
|
|
12
12
|
* validateArray(value);
|
13
13
|
* ```
|
14
14
|
*/
|
15
|
-
export declare const array: <TItem extends unknown, TValues = unknown>(...rules: ValidationRule<TItem[], TValues>[]) =>
|
15
|
+
export declare const array: <TItem extends unknown, TValues = unknown>(...rules: ValidationRule<TItem[], TValues>[]) => {
|
16
|
+
(value: unknown, prevCtx?: import("../core").ValidationContext<TValues> | undefined): import("../core").ValidationResult;
|
17
|
+
define(overridesDefOptions: Partial<{}> & {
|
18
|
+
requiredErrorMessage?: string | undefined;
|
19
|
+
typeErrorMessage?: string | undefined;
|
20
|
+
isOptional?: boolean | undefined;
|
21
|
+
}): any;
|
22
|
+
};
|
package/arrayItem/arrayItem.d.ts
CHANGED
@@ -24,12 +24,4 @@ import { ValidationRule } from '../core';
|
|
24
24
|
* validateArray(values);
|
25
25
|
* ```
|
26
26
|
*/
|
27
|
-
export declare const arrayItem: <TItem extends unknown, TValues = unknown>(...rules: ValidationRule<TItem, TValues>[]) => (value: TItem[], prevCtx?:
|
28
|
-
global: Readonly<{
|
29
|
-
values: TValues;
|
30
|
-
overrides: Readonly<{
|
31
|
-
objectIsPartial: boolean;
|
32
|
-
}>;
|
33
|
-
}>;
|
34
|
-
createError: ({ message, code }: import("../core").ErrorInfo) => import("../core").ValidationSimpleError<{}>;
|
35
|
-
}> | undefined) => import("../core").ValidationResult;
|
27
|
+
export declare const arrayItem: <TItem extends unknown, TValues = unknown>(...rules: ValidationRule<TItem, TValues>[]) => (value: TItem[], prevCtx?: import("../core").ValidationContext<TValues> | undefined) => import("../core").ValidationResult;
|
package/boolean/boolean.d.ts
CHANGED
@@ -10,4 +10,11 @@ import { ValidationRule } from '../core';
|
|
10
10
|
* validate(true);
|
11
11
|
* ```
|
12
12
|
*/
|
13
|
-
export declare const boolean: <TValues>(...rules: ValidationRule<boolean, TValues>[]) =>
|
13
|
+
export declare const boolean: <TValues>(...rules: ValidationRule<boolean, TValues>[]) => {
|
14
|
+
(value: unknown, prevCtx?: import("../core").ValidationContext<TValues> | undefined): import("../core").ValidationResult;
|
15
|
+
define(overridesDefOptions: Partial<{}> & {
|
16
|
+
requiredErrorMessage?: string | undefined;
|
17
|
+
typeErrorMessage?: string | undefined;
|
18
|
+
isOptional?: boolean | undefined;
|
19
|
+
}): any;
|
20
|
+
};
|
package/core/context/types.d.ts
CHANGED
@@ -1,26 +1,27 @@
|
|
1
|
+
import { DeepPartial, DeepReadonly } from 'utility-types';
|
1
2
|
import { createSimpleError } from '../errors';
|
2
3
|
/**
|
3
4
|
* @description Контекст, который доступен в каждом правиле
|
4
5
|
*/
|
5
|
-
export type ValidationContext<TValues> =
|
6
|
+
export type ValidationContext<TValues> = DeepReadonly<{
|
6
7
|
/**
|
7
8
|
* @description Глобальные значения, идущие от самого верхнего правила к самому нижнему
|
8
9
|
*/
|
9
|
-
global:
|
10
|
+
global: {
|
10
11
|
/**
|
11
12
|
* @description Значения, которые валидируется guard самого высоко порядка
|
12
13
|
*/
|
13
|
-
values: TValues
|
14
|
+
values: DeepPartial<TValues>;
|
14
15
|
/**
|
15
16
|
* @description Глобальные переопределения (сквозные для всех правил)
|
16
17
|
*/
|
17
|
-
overrides:
|
18
|
+
overrides: {
|
18
19
|
/**
|
19
20
|
* @description Делает для всех объектов в схеме все свойства необязательными
|
20
21
|
*/
|
21
22
|
objectIsPartial: boolean;
|
22
|
-
}
|
23
|
-
}
|
23
|
+
};
|
24
|
+
};
|
24
25
|
/**
|
25
26
|
* @description Фабрика ошибок. Возвращает новую ошибку валидации
|
26
27
|
*/
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import { ValidationResult
|
1
|
+
import { ValidationResult } from '../../types';
|
2
2
|
import { ValidationContext } from '../../context';
|
3
3
|
type DefOptions<AddDefOptions extends Record<string, unknown>> = Partial<AddDefOptions> & {
|
4
4
|
/**
|
@@ -17,18 +17,18 @@ type DefOptions<AddDefOptions extends Record<string, unknown>> = Partial<AddDefO
|
|
17
17
|
*/
|
18
18
|
isOptional?: boolean;
|
19
19
|
};
|
20
|
-
type GuardValue
|
20
|
+
type GuardValue = unknown;
|
21
21
|
/**
|
22
22
|
* @description Интерфейс функции guard, которая в прототипе содержит метод define
|
23
23
|
*/
|
24
|
-
export interface Guard<
|
25
|
-
(value: GuardValue
|
24
|
+
export interface Guard<TValues = unknown, AddDefOptions extends Record<string, unknown> = {}> {
|
25
|
+
(value: GuardValue, ctx?: ValidationContext<TValues>): ValidationResult;
|
26
26
|
/**
|
27
27
|
* @description Функция для создания нового guard с переопределенными дефолтными параметрами. Возвращает новый guard
|
28
28
|
* @param options - параметры, позволяющие переопределить дефолтные настройки guard
|
29
29
|
* @example string.define({ requiredMessage: 'ИНН не может быть пустым' })(inn())
|
30
30
|
*/
|
31
|
-
define(options: DefOptions<AddDefOptions>): Guard<
|
31
|
+
define(options: DefOptions<AddDefOptions>): Guard<TValues, AddDefOptions>;
|
32
32
|
}
|
33
33
|
/**
|
34
34
|
* @description Функция, которая позволяет определять частную логику для guard
|
@@ -50,5 +50,8 @@ type GuardExecutor<TValues, AddDefOptions extends Record<string, unknown>> = (va
|
|
50
50
|
* });
|
51
51
|
* ```
|
52
52
|
*/
|
53
|
-
export declare const createGuard: <
|
53
|
+
export declare const createGuard: <TValues, AddDefOptions extends Record<string, unknown> = {}>(executeGuard: GuardExecutor<TValues, AddDefOptions>) => {
|
54
|
+
(value: unknown, prevCtx?: ValidationContext<TValues> | undefined): ValidationResult;
|
55
|
+
define(overridesDefOptions: DefOptions<AddDefOptions>): any;
|
56
|
+
};
|
54
57
|
export {};
|
@@ -30,13 +30,5 @@ type RuleExecutor<ValidationType extends ValidationTypes, TValues> = (value: Val
|
|
30
30
|
* }, params);
|
31
31
|
* ```
|
32
32
|
*/
|
33
|
-
export declare const createRule: <ValidationType extends unknown, TValues = unknown>(executor: RuleExecutor<ValidationType, TValues>, commonParams?: CommonRuleParams<ValidationType> | undefined) => (value: ValidationType, prevCtx?:
|
34
|
-
global: Readonly<{
|
35
|
-
values: TValues;
|
36
|
-
overrides: Readonly<{
|
37
|
-
objectIsPartial: boolean;
|
38
|
-
}>;
|
39
|
-
}>;
|
40
|
-
createError: ({ message, code }: import("../..").ErrorInfo) => import("../..").ValidationSimpleError<{}>;
|
41
|
-
}> | undefined) => ValidationResult;
|
33
|
+
export declare const createRule: <ValidationType extends unknown, TValues = unknown>(executor: RuleExecutor<ValidationType, TValues>, commonParams?: CommonRuleParams<ValidationType> | undefined) => (value: ValidationType, prevCtx?: ValidationContext<TValues> | undefined) => ValidationResult;
|
42
34
|
export {};
|
@@ -8,12 +8,4 @@ export declare const required: ({ message, }?: {
|
|
8
8
|
* @default Обязательно
|
9
9
|
*/
|
10
10
|
message?: string | undefined;
|
11
|
-
}) => (value: unknown, prevCtx?:
|
12
|
-
global: Readonly<{
|
13
|
-
values: unknown;
|
14
|
-
overrides: Readonly<{
|
15
|
-
objectIsPartial: boolean;
|
16
|
-
}>;
|
17
|
-
}>;
|
18
|
-
createError: ({ message, code }: import("../..").ErrorInfo) => import("../..").ValidationSimpleError<{}>;
|
19
|
-
}> | undefined) => import("../..").ValidationResult;
|
11
|
+
}) => (value: unknown, prevCtx?: import("../..").ValidationContext<unknown> | undefined) => import("../..").ValidationResult;
|
package/date/date.d.ts
CHANGED
@@ -15,5 +15,12 @@ type AdditionalDefOptions = {
|
|
15
15
|
* validate(new Date('22.22.2022'));
|
16
16
|
* ```
|
17
17
|
*/
|
18
|
-
export declare const date: <TValues>(...rules: ValidationRule<Date, TValues>[]) =>
|
18
|
+
export declare const date: <TValues>(...rules: ValidationRule<Date, TValues>[]) => {
|
19
|
+
(value: unknown, prevCtx?: import("../core").ValidationContext<TValues> | undefined): import("../core").ValidationResult;
|
20
|
+
define(overridesDefOptions: Partial<AdditionalDefOptions> & {
|
21
|
+
requiredErrorMessage?: string | undefined;
|
22
|
+
typeErrorMessage?: string | undefined;
|
23
|
+
isOptional?: boolean | undefined;
|
24
|
+
}): any;
|
25
|
+
};
|
19
26
|
export {};
|
package/date/index.d.ts
CHANGED
package/date/index.js
CHANGED
@@ -18,12 +18,4 @@ import { Guard } from '../core';
|
|
18
18
|
* const result = validate({ info: { info: {} } });
|
19
19
|
* ```
|
20
20
|
*/
|
21
|
-
export declare const deepPartial: <TValues>(guard: Guard<
|
22
|
-
global: Readonly<{
|
23
|
-
values: TValues;
|
24
|
-
overrides: Readonly<{
|
25
|
-
objectIsPartial: boolean;
|
26
|
-
}>;
|
27
|
-
}>;
|
28
|
-
createError: ({ message, code }: import("../core").ErrorInfo) => import("../core").ValidationSimpleError<{}>;
|
29
|
-
}> | undefined) => import("../core").ValidationResult;
|
21
|
+
export declare const deepPartial: <TValues>(guard: Guard<TValues, {}>) => (value: unknown, prevCtx?: import("../core").ValidationContext<TValues> | undefined) => import("../core").ValidationResult;
|
package/email/email.d.ts
CHANGED
@@ -16,13 +16,5 @@ type EmailParams = {
|
|
16
16
|
* validate('example@mail.ru');
|
17
17
|
* ```
|
18
18
|
*/
|
19
|
-
export declare const email: <TValues>(params?: EmailParams) => (value: string, prevCtx?:
|
20
|
-
global: Readonly<{
|
21
|
-
values: TValues;
|
22
|
-
overrides: Readonly<{
|
23
|
-
objectIsPartial: boolean;
|
24
|
-
}>;
|
25
|
-
}>;
|
26
|
-
createError: ({ message, code }: import("../core").ErrorInfo) => import("../core").ValidationSimpleError<{}>;
|
27
|
-
}> | undefined) => import("../core").ValidationResult;
|
19
|
+
export declare const email: <TValues>(params?: EmailParams) => (value: string, prevCtx?: import("../core").ValidationContext<TValues> | undefined) => import("../core").ValidationResult;
|
28
20
|
export {};
|
package/index.d.ts
CHANGED
@@ -1,12 +1,15 @@
|
|
1
1
|
export { object, OBJECT_TYPE_ERROR_INFO, type Schema, type SchemaValue, } from './object';
|
2
2
|
export { optional } from './optional';
|
3
3
|
export { string, STRING_TYPE_ERROR_INFO } from './string';
|
4
|
+
export { date, INVALID_DATE_ERROR_INFO, DATE_TYPE_ERROR_INFO } from './date';
|
4
5
|
export { number, NAN_NUMBER_ERROR_INFO, NUMBER_TYPE_ERROR_INFO, INFINITY_NUMBER_ERROR_INFO, } from './number';
|
5
6
|
export { boolean, BOOLEAN_TYPE_ERROR_INFO } from './boolean';
|
6
7
|
export { array, ARRAY_TYPE_ERROR_INFO } from './array';
|
7
8
|
export { arrayItem } from './arrayItem';
|
8
9
|
export { deepPartial } from './deepPartial';
|
9
10
|
export { min, STRING_MIN_ERROR_CODE, ARRAY_MIN_ERROR_CODE, DATE_MIN_ERROR_CODE, NUMBER_MIN_ERROR_CODE, } from './min';
|
11
|
+
export { max, STRING_MAX_ERROR_CODE, ARRAY_MAX_ERROR_CODE, DATE_MAX_ERROR_CODE, NUMBER_MAX_ERROR_CODE, } from './max';
|
12
|
+
export { integer, INTEGER_ERROR_INFO } from './integer';
|
10
13
|
export { or } from './or';
|
11
14
|
export { pattern, PATTERN_ERROR_CODE } from './pattern';
|
12
15
|
export { onlyNumber, ONLY_NUMBER_ERROR_CODE } from './onlyNumber';
|
package/index.js
CHANGED
@@ -1,12 +1,15 @@
|
|
1
1
|
export { object, OBJECT_TYPE_ERROR_INFO, } from './object';
|
2
2
|
export { optional } from './optional';
|
3
3
|
export { string, STRING_TYPE_ERROR_INFO } from './string';
|
4
|
+
export { date, INVALID_DATE_ERROR_INFO, DATE_TYPE_ERROR_INFO } from './date';
|
4
5
|
export { number, NAN_NUMBER_ERROR_INFO, NUMBER_TYPE_ERROR_INFO, INFINITY_NUMBER_ERROR_INFO, } from './number';
|
5
6
|
export { boolean, BOOLEAN_TYPE_ERROR_INFO } from './boolean';
|
6
7
|
export { array, ARRAY_TYPE_ERROR_INFO } from './array';
|
7
8
|
export { arrayItem } from './arrayItem';
|
8
9
|
export { deepPartial } from './deepPartial';
|
9
10
|
export { min, STRING_MIN_ERROR_CODE, ARRAY_MIN_ERROR_CODE, DATE_MIN_ERROR_CODE, NUMBER_MIN_ERROR_CODE, } from './min';
|
11
|
+
export { max, STRING_MAX_ERROR_CODE, ARRAY_MAX_ERROR_CODE, DATE_MAX_ERROR_CODE, NUMBER_MAX_ERROR_CODE, } from './max';
|
12
|
+
export { integer, INTEGER_ERROR_INFO } from './integer';
|
10
13
|
export { or } from './or';
|
11
14
|
export { pattern, PATTERN_ERROR_CODE } from './pattern';
|
12
15
|
export { onlyNumber, ONLY_NUMBER_ERROR_CODE } from './onlyNumber';
|
package/innIP/innIP.d.ts
CHANGED
@@ -13,13 +13,5 @@ type InnIPParams = CommonRuleParams<string> & {
|
|
13
13
|
* validate('384212952720');
|
14
14
|
* ```
|
15
15
|
*/
|
16
|
-
export declare const innIP: <TValues>(params?: InnIPParams) => (value: string, prevCtx?:
|
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;
|
16
|
+
export declare const innIP: <TValues>(params?: InnIPParams) => (value: string, prevCtx?: import("../core").ValidationContext<TValues> | undefined) => import("../core").ValidationResult;
|
25
17
|
export {};
|
package/innUL/innUL.d.ts
CHANGED
@@ -13,13 +13,5 @@ type InnULParams = CommonRuleParams<string> & {
|
|
13
13
|
* validate('7728168971');
|
14
14
|
* ```
|
15
15
|
*/
|
16
|
-
export declare const innUL: <TValues>(params?: InnULParams) => (value: string, prevCtx?:
|
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;
|
16
|
+
export declare const innUL: <TValues>(params?: InnULParams) => (value: string, prevCtx?: import("../core").ValidationContext<TValues> | undefined) => import("../core").ValidationResult;
|
25
17
|
export {};
|
package/integer/index.js
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
type IntegerParams = {
|
2
|
+
/**
|
3
|
+
* @description Замена стандартного сообщения ошибки.
|
4
|
+
*/
|
5
|
+
message?: string;
|
6
|
+
};
|
7
|
+
/**
|
8
|
+
* @description
|
9
|
+
* Проверяет является ли значение целым числом.
|
10
|
+
* @example
|
11
|
+
* ```ts
|
12
|
+
* const validate = number(integer(5));
|
13
|
+
*
|
14
|
+
* // undefined
|
15
|
+
* validate(5)
|
16
|
+
*
|
17
|
+
* // undefined
|
18
|
+
* validate(7)
|
19
|
+
*
|
20
|
+
* // { message: 'Только целые числа' }
|
21
|
+
* validate(3.14)
|
22
|
+
* ```
|
23
|
+
*/
|
24
|
+
export declare const integer: <TValues>(params?: IntegerParams) => (value: number, prevCtx?: import("../core").ValidationContext<TValues> | undefined) => import("../core").ValidationResult;
|
25
|
+
export {};
|
@@ -0,0 +1,28 @@
|
|
1
|
+
import { createRule } from '../core';
|
2
|
+
import { INTEGER_ERROR_INFO } from './constants';
|
3
|
+
/**
|
4
|
+
* @description
|
5
|
+
* Проверяет является ли значение целым числом.
|
6
|
+
* @example
|
7
|
+
* ```ts
|
8
|
+
* const validate = number(integer(5));
|
9
|
+
*
|
10
|
+
* // undefined
|
11
|
+
* validate(5)
|
12
|
+
*
|
13
|
+
* // undefined
|
14
|
+
* validate(7)
|
15
|
+
*
|
16
|
+
* // { message: 'Только целые числа' }
|
17
|
+
* validate(3.14)
|
18
|
+
* ```
|
19
|
+
*/
|
20
|
+
export const integer = (params) => createRule((value, ctx) => {
|
21
|
+
if (!Number.isInteger(value)) {
|
22
|
+
return ctx.createError({
|
23
|
+
message: (params === null || params === void 0 ? void 0 : params.message) || INTEGER_ERROR_INFO.message,
|
24
|
+
code: INTEGER_ERROR_INFO.code,
|
25
|
+
});
|
26
|
+
}
|
27
|
+
return undefined;
|
28
|
+
});
|
package/kpp/kpp.d.ts
CHANGED
@@ -13,13 +13,5 @@ type KPPParams = CommonRuleParams<string> & {
|
|
13
13
|
* validate('770201001');
|
14
14
|
* ```
|
15
15
|
*/
|
16
|
-
export declare const kpp: <TValues>(params?: KPPParams) => (value: string, prevCtx?:
|
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;
|
16
|
+
export declare const kpp: <TValues>(params?: KPPParams) => (value: string, prevCtx?: import("../core").ValidationContext<TValues> | undefined) => import("../core").ValidationResult;
|
25
17
|
export {};
|
@@ -15,13 +15,5 @@ type MobilePhoneParams = CommonRuleParams<string> & {
|
|
15
15
|
* validate('79999999999');
|
16
16
|
* ```
|
17
17
|
*/
|
18
|
-
export declare const mobilePhone: <TValues>(params?: MobilePhoneParams) => (value: string, prevCtx?:
|
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;
|
18
|
+
export declare const mobilePhone: <TValues>(params?: MobilePhoneParams) => (value: string, prevCtx?: import("../core").ValidationContext<TValues> | undefined) => import("../core").ValidationResult;
|
27
19
|
export {};
|
package/number/number.d.ts
CHANGED
@@ -15,5 +15,12 @@ type AdditionalDefOptions = {
|
|
15
15
|
* validate(24);
|
16
16
|
* ```
|
17
17
|
*/
|
18
|
-
export declare const number: <TValues>(...rules: ValidationRule<number, TValues>[]) =>
|
18
|
+
export declare const number: <TValues>(...rules: ValidationRule<number, TValues>[]) => {
|
19
|
+
(value: unknown, prevCtx?: import("../core").ValidationContext<TValues> | undefined): import("../core").ValidationResult;
|
20
|
+
define(overridesDefOptions: Partial<AdditionalDefOptions> & {
|
21
|
+
requiredErrorMessage?: string | undefined;
|
22
|
+
typeErrorMessage?: string | undefined;
|
23
|
+
isOptional?: boolean | undefined;
|
24
|
+
}): any;
|
25
|
+
};
|
19
26
|
export {};
|
package/object/object.d.ts
CHANGED
@@ -4,8 +4,8 @@ import { Guard, ValidationContext, ValidationRule } from '../core';
|
|
4
4
|
* Переопределение необходимо для того, чтобы ts показывал, что ctx required в кастомных правилах
|
5
5
|
*/
|
6
6
|
interface ObjectPropGuard<TValues> {
|
7
|
-
(value: Parameters<Guard<
|
8
|
-
define: Guard<
|
7
|
+
(value: Parameters<Guard<TValues>>[0], ctx: ValidationContext<TValues>): ReturnType<Guard<TValues>>;
|
8
|
+
define: Guard<TValues>['define'];
|
9
9
|
}
|
10
10
|
type AdditionalDefOptions = {
|
11
11
|
/**
|
@@ -13,10 +13,6 @@ type AdditionalDefOptions = {
|
|
13
13
|
*/
|
14
14
|
isPartial?: boolean;
|
15
15
|
};
|
16
|
-
/**
|
17
|
-
* @description Тип, который необходим для того, чтобы object невозможно было использовать без использования generic
|
18
|
-
*/
|
19
|
-
type NeverSchema = Record<'__never', never>;
|
20
16
|
/**
|
21
17
|
* @description Возможные значения, принимаемые схемой
|
22
18
|
*/
|
@@ -48,5 +44,12 @@ export type Schema<TValue extends Record<string, unknown>, TValues = unknown> =
|
|
48
44
|
* });
|
49
45
|
* ```
|
50
46
|
*/
|
51
|
-
export declare const object: <Value extends Record<string, unknown
|
47
|
+
export declare const object: <Value extends Record<string, unknown>, TValues = unknown>(schema: Schema<Value, TValues>) => {
|
48
|
+
(value: unknown, prevCtx?: ValidationContext<TValues> | undefined): import("../core").ValidationResult;
|
49
|
+
define(overridesDefOptions: Partial<AdditionalDefOptions> & {
|
50
|
+
requiredErrorMessage?: string | undefined;
|
51
|
+
typeErrorMessage?: string | undefined;
|
52
|
+
isOptional?: boolean | undefined;
|
53
|
+
}): any;
|
54
|
+
};
|
52
55
|
export {};
|
package/object/object.js
CHANGED
@@ -35,9 +35,7 @@ export const object = (schema) => createGuard((value, ctx, { typeErrorMessage, i
|
|
35
35
|
const isOptional = ctx.global.overrides.objectIsPartial || isPartial;
|
36
36
|
return schemaEntries.reduce((errorMap, [key, rule]) => {
|
37
37
|
const isGuard = 'define' in rule;
|
38
|
-
const callRule = isGuard && isOptional
|
39
|
-
? optional(rule)
|
40
|
-
: rule;
|
38
|
+
const callRule = isGuard && isOptional ? optional(rule) : rule;
|
41
39
|
errorMap[key] = callRule(value[key], ctx);
|
42
40
|
return errorMap;
|
43
41
|
}, {});
|
package/ogrnIP/ogrnIP.d.ts
CHANGED
@@ -13,13 +13,5 @@ type OgrnIPParams = CommonRuleParams<string> & {
|
|
13
13
|
* validate('7728168971');
|
14
14
|
* ```
|
15
15
|
*/
|
16
|
-
export declare const ogrnIP: <TValues>(params?: OgrnIPParams) => (value: string, prevCtx?:
|
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;
|
16
|
+
export declare const ogrnIP: <TValues>(params?: OgrnIPParams) => (value: string, prevCtx?: import("../core").ValidationContext<TValues> | undefined) => import("../core").ValidationResult;
|
25
17
|
export {};
|
package/ogrnUL/ogrnUL.d.ts
CHANGED
@@ -13,13 +13,5 @@ type OgrnULParams = CommonRuleParams<string> & {
|
|
13
13
|
* validate('7728168971');
|
14
14
|
* ```
|
15
15
|
*/
|
16
|
-
export declare const ogrnUL: <TValues>(params?: OgrnULParams) => (value: string, prevCtx?:
|
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;
|
16
|
+
export declare const ogrnUL: <TValues>(params?: OgrnULParams) => (value: string, prevCtx?: import("../core").ValidationContext<TValues> | undefined) => import("../core").ValidationResult;
|
25
17
|
export {};
|
@@ -12,13 +12,5 @@ type OnlyNumberParams = {
|
|
12
12
|
* validate('123');
|
13
13
|
* ```
|
14
14
|
*/
|
15
|
-
export declare const onlyNumber: <TValues>(params?: OnlyNumberParams) => (value: string, prevCtx?:
|
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;
|
15
|
+
export declare const onlyNumber: <TValues>(params?: OnlyNumberParams) => (value: string, prevCtx?: import("../core").ValidationContext<TValues> | undefined) => import("../core").ValidationResult;
|
24
16
|
export {};
|
package/optional/optional.d.ts
CHANGED
@@ -4,4 +4,4 @@ import { Guard } from '../core';
|
|
4
4
|
* @param guard - правило, проверяющее тип значения
|
5
5
|
* @example object({ name: optional(string(min(22))) })
|
6
6
|
*/
|
7
|
-
export declare const optional: <
|
7
|
+
export declare const optional: <TValues>(guard: Guard<TValues, {}>) => Guard<TValues, {}>;
|
package/optional/optional.js
CHANGED
@@ -1,15 +1,6 @@
|
|
1
|
-
import { string } from '../string';
|
2
1
|
/**
|
3
2
|
* @description Выключает проверку на required в guard
|
4
3
|
* @param guard - правило, проверяющее тип значения
|
5
4
|
* @example object({ name: optional(string(min(22))) })
|
6
5
|
*/
|
7
6
|
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/or.d.ts
CHANGED
@@ -11,12 +11,4 @@ import { ValidationResult, ValidationRule } from '../core';
|
|
11
11
|
* const result = validate('string');
|
12
12
|
* ```
|
13
13
|
*/
|
14
|
-
export declare const or: <TValues>(...rules: ValidationRule<unknown, TValues>[]) => (value: unknown, prevCtx?:
|
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;
|
14
|
+
export declare const or: <TValues>(...rules: ValidationRule<unknown, TValues>[]) => (value: unknown, prevCtx?: import("../core").ValidationContext<TValues> | undefined) => ValidationResult;
|
package/package.json
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
{
|
2
2
|
"name": "@astral/validations",
|
3
|
-
"version": "3.0.0-beta.
|
3
|
+
"version": "3.0.0-beta.3",
|
4
4
|
"browser": "./index.js",
|
5
5
|
"main": "./index.js",
|
6
6
|
"dependencies": {
|
7
|
-
"is-plain-obj": "^4.1.0"
|
7
|
+
"is-plain-obj": "^4.1.0",
|
8
|
+
"utility-types": "^3.10.0"
|
8
9
|
},
|
9
10
|
"author": "Astral.Soft",
|
10
11
|
"license": "MIT",
|
package/partial/partial.d.ts
CHANGED
@@ -1,10 +1,16 @@
|
|
1
|
+
import { object } from '../object';
|
1
2
|
/**
|
2
3
|
* @description Выключает проверку на required для всех полей объекта
|
3
4
|
* @param objectGuard
|
4
5
|
* @example partial(object({ name: string() }))
|
5
6
|
*/
|
6
|
-
export declare const partial: (objectGuard:
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
}
|
7
|
+
export declare const partial: (objectGuard: ReturnType<typeof object>) => {
|
8
|
+
(value: unknown, prevCtx?: import("../core").ValidationContext<unknown> | undefined): import("../core").ValidationResult;
|
9
|
+
define(overridesDefOptions: Partial<{
|
10
|
+
isPartial?: boolean | undefined;
|
11
|
+
}> & {
|
12
|
+
requiredErrorMessage?: string | undefined;
|
13
|
+
typeErrorMessage?: string | undefined;
|
14
|
+
isOptional?: boolean | undefined;
|
15
|
+
}): any;
|
16
|
+
};
|
package/pattern/pattern.d.ts
CHANGED
@@ -14,13 +14,5 @@ type PatternParams = {
|
|
14
14
|
* string(pattern(/[0-9]/))
|
15
15
|
* ```
|
16
16
|
*/
|
17
|
-
export declare const pattern: <TValues>(regex: RegExp, params?: PatternParams) => (value: string, prevCtx?:
|
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;
|
17
|
+
export declare const pattern: <TValues>(regex: RegExp, params?: PatternParams) => (value: string, prevCtx?: import("../core").ValidationContext<TValues> | undefined) => import("../core").ValidationResult;
|
26
18
|
export {};
|
package/snils/snils.d.ts
CHANGED
@@ -13,13 +13,5 @@ type SnilsParams = CommonRuleParams<string> & {
|
|
13
13
|
* validate('15657325992');
|
14
14
|
* ```
|
15
15
|
*/
|
16
|
-
export declare const snils: <TValues>(params?: SnilsParams) => (value: string, prevCtx?:
|
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;
|
16
|
+
export declare const snils: <TValues>(params?: SnilsParams) => (value: string, prevCtx?: import("../core").ValidationContext<TValues> | undefined) => import("../core").ValidationResult;
|
25
17
|
export {};
|
package/string/string.d.ts
CHANGED
@@ -1,2 +1,9 @@
|
|
1
1
|
import { ValidationRule } from '../core';
|
2
|
-
export declare const string: <TValues>(...rules: ValidationRule<string, TValues>[]) =>
|
2
|
+
export declare const string: <TValues>(...rules: ValidationRule<string, TValues>[]) => {
|
3
|
+
(value: unknown, prevCtx?: import("../core").ValidationContext<TValues> | undefined): import("../core").ValidationResult;
|
4
|
+
define(overridesDefOptions: Partial<{}> & {
|
5
|
+
requiredErrorMessage?: string | undefined;
|
6
|
+
typeErrorMessage?: string | undefined;
|
7
|
+
isOptional?: boolean | undefined;
|
8
|
+
}): any;
|
9
|
+
};
|
package/transform/transform.d.ts
CHANGED
@@ -14,13 +14,5 @@ type Transformer<TValue, TResult> = (value: TValue) => TResult;
|
|
14
14
|
* );
|
15
15
|
* ```
|
16
16
|
*/
|
17
|
-
export declare const transform: <TValue extends unknown, TResult extends unknown, TValues>(transformer: Transformer<TValue, TResult>, ...rules: ValidationRule<TResult, TValues>[]) => (value: TValue, prevCtx?:
|
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;
|
17
|
+
export declare const transform: <TValue extends unknown, TResult extends unknown, TValues>(transformer: Transformer<TValue, TResult>, ...rules: ValidationRule<TResult, TValues>[]) => (value: TValue, prevCtx?: import("../core").ValidationContext<TValues> | undefined) => import("../core").ValidationResult;
|
26
18
|
export {};
|
package/when/when.d.ts
CHANGED
@@ -35,13 +35,5 @@ type Params<TValues> = {
|
|
35
35
|
* const result2 = validate({ isAgree: true, name: '' });
|
36
36
|
* ```
|
37
37
|
*/
|
38
|
-
export declare const when: <TValues>({ is, then, otherwise }: Params<TValues>) => (value: unknown, prevCtx?:
|
39
|
-
global: Readonly<{
|
40
|
-
values: TValues;
|
41
|
-
overrides: Readonly<{
|
42
|
-
objectIsPartial: boolean;
|
43
|
-
}>;
|
44
|
-
}>;
|
45
|
-
createError: ({ message, code }: import("../core").ErrorInfo) => import("../core").ValidationSimpleError<{}>;
|
46
|
-
}> | undefined) => import("../core").ValidationResult;
|
38
|
+
export declare const when: <TValues>({ is, then, otherwise }: Params<TValues>) => (value: unknown, prevCtx?: ValidationContext<TValues> | undefined) => import("../core").ValidationResult;
|
47
39
|
export {};
|