@strictly/define 0.0.13 → 0.0.15

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 (31) hide show
  1. package/.out/tsconfig.tsbuildinfo +1 -1
  2. package/.out/types/builders.d.ts +21 -20
  3. package/.out/types/builders.js +8 -5
  4. package/.out/types/flattened_types_of_validating_type.d.ts +4 -4
  5. package/.out/types/flattened_validators_of_validating_type.d.ts +7 -6
  6. package/.out/types/specs/flattened_validators_of_validating_type.tests.js +2 -2
  7. package/.out/types/validating_definitions.d.ts +14 -12
  8. package/.out/types/validating_type_def_with_error.d.ts +13 -13
  9. package/.out/validation/validator.d.ts +1 -0
  10. package/.out/validation/validator.js +4 -0
  11. package/.out/validation/validators/composite_validator.d.ts +7 -0
  12. package/.out/validation/validators/composite_validator.js +32 -0
  13. package/.out/validation/validators/minimum_string_length_validator.d.ts +1 -1
  14. package/.turbo/turbo-build.log +8 -8
  15. package/.turbo/turbo-check-types.log +1 -1
  16. package/.turbo/turbo-release$colon$exports.log +1 -1
  17. package/dist/index.cjs +45 -5
  18. package/dist/index.d.cts +80 -77
  19. package/dist/index.d.ts +80 -77
  20. package/dist/index.js +44 -5
  21. package/package.json +1 -1
  22. package/types/builders.ts +46 -26
  23. package/types/flattened_types_of_validating_type.ts +5 -2
  24. package/types/flattened_validators_of_validating_type.ts +7 -9
  25. package/types/specs/builder.tests.ts +31 -31
  26. package/types/specs/flattened_validators_of_validating_type.tests.ts +7 -7
  27. package/types/validating_definitions.ts +27 -15
  28. package/types/validating_type_def_with_error.ts +23 -23
  29. package/validation/validator.ts +16 -0
  30. package/validation/validators/composite_validator.ts +42 -0
  31. package/validation/validators/minimum_string_length_validator.ts +1 -1
@@ -8,7 +8,8 @@ import { type ValueOfType, type ValueOfTypeDef } from './value_of_type';
8
8
  declare class TypeDefBuilder<T extends ValidatingTypeDef> implements ValidatingType<T> {
9
9
  readonly definition: T;
10
10
  constructor(definition: T);
11
- enforce<E2>(rule: Rule<E2, ValueOfType<Type<T>>> | Validator<ValueOfType<Type<T>>, E2, never, never>): TypeDefBuilder<ValidatingTypeDefWithError<T, E2>>;
11
+ enforce<E2, C2>(): TypeDefBuilder<ValidatingTypeDefWithError<T, E2, C2>>;
12
+ enforce<E2, C2>(rule: Validator<ValueOfType<Type<T>>, E2, string, C2>): TypeDefBuilder<ValidatingTypeDefWithError<T, E2, C2>>;
12
13
  required(): TypeDefBuilder<T>;
13
14
  readonly(): TypeDefBuilder<T>;
14
15
  get _type(): TypeOfType<Type<T>>;
@@ -48,29 +49,29 @@ declare class RecordTypeDefBuilder<T extends ValidatingRecordTypeDef> extends Ty
48
49
  readonly readonly: boolean;
49
50
  }>;
50
51
  }
