@strictly/define 0.0.12 → 0.0.14

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 (34) hide show
  1. package/.out/transformers/flatteners/json_path.d.ts +6 -2
  2. package/.out/transformers/flatteners/json_path.js +15 -2
  3. package/.out/transformers/flatteners/specs/json_paths.tests.d.ts +1 -0
  4. package/.out/transformers/flatteners/specs/json_paths.tests.js +50 -0
  5. package/.out/tsconfig.tsbuildinfo +1 -1
  6. package/.out/types/builders.d.ts +21 -20
  7. package/.out/types/builders.js +8 -5
  8. package/.out/types/flattened_types_of_validating_type.d.ts +4 -4
  9. package/.out/types/flattened_validators_of_validating_type.d.ts +7 -6
  10. package/.out/types/specs/flattened_validators_of_validating_type.tests.js +2 -2
  11. package/.out/types/validating_definitions.d.ts +14 -12
  12. package/.out/types/validating_type_def_with_error.d.ts +13 -13
  13. package/.out/validation/validator.d.ts +1 -0
  14. package/.out/validation/validator.js +4 -0
  15. package/.out/validation/validators/composite_validator.d.ts +7 -0
  16. package/.out/validation/validators/composite_validator.js +32 -0
  17. package/.turbo/turbo-build.log +8 -8
  18. package/.turbo/turbo-check-types.log +1 -1
  19. package/dist/index.cjs +86 -33
  20. package/dist/index.d.cts +85 -79
  21. package/dist/index.d.ts +85 -79
  22. package/dist/index.js +62 -9
  23. package/package.json +1 -1
  24. package/transformers/flatteners/json_path.ts +44 -4
  25. package/transformers/flatteners/specs/json_paths.tests.ts +69 -0
  26. package/types/builders.ts +46 -26
  27. package/types/flattened_types_of_validating_type.ts +5 -2
  28. package/types/flattened_validators_of_validating_type.ts +7 -9
  29. package/types/specs/builder.tests.ts +31 -31
  30. package/types/specs/flattened_validators_of_validating_type.tests.ts +7 -7
  31. package/types/validating_definitions.ts +27 -15
  32. package/types/validating_type_def_with_error.ts +23 -23
  33. package/validation/validator.ts +21 -0
  34. package/validation/validators/composite_validator.ts +42 -0
