@astral/validations 3.3.0 → 4.1.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.
Files changed (79) hide show
  1. package/README.md +129 -18
  2. package/any/any.d.ts +1 -1
  3. package/array/array.d.ts +2 -2
  4. package/arrayItem/arrayItem.d.ts +1 -1
  5. package/boolean/boolean.d.ts +2 -2
  6. package/core/compose/compose.d.ts +1 -1
  7. package/core/context/createContext/createContext.d.ts +2 -2
  8. package/core/context/createContext/createContext.js +7 -2
  9. package/core/context/types.d.ts +6 -2
  10. package/core/guard/createGuard/createGuard.d.ts +7 -7
  11. package/core/guard/createGuard/createGuard.js +1 -1
  12. package/core/rule/createRule/createRule.d.ts +3 -3
  13. package/core/rule/required/required.d.ts +1 -1
  14. package/core/rule/types.d.ts +3 -3
  15. package/core/utils/index.d.ts +4 -0
  16. package/core/utils/index.js +4 -0
  17. package/core/utils/isFullNameValidCharacters/index.d.ts +1 -0
  18. package/core/utils/isFullNameValidCharacters/index.js +1 -0
  19. package/core/utils/isFullNameValidCharacters/isFullNameValidCharacters.d.ts +5 -0
  20. package/core/utils/isFullNameValidCharacters/isFullNameValidCharacters.js +5 -0
  21. package/core/utils/isHasConsecutiveChars/index.d.ts +1 -0
  22. package/core/utils/isHasConsecutiveChars/index.js +1 -0
  23. package/core/utils/isHasConsecutiveChars/isHasConsecutiveChars.d.ts +4 -0
  24. package/core/utils/isHasConsecutiveChars/isHasConsecutiveChars.js +4 -0
  25. package/core/utils/isStartsWithAndEndsWithLetter/index.d.ts +1 -0
  26. package/core/utils/isStartsWithAndEndsWithLetter/index.js +1 -0
  27. package/core/utils/isStartsWithAndEndsWithLetter/isStartsWithAndEndsWithLetter.d.ts +4 -0
  28. package/core/utils/isStartsWithAndEndsWithLetter/isStartsWithAndEndsWithLetter.js +4 -0
  29. package/core/utils/isValidFullNameLength/index.d.ts +1 -0
  30. package/core/utils/isValidFullNameLength/index.js +1 -0
  31. package/core/utils/isValidFullNameLength/isValidFullNameLength.d.ts +4 -0
  32. package/core/utils/isValidFullNameLength/isValidFullNameLength.js +4 -0
  33. package/date/date.d.ts +2 -2
  34. package/deepPartial/deepPartial.d.ts +1 -1
  35. package/email/email.d.ts +1 -1
  36. package/index.d.ts +3 -0
  37. package/index.js +3 -0
  38. package/innIP/innIP.d.ts +1 -1
  39. package/innUL/innUL.d.ts +1 -1
  40. package/integer/integer.d.ts +1 -1
  41. package/kpp/kpp.d.ts +1 -1
  42. package/max/max.d.ts +3 -3
  43. package/min/min.d.ts +3 -3
  44. package/mobilePhone/mobilePhone.d.ts +1 -1
  45. package/number/number.d.ts +2 -2
  46. package/object/object.d.ts +8 -8
  47. package/object/object.js +5 -4
  48. package/ogrnIP/ogrnIP.d.ts +1 -1
  49. package/ogrnUL/ogrnUL.d.ts +1 -1
  50. package/onlyNumber/onlyNumber.d.ts +1 -1
  51. package/optional/optional.d.ts +1 -1
  52. package/or/or.d.ts +1 -1
  53. package/package.json +1 -1
  54. package/partial/partial.d.ts +1 -1
  55. package/pattern/pattern.d.ts +1 -1
  56. package/personName/constants.d.ts +2 -0
  57. package/personName/constants.js +5 -0
  58. package/personName/index.d.ts +2 -0
  59. package/personName/index.js +2 -0
  60. package/personName/personName.d.ts +17 -0
  61. package/personName/personName.js +29 -0
  62. package/personPatronymic/constants.d.ts +2 -0
  63. package/personPatronymic/constants.js +5 -0
  64. package/personPatronymic/index.d.ts +2 -0
  65. package/personPatronymic/index.js +2 -0
  66. package/personPatronymic/personPatronymic.d.ts +17 -0
  67. package/personPatronymic/personPatronymic.js +29 -0
  68. package/personSurname/constants.d.ts +2 -0
  69. package/personSurname/constants.js +5 -0
  70. package/personSurname/index.d.ts +2 -0
  71. package/personSurname/index.js +2 -0
  72. package/personSurname/personSurname.d.ts +17 -0
  73. package/personSurname/personSurname.js +29 -0
  74. package/positiveNumber/positiveNumber.d.ts +1 -1
  75. package/snils/snils.d.ts +1 -1
  76. package/string/string.d.ts +2 -2
  77. package/transform/transform.d.ts +1 -1
  78. package/when/when.d.ts +6 -6
  79. package/when/when.js +2 -2