51
- declare class ObjectTypeDefBuilder<E, Fields extends Readonly<Record<ObjectFieldKey, ValidatingTypeDef>> = {}> extends TypeDefBuilder<ValidatingObjectTypeDef<E, Fields>> {
52
- field<Name extends string, T extends ValidatingTypeDef>(name: Name, { definition }: Type<T>): ObjectTypeDefBuilder<E, Fields & Record<Name, T>>;
53
- field<Name extends string, T extends ValidatingTypeDef, RequiredError>(name: Name, { definition }: Type<T>, rule: Rule<RequiredError, ValueOfTypeDef<T>>): ObjectTypeDefBuilder<E, Fields & Record<Name, ValidatingTypeDefWithError<T, RequiredError>>>;
54
- readonlyField<Name extends string, T extends ValidatingTypeDef>(name: Name, { definition }: Type<T>): ObjectTypeDefBuilder<E, Fields & Readonly<Record<Name, T>>>;
55
- optionalField<Name extends string, T extends ValidatingTypeDef>(name: Name, { definition }: Type<T>): ObjectTypeDefBuilder<E, Fields & Partial<Record<Name, T>>>;
56
- readonlyOptionalField<Name extends string, T extends TypeDef>(name: Name, { definition }: Type<T>): ObjectTypeDefBuilder<E, Fields & Partial<Readonly<Record<Name, T>>>>;
52
+ declare class ObjectTypeDefBuilder<E, C, Fields extends Readonly<Record<ObjectFieldKey, ValidatingTypeDef>> = {}> extends TypeDefBuilder<ValidatingObjectTypeDef<E, C, Fields>> {
53
+ field<Name extends string, T extends ValidatingTypeDef>(name: Name, { definition }: Type<T>): ObjectTypeDefBuilder<E, C, Fields & Record<Name, T>>;
54
+ field<Name extends string, T extends ValidatingTypeDef, RequiredError, C2>(name: Name, { definition }: Type<T>, rule: Rule<RequiredError, ValueOfTypeDef<T>>): ObjectTypeDefBuilder<E, C & C2, Fields & Record<Name, ValidatingTypeDefWithError<T, RequiredError, C & C2>>>;
55
+ readonlyField<Name extends string, T extends ValidatingTypeDef>(name: Name, { definition }: Type<T>): ObjectTypeDefBuilder<E, C, Fields & Readonly<Record<Name, T>>>;
56
+ optionalField<Name extends string, T extends ValidatingTypeDef>(name: Name, { definition }: Type<T>): ObjectTypeDefBuilder<E, C, Fields & Partial<Record<Name, T>>>;
57
+ readonlyOptionalField<Name extends string, T extends TypeDef>(name: Name, { definition }: Type<T>): ObjectTypeDefBuilder<E, C, Fields & Partial<Readonly<Record<Name, T>>>>;
57
58
  }
58
- declare class UnionTypeDefBuilder<E, D extends string | null, U extends Record<UnionKey, TypeDef>> extends TypeDefBuilder<ValidatingUnionTypeDef<E, D, U>> {
59
- or<K extends Exclude<UnionKey, keyof U>, T extends TypeDef>(k: K, { definition: typeDef, }: Type<T>): UnionTypeDefBuilder<E, D, Readonly<Record<K, T>> & U>;
59
+ declare class UnionTypeDefBuilder<E, C, D extends string | null, U extends Record<UnionKey, TypeDef>> extends TypeDefBuilder<ValidatingUnionTypeDef<E, C, D, U>> {
60
+ or<K extends Exclude<UnionKey, keyof U>, T extends TypeDef>(k: K, { definition: typeDef, }: Type<T>): UnionTypeDefBuilder<E, C, D, Readonly<Record<K, T>> & U>;
60
61
  }
61
- export declare function literal<T>(value?: [T]): TypeDefBuilder<ValidatingLiteralTypeDef<never, T>>;
62
- export declare const stringType: TypeDefBuilder<ValidatingLiteralTypeDef<never, string>>;
63
- export declare const numberType: TypeDefBuilder<ValidatingLiteralTypeDef<never, number>>;
64
- export declare const booleanType: TypeDefBuilder<ValidatingLiteralTypeDef<never, boolean>>;
65
- export declare const nullType: TypeDefBuilder<ValidatingLiteralTypeDef<never, null>>;
66
- export declare function nullable<T extends ValidatingTypeDef>(nonNullable: ValidatingType<T>): UnionTypeDefBuilder<never, null, {
62
+ export declare function literal<T>(value?: [T]): TypeDefBuilder<ValidatingLiteralTypeDef<never, {}, T>>;
63
+ export declare const stringType: TypeDefBuilder<ValidatingLiteralTypeDef<never, {}, string>>;
64
+ export declare const numberType: TypeDefBuilder<ValidatingLiteralTypeDef<never, {}, number>>;
65
+ export declare const booleanType: TypeDefBuilder<ValidatingLiteralTypeDef<never, {}, boolean>>;
66
+ export declare const nullType: TypeDefBuilder<ValidatingLiteralTypeDef<never, {}, null>>;
67
+ export declare function nullable<T extends ValidatingTypeDef>(nonNullable: ValidatingType<T>): UnionTypeDefBuilder<never, {}, null, {
67
68
  readonly ['0']: T;
68
- readonly ['1']: ValidatingLiteralTypeDef<never, null>;
69
+ readonly ['1']: ValidatingLiteralTypeDef<never, {}, null>;
69
70
  }>;
70
71
  export declare function list<T extends ValidatingTypeDef>(elements: ValidatingType<T>): ListTypeDefBuilder<{
71
72
  readonly type: TypeDefType.List;
72
73
  elements: T;
73
- readonly rule: Rule<never>;
74
+ readonly rule: Rule<never, {}>;
74
75
  readonly readonly: boolean;
75
76
  readonly required: boolean;
76
77
  }>;
@@ -78,11 +79,11 @@ export declare function record<V extends ValidatingType, K extends RecordKeyType
78
79
  readonly type: TypeDefType.Record;
79
80
  readonly keyPrototype: K;
80
81
  valueTypeDef: V["definition"];
81
- readonly rule: Rule<never>;
82
+ readonly rule: Rule<never, {}>;
82
83
  readonly readonly: boolean;
83
84
  readonly required: boolean;
84
85
  }>;
85
86
  export declare function object(): ObjectTypeDefBuilder<never, {}>;
86
- export declare function union<D extends null>(): UnionTypeDefBuilder<never, D, {}>;
87
- export declare function union<D extends string>(discriminator: D): UnionTypeDefBuilder<never, D, {}>;
87
+ export declare function union<D extends null>(): UnionTypeDefBuilder<never, {}, D, {}>;
88
+ export declare function union<D extends string>(discriminator: D): UnionTypeDefBuilder<never, {}, D, {}>;
88
89
  export {};
@@ -16,11 +16,13 @@ class TypeDefBuilder {
16
16
  enforce(rule) {
17
17
  return new TypeDefBuilder(
18
18
  // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
19
- Object.assign(Object.assign(Object.assign({}, this.definition), (isAnnotatedValidator(rule) ? mergeAnnotations(rule.annotations(null, null), this.definition) : {})), {
19
+ Object.assign(Object.assign(Object.assign({}, this.definition), (rule != null && isAnnotatedValidator(rule)
20
+ ? mergeAnnotations(rule.annotations(null, null), this.definition)
21
+ : {})), {
20
22
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
21
- rule: (value) => {
23
+ rule: (value, valuePath, context) => {
22
24
  var _a;
23
- return (_a = this.definition.rule(value)) !== null && _a !== void 0 ? _a : validate(rule, value, null, null);
25
+ return (_a = this.definition.rule(value, valuePath, context)) !== null && _a !== void 0 ? _a : (rule && validate(rule, value, valuePath, context));
24
26
  } }));
25
27
  }
26
28
  required() {
@@ -66,8 +68,9 @@ class ObjectTypeDefBuilder extends TypeDefBuilder {
66
68
  // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
67
69
  fields: Object.assign(Object.assign({}, this.definition.fields), { [name]: Object.assign(Object.assign({}, definition), {
68
70
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
69
- rule: function (v) {
70
- return definition.rule(v) || (rule === null || rule === void 0 ? void 0 : rule(v));
71
+ rule: function (v, valuePath, context) {
72
+ var _a;
73
+ return (_a = definition.rule(v, valuePath, context)) !== null && _a !== void 0 ? _a : (rule && validate(rule, v, valuePath, context));
71
74
  } }) }) }));
72
75
  }
73
76
  readonlyField(name, { definition }) {
@@ -2,7 +2,7 @@ import { type UnionToIntersection } from 'type-fest';
2
2
  import { type Type, type TypeDef } from './definitions';
3
3
  import { type Depths, type StartingDepth } from './flattened';
4
4
  import { type PathOf } from './path_of';
5
- import { type ErrorOfValidatingTypeDef, type ValidatingListTypeDef, type ValidatingLiteralTypeDef, type ValidatingObjectTypeDef, type ValidatingRecordTypeDef, type ValidatingUnionTypeDef } from './validating_definitions';
5
+ import { type ContextOfValidatingTypeDef, type ErrorOfValidatingTypeDef, type ValidatingListTypeDef, type ValidatingLiteralTypeDef, type ValidatingObjectTypeDef, type ValidatingRecordTypeDef, type ValidatingUnionTypeDef } from './validating_definitions';
6
6
  export type FlattenedTypesOfValidatingType<T extends Type, SegmentOverride extends string | null, Path extends string = '$', Depth extends number = StartingDepth> = InternalFlattenedTypeDefsOf<T['definition'], SegmentOverride, Path, '', Depth>;
7
7
  type InternalFlattenedTypeDefsOf<T extends TypeDef, SegmentOverride extends string | null, Path extends string, Qualifier extends string, Depth extends number> = {
8
8
  readonly [K in Path]: Type<T>;
@@ -11,13 +11,13 @@ type InternalFlattenedTypeDefsOfChildren<T extends TypeDef, SegmentOverride exte
11
11
  type InternalFlattenedTypeDefsOfLiteralChildren = {};
12
12
  type InternalFlattenedTypeDefsOfListChildren<T extends ValidatingListTypeDef, SegmentOverride extends string | null, Path extends string, Depth extends number> = InternalFlattenedTypeDefsOf<T['elements'], SegmentOverride, PathOf<Path, number, SegmentOverride>, '', Depth>;
13
13
  type InternalFlattenedTypeDefsOfRecordChildren<T extends ValidatingRecordTypeDef, SegmentOverride extends string | null, Path extends string, Depth extends number> = InternalFlattenedTypeDefsOf<T['valueTypeDef'], SegmentOverride, PathOf<Path, T['keyPrototype'], SegmentOverride>, '', Depth>;
14
- type InternalFlattenedTypeDefsOfObjectChildren<T extends ValidatingObjectTypeDef, SegmentOverride extends string | null, Path extends string, Qualifier extends string, Depth extends number> = T extends ValidatingObjectTypeDef<infer _E, infer Fields> ? keyof Fields extends string ? UnionToIntersection<{
15
- readonly [K in keyof Fields]-?: undefined extends Fields[K] ? InternalFlattenedTypeDefsOf<ValidatingUnionTypeDef<ErrorOfValidatingTypeDef<Exclude<Fields[K], undefined>>, null, {
14
+ type InternalFlattenedTypeDefsOfObjectChildren<T extends ValidatingObjectTypeDef, SegmentOverride extends string | null, Path extends string, Qualifier extends string, Depth extends number> = T extends ValidatingObjectTypeDef<infer _E, infer _C, infer Fields> ? keyof Fields extends string ? UnionToIntersection<{
15
+ readonly [K in keyof Fields]-?: undefined extends Fields[K] ? InternalFlattenedTypeDefsOf<ValidatingUnionTypeDef<ErrorOfValidatingTypeDef<Exclude<Fields[K], undefined>>, ContextOfValidatingTypeDef<Exclude<Fields[K], undefined>>, null, {
16
16
  readonly '0': Exclude<Fields[K], undefined>;
17
17
  readonly '1': ValidatingLiteralTypeDef<undefined>;
18
18
  }>, SegmentOverride, PathOf<Path, `${Qualifier}${K}`, null>, '', Depth> : InternalFlattenedTypeDefsOf<Exclude<Fields[K], undefined>, SegmentOverride, PathOf<Path, `${Qualifier}${K}`, null>, '', Depth>;
19
19
  }[keyof Fields]> : never : never;
20
- type InternalFlattenedTypeDefsOfUnionChildren<T extends ValidatingUnionTypeDef, SegmentOverride extends string | null, Path extends string, Qualifier extends string, Depth extends number> = T extends ValidatingUnionTypeDef<infer _E, infer D, infer Unions> ? keyof Unions extends string ? D extends null ? UnionToIntersection<{
20
+ type InternalFlattenedTypeDefsOfUnionChildren<T extends ValidatingUnionTypeDef, SegmentOverride extends string | null, Path extends string, Qualifier extends string, Depth extends number> = T extends ValidatingUnionTypeDef<infer _E, infer _C, infer D, infer Unions> ? keyof Unions extends string ? D extends null ? UnionToIntersection<{
21
21
  readonly [K in keyof Unions]: InternalFlattenedTypeDefsOfChildren<Unions[K], SegmentOverride, Path, '', Depth>;
22
22
  }[keyof Unions]> : UnionToIntersection<{
23
23
  readonly [K in keyof Unions]: InternalFlattenedTypeDefsOfChildren<Unions[K], SegmentOverride, Path, `${Qualifier}${K}:`, Depth>;
@@ -1,10 +1,11 @@
1
+ import { type Simplify } from 'type-fest';
1
2
  import { type Validator } from 'validation/validator';
2
3
  import { type FlattenedTypesOfType } from './flattened_types_of_type';
3
- import { type ReadonlyOfTypeDef, type ReadonlyTypeOfType } from './readonly_type_of_type';
4
+ import { type ReadonlyOfTypeDef } from './readonly_type_of_type';
4
5
  import { type ErrorOfValidatingTypeDef, type ValidatingType, type ValidatingTypeDef } from './validating_definitions';
5
- import { type ValueOfType, type ValueOfTypeDef } from './value_of_type';
6
- type ValidatorOfValidatingType<T extends ValidatingTypeDef, ValuePath extends string, Context> = T extends ValidatingTypeDef<infer E> ? Validator<ValueOfTypeDef<ReadonlyOfTypeDef<T>>, E, ValuePath, Context> : never;
7
- export type FlattenedValidatorsOfValidatingType<T extends ValidatingType, TypePathsToValuePaths extends Readonly<Record<keyof FlattenedTypes, string>>, FlattenedTypes extends Readonly<Record<string, ValidatingType>> = FlattenedTypesOfType<T, '*'>, Context = ValueOfType<ReadonlyTypeOfType<T>>> = {
8
- [K in keyof FlattenedTypes as ErrorOfValidatingTypeDef<FlattenedTypes[K]['definition']> extends never ? never : K]: ValidatorOfValidatingType<FlattenedTypes[K]['definition'], TypePathsToValuePaths[K], Context>;
9
- };
6
+ import { type ValueOfTypeDef } from './value_of_type';
7
+ type ValidatorOfValidatingType<T extends ValidatingTypeDef, ValuePath extends string> = T extends ValidatingTypeDef<infer E, infer C> ? Validator<ValueOfTypeDef<ReadonlyOfTypeDef<T>>, E, ValuePath, C> : never;
8
+ export type FlattenedValidatorsOfValidatingType<T extends ValidatingType, TypePathsToValuePaths extends Readonly<Record<keyof FlattenedTypes, string>>, FlattenedTypes extends Readonly<Record<string, ValidatingType>> = FlattenedTypesOfType<T, '*'>> = Simplify<{
9
+ [K in keyof FlattenedTypes as ErrorOfValidatingTypeDef<FlattenedTypes[K]['definition']> extends never ? never : K]: ValidatorOfValidatingType<FlattenedTypes[K]['definition'], TypePathsToValuePaths[K]>;
10
+ }>;
10
11
  export {};
@@ -10,10 +10,10 @@ describe('FlattenedValidatorsOfValidatingType', function () {
10
10
  });
11
11
  describe('list', function () {
12
12
  const literalType = numberType.enforce(function () {
13
- return 'x';
13
+ return 'Y';
14
14
  });
15
15
  const listType = list(literalType.narrow).enforce(function () {
16
- return 'y';
16
+ return 'X';
17
17
  });
18
18
  it('equals expected type', function () {
19
19
  expectTypeOf().toEqualTypeOf();
@@ -1,48 +1,50 @@
1
+ import { type FunctionalValidator } from 'validation/validator';
1
2
  import { type ObjectFieldKey, type RecordKeyType, type TypeDefType, type UnionKey } from './definitions';
2
3
  export type ValidatingType<T extends ValidatingTypeDef = ValidatingTypeDef> = {
3
4
  readonly definition: T;
4
5
  };
5
- export type Rule<E = any, V = any> = (v: V) => E | null;
6
6
  export type ErrorOfValidatingTypeDef<T extends ValidatingTypeDef> = T extends ValidatingTypeDef<infer E> ? E : never;
7
- export type ValidatingTypeDef<E = any> = ValidatingLiteralTypeDef<E> | ValidatingListTypeDef<E> | ValidatingRecordTypeDef<E> | ValidatingObjectTypeDef<E> | ValidatingUnionTypeDef<E>;
7
+ export type ContextOfValidatingTypeDef<T extends ValidatingTypeDef> = T extends ValidatingTypeDef<infer _E, infer C> ? C : never;
8
+ export type Rule<E, C, V = any> = FunctionalValidator<V, E, string, C>;
9
+ export type ValidatingTypeDef<E = any, C = any> = ValidatingLiteralTypeDef<E, C> | ValidatingListTypeDef<E, C> | ValidatingRecordTypeDef<E, C> | ValidatingObjectTypeDef<E, C> | ValidatingUnionTypeDef<E, C>;
8
10
  type AnyTypeDef = any;
9
- export type ValidatingLiteralTypeDef<E = any, V = any> = {
11
+ export type ValidatingLiteralTypeDef<E = any, C = any, V = any> = {
10
12
  readonly type: TypeDefType.Literal;
11
13
  readonly valuePrototype: [V];
12
- readonly rule: Rule<E>;
14
+ readonly rule: Rule<E, C>;
13
15
  readonly required: boolean;
14
16
  readonly readonly: boolean;
15
17
  };
16
- export type ValidatingListTypeDef<E = any, Ele extends ValidatingTypeDef = AnyTypeDef> = {
18
+ export type ValidatingListTypeDef<E = any, C = any, Ele extends ValidatingTypeDef = AnyTypeDef> = {
17
19
  readonly type: TypeDefType.List;
18
20
  readonly elements: Ele;
19
- readonly rule: Rule<E>;
21
+ readonly rule: Rule<E, C>;
20
22
  readonly required: boolean;
21
23
  readonly readonly: boolean;
22
24
  };
23
- export type ValidatingRecordTypeDef<E = any, K extends RecordKeyType = RecordKeyType, V extends ValidatingTypeDef | undefined = AnyTypeDef> = {
25
+ export type ValidatingRecordTypeDef<E = any, C = any, K extends RecordKeyType = RecordKeyType, V extends ValidatingTypeDef | undefined = AnyTypeDef> = {
24
26
  readonly type: TypeDefType.Record;
25
27
  readonly keyPrototype: K;
26
28
  readonly valueTypeDef: V;
27
- readonly rule: Rule<E>;
29
+ readonly rule: Rule<E, C>;
28
30
  readonly required: boolean;
29
31
  readonly readonly: boolean;
30
32
  };
31
33
  export type ValidatingObjectTypeDefFields = {
32
34
  [Key: ObjectFieldKey]: AnyTypeDef;
33
35
  };
34
- export type ValidatingObjectTypeDef<E = any, Fields extends ValidatingObjectTypeDefFields = ValidatingObjectTypeDefFields> = {
36
+ export type ValidatingObjectTypeDef<E = any, C = any, Fields extends ValidatingObjectTypeDefFields = ValidatingObjectTypeDefFields> = {
35
37
  readonly type: TypeDefType.Object;
36
38
  readonly fields: Fields;
37
- readonly rule: Rule<E>;
39
+ readonly rule: Rule<E, C>;
38
40
  readonly required: boolean;
39
41
  readonly readonly: boolean;
40
42
  };
41
- export type ValidatingUnionTypeDef<E = any, D extends string | null = string | null, U extends Readonly<Record<UnionKey, AnyTypeDef>> = Readonly<Record<UnionKey, AnyTypeDef>>> = {
43
+ export type ValidatingUnionTypeDef<E = any, C = any, D extends string | null = string | null, U extends Readonly<Record<UnionKey, AnyTypeDef>> = Readonly<Record<UnionKey, AnyTypeDef>>> = {
42
44
  readonly discriminator: D;
43
45
  readonly type: TypeDefType.Union;
44
46
  readonly unions: U;
45
- readonly rule: Rule<E>;
47
+ readonly rule: Rule<E, C>;
46
48
  readonly required: boolean;
47
49
  readonly readonly: boolean;
48
50
  };
@@ -1,54 +1,54 @@
1
1
  import { type IsFieldReadonly } from '@strictly/base';
2
2
  import { type TypeDefType } from 'types/definitions';
3
3
  import { type Rule, type ValidatingListTypeDef, type ValidatingLiteralTypeDef, type ValidatingObjectTypeDef, type ValidatingRecordTypeDef, type ValidatingTypeDef, type ValidatingUnionTypeDef } from './validating_definitions';
4
- export type ValidatingTypeDefWithError<T extends ValidatingTypeDef, E> = T extends ValidatingLiteralTypeDef ? ValidatingLiteralTypeDefWithError<T, E> : T extends ValidatingListTypeDef ? ValidatingListTypeDefWithError<T, E> : T extends ValidatingRecordTypeDef ? ValidatingRecordTypeDefWithError<T, E> : T extends ValidatingObjectTypeDef ? ValidatingObjectTypeDefWithError<T, E> : T extends ValidatingUnionTypeDef ? ValidatingUnionTypeDefWithError<T, E> : never;
5
- type ValidatingLiteralTypeDefWithError<T extends ValidatingLiteralTypeDef, E2> = T extends ValidatingLiteralTypeDef<infer E1, infer V> ? {
4
+ export type ValidatingTypeDefWithError<T extends ValidatingTypeDef, E, C> = T extends ValidatingLiteralTypeDef ? ValidatingLiteralTypeDefWithError<T, E, C> : T extends ValidatingListTypeDef ? ValidatingListTypeDefWithError<T, E, C> : T extends ValidatingRecordTypeDef ? ValidatingRecordTypeDefWithError<T, E, C> : T extends ValidatingObjectTypeDef ? ValidatingObjectTypeDefWithError<T, E, C> : T extends ValidatingUnionTypeDef ? ValidatingUnionTypeDefWithError<T, E, C> : never;
5
+ type ValidatingLiteralTypeDefWithError<T extends ValidatingLiteralTypeDef, E2, C2> = T extends ValidatingLiteralTypeDef<infer E1, infer C1, infer V> ? {
6
6
  readonly type: TypeDefType.Literal;
7
7
  readonly valuePrototype: [V];
8
- readonly rule: Rule<E1 | E2>;
8
+ readonly rule: Rule<E1 | E2, C1 & C2>;
9
9
  readonly required: boolean;
10
10
  readonly readonly: boolean;
11
11
  } : never;
12
- type ValidatingListTypeDefWithError<T extends ValidatingListTypeDef, E2> = T extends ValidatingListTypeDef<infer E1, infer E> ? IsFieldReadonly<T, 'elements'> extends true ? {
12
+ type ValidatingListTypeDefWithError<T extends ValidatingListTypeDef, E2, C2> = T extends ValidatingListTypeDef<infer E1, infer C1, infer E> ? IsFieldReadonly<T, 'elements'> extends true ? {
13
13
  readonly type: TypeDefType.List;
14
14
  readonly elements: E;
15
- readonly rule: Rule<E1 | E2>;
15
+ readonly rule: Rule<E1 | E2, C1 & C2>;
16
16
  readonly required: boolean;
17
17
  readonly readonly: boolean;
18
18
  } : {
19
19
  readonly type: TypeDefType.List;
20
20
  elements: E;
21
- readonly rule: Rule<E1 | E2>;
21
+ readonly rule: Rule<E1 | E2, C1 & C2>;
22
22
  readonly required: boolean;
23
23
  readonly readonly: boolean;
24
24
  } : never;
25
- type ValidatingRecordTypeDefWithError<T extends ValidatingRecordTypeDef, E2> = T extends ValidatingRecordTypeDef<infer E1, infer K, infer V> ? IsFieldReadonly<T, 'valueTypeDef'> extends true ? {
25
+ type ValidatingRecordTypeDefWithError<T extends ValidatingRecordTypeDef, E2, C2> = T extends ValidatingRecordTypeDef<infer E1, infer C1, infer K, infer V> ? IsFieldReadonly<T, 'valueTypeDef'> extends true ? {
26
26
  readonly type: TypeDefType.Record;
27
27
  readonly keyPrototype: K;
28
28
  readonly valueTypeDef: V;
29
- readonly rule: Rule<E1 | E2>;
29
+ readonly rule: Rule<E1 | E2, C1 & C2>;
30
30
  readonly required: boolean;
31
31
  readonly readonly: boolean;
32
32
  } : {
33
33
  readonly type: TypeDefType.Record;
34
34
  readonly keyPrototype: K;
35
35
  valueTypeDef: V;
36
- readonly rule: Rule<E1 | E2>;
36
+ readonly rule: Rule<E1 | E2, C1 & C2>;
37
37
  readonly required: boolean;
38
38
  readonly readonly: boolean;
39
39
  } : never;
40
- type ValidatingObjectTypeDefWithError<T extends ValidatingObjectTypeDef, E2> = T extends ValidatingObjectTypeDef<infer E1, infer Fields> ? {
40
+ type ValidatingObjectTypeDefWithError<T extends ValidatingObjectTypeDef, E2, C2> = T extends ValidatingObjectTypeDef<infer E1, infer C1, infer Fields> ? {
41
41
  readonly type: TypeDefType.Object;
42
42
  readonly fields: Fields;
43
- readonly rule: Rule<E1 | E2>;
43
+ readonly rule: Rule<E1 | E2, C1 & C2>;
44
44
  readonly required: boolean;
45
45
  readonly readonly: boolean;
46
46
  } : never;
47
- type ValidatingUnionTypeDefWithError<T extends ValidatingUnionTypeDef, E2> = T extends ValidatingUnionTypeDef<infer E1, infer D, infer U> ? {
47
+ type ValidatingUnionTypeDefWithError<T extends ValidatingUnionTypeDef, E2, C2> = T extends ValidatingUnionTypeDef<infer E1, infer C1, infer D, infer U> ? {
48
48
  readonly type: TypeDefType.Union;
49
49
  readonly discriminator: D;
50
50
  readonly unions: U;
51
- readonly rule: Rule<E1 | E2>;
51
+ readonly rule: Rule<E1 | E2, C1 & C2>;
52
52
  readonly required: boolean;
53
53
  readonly readonly: boolean;
54
54
  } : never;
@@ -16,6 +16,7 @@ export type ValidationError<Type extends string, Data = {}> = Simplify<{
16
16
  export declare function isFunctionalValidator<V, E, ValuePath extends string, Context>(v: Validator<V, E, ValuePath, Context>): v is FunctionalValidator<V, E, ValuePath, Context>;
17
17
  export declare function isAnnotatedValidator<V, E, ValuePath extends string, Context>(v: Validator<V, E, ValuePath, Context>): v is AnnotatedValidator<V, E, ValuePath, Context>;
18
18
  export declare function validate<V = any, E = any, ValuePath extends string = any, Context = any>(validator: Validator<V, E, ValuePath, Context>, v: V, valuePath: ValuePath, context: Context): E | null;
19
+ export declare function mergeValidators<V, E1, E2, P1 extends string, P2 extends string, C1, C2>(v1: Validator<V, E1, P1, C1>, v2: Validator<V, E2, P2, C2>): Validator<V, E1 | E2, P1 & P2, C1 & C2>;
19
20
  export declare function annotations<V = any, E = any, ValuePath extends string = any, Context = any>(validator: Validator<V, E, ValuePath, Context>, valuePath: ValuePath, context: Context): Annotations;
20
21
  export declare function mergeAnnotations(a1: Annotations, a2: Annotations): {
21
22
  readonly: boolean;
@@ -1,3 +1,4 @@
1
+ import { CompositeValidator } from './validators/composite_validator';
1
2
  export function isFunctionalValidator(v) {
2
3
  return typeof v === 'function';
3
4
  }
@@ -12,6 +13,9 @@ export function validate(validator, v, valuePath, context) {
12
13
  return validator(v, valuePath, context);
13
14
  }
14
15
  }
16
+ export function mergeValidators(v1, v2) {
17
+ return new CompositeValidator(v1, v2);
18
+ }
15
19
  export function annotations(validator, valuePath, context) {
16
20
  if (isAnnotatedValidator(validator)) {
17
21
  return validator.annotations(valuePath, context);
@@ -0,0 +1,7 @@
1
+ import { type AnnotatedValidator, type Annotations, type Validator } from 'validation/validator';
2
+ export declare class CompositeValidator<V, E, ValuePath extends string, C> implements AnnotatedValidator<V, E, ValuePath, C> {
3
+ private readonly validators;
4
+ constructor(...validators: readonly Validator<V, E, ValuePath, C>[]);
5
+ validate(v: V, valuePath: ValuePath, context: C): E | null;
6
+ annotations(valuePath: ValuePath, context: C): Annotations;
7
+ }
@@ -0,0 +1,32 @@
1
+ import { annotations, validate, } from 'validation/validator';
2
+ export class CompositeValidator {
3
+ constructor(...validators) {
4
+ Object.defineProperty(this, "validators", {
5
+ enumerable: true,
6
+ configurable: true,
7
+ writable: true,
8
+ value: void 0
9
+ });
10
+ this.validators = validators;
11
+ }
12
+ validate(v, valuePath, context) {
13
+ return this.validators.reduce((error, validator) => {
14
+ if (error != null) {
15
+ return error;
16
+ }
17
+ return validate(validator, v, valuePath, context);
18
+ }, null);
19
+ }
20
+ annotations(valuePath, context) {
21
+ return this.validators.reduce(({ required, readonly, }, validator) => {
22
+ const { readonly: validatorReadonly, required: validatorRequired, } = annotations(validator, valuePath, context);
23
+ return {
24
+ required: required || validatorRequired,
25
+ readonly: readonly || validatorReadonly,
26
+ };
27
+ }, {
28
+ required: false,
29
+ readonly: false,
30
+ });
31
+ }
32
+ }
@@ -5,7 +5,7 @@ export type MinimumStringLengthValidationError = {
5
5
  receivedLength: number;
6
6
  minimumLength: number;
7
7
  };
8
- export declare class MinimumStringLengthValidator implements AnnotatedValidator<string, MinimumStringLengthValidationError, never, never> {
8
+ export declare class MinimumStringLengthValidator implements AnnotatedValidator<string, MinimumStringLengthValidationError, never, unknown> {
9
9
  private readonly minimumLength;
10
10
  constructor(minimumLength: number);
11
11
  validate(value: string): MinimumStringLengthValidationError | null;
@@ -7,12 +7,12 @@ $ tsup
7
7
  CLI Target: es6
8
8
  CJS Build start
9
9
  ESM Build start
10
- CJS dist/index.cjs 31.10 KB
11
- CJS ⚡️ Build success in 55ms
12
- ESM dist/index.js 28.71 KB
13
- ESM ⚡️ Build success in 56ms
10
+ CJS dist/index.cjs 32.25 KB
11
+ CJS ⚡️ Build success in 68ms
12
+ ESM dist/index.js 29.82 KB
13
+ ESM ⚡️ Build success in 69ms
14
14
  DTS Build start
15
- DTS ⚡️ Build success in 5419ms
16
- DTS dist/index.d.cts 40.77 KB
17
- DTS dist/index.d.ts 40.77 KB
18
- Done in 6.25s.
15
+ DTS ⚡️ Build success in 5614ms
16
+ DTS dist/index.d.cts 41.51 KB
17
+ DTS dist/index.d.ts 41.51 KB
18
+ Done in 6.43s.
@@ -1,3 +1,3 @@
1
1
  yarn run v1.22.22
2
2
  $ tsc
3
- Done in 4.83s.
3
+ Done in 4.96s.
@@ -1,3 +1,3 @@
1
1
  yarn run v1.22.22
2
2
  $ json -f package.json -f package.exports.json --merge > package.release.json
3
- Done in 0.12s.
3
+ Done in 0.11s.
package/dist/index.cjs CHANGED
@@ -64,6 +64,7 @@ __export(index_exports, {
64
64
  list: () => list,
65
65
  literal: () => literal,
66
66
  mergeAnnotations: () => mergeAnnotations,
67
+ mergeValidators: () => mergeValidators,
67
68
  mobxCopy: () => mobxCopy,
68
69
  nullType: () => nullType,
69
70
  nullable: () => nullable,
@@ -703,6 +704,40 @@ function internalJsonValuePathToTypePath(typeDef, valueSteps, allowMissingPaths,
703
704
  }
704
705
  }
705
706
 
707
+ // validation/validators/composite_validator.ts
708
+ var CompositeValidator = class {
709
+ constructor(...validators) {
710
+ __publicField(this, "validators");
711
+ this.validators = validators;
712
+ }
713
+ validate(v, valuePath, context) {
714
+ return this.validators.reduce((error, validator) => {
715
+ if (error != null) {
716
+ return error;
717
+ }
718
+ return validate(validator, v, valuePath, context);
719
+ }, null);
720
+ }
721
+ annotations(valuePath, context) {
722
+ return this.validators.reduce(({
723
+ required,
724
+ readonly
725
+ }, validator) => {
726
+ const {
727
+ readonly: validatorReadonly,
728
+ required: validatorRequired
729
+ } = annotations(validator, valuePath, context);
730
+ return {
731
+ required: required || validatorRequired,
732
+ readonly: readonly || validatorReadonly
733
+ };
734
+ }, {
735
+ required: false,
736
+ readonly: false
737
+ });
738
+ }
739
+ };
740
+
706
741
  // validation/validator.ts
707
742
  function isFunctionalValidator(v) {
708
743
  return typeof v === "function";
@@ -717,6 +752,9 @@ function validate(validator, v, valuePath, context) {
717
752
  return validator(v, valuePath, context);
718
753
  }
719
754
  }
755
+ function mergeValidators(v1, v2) {
756
+ return new CompositeValidator(v1, v2);
757
+ }
720
758
  function annotations(validator, valuePath, context) {
721
759
  if (isAnnotatedValidator(validator)) {
722
760
  return validator.annotations(valuePath, context);
@@ -822,11 +860,11 @@ var TypeDefBuilder = class _TypeDefBuilder {
822
860
  enforce(rule) {
823
861
  return new _TypeDefBuilder(
824
862
  // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
825
- __spreadProps(__spreadValues(__spreadValues({}, this.definition), isAnnotatedValidator(rule) ? mergeAnnotations(rule.annotations(null, null), this.definition) : {}), {
863
+ __spreadProps(__spreadValues(__spreadValues({}, this.definition), rule != null && isAnnotatedValidator(rule) ? mergeAnnotations(rule.annotations(null, null), this.definition) : {}), {
826
864
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
827
- rule: (value) => {
865
+ rule: (value, valuePath, context) => {
828
866
  var _a;
829
- return (_a = this.definition.rule(value)) != null ? _a : validate(rule, value, null, null);
867
+ return (_a = this.definition.rule(value, valuePath, context)) != null ? _a : rule && validate(rule, value, valuePath, context);
830
868
  }
831
869
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
832
870
  })
@@ -890,8 +928,9 @@ var ObjectTypeDefBuilder = class _ObjectTypeDefBuilder extends TypeDefBuilder {
890
928
  fields: __spreadProps(__spreadValues({}, this.definition.fields), {
891
929
  [name]: __spreadProps(__spreadValues({}, definition), {
892
930
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
893
- rule: function(v) {
894
- return definition.rule(v) || (rule == null ? void 0 : rule(v));
931
+ rule: function(v, valuePath, context) {
932
+ var _a;
933
+ return (_a = definition.rule(v, valuePath, context)) != null ? _a : rule && validate(rule, v, valuePath, context);
895
934
  }
896
935
  })
897
936
  })
@@ -1153,6 +1192,7 @@ var RegexpValidator = _RegexpValidator;
1153
1192
  list,
1154
1193
  literal,
1155
1194
  mergeAnnotations,
1195
+ mergeValidators,
1156
1196
  mobxCopy,
1157
1197
  nullType,
1158
1198
  nullable,