@astral/validations 3.0.0-beta.1 → 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 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
  - Возвращает ошибку если:
@@ -890,7 +912,7 @@ type Values = { name: string; isAgree: boolean };
890
912
 
891
913
  const validate = object<Values, Values>({
892
914
  name: when({
893
- is: (_, ctx) => ctx.global.values.isAgree,
915
+ is: (_, ctx) => Boolean(ctx.global.values.isAgree),
894
916
  then: string(),
895
917
  otherwise: any(),
896
918
  }),
@@ -944,7 +966,7 @@ type Values = {
944
966
  nickname: string;
945
967
  };
946
968
 
947
- const validate = object<Values>({
969
+ const validate = object<Values, Values>({
948
970
  name: string(),
949
971
  nickname: string((value, ctx) => {
950
972
  if (value.includes('_')) {
@@ -976,7 +998,7 @@ type Values = {
976
998
 
977
999
  const validate = object<Values, Values>({
978
1000
  password: string(min(9)),
979
- repeatPassword: string<Values>(min(9), (value, ctx) => {
1001
+ repeatPassword: string(min(9), (value, ctx) => {
980
1002
  if (value !== ctx.global.values.password) {
981
1003
  return ctx.createError({
982
1004
  message: 'Пароли не совпадают',
@@ -1069,7 +1091,7 @@ type Values = { name: string; isAgree: boolean };
1069
1091
 
1070
1092
  const validate = object<Values, Values>({
1071
1093
  name: when({
1072
- is: (_, ctx) => ctx.global.values.isAgree,
1094
+ is: (_, ctx) => Boolean(ctx.global.values.isAgree),
1073
1095
  then: string(),
1074
1096
  otherwise: any(),
1075
1097
  }),
@@ -1094,7 +1116,7 @@ type Values = {
1094
1116
 
1095
1117
  const validate = object<Values, Values>({
1096
1118
  name: string(),
1097
- info: when<Values>({
1119
+ info: when({
1098
1120
  is: (_, ctx) => ctx.global.values.name === 'Vasya',
1099
1121
  then: object<ValuesInfo>({ surname: string() }),
1100
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?: Readonly<{
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>[]) => import("../core").Guard<unknown[], 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
+ };
@@ -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?: Readonly<{
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;
@@ -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>[]) => import("../core").Guard<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
+ };
@@ -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> = Readonly<{
6
+ export type ValidationContext<TValues> = DeepReadonly<{
6
7
  /**
7
8
  * @description Глобальные значения, идущие от самого верхнего правила к самому нижнему
8
9
  */
9
- global: Readonly<{
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: Readonly<{
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, ValidationTypes } from '../../types';
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<ValidationType> = ValidationType | undefined | null | unknown;
20
+ type GuardValue = unknown;
21
21
  /**
22
22
  * @description Интерфейс функции guard, которая в прототипе содержит метод define
23
23
  */
24
- export interface Guard<ValidationType extends ValidationTypes, TValues, AddDefOptions extends Record<string, unknown> = {}> {
25
- (value: GuardValue<ValidationType>, ctx?: ValidationContext<TValues>): ValidationResult;
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<ValidationType, TValues, AddDefOptions>;
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: <ValidationType extends unknown, TValues, AddDefOptions extends Record<string, unknown> = {}>(executeGuard: GuardExecutor<TValues, AddDefOptions>) => Guard<ValidationType, TValues, AddDefOptions>;
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?: Readonly<{
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?: Readonly<{
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>[]) => import("../core").Guard<Date, TValues, AdditionalDefOptions>;
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 {};
@@ -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<unknown, TValues, {}>) => (value: unknown, prevCtx?: Readonly<{
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?: Readonly<{
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
@@ -8,6 +8,8 @@ export { array, ARRAY_TYPE_ERROR_INFO } from './array';
8
8
  export { arrayItem } from './arrayItem';
9
9
  export { deepPartial } from './deepPartial';
10
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';
11
13
  export { or } from './or';
12
14
  export { pattern, PATTERN_ERROR_CODE } from './pattern';
13
15
  export { onlyNumber, ONLY_NUMBER_ERROR_CODE } from './onlyNumber';
package/index.js CHANGED
@@ -8,6 +8,8 @@ export { array, ARRAY_TYPE_ERROR_INFO } from './array';
8
8
  export { arrayItem } from './arrayItem';
9
9
  export { deepPartial } from './deepPartial';
10
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';
11
13
  export { or } from './or';
12
14
  export { pattern, PATTERN_ERROR_CODE } from './pattern';
13
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?: 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;
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?: 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;
16
+ export declare const innUL: <TValues>(params?: InnULParams) => (value: string, prevCtx?: import("../core").ValidationContext<TValues> | undefined) => import("../core").ValidationResult;
25
17
  export {};
@@ -0,0 +1,2 @@
1
+ import { ErrorInfo } from '../core';
2
+ export declare const INTEGER_ERROR_INFO: ErrorInfo;
@@ -0,0 +1,5 @@
1
+ import { createErrorCode } from '../core';
2
+ export const INTEGER_ERROR_INFO = {
3
+ code: createErrorCode('integer'),
4
+ message: 'Только целые числа',
5
+ };
@@ -0,0 +1,2 @@
1
+ export * from './constants';
2
+ export * from './integer';
@@ -0,0 +1,2 @@
1
+ export * from './constants';
2
+ export * from './integer';
@@ -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?: 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;
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?: 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;
18
+ export declare const mobilePhone: <TValues>(params?: MobilePhoneParams) => (value: string, prevCtx?: import("../core").ValidationContext<TValues> | undefined) => import("../core").ValidationResult;
27
19
  export {};
@@ -15,5 +15,12 @@ type AdditionalDefOptions = {
15
15
  * validate(24);
16
16
  * ```
17
17
  */
18
- export declare const number: <TValues>(...rules: ValidationRule<number, TValues>[]) => import("../core").Guard<number, TValues, AdditionalDefOptions>;
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 {};
@@ -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<unknown, TValues>>[0], ctx: ValidationContext<TValues>): ReturnType<Guard<unknown, TValues>>;
8
- define: Guard<unknown, TValues>['define'];
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> = NeverSchema, TValues = unknown>(schema: Schema<Value, TValues>) => Guard<Value, TValues, AdditionalDefOptions>;
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
  }, {});
@@ -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?: 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;
16
+ export declare const ogrnIP: <TValues>(params?: OgrnIPParams) => (value: string, prevCtx?: import("../core").ValidationContext<TValues> | undefined) => import("../core").ValidationResult;
25
17
  export {};
@@ -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?: 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;
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?: 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;
15
+ export declare const onlyNumber: <TValues>(params?: OnlyNumberParams) => (value: string, prevCtx?: import("../core").ValidationContext<TValues> | undefined) => import("../core").ValidationResult;
24
16
  export {};
@@ -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: <ValidationType extends unknown, TValues>(guard: Guard<ValidationType, TValues, {}>) => Guard<ValidationType, TValues, {}>;
7
+ export declare const optional: <TValues>(guard: Guard<TValues, {}>) => Guard<TValues, {}>;
@@ -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?: 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;
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.1",
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",
@@ -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: import("../core").Guard<Record<string, unknown>, unknown, {
7
- isPartial?: boolean | undefined;
8
- }>) => import("../core").Guard<Record<string, unknown>, unknown, {
9
- isPartial?: boolean | undefined;
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
+ };
@@ -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?: 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;
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?: 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;
16
+ export declare const snils: <TValues>(params?: SnilsParams) => (value: string, prevCtx?: import("../core").ValidationContext<TValues> | undefined) => import("../core").ValidationResult;
25
17
  export {};
@@ -1,2 +1,9 @@
1
1
  import { ValidationRule } from '../core';
2
- export declare const string: <TValues>(...rules: ValidationRule<string, TValues>[]) => import("../core").Guard<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
+ };
@@ -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?: 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;
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?: Readonly<{
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 {};