@strictly/define 0.0.6 → 0.0.8

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 (45) hide show
  1. package/.out/tsconfig.tsbuildinfo +1 -1
  2. package/.out/types/builders.d.ts +2 -1
  3. package/.out/types/builders.js +3 -1
  4. package/.out/types/specs/flattened_accessors_of_type.tests.js +1 -2
  5. package/.out/types/specs/flattened_types_of_type.tests.js +8 -16
  6. package/.out/types/specs/flattened_validators_of_validating_type.tests.js +2 -4
  7. package/.out/types/specs/flattened_values_of_type.tests.js +2 -4
  8. package/.out/types/specs/partial_type_of_type.tests.js +6 -12
  9. package/.out/types/specs/paths_of_type.tests.js +23 -39
  10. package/.out/types/specs/readonly_type_of_type.tests.js +7 -14
  11. package/.out/types/specs/strict_definitions.tests.js +2 -4
  12. package/.out/types/specs/type_of_type.tests.js +3 -6
  13. package/.out/types/specs/value_of_type.tests.js +13 -26
  14. package/.out/types/specs/value_to_type_paths_of_type.tests.js +8 -16
  15. package/.out/validation/specs/errors_of_validators.tests.js +1 -2
  16. package/.out/validation/specs/validators_of_values.tests.js +3 -6
  17. package/.out/validation/validator.d.ts +2 -2
  18. package/.out/validation/validator.js +0 -1
  19. package/.out/validation/validators/defined_validator.d.ts +1 -1
  20. package/.out/validation/validators/minimum_string_length_validator.d.ts +1 -1
  21. package/.turbo/turbo-build.log +8 -8
  22. package/.turbo/turbo-check-types.log +1 -1
  23. package/.turbo/turbo-release$colon$exports.log +1 -1
  24. package/dist/index.cjs +27 -26
  25. package/dist/index.d.cts +5 -5
  26. package/dist/index.d.ts +5 -5
  27. package/dist/index.js +27 -26
  28. package/package.json +1 -1
  29. package/types/builders.ts +8 -2
  30. package/types/specs/flattened_accessors_of_type.tests.ts +2 -2
  31. package/types/specs/flattened_types_of_type.tests.ts +16 -16
  32. package/types/specs/flattened_validators_of_validating_type.tests.ts +4 -4
  33. package/types/specs/flattened_values_of_type.tests.ts +4 -4
  34. package/types/specs/partial_type_of_type.tests.ts +12 -12
  35. package/types/specs/paths_of_type.tests.ts +39 -39
  36. package/types/specs/readonly_type_of_type.tests.ts +14 -14
  37. package/types/specs/strict_definitions.tests.ts +6 -6
  38. package/types/specs/type_of_type.tests.ts +6 -6
  39. package/types/specs/value_of_type.tests.ts +26 -26
  40. package/types/specs/value_to_type_paths_of_type.tests.ts +16 -16
  41. package/validation/specs/errors_of_validators.tests.ts +2 -2
  42. package/validation/specs/validators_of_values.tests.ts +6 -6
  43. package/validation/validator.ts +13 -4
  44. package/validation/validators/defined_validator.ts +1 -3
  45. package/validation/validators/minimum_string_length_validator.ts +2 -2
@@ -1,24 +1,21 @@
1
1
  import { booleanType, list, nullable, numberType, object, record, stringType, union, } from 'types/builders';