@@ -1,3 +1,4 @@
1
+ import { type FunctionalValidator } from 'validation/validator'
1
2
  import {
2
3
  type ObjectFieldKey,
3
4
  type RecordKeyType,
@@ -11,18 +12,21 @@ export type ValidatingType<T extends ValidatingTypeDef = ValidatingTypeDef> = {
11
12
  readonly definition: T,
12
13
  }
13
14
 
14
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
15
- export type Rule<E = any, V = any> = (v: V) => E | null
16
-
17
15
  export type ErrorOfValidatingTypeDef<T extends ValidatingTypeDef> = T extends ValidatingTypeDef<infer E> ? E : never
18
16
 
17
+ export type ContextOfValidatingTypeDef<T extends ValidatingTypeDef> = T extends ValidatingTypeDef<infer _E, infer C> ? C
18
+ : never
19
+
20
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
21
+ export type Rule<E, C, V = any> = FunctionalValidator<V, E, string, C>
22
+
19
23
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
20
- export type ValidatingTypeDef<E = any> =
21
- | ValidatingLiteralTypeDef<E>
22
- | ValidatingListTypeDef<E>
23
- | ValidatingRecordTypeDef<E>
24
- | ValidatingObjectTypeDef<E>
25
- | ValidatingUnionTypeDef<E>
24
+ export type ValidatingTypeDef<E = any, C = any> =
25
+ | ValidatingLiteralTypeDef<E, C>
26
+ | ValidatingListTypeDef<E, C>
27
+ | ValidatingRecordTypeDef<E, C>
28
+ | ValidatingObjectTypeDef<E, C>
29
+ | ValidatingUnionTypeDef<E, C>
26
30
 
27
31
  // used to avoid TS complaining about circular references
28
32
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -30,10 +34,10 @@ type AnyTypeDef = any
30
34
 
31
35
  // literal
32
36
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
33
- export type ValidatingLiteralTypeDef<E = any, V = any> = {
37
+ export type ValidatingLiteralTypeDef<E = any, C = any, V = any> = {
34
38
  readonly type: TypeDefType.Literal,
35
39
  readonly valuePrototype: [V],
36
- readonly rule: Rule<E>,
40
+ readonly rule: Rule<E, C>,
37
41
  readonly required: boolean,
38
42
  readonly readonly: boolean,
39
43
  }
@@ -42,12 +46,14 @@ export type ValidatingLiteralTypeDef<E = any, V = any> = {
42
46
  export type ValidatingListTypeDef<
43
47
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
44
48
  E = any,
49
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
50
+ C = any,
45
51
  Ele extends ValidatingTypeDef = AnyTypeDef,
46
52
  > = {
47
53
  readonly type: TypeDefType.List,
48
54
  // readonly is inherited by the output
49
55
  readonly elements: Ele,
50
- readonly rule: Rule<E>,
56
+ readonly rule: Rule<E, C>,
51
57
  readonly required: boolean,
52
58
  readonly readonly: boolean,
53
59
  }
@@ -56,6 +62,8 @@ export type ValidatingListTypeDef<
56
62
  export type ValidatingRecordTypeDef<
57
63
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
58
64
  E = any,
65
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
66
+ C = any,
59
67
  K extends RecordKeyType = RecordKeyType,
60
68
  // if `V` includes `undefined` the map is partial
61
69
  V extends ValidatingTypeDef | undefined = AnyTypeDef,
@@ -65,7 +73,7 @@ export type ValidatingRecordTypeDef<
65
73
  readonly keyPrototype: K,
66
74
  // readonly is inherited by the output
67
75
  readonly valueTypeDef: V,
68
- readonly rule: Rule<E>,
76
+ readonly rule: Rule<E, C>,
69
77
  readonly required: boolean,
70
78
  readonly readonly: boolean,
71
79
  }
@@ -85,11 +93,13 @@ export type ValidatingObjectTypeDefFields = {
85
93
  export type ValidatingObjectTypeDef<
86
94
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
87
95
  E = any,
96
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
97
+ C = any,
88
98
  Fields extends ValidatingObjectTypeDefFields = ValidatingObjectTypeDefFields,
89
99
  > = {
90
100
  readonly type: TypeDefType.Object,
91
101
  readonly fields: Fields,
92
- readonly rule: Rule<E>,
102
+ readonly rule: Rule<E, C>,
93
103
  readonly required: boolean,
94
104
  readonly readonly: boolean,
95
105
  }
@@ -97,13 +107,15 @@ export type ValidatingObjectTypeDef<
97
107
  export type ValidatingUnionTypeDef<
98
108
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
99
109
  E = any,
110
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
111
+ C = any,
100
112
  D extends string | null = string | null,
101
113
  U extends Readonly<Record<UnionKey, AnyTypeDef>> = Readonly<Record<UnionKey, AnyTypeDef>>,
102
114
  > = {
103
115
  readonly discriminator: D,
104
116
  readonly type: TypeDefType.Union,
105
117
  readonly unions: U,
106
- readonly rule: Rule<E>,
118
+ readonly rule: Rule<E, C>,
107
119
  readonly required: boolean,
108
120
  readonly readonly: boolean,
109
121
  }
@@ -17,47 +17,47 @@ import {
17
17
  // ? T & { rule: Rule<E1 | E2> }
18
18
  // : never
19
19
 
20
- export type ValidatingTypeDefWithError<T extends ValidatingTypeDef, E> = T extends ValidatingLiteralTypeDef
21
- ? ValidatingLiteralTypeDefWithError<T, E>
22
- : T extends ValidatingListTypeDef ? ValidatingListTypeDefWithError<T, E>
23
- : T extends ValidatingRecordTypeDef ? ValidatingRecordTypeDefWithError<T, E>
24
- : T extends ValidatingObjectTypeDef ? ValidatingObjectTypeDefWithError<T, E>
25
- : T extends ValidatingUnionTypeDef ? ValidatingUnionTypeDefWithError<T, E>
20
+ export type ValidatingTypeDefWithError<T extends ValidatingTypeDef, E, C> = T extends ValidatingLiteralTypeDef
21
+ ? ValidatingLiteralTypeDefWithError<T, E, C>
22
+ : T extends ValidatingListTypeDef ? ValidatingListTypeDefWithError<T, E, C>
23
+ : T extends ValidatingRecordTypeDef ? ValidatingRecordTypeDefWithError<T, E, C>
24
+ : T extends ValidatingObjectTypeDef ? ValidatingObjectTypeDefWithError<T, E, C>
25
+ : T extends ValidatingUnionTypeDef ? ValidatingUnionTypeDefWithError<T, E, C>
26
26
  : never
27
27
 
28
- type ValidatingLiteralTypeDefWithError<T extends ValidatingLiteralTypeDef, E2> = T extends
29
- ValidatingLiteralTypeDef<infer E1, infer V> ? {
28
+ type ValidatingLiteralTypeDefWithError<T extends ValidatingLiteralTypeDef, E2, C2> = T extends
29
+ ValidatingLiteralTypeDef<infer E1, infer C1, infer V> ? {
30
30
  readonly type: TypeDefType.Literal,
31
31
  readonly valuePrototype: [V],
32
- readonly rule: Rule<E1 | E2>,
32
+ readonly rule: Rule<E1 | E2, C1 & C2>,
33
33
  readonly required: boolean,
34
34
  readonly readonly: boolean,
35
35
  }
36
36
  : never
37
37
 
38
- type ValidatingListTypeDefWithError<T extends ValidatingListTypeDef, E2> = T extends
39
- ValidatingListTypeDef<infer E1, infer E> ? IsFieldReadonly<T, 'elements'> extends true ? {
38
+ type ValidatingListTypeDefWithError<T extends ValidatingListTypeDef, E2, C2> = T extends
39
+ ValidatingListTypeDef<infer E1, infer C1, infer E> ? IsFieldReadonly<T, 'elements'> extends true ? {
40
40
  readonly type: TypeDefType.List,
41
41
  readonly elements: E,
42
- readonly rule: Rule<E1 | E2>,
42
+ readonly rule: Rule<E1 | E2, C1 & C2>,
43
43
  readonly required: boolean,
44
44
  readonly readonly: boolean,
45
45
  }
46
46
  : {
47
47
  readonly type: TypeDefType.List,
48
48
  elements: E,
49
- readonly rule: Rule<E1 | E2>,
49
+ readonly rule: Rule<E1 | E2, C1 & C2>,
50
50
  readonly required: boolean,
51
51
  readonly readonly: boolean,
52
52
  }
53
53
  : never
54
54
 
55
- type ValidatingRecordTypeDefWithError<T extends ValidatingRecordTypeDef, E2> = T extends
56
- ValidatingRecordTypeDef<infer E1, infer K, infer V> ? IsFieldReadonly<T, 'valueTypeDef'> extends true ? {
55
+ type ValidatingRecordTypeDefWithError<T extends ValidatingRecordTypeDef, E2, C2> = T extends
56
+ ValidatingRecordTypeDef<infer E1, infer C1, infer K, infer V> ? IsFieldReadonly<T, 'valueTypeDef'> extends true ? {
57
57
  readonly type: TypeDefType.Record,
58
58
  readonly keyPrototype: K,
59
59
  readonly valueTypeDef: V,
60
- readonly rule: Rule<E1 | E2>,
60
+ readonly rule: Rule<E1 | E2, C1 & C2>,
61
61
  readonly required: boolean,
62
62
  readonly readonly: boolean,
63
63
  }
@@ -65,28 +65,28 @@ type ValidatingRecordTypeDefWithError<T extends ValidatingRecordTypeDef, E2> = T
65
65
  readonly type: TypeDefType.Record,
66
66
  readonly keyPrototype: K,
67
67
  valueTypeDef: V,
68
- readonly rule: Rule<E1 | E2>,
68
+ readonly rule: Rule<E1 | E2, C1 & C2>,
69
69
  readonly required: boolean,
70
70
  readonly readonly: boolean,
71
71
  }
72
72
  : never
73
73
 
74
- type ValidatingObjectTypeDefWithError<T extends ValidatingObjectTypeDef, E2> = T extends
75
- ValidatingObjectTypeDef<infer E1, infer Fields> ? {
74
+ type ValidatingObjectTypeDefWithError<T extends ValidatingObjectTypeDef, E2, C2> = T extends
75
+ ValidatingObjectTypeDef<infer E1, infer C1, infer Fields> ? {
76
76
  readonly type: TypeDefType.Object,
77
77
  readonly fields: Fields,
78
- readonly rule: Rule<E1 | E2>,
78
+ readonly rule: Rule<E1 | E2, C1 & C2>,
79
79
  readonly required: boolean,
80
80
  readonly readonly: boolean,
81
81
  }
82
82
  : never
83
83
 
84
- type ValidatingUnionTypeDefWithError<T extends ValidatingUnionTypeDef, E2> = T extends
85
- ValidatingUnionTypeDef<infer E1, infer D, infer U> ? {
84
+ type ValidatingUnionTypeDefWithError<T extends ValidatingUnionTypeDef, E2, C2> = T extends
85
+ ValidatingUnionTypeDef<infer E1, infer C1, infer D, infer U> ? {
86
86
  readonly type: TypeDefType.Union,
87
87
  readonly discriminator: D,
88
88
  readonly unions: U,
89
- readonly rule: Rule<E1 | E2>,
89
+ readonly rule: Rule<E1 | E2, C1 & C2>,
90
90
  readonly required: boolean,
91
91
  readonly readonly: boolean,
92
92
  }
@@ -1,4 +1,5 @@
1
1
  import { type Simplify } from 'type-fest'
2
+ import { CompositeValidator } from './validators/composite_validator'
2
3
 
3
4
  export type Annotations = {
4
5
  readonly required: boolean,
@@ -89,6 +90,26 @@ export function validate<
89
90
  }
90
91
  }
91
92
 
93
+ export function mergeValidators<
94
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
95
+ V = any,
96
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
97
+ E1 = any,
98
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
99
+ E2 = any,
100
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
101
+ ValuePath extends string = any,
102
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
103
+ C1 = any,
104
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
105
+ C2 = any,
106
+ >(
107
+ v1: Validator<V, E1, ValuePath, C1>,
108
+ v2: Validator<V, E2, ValuePath, C2>,
109
+ ): Validator<V, E1 | E2, ValuePath, C1 & C2> {
110
+ return new CompositeValidator<V, E1 | E2, ValuePath, C1 & C2>(v1, v2)
111
+ }
112
+
92
113
  export function annotations<
93
114
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
94
115
  V = any,
@@ -0,0 +1,42 @@
1
+ import {
2
+ type AnnotatedValidator,
3
+ type Annotations,
4
+ annotations,
5
+ validate,
6
+ type Validator,
7
+ } from 'validation/validator'
8
+
9
+ export class CompositeValidator<V, E, ValuePath extends string, C> implements AnnotatedValidator<V, E, ValuePath, C> {
10
+ private readonly validators: readonly Validator<V, E, ValuePath, C>[]
11
+ constructor(...validators: readonly Validator<V, E, ValuePath, C>[]) {
12
+ this.validators = validators
13
+ }
14
+
15
+ validate(v: V, valuePath: ValuePath, context: C) {
16
+ return this.validators.reduce<E | null>((error, validator) => {
17
+ if (error != null) {
18
+ return error
19
+ }
20
+ return validate(validator, v, valuePath, context)
21
+ }, null)
22
+ }
23
+
24
+ annotations(valuePath: ValuePath, context: C): Annotations {
25
+ return this.validators.reduce<Annotations>(({
26
+ required,
27
+ readonly,
28
+ }, validator) => {
29
+ const {
30
+ readonly: validatorReadonly,
31
+ required: validatorRequired,
32
+ } = annotations(validator, valuePath, context)
33
+ return {
34
+ required: required || validatorRequired,
35
+ readonly: readonly || validatorReadonly,
36
+ }
37
+ }, {
38
+ required: false,
39
+ readonly: false,
40
+ })
41
+ }
42
+ }