@@ -21,5 +21,5 @@ type IntegerParams = {
21
21
  * validate(3.14)
22
22
  * ```
23
23
  */
24
- export declare const integer: <TValues>(params?: IntegerParams) => (value: number, prevCtx?: import("../core").ValidationContext<TValues> | undefined) => import("../core").ValidationResult;
24
+ export declare const integer: <TLastSchemaValues extends Record<string, unknown>>(params?: IntegerParams) => (value: number, prevCtx?: import("../core").ValidationContext<TLastSchemaValues> | undefined) => import("../core").ValidationResult;
25
25
  export {};
package/kpp/kpp.d.ts CHANGED
@@ -13,5 +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?: import("../core").ValidationContext<TValues> | undefined) => import("../core").ValidationResult;
16
+ export declare const kpp: <TLastSchemaValues extends Record<string, unknown>>(params?: KPPParams) => (value: string, prevCtx?: import("../core").ValidationContext<TLastSchemaValues> | undefined) => import("../core").ValidationResult;
17
17
  export {};
package/max/max.d.ts CHANGED
@@ -5,7 +5,7 @@ type MaxParams<ValidationType> = {
5
5
  /**
6
6
  * @description Сообщение ошибки
7
7
  */
8
- getMessage?: (threshold: CommonThreshold, value: ValidationType, ctx: ValidationContext<unknown>) => string;
8
+ getMessage?: (threshold: CommonThreshold, value: ValidationType, ctx: ValidationContext) => string;
9
9
  };
10
10
  /**
11
11
  * @description Проверяет значение на соответствие максимуму. Работает с: string, array, Date, number
@@ -21,6 +21,6 @@ type MaxParams<ValidationType> = {
21
21
  * date(max(new Date())));
22
22
  * ```
23
23
  */
24
- export declare function max<ValidationType extends Date>(threshold: Date, params?: MaxParams<ValidationType>): ReturnType<typeof createRule<Date, unknown>>;
25
- export declare function max<ValidationType extends BaseMaxValidationTypes>(threshold: number, params?: MaxParams<ValidationType>): ReturnType<typeof createRule<BaseMaxValidationTypes, unknown>>;
24
+ export declare function max<ValidationType extends Date>(threshold: Date, params?: MaxParams<ValidationType>): ReturnType<typeof createRule<Date>>;
25
+ export declare function max<ValidationType extends BaseMaxValidationTypes>(threshold: number, params?: MaxParams<ValidationType>): ReturnType<typeof createRule<BaseMaxValidationTypes>>;
26
26
  export {};
package/min/min.d.ts CHANGED
@@ -5,7 +5,7 @@ type MinParams<ValidationType> = {
5
5
  /**
6
6
  * @description Сообщение ошибки
7
7
  */
8
- getMessage?: (threshold: CommonThreshold, value: ValidationType, ctx: ValidationContext<unknown>) => string;
8
+ getMessage?: (threshold: CommonThreshold, value: ValidationType, ctx: ValidationContext) => string;
9
9
  };
10
10
  /**
11
11
  * @description Проверяет значение на соответствие минимуму. Работает с: string, array, Date, number
@@ -21,6 +21,6 @@ type MinParams<ValidationType> = {
21
21
  * date(min(new Date())));
22
22
  * ```
23
23
  */
24
- export declare function min<ValidationType extends Date>(threshold: Date, params?: MinParams<ValidationType>): ReturnType<typeof createRule<Date, unknown>>;
25
- export declare function min<ValidationType extends BaseMinValidationTypes>(threshold: number, params?: MinParams<ValidationType>): ReturnType<typeof createRule<BaseMinValidationTypes, unknown>>;
24
+ export declare function min<ValidationType extends Date>(threshold: Date, params?: MinParams<ValidationType>): ReturnType<typeof createRule<Date>>;
25
+ export declare function min<ValidationType extends BaseMinValidationTypes>(threshold: number, params?: MinParams<ValidationType>): ReturnType<typeof createRule<BaseMinValidationTypes>>;
26
26
  export {};
@@ -15,5 +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?: import("../core").ValidationContext<TValues> | undefined) => import("../core").ValidationResult;
18
+ export declare const mobilePhone: <TLastSchemaValues extends Record<string, unknown>>(params?: MobilePhoneParams) => (value: string, prevCtx?: import("../core").ValidationContext<TLastSchemaValues> | undefined) => import("../core").ValidationResult;
19
19
  export {};