2
2
  describe('ValueToTypePathsOfType', function () {
3
3
  describe('literal', function () {
4
- let t;
5
4
  it('equals expected type', function () {
6
- expectTypeOf(t).toEqualTypeOf();
5
+ expectTypeOf().toEqualTypeOf();
7
6
  });
8
7
  });
9
8
  describe('list', function () {
10
9
  const builder = list(list(numberType));
11
- let t;
12
10
  it('equals expected type', function () {
13
- expectTypeOf(t).toEqualTypeOf();
11
+ expectTypeOf().toEqualTypeOf();
14
12
  });
15
13
  });
16
14
  describe('record', function () {
17
15
  const l = list(numberType);
18
16
  const builder = record(l);
19
- let t;
20
17
  it('equals expected type', function () {
21
- expectTypeOf(t).toEqualTypeOf();
18
+ expectTypeOf().toEqualTypeOf();
22
19
  });
23
20
  it('allows lookup of type path', function () {
24
21
  expectTypeOf().toEqualTypeOf();
@@ -30,9 +27,8 @@ describe('ValueToTypePathsOfType', function () {
30
27
  .optionalField('b', booleanType)
31
28
  .readonlyField('c', stringType)
32
29
  .readonlyOptionalField('d', stringType);
33
- let t;
34
30
  it('equals expected type', function () {
35
- expectTypeOf(t).toEqualTypeOf();
31
+ expectTypeOf().toEqualTypeOf();
36
32
  });
37
33
  it('has the same value paths', function () {
38
34
  expectTypeOf().toEqualTypeOf();
@@ -46,18 +42,16 @@ describe('ValueToTypePathsOfType', function () {
46
42
  const builder = union()
47
43
  .or('1', list(numberType))
48
44
  .or('2', stringType);
49
- let t;
50
45
  it('equals expected type', function () {
51
- expectTypeOf(t).toEqualTypeOf();
46
+ expectTypeOf().toEqualTypeOf();
52
47
  });
53
48
  });
54
49
  describe('discriminated', function () {
55
50
  const builder = union('d')
56
51
  .or('1', object().field('a', booleanType).field('b', numberType))
57
52
  .or('2', object().field('x', numberType).field('y', stringType));
58
- let t;
59
53
  it('equals expected type', function () {
60
- expectTypeOf(t).toEqualTypeOf();
54
+ expectTypeOf().toEqualTypeOf();
61
55
  });
62
56
  it('has the same value paths', function () {
63
57
  expectTypeOf().toEqualTypeOf();
@@ -69,16 +63,14 @@ describe('ValueToTypePathsOfType', function () {
69
63
  });
70
64
  describe('readonly', function () {
71
65
  const builder = list(list(numberType)).readonly();
72
- let t;
73
66
  it('equals expected type', function () {
74
- expectTypeOf(t).toEqualTypeOf();
67
+ expectTypeOf().toEqualTypeOf();
75
68
  });
76
69
  });
77
70
  describe('nullable', function () {
78
71
  const builder = nullable(list(nullable(list(numberType))));
79
- let t;
80
72
  it('equals expected type', function () {
81
- expectTypeOf(t).toEqualTypeOf();
73
+ expectTypeOf().toEqualTypeOf();
82
74
  });
83
75
  it('has the same value paths', function () {
84
76
  expectTypeOf().toEqualTypeOf();
@@ -1,8 +1,7 @@
1
1
  describe('ErrorsOfValidators', function () {
2
2
  describe('simple', function () {
3
- let t;
4
3
  it('equals expected type', function () {
5
- expectTypeOf().toEqualTypeOf(t);
4
+ expectTypeOf().toEqualTypeOf();
6
5
  });
7
6
  });
8
7
  });
@@ -1,20 +1,17 @@
1
1
  describe('FlattenedValidatorsOfType', function () {
2
2
  describe('literal', function () {
3
- let t;
4
3
  it('has the expected type', function () {
5
- expectTypeOf().toEqualTypeOf(t);
4
+ expectTypeOf().toEqualTypeOf();
6
5
  });
7
6
  });
8
7
  describe('list', function () {
9
- let t;
10
8
  it('has the expected type', function () {
11
- expectTypeOf().toEqualTypeOf(t);
9
+ expectTypeOf().toEqualTypeOf();
12
10
  });
13
11
  });
14
12
  describe('with defaults', function () {
15
- let t;
16
13
  it('has the expected type', function () {
17
- expectTypeOf().toEqualTypeOf(t);
14
+ expectTypeOf().toEqualTypeOf();
18
15
  });
19
16
  });
20
17
  });
@@ -12,8 +12,8 @@ export type ErrorOfValidator<V extends Validator> = V extends Validator<infer _V
12
12
  export type ValidationError<Type extends string, Data = {}> = Simplify<{
13
13
  type: Type;
14
14
  } & Data>;
15
- export declare function isFunctionalValidator(v: Validator): v is FunctionalValidator;
16
- export declare function isAnnotatedValidator(v: Validator): v is AnnotatedValidator;
15
+ export declare function isFunctionalValidator<V, E, ValuePath extends string, Context>(v: Validator<V, E, ValuePath, Context>): v is FunctionalValidator<V, E, ValuePath, Context>;
16
+ export declare function isAnnotatedValidator<V, E, ValuePath extends string, Context>(v: Validator<V, E, ValuePath, Context>): v is AnnotatedValidator<V, E, ValuePath, Context>;
17
17
  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;
18
18
  export declare function annotations<V = any, E = any, ValuePath extends string = any, Context = any>(validator: Validator<V, E, ValuePath, Context>, valuePath: ValuePath, context: Context): {
19
19
  readonly required: boolean;
@@ -9,7 +9,6 @@ export function validate(validator, v, valuePath, context) {
9
9
  return validator.validate(v, valuePath, context);
10
10
  }
11
11
  else {
12
- // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
13
12
  return validator(v, valuePath, context);
14
13
  }
15
14
  }
@@ -1,5 +1,5 @@
1
1
  import { type AnnotatedValidator } from 'validation/validator';
2
- export declare class DefinedValidator<V, E, ValuePath extends string, Context> implements AnnotatedValidator<V | null | undefined, E, ValuePath, Context> {
2
+ export declare class DefinedValidator<V, E> implements AnnotatedValidator<V | null | undefined, E, never, never> {
3
3
  private readonly error;
4
4
  constructor(error: E);
5
5
  validate(v: V | null | undefined): E | null;
@@ -5,7 +5,7 @@ export type MinimumStringLengthValidationError = {
5
5
  receivedLength: number;
6
6
  minimumLength: number;
7
7
  };
8
- export declare class MinimumStringLengthValidator<ValuePath extends string, Context> implements AnnotatedValidator<string, MinimumStringLengthValidationError, ValuePath, Context> {
8
+ export declare class MinimumStringLengthValidator implements AnnotatedValidator<string, MinimumStringLengthValidationError, never, never> {
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: esnext
8
8
  CJS Build start
9
9
  ESM Build start
10
- CJS dist/index.cjs 26.92 KB
11
- CJS ⚡️ Build success in 59ms
12
- ESM dist/index.js 24.72 KB
13
- ESM ⚡️ Build success in 71ms
10
+ CJS dist/index.cjs 27.01 KB
11
+ CJS ⚡️ Build success in 39ms
12
+ ESM dist/index.js 24.82 KB
13
+ ESM ⚡️ Build success in 41ms
14
14
  DTS Build start
15
- DTS ⚡️ Build success in 5591ms
16
- DTS dist/index.d.cts 38.05 KB
17
- DTS dist/index.d.ts 38.05 KB
18
- Done in 6.43s.
15
+ DTS ⚡️ Build success in 5294ms
16
+ DTS dist/index.d.cts 38.21 KB
17
+ DTS dist/index.d.ts 38.21 KB
18
+ Done in 6.14s.
@@ -1,3 +1,3 @@
1
1
  yarn run v1.22.22
2
2
  $ tsc
3
- Done in 5.25s.
3
+ Done in 4.75s.
@@ -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.11s.
3
+ Done in 0.12s.
package/dist/index.cjs CHANGED
@@ -671,6 +671,31 @@ function internalJsonValuePathToTypePath(typeDef, valueSteps, allowMissingPaths,
671
671
  }
672
672
  }
673
673
 
674
+ // validation/validator.ts
675
+ function isFunctionalValidator(v) {
676
+ return typeof v === "function";
677
+ }
678
+ function isAnnotatedValidator(v) {
679
+ return typeof v !== "function";
680
+ }
681
+ function validate(validator, v, valuePath, context) {
682
+ if (isAnnotatedValidator(validator)) {
683
+ return validator.validate(v, valuePath, context);
684
+ } else {
685
+ return validator(v, valuePath, context);
686
+ }
687
+ }
688
+ function annotations(validator, valuePath, context) {
689
+ if (isAnnotatedValidator(validator)) {
690
+ return validator.annotations(valuePath, context);
691
+ } else {
692
+ return {
693
+ required: false,
694
+ readonly: false
695
+ };
696
+ }
697
+ }
698
+
674
699
  // types/type_of_type.ts
675
700
  var import_base6 = require("@strictly/base");
676
701
  function typeOfType({ definition }) {
@@ -761,9 +786,10 @@ var TypeDefBuilder = class _TypeDefBuilder {
761
786
  // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
762
787
  {
763
788
  ...this.definition,
789
+ ...isAnnotatedValidator(rule) ? rule.annotations(null, null) : {},
764
790
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
765
791
  rule: (value) => {
766
- return this.definition.rule(value) || rule(value);
792
+ return this.definition.rule(value) ?? validate(rule, value, null, null);
767
793
  }
768
794
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
769
795
  }
@@ -982,31 +1008,6 @@ function union(discriminator) {
982
1008
  );
983
1009
  }
984
1010
 
985
- // validation/validator.ts
986
- function isFunctionalValidator(v) {
987
- return typeof v === "function";
988
- }
989
- function isAnnotatedValidator(v) {
990
- return typeof v !== "function";
991
- }
992
- function validate(validator, v, valuePath, context) {
993
- if (isAnnotatedValidator(validator)) {
994
- return validator.validate(v, valuePath, context);
995
- } else {
996
- return validator(v, valuePath, context);
997
- }
998
- }
999
- function annotations(validator, valuePath, context) {
1000
- if (isAnnotatedValidator(validator)) {
1001
- return validator.annotations(valuePath, context);
1002
- } else {
1003
- return {
1004
- required: false,
1005
- readonly: false
1006
- };
1007
- }
1008
- }
1009
-
1010
1011
  // validation/validators/defined_validator.ts
1011
1012
  var DefinedValidator = class {
1012
1013
  constructor(error) {
package/dist/index.d.cts CHANGED
@@ -279,8 +279,8 @@ type ErrorOfValidator<V extends Validator> = V extends Validator<infer _V, infer
279
279
  type ValidationError<Type extends string, Data = {}> = Simplify<{
280
280
  type: Type;
281
281
  } & Data>;
282
- declare function isFunctionalValidator(v: Validator): v is FunctionalValidator;
283
- declare function isAnnotatedValidator(v: Validator): v is AnnotatedValidator;
282
+ declare function isFunctionalValidator<V, E, ValuePath extends string, Context>(v: Validator<V, E, ValuePath, Context>): v is FunctionalValidator<V, E, ValuePath, Context>;
283
+ declare function isAnnotatedValidator<V, E, ValuePath extends string, Context>(v: Validator<V, E, ValuePath, Context>): v is AnnotatedValidator<V, E, ValuePath, Context>;
284
284
  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;
285
285
  declare function annotations<V = any, E = any, ValuePath extends string = any, Context = any>(validator: Validator<V, E, ValuePath, Context>, valuePath: ValuePath, context: Context): {
286
286
  readonly required: boolean;
@@ -402,7 +402,7 @@ type ValidatingUnionTypeDefWithError<T extends ValidatingUnionTypeDef, E2> = T e
402
402
  declare class TypeDefBuilder<T extends ValidatingTypeDef> implements ValidatingType<T> {
403
403
  readonly definition: T;
404
404
  constructor(definition: T);
405
- enforce<E2>(rule: Rule<E2, ValueOfType<Type<T>>>): TypeDefBuilder<ValidatingTypeDefWithError<T, E2>>;
405
+ enforce<E2>(rule: Rule<E2, ValueOfType<Type<T>>> | Validator<ValueOfType<Type<T>>, E2, never, never>): TypeDefBuilder<ValidatingTypeDefWithError<T, E2>>;
406
406
  required(): TypeDefBuilder<ValidatingTypeDefWithError<T, never>>;
407
407
  required<RequiredError>(rule: Rule<RequiredError, ValueOfType<typeof this.narrow>>): TypeDefBuilder<ValidatingTypeDefWithError<T, RequiredError>>;
408
408
  readonly(): TypeDefBuilder<T>;
@@ -519,7 +519,7 @@ type ValueTypesOfDiscriminatedUnion<U extends UnionTypeDef> = U extends UnionTyp
519
519
  };
520
520
  } : never : never;
521
521
 
522
- declare class DefinedValidator<V, E, ValuePath extends string, Context> implements AnnotatedValidator<V | null | undefined, E, ValuePath, Context> {
522
+ declare class DefinedValidator<V, E> implements AnnotatedValidator<V | null | undefined, E, never, never> {
523
523
  private readonly error;
524
524
  constructor(error: E);
525
525
  validate(v: V | null | undefined): E | null;
@@ -535,7 +535,7 @@ type MinimumStringLengthValidationError = {
535
535
  receivedLength: number;
536
536
  minimumLength: number;
537
537
  };
538
- declare class MinimumStringLengthValidator<ValuePath extends string, Context> implements AnnotatedValidator<string, MinimumStringLengthValidationError, ValuePath, Context> {
538
+ declare class MinimumStringLengthValidator implements AnnotatedValidator<string, MinimumStringLengthValidationError, never, never> {
539
539
  private readonly minimumLength;
540
540
  constructor(minimumLength: number);
541
541
  validate(value: string): MinimumStringLengthValidationError | null;
package/dist/index.d.ts CHANGED
@@ -279,8 +279,8 @@ type ErrorOfValidator<V extends Validator> = V extends Validator<infer _V, infer
279
279
  type ValidationError<Type extends string, Data = {}> = Simplify<{
280
280
  type: Type;
281
281
  } & Data>;
282
- declare function isFunctionalValidator(v: Validator): v is FunctionalValidator;
283
- declare function isAnnotatedValidator(v: Validator): v is AnnotatedValidator;
282
+ declare function isFunctionalValidator<V, E, ValuePath extends string, Context>(v: Validator<V, E, ValuePath, Context>): v is FunctionalValidator<V, E, ValuePath, Context>;
283
+ declare function isAnnotatedValidator<V, E, ValuePath extends string, Context>(v: Validator<V, E, ValuePath, Context>): v is AnnotatedValidator<V, E, ValuePath, Context>;
284
284
  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;
285
285
  declare function annotations<V = any, E = any, ValuePath extends string = any, Context = any>(validator: Validator<V, E, ValuePath, Context>, valuePath: ValuePath, context: Context): {
286
286
  readonly required: boolean;
@@ -402,7 +402,7 @@ type ValidatingUnionTypeDefWithError<T extends ValidatingUnionTypeDef, E2> = T e
402
402
  declare class TypeDefBuilder<T extends ValidatingTypeDef> implements ValidatingType<T> {
403
403
  readonly definition: T;
404
404
  constructor(definition: T);
405
- enforce<E2>(rule: Rule<E2, ValueOfType<Type<T>>>): TypeDefBuilder<ValidatingTypeDefWithError<T, E2>>;
405
+ enforce<E2>(rule: Rule<E2, ValueOfType<Type<T>>> | Validator<ValueOfType<Type<T>>, E2, never, never>): TypeDefBuilder<ValidatingTypeDefWithError<T, E2>>;
406
406
  required(): TypeDefBuilder<ValidatingTypeDefWithError<T, never>>;
407
407
  required<RequiredError>(rule: Rule<RequiredError, ValueOfType<typeof this.narrow>>): TypeDefBuilder<ValidatingTypeDefWithError<T, RequiredError>>;
408
408
  readonly(): TypeDefBuilder<T>;
@@ -519,7 +519,7 @@ type ValueTypesOfDiscriminatedUnion<U extends UnionTypeDef> = U extends UnionTyp
519
519
  };
520
520
  } : never : never;
521
521
 
522
- declare class DefinedValidator<V, E, ValuePath extends string, Context> implements AnnotatedValidator<V | null | undefined, E, ValuePath, Context> {
522
+ declare class DefinedValidator<V, E> implements AnnotatedValidator<V | null | undefined, E, never, never> {
523
523
  private readonly error;
524
524
  constructor(error: E);
525
525
  validate(v: V | null | undefined): E | null;
@@ -535,7 +535,7 @@ type MinimumStringLengthValidationError = {
535
535
  receivedLength: number;
536
536
  minimumLength: number;
537
537
  };
538
- declare class MinimumStringLengthValidator<ValuePath extends string, Context> implements AnnotatedValidator<string, MinimumStringLengthValidationError, ValuePath, Context> {
538
+ declare class MinimumStringLengthValidator implements AnnotatedValidator<string, MinimumStringLengthValidationError, never, never> {
539
539
  private readonly minimumLength;
540
540
  constructor(minimumLength: number);
541
541
  validate(value: string): MinimumStringLengthValidationError | null;
package/dist/index.js CHANGED
@@ -640,6 +640,31 @@ function internalJsonValuePathToTypePath(typeDef, valueSteps, allowMissingPaths,
640
640
  }
641
641
  }
642
642
 
643
+ // validation/validator.ts
644
+ function isFunctionalValidator(v) {
645
+ return typeof v === "function";
646
+ }
647
+ function isAnnotatedValidator(v) {
648
+ return typeof v !== "function";
649
+ }
650
+ function validate(validator, v, valuePath, context) {
651
+ if (isAnnotatedValidator(validator)) {
652
+ return validator.validate(v, valuePath, context);
653
+ } else {
654
+ return validator(v, valuePath, context);
655
+ }
656
+ }
657
+ function annotations(validator, valuePath, context) {
658
+ if (isAnnotatedValidator(validator)) {
659
+ return validator.annotations(valuePath, context);
660
+ } else {
661
+ return {
662
+ required: false,
663
+ readonly: false
664
+ };
665
+ }
666
+ }
667
+
643
668
  // types/type_of_type.ts
644
669
  import {
645
670
  map as map2,
@@ -733,9 +758,10 @@ var TypeDefBuilder = class _TypeDefBuilder {
733
758
  // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
734
759
  {
735
760
  ...this.definition,
761
+ ...isAnnotatedValidator(rule) ? rule.annotations(null, null) : {},
736
762
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
737
763
  rule: (value) => {
738
- return this.definition.rule(value) || rule(value);
764
+ return this.definition.rule(value) ?? validate(rule, value, null, null);
739
765
  }
740
766
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
741
767
  }
@@ -954,31 +980,6 @@ function union(discriminator) {
954
980
  );
955
981
  }
956
982
 
957
- // validation/validator.ts
958
- function isFunctionalValidator(v) {
959
- return typeof v === "function";
960
- }
961
- function isAnnotatedValidator(v) {
962
- return typeof v !== "function";
963
- }
964
- function validate(validator, v, valuePath, context) {
965
- if (isAnnotatedValidator(validator)) {
966
- return validator.validate(v, valuePath, context);
967
- } else {
968
- return validator(v, valuePath, context);
969
- }
970
- }
971
- function annotations(validator, valuePath, context) {
972
- if (isAnnotatedValidator(validator)) {
973
- return validator.annotations(valuePath, context);
974
- } else {
975
- return {
976
- required: false,
977
- readonly: false
978
- };
979
- }
980
- }
981
-
982
983
  // validation/validators/defined_validator.ts
983
984
  var DefinedValidator = class {
984
985
  constructor(error) {
package/package.json CHANGED
@@ -35,7 +35,7 @@
35
35
  "test:watch": "vitest"
36
36
  },
37
37
  "type": "module",
38
- "version": "0.0.6",
38
+ "version": "0.0.8",
39
39
  "exports": {
40
40
  ".": {
41
41
  "import": {
package/types/builders.ts CHANGED
@@ -1,6 +1,11 @@
1
1
  import {
2
2
  type IsFieldReadonly,
3
3
  } from '@strictly/base'
4
+ import {
5
+ isAnnotatedValidator,
6
+ validate,
7
+ type Validator,
8
+ } from 'validation/validator'
4
9
  import {
5
10
  type ObjectFieldKey,
6
11
  type RecordKeyType,
@@ -37,14 +42,15 @@ class TypeDefBuilder<T extends ValidatingTypeDef> implements ValidatingType<T> {
37
42
  constructor(readonly definition: T) {
38
43
  }
39
44
 
40
- enforce<E2>(rule: Rule<E2, ValueOfType<Type<T>>>) {
45
+ enforce<E2>(rule: Rule<E2, ValueOfType<Type<T>>> | Validator<ValueOfType<Type<T>>, E2, never, never>) {
41
46
  return new TypeDefBuilder<ValidatingTypeDefWithError<T, E2>>(
42
47
  // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
43
48
  {
44
49
  ...this.definition,
50
+ ...(isAnnotatedValidator(rule) ? rule.annotations(null!, null!) : {}),
45
51
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
46
52
  rule: (value: any) => {
47
- return this.definition.rule(value) || rule(value)
53
+ return this.definition.rule(value) ?? validate(rule, value, null!, null!)
48
54
  },
49
55
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
50
56
  } as any,
@@ -10,7 +10,7 @@ describe('FlattenedAccessorsOfType', function () {
10
10
  const builder = record<typeof numberType, string>(numberType)
11
11
  type V = FlattenedAccessorsOfType<typeof builder>
12
12
 
13
- let v: {
13
+ type C = {
14
14
  readonly $: {
15
15
  readonly value: Record<string, number>,
16
16
  set: (v: Record<string, number>) => void,
@@ -21,7 +21,7 @@ describe('FlattenedAccessorsOfType', function () {
21
21
  },
22
22
  }
23
23
  it('equals expected type', function () {
24
- expectTypeOf(v).toEqualTypeOf<V>()
24
+ expectTypeOf<C>().toEqualTypeOf<V>()
25
25
  })
26
26
  })
27
27
  })
@@ -15,7 +15,7 @@ describe('FlattenedTypesOfType', function () {
15
15
  describe('literal', function () {
16
16
  type T = FlattenedTypesOfType<typeof numberType._type, null>
17
17
 
18
- let t: {
18
+ type C = {
19
19
  readonly $: {
20
20
  readonly definition: {
21
21
  readonly type: TypeDefType.Literal,
@@ -24,7 +24,7 @@ describe('FlattenedTypesOfType', function () {
24
24
  },
25
25
  }
26
26
  it('equals expected type', function () {
27
- expectTypeOf(t).toEqualTypeOf<T>()
27
+ expectTypeOf<C>().toEqualTypeOf<T>()
28
28
  })
29
29
  })
30
30
 
@@ -32,7 +32,7 @@ describe('FlattenedTypesOfType', function () {
32
32
  const builder = list(numberType)
33
33
  type T = SimplifyDeep<FlattenedTypesOfType<typeof builder._type, '*'>>
34
34
 
35
- let t: {
35
+ type C = {
36
36
  readonly $: SimplifyDeep<typeof builder._type>,
37
37
  readonly ['$.*']: {
38
38
  readonly definition: {
@@ -42,7 +42,7 @@ describe('FlattenedTypesOfType', function () {
42
42
  },
43
43
  }
44
44
  it('equals expected type', function () {
45
- expectTypeOf(t).toEqualTypeOf<T>()
45
+ expectTypeOf<C>().toEqualTypeOf<T>()
46
46
  })
47
47
  })
48
48
 
@@ -50,7 +50,7 @@ describe('FlattenedTypesOfType', function () {
50
50
  const builder = record<typeof numberType, 'a' | 'b'>(numberType)
51
51
  type T = SimplifyDeep<FlattenedTypesOfType<typeof builder._type, '*'>>
52
52
 
53
- let t: {
53
+ type C = {
54
54
  readonly $: SimplifyDeep<typeof builder._type>,
55
55
  readonly ['$.*']: {
56
56
  readonly definition: {
@@ -60,7 +60,7 @@ describe('FlattenedTypesOfType', function () {
60
60
  },
61
61
  }
62
62
  it('equals expected type', function () {
63
- expectTypeOf<typeof t>(t).toEqualTypeOf<T>()
63
+ expectTypeOf<C>().toEqualTypeOf<T>()
64
64
  })
65
65
  })
66
66
 
@@ -72,7 +72,7 @@ describe('FlattenedTypesOfType', function () {
72
72
  .readonlyField('c', booleanType)
73
73
  .readonlyOptionalField('d', stringType)
74
74
  type T = SimplifyDeep<FlattenedTypesOfType<typeof builder._type, null>>
75
- let t: {
75
+ type C = {
76
76
  readonly $: SimplifyDeep<typeof builder._type>,
77
77
  readonly ['$.a']: {
78
78
  readonly definition: {
@@ -120,7 +120,7 @@ describe('FlattenedTypesOfType', function () {
120
120
  },
121
121
  }
122
122
  it('equals expected type', function () {
123
- expectTypeOf(t).toEqualTypeOf<T>()
123
+ expectTypeOf<C>().toEqualTypeOf<T>()
124
124
  })
125
125
  })
126
126
 
@@ -128,7 +128,7 @@ describe('FlattenedTypesOfType', function () {
128
128
  const builder = object().optionalField('a', stringType)
129
129
  type T = SimplifyDeep<FlattenedTypesOfType<typeof builder._type, null>>
130
130
 
131
- let t: {
131
+ type C = {
132
132
  readonly $: SimplifyDeep<typeof builder._type>,
133
133
  readonly '$.a': {
134
134
  readonly definition: {
@@ -149,7 +149,7 @@ describe('FlattenedTypesOfType', function () {
149
149
  }
150
150
 
151
151
  it('equals expected type', function () {
152
- expectTypeOf(t).toEqualTypeOf<T>()
152
+ expectTypeOf<C>().toEqualTypeOf<T>()
153
153
  })
154
154
  })
155
155
  })
@@ -163,7 +163,7 @@ describe('union', function () {
163
163
  .or('y', object().field('b', numberType))
164
164
  type T = SimplifyDeep<FlattenedTypesOfType<typeof builder._type, null>>
165
165
 
166
- let t: {
166
+ type C = {
167
167
  readonly $: SimplifyDeep<typeof builder._type>,
168
168
  readonly ['$.a']: {
169
169
  readonly definition: {
@@ -180,7 +180,7 @@ describe('union', function () {
180
180
  }
181
181
 
182
182
  it('equals expected type', function () {
183
- expectTypeOf(t).toEqualTypeOf<T>()
183
+ expectTypeOf<C>().toEqualTypeOf<T>()
184
184
  })
185
185
  })
186
186
 
@@ -190,7 +190,7 @@ describe('union', function () {
190
190
  .or('2', object().field('a', numberType))
191
191
  type T = SimplifyDeep<FlattenedTypesOfType<typeof builder._type, null>>
192
192
 
193
- let t: {
193
+ type C = {
194
194
  readonly $: SimplifyDeep<typeof builder._type>,
195
195
  readonly ['$.1:a']: {
196
196
  readonly definition: {
@@ -206,7 +206,7 @@ describe('union', function () {
206
206
  },
207
207
  }
208
208
  it('equals expected type', function () {
209
- expectTypeOf(t).toEqualTypeOf<T>()
209
+ expectTypeOf<C>().toEqualTypeOf<T>()
210
210
  })
211
211
  })
212
212
 
@@ -225,7 +225,7 @@ describe('union', function () {
225
225
  .or('s', object().field('c', stringType)),
226
226
  )
227
227
  type T = SimplifyDeep<FlattenedTypesOfType<typeof builder._type, null>>
228
- let t: {
228
+ type C = {
229
229
  readonly $: SimplifyDeep<typeof builder._type>,
230
230
  readonly ['$.1:p:a']: {
231
231
  readonly definition: {
@@ -253,7 +253,7 @@ describe('union', function () {
253
253
  },
254
254
  }
255
255
  it('equals expected type', function () {
256
- expectTypeOf(t).toEqualTypeOf<T>()
256
+ expectTypeOf<C>().toEqualTypeOf<T>()
257
257
  })
258
258
  })
259
259
  })
@@ -17,12 +17,12 @@ describe('FlattenedValidatorsOfValidatingType', function () {
17
17
  Reverse<ValueToTypePathsOfType<typeof literalType>>
18
18
  >
19
19
 
20
- let t: {
20
+ type C = {
21
21
  readonly $: Validator<number, 'a', '$', number>,
22
22
  }
23
23
 
24
24
  it('equals expected type', function () {
25
- expectTypeOf<T>().toEqualTypeOf(t)
25
+ expectTypeOf<T>().toEqualTypeOf<C>()
26
26
  })
27
27
  })
28
28
 
@@ -35,13 +35,13 @@ describe('FlattenedValidatorsOfValidatingType', function () {
35
35
  })
36
36
  type T = FlattenedValidatorsOfValidatingType<typeof listType, Reverse<ValueToTypePathsOfType<typeof listType>>>
37
37
 
38
- let t: {
38
+ type C = {
39
39
  readonly $: Validator<readonly number[], 'y', '$', readonly number[]>,
40
40
  readonly '$.*': Validator<number, 'x', `$.${number}`, readonly number[]>,
41
41
  }
42
42
 
43
43
  it('equals expected type', function () {
44
- expectTypeOf<T>().toEqualTypeOf(t)
44
+ expectTypeOf<T>().toEqualTypeOf<C>()
45
45
  })
46
46
  })
47
47
  })