@@ -15,8 +15,8 @@ type AdditionalDefOptions = {
15
15
  * validate(24);
16
16
  * ```
17
17
  */
18
- export declare const number: <TValues>(...rules: ValidationRule<number, TValues>[]) => {
19
- (value: unknown, prevCtx?: import("../core").ValidationContext<TValues> | undefined): import("../core").ValidationResult;
18
+ export declare const number: <TLastSchemaValues extends Record<string, unknown>>(...rules: ValidationRule<number, TLastSchemaValues>[]) => {
19
+ (value: unknown, prevCtx?: import("../core").ValidationContext<TLastSchemaValues> | undefined): import("../core").ValidationResult;
20
20
  define(overridesDefOptions: Partial<AdditionalDefOptions> & {
21
21
  requiredErrorMessage?: string | undefined;
22
22
  typeErrorMessage?: string | undefined;
@@ -3,9 +3,9 @@ import { Guard, ValidationContext, ValidationRule } from '../core';
3
3
  * @description Специальный итерфейс Guard для object. В данном интерфейсе ctx required
4
4
  * Переопределение необходимо для того, чтобы ts показывал, что ctx required в кастомных правилах
5
5
  */
6
- interface ObjectPropGuard<TValues> {
7
- (value: Parameters<Guard<TValues>>[0], ctx: ValidationContext<TValues>): ReturnType<Guard<TValues>>;
8
- define: Guard<TValues>['define'];
6
+ interface ObjectPropGuard<TLastSchemaValues extends Record<string, unknown>> {
7
+ (value: Parameters<Guard<TLastSchemaValues>>[0], ctx: ValidationContext<TLastSchemaValues>): ReturnType<Guard<TLastSchemaValues>>;
8
+ define: Guard<TLastSchemaValues>['define'];
9
9
  }
10
10
  type AdditionalDefOptions = {
11
11
  /**
@@ -16,11 +16,11 @@ type AdditionalDefOptions = {
16
16
  /**
17
17
  * @description Возможные значения, принимаемые схемой
18
18
  */
19
- export type SchemaValue<TValues> = ObjectPropGuard<TValues> | ValidationRule<unknown, TValues>;
19
+ export type SchemaValue<TValue extends Record<string, unknown>> = ObjectPropGuard<TValue> | ValidationRule<unknown, TValue>;
20
20
  /**
21
21
  * @description Схема правил валдиации для объекта
22
22
  */
23
- export type Schema<TValue extends Record<string, unknown>, TValues = unknown> = Record<keyof TValue, SchemaValue<TValues>>;
23
+ export type Schema<TValue extends Record<string, unknown>> = Record<keyof TValue, SchemaValue<TValue>>;
24
24
  /**
25
25
  * @description Guard для объекта
26
26
  * @param schema - схема валидации объекта
@@ -44,13 +44,13 @@ export type Schema<TValue extends Record<string, unknown>, TValues = unknown> =
44
44
  * });
45
45
  * ```
46
46
  */
47
- export declare const object: <TValue extends Record<string, unknown>, TValues = unknown>(schema: Schema<TValue, TValues>) => {
48
- (value: unknown, prevCtx?: ValidationContext<TValues> | undefined): import("../core").ValidationResult;
47
+ export declare const object: <TValue extends Record<string, unknown>, TLastSchemaValues extends Record<string, unknown> = {}>(schema: Schema<TValue>) => {
48
+ (value: unknown, prevCtx?: ValidationContext<TLastSchemaValues> | undefined): import("../core").ValidationResult;
49
49
  define(overridesDefOptions: Partial<AdditionalDefOptions> & {
50
50
  requiredErrorMessage?: string | undefined;
51
51
  typeErrorMessage?: string | undefined;
52
52
  isOptional?: boolean | undefined;
53
53
  }): any;
54
54
  };
55
- export type ObjectGuard<TValue extends Record<string, unknown>, TValues = unknown> = ReturnType<typeof object<TValue, TValues>>;
55
+ export type ObjectGuard<TValue extends Record<string, unknown>> = ReturnType<typeof object<TValue>>;
56
56
  export {};
package/object/object.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import isPlainObject from 'is-plain-obj';
2
- import { createErrorMap, createGuard, } from '../core';
2
+ import { createContext, createErrorMap, createGuard, } from '../core';
3
3
  import { optional } from '../optional';
4
4
  import { isEmptyErrors } from './isEmptyErrors';
5
5
  import { OBJECT_TYPE_ERROR_INFO } from './constants';
@@ -27,16 +27,17 @@ import { OBJECT_TYPE_ERROR_INFO } from './constants';
27
27
  * ```
28
28
  */
29
29
  export const object = (schema) => createGuard((value, ctx, { typeErrorMessage, isPartial }) => {
30
+ const context = createContext(ctx, value, value);
30
31
  if (!isPlainObject(value)) {
31
- return ctx.createError(Object.assign(Object.assign({}, OBJECT_TYPE_ERROR_INFO), { message: typeErrorMessage || OBJECT_TYPE_ERROR_INFO.message }));
32
+ return context.createError(Object.assign(Object.assign({}, OBJECT_TYPE_ERROR_INFO), { message: typeErrorMessage || OBJECT_TYPE_ERROR_INFO.message }));
32
33
  }
33
34
  const generateErrorMap = () => {
34
35
  const schemaEntries = Object.entries(schema);
35
- const isOptional = ctx.global.overrides.objectIsPartial || isPartial;
36
+ const isOptional = context.global.overrides.objectIsPartial || isPartial;
36
37
  return schemaEntries.reduce((errorMap, [key, rule]) => {
37
38
  const isGuard = 'define' in rule;
38
39
  const callRule = isGuard && isOptional ? optional(rule) : rule;
39
- errorMap[key] = callRule(value[key], ctx);
40
+ errorMap[key] = callRule(value[key], context);
40
41
  return errorMap;
41
42
  }, {});
42
43
  };
@@ -13,5 +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?: import("../core").ValidationContext<TValues> | undefined) => import("../core").ValidationResult;
16
+ export declare const ogrnIP: <TLastSchemaValues extends Record<string, unknown>>(params?: OgrnIPParams) => (value: string, prevCtx?: import("../core").ValidationContext<TLastSchemaValues> | undefined) => import("../core").ValidationResult;
17
17
  export {};
@@ -13,5 +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?: import("../core").ValidationContext<TValues> | undefined) => import("../core").ValidationResult;
16
+ export declare const ogrnUL: <TLastSchemaValues extends Record<string, unknown>>(params?: OgrnULParams) => (value: string, prevCtx?: import("../core").ValidationContext<TLastSchemaValues> | undefined) => import("../core").ValidationResult;
17
17
  export {};
@@ -12,5 +12,5 @@ type OnlyNumberParams = {
12
12
  * validate('123');
13
13
  * ```
14
14
  */
15
- export declare const onlyNumber: <TValues>(params?: OnlyNumberParams) => (value: string, prevCtx?: import("../core").ValidationContext<TValues> | undefined) => import("../core").ValidationResult;
15
+ export declare const onlyNumber: <TLastSchemaValues extends Record<string, unknown>>(params?: OnlyNumberParams) => (value: string, prevCtx?: import("../core").ValidationContext<TLastSchemaValues> | undefined) => import("../core").ValidationResult;
16
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: <TValues>(guard: Guard<TValues, {}>) => Guard<TValues, {}>;
7
+ export declare const optional: <TLastSchemaValues extends Record<string, unknown>>(guard: Guard<TLastSchemaValues, {}>) => Guard<TLastSchemaValues, {}>;
package/or/or.d.ts CHANGED
@@ -11,4 +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?: import("../core").ValidationContext<TValues> | undefined) => ValidationResult;
14
+ export declare const or: <TLastSchemaValues extends Record<string, unknown>>(...rules: ValidationRule<unknown, TLastSchemaValues>[]) => (value: unknown, prevCtx?: import("../core").ValidationContext<TLastSchemaValues> | undefined) => ValidationResult;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astral/validations",
3
- "version": "3.3.0",
3
+ "version": "4.1.0",
4
4
  "browser": "./index.js",
5
5
  "main": "./index.js",
6
6
  "dependencies": {
@@ -5,7 +5,7 @@ import { object } from '../object';
5
5
  * @example partial(object({ name: string() }))
6
6
  */
7
7
  export declare const partial: (objectGuard: ReturnType<typeof object>) => {
8
- (value: unknown, prevCtx?: import("../core").ValidationContext<unknown> | undefined): import("../core").ValidationResult;
8
+ (value: unknown, prevCtx?: import("../core").ValidationContext<Record<string, unknown>> | undefined): import("../core").ValidationResult;
9
9
  define(overridesDefOptions: Partial<{
10
10
  isPartial?: boolean | undefined;
11
11
  }> & {
@@ -14,5 +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?: import("../core").ValidationContext<TValues> | undefined) => import("../core").ValidationResult;
17
+ export declare const pattern: <TLastSchemaValues extends Record<string, unknown>>(regex: RegExp, params?: PatternParams) => (value: string, prevCtx?: import("../core").ValidationContext<TLastSchemaValues> | undefined) => import("../core").ValidationResult;
18
18
  export {};
@@ -0,0 +1,2 @@
1
+ import { ErrorInfo } from '../core';
2
+ export declare const PERSON_NAME_ERROR_INFO: ErrorInfo;
@@ -0,0 +1,5 @@
1
+ import { createErrorCode } from '../core';
2
+ export const PERSON_NAME_ERROR_INFO = {
3
+ code: createErrorCode('personName'),
4
+ message: 'Проверьте имя',
5
+ };
@@ -0,0 +1,2 @@
1
+ export * from './constants';
2
+ export * from './personName';
@@ -0,0 +1,2 @@
1
+ export * from './constants';
2
+ export * from './personName';
@@ -0,0 +1,17 @@
1
+ import { CommonRuleParams } from '../core';
2
+ type PersonNameParams = CommonRuleParams<string> & {
3
+ /**
4
+ * @description Замена стандартного сообщения ошибки.
5
+ */
6
+ message?: string;
7
+ };
8
+ /**
9
+ * @description Проверяет валидно ли имя
10
+ * @example
11
+ * ```ts
12
+ * const validate = string(personName());
13
+ * validate("Иван");
14
+ * ```
15
+ */
16
+ export declare const personName: <TLastSchemaValues extends Record<string, unknown>>(params?: PersonNameParams) => (value: string, prevCtx?: import("../core").ValidationContext<TLastSchemaValues> | undefined) => import("../core").ValidationResult;
17
+ export {};
@@ -0,0 +1,29 @@
1
+ import { createRule, isFullNameValidCharacters, isHasConsecutiveChars, isStartsWithAndEndsWithLetter, isValidFullNameLength, } from '../core';
2
+ import { PERSON_NAME_ERROR_INFO } from './constants';
3
+ /**
4
+ * @description Проверяет валидно ли имя
5
+ * @example
6
+ * ```ts
7
+ * const validate = string(personName());
8
+ * validate("Иван");
9
+ * ```
10
+ */
11
+ export const personName = (params) => createRule((value, ctx) => {
12
+ const createPersonNameError = () => ctx.createError({
13
+ message: (params === null || params === void 0 ? void 0 : params.message) || PERSON_NAME_ERROR_INFO.message,
14
+ code: PERSON_NAME_ERROR_INFO.code,
15
+ });
16
+ if (isValidFullNameLength(value)) {
17
+ return createPersonNameError();
18
+ }
19
+ if (isFullNameValidCharacters(value)) {
20
+ return createPersonNameError();
21
+ }
22
+ if (isStartsWithAndEndsWithLetter(value)) {
23
+ return createPersonNameError();
24
+ }
25
+ if (isHasConsecutiveChars(value)) {
26
+ return createPersonNameError();
27
+ }
28
+ return undefined;
29
+ }, { exclude: params === null || params === void 0 ? void 0 : params.exclude });
@@ -0,0 +1,2 @@
1
+ import { ErrorInfo } from '../core';
2
+ export declare const PERSON_PATRONYMIC_ERROR_INFO: ErrorInfo;
@@ -0,0 +1,5 @@
1
+ import { createErrorCode } from '../core';
2
+ export const PERSON_PATRONYMIC_ERROR_INFO = {
3
+ code: createErrorCode('personPatronymic'),
4
+ message: 'Проверьте отчество',
5
+ };
@@ -0,0 +1,2 @@
1
+ export * from './constants';
2
+ export * from './personPatronymic';
@@ -0,0 +1,2 @@
1
+ export * from './constants';
2
+ export * from './personPatronymic';
@@ -0,0 +1,17 @@
1
+ import { CommonRuleParams } from '../core';
2
+ type PersonPatronymicParams = CommonRuleParams<string> & {
3
+ /**
4
+ * @description Замена стандартного сообщения ошибки.
5
+ */
6
+ message?: string;
7
+ };
8
+ /**
9
+ * @description Проверяет валидно ли отчество
10
+ * @example
11
+ * ```ts
12
+ * const validate = string(personPatronymic());
13
+ * validate("Иванович");
14
+ * ```
15
+ */
16
+ export declare const personPatronymic: <TLastSchemaValues extends Record<string, unknown>>(params?: PersonPatronymicParams) => (value: string, prevCtx?: import("../core").ValidationContext<TLastSchemaValues> | undefined) => import("../core").ValidationResult;
17
+ export {};
@@ -0,0 +1,29 @@
1
+ import { createRule, isFullNameValidCharacters, isHasConsecutiveChars, isStartsWithAndEndsWithLetter, isValidFullNameLength, } from '../core';
2
+ import { PERSON_PATRONYMIC_ERROR_INFO } from './constants';
3
+ /**
4
+ * @description Проверяет валидно ли отчество
5
+ * @example
6
+ * ```ts
7
+ * const validate = string(personPatronymic());
8
+ * validate("Иванович");
9
+ * ```
10
+ */
11
+ export const personPatronymic = (params) => createRule((value, ctx) => {
12
+ const createPersonPatronymicError = () => ctx.createError({
13
+ message: (params === null || params === void 0 ? void 0 : params.message) || PERSON_PATRONYMIC_ERROR_INFO.message,
14
+ code: PERSON_PATRONYMIC_ERROR_INFO.code,
15
+ });
16
+ if (isValidFullNameLength(value)) {
17
+ return createPersonPatronymicError();
18
+ }
19
+ if (isFullNameValidCharacters(value)) {
20
+ return createPersonPatronymicError();
21
+ }
22
+ if (isStartsWithAndEndsWithLetter(value)) {
23
+ return createPersonPatronymicError();
24
+ }
25
+ if (isHasConsecutiveChars(value)) {
26
+ return createPersonPatronymicError();
27
+ }
28
+ return undefined;
29
+ }, { exclude: params === null || params === void 0 ? void 0 : params.exclude });
@@ -0,0 +1,2 @@
1
+ import { ErrorInfo } from '../core';
2
+ export declare const PERSON_SURNAME_ERROR_INFO: ErrorInfo;
@@ -0,0 +1,5 @@
1
+ import { createErrorCode } from '../core';
2
+ export const PERSON_SURNAME_ERROR_INFO = {
3
+ code: createErrorCode('personSurname'),
4
+ message: 'Проверьте фамилию',
5
+ };
@@ -0,0 +1,2 @@
1
+ export * from './constants';
2
+ export * from './personSurname';
@@ -0,0 +1,2 @@
1
+ export * from './constants';
2
+ export * from './personSurname';
@@ -0,0 +1,17 @@
1
+ import { CommonRuleParams } from '../core';
2
+ type PersonSurnameParams = CommonRuleParams<string> & {
3
+ /**
4
+ * @description Замена стандартного сообщения ошибки.
5
+ */
6
+ message?: string;
7
+ };
8
+ /**
9
+ * @description Проверяет валидно ли фамилия
10
+ * @example
11
+ * ```ts
12
+ * const validate = string(personSurname());
13
+ * validate("Иванов");
14
+ * ```
15
+ */
16
+ export declare const personSurname: <TLastSchemaValues extends Record<string, unknown>>(params?: PersonSurnameParams) => (value: string, prevCtx?: import("../core").ValidationContext<TLastSchemaValues> | undefined) => import("../core").ValidationResult;
17
+ export {};
@@ -0,0 +1,29 @@
1
+ import { createRule, isFullNameValidCharacters, isHasConsecutiveChars, isStartsWithAndEndsWithLetter, isValidFullNameLength, } from '../core';
2
+ import { PERSON_SURNAME_ERROR_INFO } from './constants';
3
+ /**
4
+ * @description Проверяет валидно ли фамилия
5
+ * @example
6
+ * ```ts
7
+ * const validate = string(personSurname());
8
+ * validate("Иванов");
9
+ * ```
10
+ */
11
+ export const personSurname = (params) => createRule((value, ctx) => {
12
+ const createPersonSurnameError = () => ctx.createError({
13
+ message: (params === null || params === void 0 ? void 0 : params.message) || PERSON_SURNAME_ERROR_INFO.message,
14
+ code: PERSON_SURNAME_ERROR_INFO.code,
15
+ });
16
+ if (isValidFullNameLength(value)) {
17
+ return createPersonSurnameError();
18
+ }
19
+ if (isFullNameValidCharacters(value)) {
20
+ return createPersonSurnameError();
21
+ }
22
+ if (isStartsWithAndEndsWithLetter(value)) {
23
+ return createPersonSurnameError();
24
+ }
25
+ if (isHasConsecutiveChars(value)) {
26
+ return createPersonSurnameError();
27
+ }
28
+ return undefined;
29
+ }, { exclude: params === null || params === void 0 ? void 0 : params.exclude });
@@ -21,5 +21,5 @@ type PositiveNumberParams = {
21
21
  * validate(-1)
22
22
  * ```
23
23
  */
24
- export declare const positiveNumber: <TValues>(params?: PositiveNumberParams) => (value: number, prevCtx?: import("../core").ValidationContext<TValues> | undefined) => import("../core").ValidationResult;
24
+ export declare const positiveNumber: <TLastSchemaValues extends Record<string, unknown>>(params?: PositiveNumberParams) => (value: number, prevCtx?: import("../core").ValidationContext<TLastSchemaValues> | undefined) => import("../core").ValidationResult;
25
25
  export {};
package/snils/snils.d.ts CHANGED
@@ -13,5 +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?: import("../core").ValidationContext<TValues> | undefined) => import("../core").ValidationResult;
16
+ export declare const snils: <TLastSchemaValues extends Record<string, unknown>>(params?: SnilsParams) => (value: string, prevCtx?: import("../core").ValidationContext<TLastSchemaValues> | undefined) => import("../core").ValidationResult;
17
17
  export {};
@@ -1,6 +1,6 @@
1
1
  import { ValidationRule } from '../core';
2
- export declare const string: <TValues>(...rules: ValidationRule<string, TValues>[]) => {
3
- (value: unknown, prevCtx?: import("../core").ValidationContext<TValues> | undefined): import("../core").ValidationResult;
2
+ export declare const string: <TLastSchemaValues extends Record<string, unknown>>(...rules: ValidationRule<string, TLastSchemaValues>[]) => {
3
+ (value: unknown, prevCtx?: import("../core").ValidationContext<TLastSchemaValues> | undefined): import("../core").ValidationResult;
4
4
  define(overridesDefOptions: Partial<{}> & {
5
5
  requiredErrorMessage?: string | undefined;
6
6
  typeErrorMessage?: string | undefined;
@@ -14,5 +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?: import("../core").ValidationContext<TValues> | undefined) => import("../core").ValidationResult;
17
+ export declare const transform: <TValue extends unknown, TResult extends unknown, TLastSchemaValues extends Record<string, unknown> = {}>(transformer: Transformer<TValue, TResult>, ...rules: ValidationRule<TResult, TLastSchemaValues>[]) => (value: TValue, prevCtx?: import("../core").ValidationContext<TLastSchemaValues> | undefined) => import("../core").ValidationResult;
18
18
  export {};
package/when/when.d.ts CHANGED
@@ -1,17 +1,17 @@
1
1
  import { ValidationContext, ValidationRule } from '../core';
2
- type Params<TValues> = {
2
+ type Params<TLastSchemaValues extends Record<string, unknown>> = {
3
3
  /**
4
4
  * @description Условие для выбора ветки
5
5
  */
6
- is: (value: unknown, ctx: ValidationContext<TValues>) => boolean;
6
+ is: (value: unknown, ctx: ValidationContext<TLastSchemaValues>) => boolean;
7
7
  /**
8
8
  * Правила валидации, применяемые если is === true
9
9
  */
10
- then: ValidationRule<unknown, TValues>;
10
+ then: ValidationRule<unknown, TLastSchemaValues>;
11
11
  /**
12
12
  * Правила валидации, применяемые если is === false
13
13
  */
14
- otherwise: ValidationRule<unknown, TValues>;
14
+ otherwise: ValidationRule<unknown, TLastSchemaValues>;
15
15
  };
16
16
  /**
17
17
  * @description Позволяет указывать условные валидации
@@ -19,7 +19,7 @@ type Params<TValues> = {
19
19
  * ```ts
20
20
  * type Values = { name: string; isAgree: boolean };
21
21
  *
22
- * const validate = object<Values, Values>({
22
+ * const validate = object<Values>({
23
23
  * name: when({
24
24
  * is: (_, ctx) => ctx.global.values.isAgree,
25
25
  * then: string(),
@@ -35,5 +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?: ValidationContext<TValues> | undefined) => import("../core").ValidationResult;
38
+ export declare const when: <TLastSchemaValues extends Record<string, unknown>>({ is, then, otherwise, }: Params<TLastSchemaValues>) => (value: unknown, prevCtx?: ValidationContext<TLastSchemaValues> | undefined) => import("../core").ValidationResult;
39
39
  export {};
package/when/when.js CHANGED
@@ -5,7 +5,7 @@ import { createRule } from '../core';
5
5
  * ```ts
6
6
  * type Values = { name: string; isAgree: boolean };
7
7
  *
8
- * const validate = object<Values, Values>({
8
+ * const validate = object<Values>({
9
9
  * name: when({
10
10
  * is: (_, ctx) => ctx.global.values.isAgree,
11
11
  * then: string(),
@@ -21,7 +21,7 @@ import { createRule } from '../core';
21
21
  * const result2 = validate({ isAgree: true, name: '' });
22
22
  * ```
23
23
  */
24
- export const when = ({ is, then, otherwise }) => createRule((value, ctx) => {
24
+ export const when = ({ is, then, otherwise, }) => createRule((value, ctx) => {
25
25
  if (is(value, ctx)) {
26
26
  return then(value, ctx);
27
27
  }