@strictly/define 0.0.1 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.out/index.d.ts +17 -11
- package/.out/index.js +17 -11
- package/.out/transformers/copies/copy.d.ts +3 -3
- package/.out/transformers/copies/copy_to.d.ts +3 -3
- package/.out/transformers/copies/copy_to.js +4 -0
- package/.out/transformers/copies/mobx_copy.d.ts +4 -4
- package/.out/transformers/copies/mobx_copy.js +1 -1
- package/.out/transformers/copies/specs/copy_to.tests.js +8 -8
- package/.out/transformers/copies/specs/mobx_copy.tests.js +1 -1
- package/.out/transformers/flatteners/flatten_accessors_of_type.d.ts +5 -0
- package/.out/transformers/flatteners/flatten_accessors_of_type.js +11 -0
- package/.out/transformers/flatteners/flatten_json_value_to_type_paths_of.d.ts +2 -2
- package/.out/transformers/flatteners/flatten_json_value_to_type_paths_of.js +2 -2
- package/.out/transformers/flatteners/{flatten_type_def_to.d.ts → flatten_type_to.d.ts} +1 -1
- package/.out/transformers/flatteners/{flatten_type_def_to.js → flatten_type_to.js} +1 -1
- package/.out/transformers/flatteners/{flatten_type_defs_of.d.ts → flatten_types_of_type.d.ts} +1 -1
- package/.out/transformers/flatteners/flatten_types_of_type.js +7 -0
- package/.out/transformers/flatteners/flatten_validation_errors_of_type.d.ts +22 -0
- package/.out/transformers/flatteners/flatten_validation_errors_of_type.js +10 -0
- package/.out/transformers/flatteners/flatten_validators_of_validating_type.d.ts +4 -0
- package/.out/transformers/flatteners/flatten_validators_of_validating_type.js +17 -0
- package/.out/transformers/flatteners/{flatten_value_type_to.d.ts → flatten_value_to.d.ts} +4 -4
- package/.out/transformers/flatteners/{flatten_value_type_to.js → flatten_value_to.js} +7 -2
- package/.out/transformers/flatteners/flatten_values_of_type.d.ts +3 -0
- package/.out/transformers/flatteners/flatten_values_of_type.js +7 -0
- package/.out/transformers/flatteners/specs/{flatten_accessors_of.tests.js → flatten_accessors_of_type.tests.js} +5 -6
- package/.out/transformers/flatteners/specs/flatten_json_value_to_type_paths_of.tests.js +6 -6
- package/.out/transformers/flatteners/specs/{flatten_type_def_to.tests.js → flatten_type_to.tests.js} +15 -13
- package/.out/transformers/flatteners/specs/flatten_types_of_type.tests.js +18 -0
- package/.out/transformers/flatteners/specs/flatten_validation_errors_of_type.tests.js +25 -0
- package/.out/transformers/flatteners/specs/{flatten_value_type_to.tests.js → flatten_value_to.tests.js} +81 -58
- package/.out/transformers/flatteners/specs/{flatten_value_types_of.tests.js → flatten_values_of_type.tests.js} +4 -4
- package/.out/transformers/flatteners/specs/value_path_to_type_path.tests.js +5 -3
- package/.out/transformers/flatteners/value_path_to_type_path.d.ts +1 -1
- package/.out/tsconfig.tsbuildinfo +1 -1
- package/.out/types/builders.d.ts +55 -29
- package/.out/types/builders.js +116 -32
- package/.out/types/definitions.d.ts +1 -1
- package/.out/types/flattened_accessors_of_type.d.ts +9 -0
- package/.out/types/{flattened_type_defs_of.d.ts → flattened_types_of_type.d.ts} +9 -6
- package/.out/types/flattened_types_of_validating_type.d.ts +25 -0
- package/.out/types/flattened_validators_of_validating_type.d.ts +10 -0
- package/.out/types/flattened_values_of_type.d.ts +6 -0
- package/.out/types/{mobx_value_type_of.d.ts → mobx_value_of_type.d.ts} +2 -2
- package/.out/types/{partial_type_def_of.d.ts → partial_type_of_type.d.ts} +1 -1
- package/.out/types/path_of.d.ts +1 -0
- package/.out/types/{json_paths_of.d.ts → paths_of_type.d.ts} +5 -5
- package/.out/types/readonly_type_of_type.d.ts +29 -0
- package/.out/types/specs/builder.tests.js +73 -23
- package/.out/types/specs/{flattened_accessors_of.tests.js → flattened_accessors_of_type.tests.js} +1 -1
- package/.out/types/specs/{flattened_type_defs_of.tests.js → flattened_types_of_type.tests.js} +22 -15
- package/.out/types/specs/flattened_validators_of_validating_type.tests.js +24 -0
- package/.out/types/specs/flattened_values_of_type.tests.js +18 -0
- package/.out/types/specs/{partial_type_def_of.tests.js → partial_type_of_type.tests.js} +4 -4
- package/.out/types/specs/{json_paths_of.tests.js → paths_of_type.tests.js} +19 -19
- package/.out/types/specs/{readonly_type_def_of.tests.js → readonly_type_of_type.tests.js} +6 -6
- package/.out/types/specs/type_of_type.tests.d.ts +1 -0
- package/.out/types/specs/type_of_type.tests.js +27 -0
- package/.out/types/specs/value_of_type.tests.d.ts +1 -0
- package/.out/types/specs/{value_type_of.tests.js → value_of_type.tests.js} +1 -1
- package/.out/types/specs/value_to_type_paths_of_type.tests.d.ts +1 -0
- package/.out/types/specs/{value_to_type_paths_of.tests.js → value_to_type_paths_of_type.tests.js} +9 -9
- package/.out/types/specs/value_types_of_discriminated_union.tests.js +2 -2
- package/.out/types/strict_definitions.d.ts +2 -2
- package/.out/types/type_of_type.d.ts +49 -0
- package/.out/types/type_of_type.js +70 -0
- package/.out/types/validating_definitions.d.ts +49 -0
- package/.out/types/validating_definitions.js +1 -0
- package/.out/types/validating_type_def_with_error.d.ts +55 -0
- package/.out/types/validating_type_def_with_error.js +1 -0
- package/.out/types/value_of_type.d.ts +24 -0
- package/.out/types/value_of_type.js +1 -0
- package/.out/types/{value_to_type_paths_of.d.ts → value_to_type_paths_of_type.d.ts} +13 -13
- package/.out/types/value_to_type_paths_of_type.js +1 -0
- package/.out/types/value_types_of_discriminated_union.d.ts +5 -4
- package/.out/validation/errors_of_validators.d.ts +4 -0
- package/.out/validation/errors_of_validators.js +1 -0
- package/.out/validation/specs/errors_of_validators.tests.d.ts +1 -0
- package/.out/validation/specs/errors_of_validators.tests.js +9 -0
- package/.out/validation/specs/validators_of_values.tests.d.ts +1 -0
- package/.out/validation/specs/validators_of_values.tests.js +21 -0
- package/.out/validation/validator.d.ts +21 -0
- package/.out/validation/validator.js +26 -0
- package/.out/validation/validators/defined_validator.d.ts +10 -0
- package/.out/validation/validators/defined_validator.js +18 -0
- package/.out/validation/validators/minimum_string_length_validator.d.ts +16 -0
- package/.out/validation/validators/minimum_string_length_validator.js +23 -0
- package/.out/validation/validators_of_values.d.ts +4 -0
- package/.out/validation/validators_of_values.js +1 -0
- package/.turbo/turbo-build.log +9 -9
- package/.turbo/turbo-check-types.log +1 -1
- package/.turbo/turbo-release$colon$exports.log +1 -1
- package/README.md +16 -2
- package/dist/index.cjs +364 -76
- package/dist/index.d.cts +365 -114
- package/dist/index.d.ts +365 -114
- package/dist/index.js +355 -73
- package/index.ts +17 -11
- package/package.json +11 -3
- package/transformers/copies/copy.ts +4 -4
- package/transformers/copies/copy_to.ts +11 -7
- package/transformers/copies/mobx_copy.ts +6 -6
- package/transformers/copies/specs/copy_to.tests.ts +8 -8
- package/transformers/copies/specs/mobx_copy.tests.ts +3 -3
- package/transformers/flatteners/{flatten_accessors_of.ts → flatten_accessors_of_type.ts} +10 -10
- package/transformers/flatteners/flatten_json_value_to_type_paths_of.ts +5 -5
- package/transformers/flatteners/{flatten_type_def_to.ts → flatten_type_to.ts} +1 -1
- package/transformers/flatteners/{flatten_type_defs_of.ts → flatten_types_of_type.ts} +3 -3
- package/transformers/flatteners/flatten_validation_errors_of_type.ts +93 -0
- package/transformers/flatteners/flatten_validators_of_validating_type.ts +32 -0
- package/transformers/flatteners/{flatten_value_type_to.ts → flatten_value_to.ts} +12 -7
- package/transformers/flatteners/{flatten_value_types_of.ts → flatten_values_of_type.ts} +6 -6
- package/transformers/flatteners/specs/{flatten_accessors_of.tests.ts → flatten_accessors_of_type.tests.ts} +10 -11
- package/transformers/flatteners/specs/flatten_json_value_to_type_paths_of.tests.ts +6 -6
- package/transformers/flatteners/specs/{flatten_type_def_to.tests.ts → flatten_type_to.tests.ts} +15 -13
- package/transformers/flatteners/specs/flatten_types_of_type.tests.ts +27 -0
- package/transformers/flatteners/specs/flatten_validation_errors_of_type.tests.ts +48 -0
- package/transformers/flatteners/specs/{flatten_value_type_to.tests.ts → flatten_value_to.tests.ts} +124 -91
- package/transformers/flatteners/specs/{flatten_value_types_of.tests.ts → flatten_values_of_type.tests.ts} +4 -4
- package/transformers/flatteners/specs/value_path_to_type_path.tests.ts +20 -18
- package/transformers/flatteners/value_path_to_type_path.ts +4 -4
- package/types/builders.ts +239 -71
- package/types/definitions.ts +2 -2
- package/types/{flattened_accessors_of.ts → flattened_accessors_of_type.ts} +3 -3
- package/types/{flattened_type_defs_of.ts → flattened_types_of_type.ts} +22 -12
- package/types/flattened_types_of_validating_type.ts +152 -0
- package/types/flattened_validators_of_validating_type.ts +33 -0
- package/types/flattened_values_of_type.ts +11 -0
- package/types/{mobx_value_type_of.ts → mobx_value_of_type.ts} +2 -2
- package/types/{partial_type_def_of.ts → partial_type_of_type.ts} +3 -1
- package/types/{json_path_of.ts → path_of.ts} +1 -1
- package/types/{json_paths_of.ts → paths_of_type.ts} +5 -5
- package/types/readonly_type_of_type.ts +54 -0
- package/types/specs/builder.tests.ts +205 -54
- package/types/specs/{flattened_accessors_of.tests.ts → flattened_accessors_of_type.tests.ts} +3 -3
- package/types/specs/{flattened_type_defs_of.tests.ts → flattened_types_of_type.tests.ts} +82 -34
- package/types/specs/flattened_validators_of_validating_type.tests.ts +47 -0
- package/types/specs/flattened_values_of_type.tests.ts +39 -0
- package/types/specs/{partial_type_def_of.tests.ts → partial_type_of_type.tests.ts} +11 -11
- package/types/specs/{json_paths_of.tests.ts → paths_of_type.tests.ts} +47 -47
- package/types/specs/{readonly_type_def_of.tests.ts → readonly_type_of_type.tests.ts} +14 -14
- package/types/specs/type_of_type.tests.ts +60 -0
- package/types/specs/{value_type_of.tests.ts → value_of_type.tests.ts} +15 -15
- package/types/specs/{value_to_type_paths_of.tests.ts → value_to_type_paths_of_type.tests.ts} +26 -25
- package/types/specs/value_types_of_discriminated_union.tests.ts +6 -6
- package/types/strict_definitions.ts +3 -3
- package/types/type_of_type.ts +178 -0
- package/types/validating_definitions.ts +109 -0
- package/types/validating_type_def_with_error.ts +93 -0
- package/types/value_of_type.ts +84 -0
- package/types/{value_to_type_paths_of.ts → value_to_type_paths_of_type.ts} +30 -29
- package/types/value_types_of_discriminated_union.ts +4 -3
- package/validation/errors_of_validators.ts +10 -0
- package/validation/specs/errors_of_validators.tests.ts +20 -0
- package/validation/specs/validators_of_values.tests.ts +68 -0
- package/validation/validator.ts +103 -0
- package/validation/validators/defined_validator.ts +22 -0
- package/validation/validators/minimum_string_length_validator.ts +36 -0
- package/validation/validators_of_values.ts +17 -0
- package/.out/transformers/flatteners/flatten_accessors_of.d.ts +0 -5
- package/.out/transformers/flatteners/flatten_accessors_of.js +0 -11
- package/.out/transformers/flatteners/flatten_type_defs_of.js +0 -7
- package/.out/transformers/flatteners/flatten_value_types_of.d.ts +0 -3
- package/.out/transformers/flatteners/flatten_value_types_of.js +0 -7
- package/.out/transformers/flatteners/specs/flatten_type_defs_of.tests.js +0 -17
- package/.out/types/flattened_accessors_of.d.ts +0 -9
- package/.out/types/flattened_value_types_of.d.ts +0 -6
- package/.out/types/json_path_of.d.ts +0 -1
- package/.out/types/readonly_type_def_of.d.ts +0 -29
- package/.out/types/specs/flattened_value_types_of.tests.js +0 -11
- package/.out/types/value_type_of.d.ts +0 -24
- package/transformers/flatteners/specs/flatten_type_defs_of.tests.ts +0 -26
- package/types/flattened_value_types_of.ts +0 -11
- package/types/readonly_type_def_of.ts +0 -53
- package/types/specs/flattened_value_types_of.tests.ts +0 -21
- package/types/value_type_of.ts +0 -84
- /package/.out/transformers/flatteners/specs/{flatten_accessors_of.tests.d.ts → flatten_accessors_of_type.tests.d.ts} +0 -0
- /package/.out/transformers/flatteners/specs/{flatten_type_def_to.tests.d.ts → flatten_type_to.tests.d.ts} +0 -0
- /package/.out/transformers/flatteners/specs/{flatten_type_defs_of.tests.d.ts → flatten_types_of_type.tests.d.ts} +0 -0
- /package/.out/transformers/flatteners/specs/{flatten_value_type_to.tests.d.ts → flatten_validation_errors_of_type.tests.d.ts} +0 -0
- /package/.out/transformers/flatteners/specs/{flatten_value_types_of.tests.d.ts → flatten_value_to.tests.d.ts} +0 -0
- /package/.out/{types/flattened_accessors_of.js → transformers/flatteners/specs/flatten_values_of_type.tests.d.ts} +0 -0
- /package/.out/types/{flattened_type_defs_of.js → flattened_accessors_of_type.js} +0 -0
- /package/.out/types/{flattened_value_types_of.js → flattened_types_of_type.js} +0 -0
- /package/.out/types/{json_path_of.js → flattened_types_of_validating_type.js} +0 -0
- /package/.out/types/{json_paths_of.js → flattened_validators_of_validating_type.js} +0 -0
- /package/.out/types/{partial_type_def_of.js → flattened_values_of_type.js} +0 -0
- /package/.out/types/{mobx_value_type_of.js → mobx_value_of_type.js} +0 -0
- /package/.out/types/{readonly_type_def_of.js → partial_type_of_type.js} +0 -0
- /package/.out/types/{specs/flattened_accessors_of.tests.d.ts → path_of.js} +0 -0
- /package/.out/types/{specs/flattened_type_defs_of.tests.d.ts → paths_of_type.js} +0 -0
- /package/.out/types/{specs/flattened_value_types_of.tests.d.ts → readonly_type_of_type.js} +0 -0
- /package/.out/types/specs/{json_paths_of.tests.d.ts → flattened_accessors_of_type.tests.d.ts} +0 -0
- /package/.out/types/specs/{partial_type_def_of.tests.d.ts → flattened_types_of_type.tests.d.ts} +0 -0
- /package/.out/types/specs/{readonly_type_def_of.tests.d.ts → flattened_validators_of_validating_type.tests.d.ts} +0 -0
- /package/.out/types/specs/{value_to_type_paths_of.tests.d.ts → flattened_values_of_type.tests.d.ts} +0 -0
- /package/.out/types/specs/{value_type_of.tests.d.ts → partial_type_of_type.tests.d.ts} +0 -0
- /package/.out/types/{value_to_type_paths_of.js → specs/paths_of_type.tests.d.ts} +0 -0
- /package/.out/types/{value_type_of.js → specs/readonly_type_of_type.tests.d.ts} +0 -0
|
@@ -14,14 +14,14 @@ import {
|
|
|
14
14
|
type Depths,
|
|
15
15
|
type StartingDepth,
|
|
16
16
|
} from './flattened'
|
|
17
|
-
import { type
|
|
17
|
+
import { type PathOf } from './path_of'
|
|
18
18
|
import { type StrictType } from './strict_definitions'
|
|
19
19
|
|
|
20
|
-
export type
|
|
20
|
+
export type ValueToTypePathsOfType<
|
|
21
21
|
T extends StrictType,
|
|
22
22
|
SegmentOverride extends string = '*',
|
|
23
23
|
Path extends string = '$',
|
|
24
|
-
> = SimplifyDeep<
|
|
24
|
+
> = SimplifyDeep<InternalFlattenedTypePathsOf<
|
|
25
25
|
T['definition'],
|
|
26
26
|
SegmentOverride,
|
|
27
27
|
Path,
|
|
@@ -29,7 +29,8 @@ export type ValueToTypePathsOf<
|
|
|
29
29
|
StartingDepth
|
|
30
30
|
>>
|
|
31
31
|
|
|
32
|
-
|
|
32
|
+
// TODO rename to FlattenTypePathsOfTypeDef and apply pattern to subsequent types
|
|
33
|
+
type InternalFlattenedTypePathsOf<
|
|
33
34
|
T extends TypeDef,
|
|
34
35
|
SegmentOverride extends string,
|
|
35
36
|
ValuePath extends string,
|
|
@@ -39,9 +40,9 @@ type InternalFlattenedJsonPathsOf<
|
|
|
39
40
|
& {
|
|
40
41
|
readonly [K in ValuePath]: TypePath
|
|
41
42
|
}
|
|
42
|
-
&
|
|
43
|
+
& InternalFlattenedTypePathsOfChildren<T, SegmentOverride, ValuePath, TypePath, '', Depth>
|
|
43
44
|
|
|
44
|
-
type
|
|
45
|
+
type InternalFlattenedTypePathsOfChildren<
|
|
45
46
|
T extends TypeDef,
|
|
46
47
|
SegmentOverride extends string,
|
|
47
48
|
ValuePath extends string,
|
|
@@ -50,22 +51,22 @@ type InternalFlattenedJsonPathsOfChildren<
|
|
|
50
51
|
Depth extends number,
|
|
51
52
|
NextDepth extends number = Depths[Depth],
|
|
52
53
|
> = NextDepth extends -1 ? never
|
|
53
|
-
: T extends LiteralTypeDef ?
|
|
54
|
-
: T extends ListTypeDef ?
|
|
54
|
+
: T extends LiteralTypeDef ? InternalFlattenedTypePathsOfLiteralChildren
|
|
55
|
+
: T extends ListTypeDef ? InternalFlattenedTypePathsOfListChildren<
|
|
55
56
|
T,
|
|
56
57
|
SegmentOverride,
|
|
57
58
|
ValuePath,
|
|
58
59
|
TypePath,
|
|
59
60
|
NextDepth
|
|
60
61
|
>
|
|
61
|
-
: T extends RecordTypeDef ?
|
|
62
|
+
: T extends RecordTypeDef ? InternalFlattenedTypePathsOfRecordChildren<
|
|
62
63
|
T,
|
|
63
64
|
SegmentOverride,
|
|
64
65
|
ValuePath,
|
|
65
66
|
TypePath,
|
|
66
67
|
NextDepth
|
|
67
68
|
>
|
|
68
|
-
: T extends ObjectTypeDef ?
|
|
69
|
+
: T extends ObjectTypeDef ? InternalFlattenedTypePathsOfObjectChildren<
|
|
69
70
|
T,
|
|
70
71
|
SegmentOverride,
|
|
71
72
|
ValuePath,
|
|
@@ -73,7 +74,7 @@ type InternalFlattenedJsonPathsOfChildren<
|
|
|
73
74
|
Qualifier,
|
|
74
75
|
NextDepth
|
|
75
76
|
>
|
|
76
|
-
: T extends UnionTypeDef ?
|
|
77
|
+
: T extends UnionTypeDef ? InternalFlattenedTypePathsOfUnionChildren<
|
|
77
78
|
T,
|
|
78
79
|
SegmentOverride,
|
|
79
80
|
ValuePath,
|
|
@@ -83,37 +84,37 @@ type InternalFlattenedJsonPathsOfChildren<
|
|
|
83
84
|
>
|
|
84
85
|
: never
|
|
85
86
|
|
|
86
|
-
type
|
|
87
|
+
type InternalFlattenedTypePathsOfLiteralChildren = {}
|
|
87
88
|
|
|
88
|
-
type
|
|
89
|
+
type InternalFlattenedTypePathsOfListChildren<
|
|
89
90
|
T extends ListTypeDef,
|
|
90
91
|
SegmentOverride extends string,
|
|
91
92
|
ValuePath extends string,
|
|
92
93
|
TypePath extends string,
|
|
93
94
|
Depth extends number,
|
|
94
|
-
> =
|
|
95
|
+
> = InternalFlattenedTypePathsOf<
|
|
95
96
|
T['elements'],
|
|
96
97
|
SegmentOverride,
|
|
97
|
-
|
|
98
|
-
|
|
98
|
+
PathOf<ValuePath, number>,
|
|
99
|
+
PathOf<TypePath, number, SegmentOverride>,
|
|
99
100
|
Depth
|
|
100
101
|
>
|
|
101
102
|
|
|
102
|
-
type
|
|
103
|
+
type InternalFlattenedTypePathsOfRecordChildren<
|
|
103
104
|
T extends RecordTypeDef,
|
|
104
105
|
SegmentOverride extends string,
|
|
105
106
|
ValuePath extends string,
|
|
106
107
|
TypePath extends string,
|
|
107
108
|
Depth extends number,
|
|
108
|
-
> =
|
|
109
|
+
> = InternalFlattenedTypePathsOf<
|
|
109
110
|
T['valueTypeDef'],
|
|
110
111
|
SegmentOverride,
|
|
111
|
-
|
|
112
|
-
|
|
112
|
+
PathOf<ValuePath, T['keyPrototype']>,
|
|
113
|
+
PathOf<TypePath, T['keyPrototype'], SegmentOverride>,
|
|
113
114
|
Depth
|
|
114
115
|
>
|
|
115
116
|
|
|
116
|
-
type
|
|
117
|
+
type InternalFlattenedTypePathsOfObjectChildren<
|
|
117
118
|
T extends ObjectTypeDef,
|
|
118
119
|
SegmentOverride extends string,
|
|
119
120
|
ValuePath extends string,
|
|
@@ -122,11 +123,11 @@ type InternalFlattenedJsonPathsOfObjectChildren<
|
|
|
122
123
|
Depth extends number,
|
|
123
124
|
> = T extends ObjectTypeDef<infer Fields> ? keyof Fields extends string ? UnionToIntersection<
|
|
124
125
|
{
|
|
125
|
-
readonly [K in keyof Fields]-?:
|
|
126
|
+
readonly [K in keyof Fields]-?: InternalFlattenedTypePathsOf<
|
|
126
127
|
Exclude<Fields[K], undefined>,
|
|
127
128
|
SegmentOverride,
|
|
128
|
-
|
|
129
|
-
|
|
129
|
+
PathOf<ValuePath, `${Qualifier}${K}`>,
|
|
130
|
+
PathOf<TypePath, `${Qualifier}${K}`>,
|
|
130
131
|
Depth
|
|
131
132
|
>
|
|
132
133
|
}[keyof Fields]
|
|
@@ -134,7 +135,7 @@ type InternalFlattenedJsonPathsOfObjectChildren<
|
|
|
134
135
|
: never
|
|
135
136
|
: never
|
|
136
137
|
|
|
137
|
-
// type
|
|
138
|
+
// type InternalFlattenedTypePathsOfUnionChildren<
|
|
138
139
|
// T extends UnionTypeDef,
|
|
139
140
|
// SegmentOverride extends string,
|
|
140
141
|
// ValuePath extends string,
|
|
@@ -142,7 +143,7 @@ type InternalFlattenedJsonPathsOfObjectChildren<
|
|
|
142
143
|
// Depth extends number,
|
|
143
144
|
// > = T extends UnionTypeDef<infer D, infer Unions> ?
|
|
144
145
|
// & ({
|
|
145
|
-
// [K in keyof Unions]:
|
|
146
|
+
// [K in keyof Unions]: InternalFlattenedTypePathsOfChildren<
|
|
146
147
|
// Unions[K],
|
|
147
148
|
// SegmentOverride,
|
|
148
149
|
// ValuePath,
|
|
@@ -153,7 +154,7 @@ type InternalFlattenedJsonPathsOfObjectChildren<
|
|
|
153
154
|
// & (D extends string ? ReadonlyRecord<JsonPathOf<ValuePath, D>, JsonPathOf<TypePath, D>> : {})
|
|
154
155
|
// : never
|
|
155
156
|
|
|
156
|
-
type
|
|
157
|
+
type InternalFlattenedTypePathsOfUnionChildren<
|
|
157
158
|
T extends UnionTypeDef,
|
|
158
159
|
SegmentOverride extends string,
|
|
159
160
|
ValuePath extends string,
|
|
@@ -161,7 +162,7 @@ type InternalFlattenedJsonPathsOfUnionChildren<
|
|
|
161
162
|
Qualifier extends string,
|
|
162
163
|
Depth extends number,
|
|
163
164
|
> = T extends UnionTypeDef<infer D, infer Unions> ? keyof Unions extends string ? D extends null ? UnionToIntersection<{
|
|
164
|
-
readonly [K in keyof Unions]:
|
|
165
|
+
readonly [K in keyof Unions]: InternalFlattenedTypePathsOfChildren<
|
|
165
166
|
Unions[K],
|
|
166
167
|
SegmentOverride,
|
|
167
168
|
ValuePath,
|
|
@@ -171,7 +172,7 @@ type InternalFlattenedJsonPathsOfUnionChildren<
|
|
|
171
172
|
>
|
|
172
173
|
}[keyof Unions]>
|
|
173
174
|
: UnionToIntersection<{
|
|
174
|
-
readonly [K in keyof Unions]:
|
|
175
|
+
readonly [K in keyof Unions]: InternalFlattenedTypePathsOfChildren<
|
|
175
176
|
Unions[K],
|
|
176
177
|
SegmentOverride,
|
|
177
178
|
ValuePath,
|
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import { type UnionTypeDef } from './definitions'
|
|
2
|
-
import { type
|
|
2
|
+
import { type ReadonlyTypeOfType } from './readonly_type_of_type'
|
|
3
|
+
import { type ValueOfType } from './value_of_type'
|
|
3
4
|
|
|
4
5
|
// gets a record of the values types for a discriminated union mapped according to
|
|
5
6
|
// their discriminators
|
|
6
7
|
export type ValueTypesOfDiscriminatedUnion<
|
|
7
8
|
U extends UnionTypeDef,
|
|
8
9
|
> = U extends UnionTypeDef<infer D, infer Unions> ? D extends string ? {
|
|
9
|
-
[K in keyof Unions]:
|
|
10
|
-
[KD in D]: K
|
|
10
|
+
[K in keyof Unions]: ValueOfType<ReadonlyTypeOfType<{ definition: Unions[K] }>> & {
|
|
11
|
+
readonly [KD in D]: K
|
|
11
12
|
}
|
|
12
13
|
}
|
|
13
14
|
: never
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type ErrorsOfValidators } from 'validation/errors_of_validators'
|
|
2
|
+
import { type Validator } from 'validation/validator'
|
|
3
|
+
|
|
4
|
+
describe('ErrorsOfValidators', function () {
|
|
5
|
+
describe('simple', function () {
|
|
6
|
+
type T = ErrorsOfValidators<{
|
|
7
|
+
x: Validator<string, 'a'>,
|
|
8
|
+
y: Validator<number, 'b'>,
|
|
9
|
+
}>
|
|
10
|
+
|
|
11
|
+
let t: {
|
|
12
|
+
readonly x: 'a',
|
|
13
|
+
readonly y: 'b',
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
it('equals expected type', function () {
|
|
17
|
+
expectTypeOf<T>().toEqualTypeOf(t)
|
|
18
|
+
})
|
|
19
|
+
})
|
|
20
|
+
})
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { type Validator } from 'validation/validator'
|
|
2
|
+
import { type ValidatorsOfValues } from 'validation/validators_of_values'
|
|
3
|
+
|
|
4
|
+
describe('FlattenedValidatorsOfType', function () {
|
|
5
|
+
describe('literal', function () {
|
|
6
|
+
type T = ValidatorsOfValues<
|
|
7
|
+
{
|
|
8
|
+
$: 'a' | 'b' | 'c',
|
|
9
|
+
},
|
|
10
|
+
{
|
|
11
|
+
$: '$',
|
|
12
|
+
},
|
|
13
|
+
1
|
|
14
|
+
>
|
|
15
|
+
|
|
16
|
+
let t: {
|
|
17
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
18
|
+
readonly $: Validator<'a' | 'b' | 'c', any, '$', 1>,
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
it('has the expected type', function () {
|
|
22
|
+
expectTypeOf<T>().toEqualTypeOf(t)
|
|
23
|
+
})
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
describe('list', function () {
|
|
27
|
+
type T = ValidatorsOfValues<
|
|
28
|
+
{
|
|
29
|
+
$: number[],
|
|
30
|
+
'$.*': number,
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
$: '$',
|
|
34
|
+
'$.*': `$.${number}`,
|
|
35
|
+
},
|
|
36
|
+
2
|
|
37
|
+
>
|
|
38
|
+
|
|
39
|
+
let t: {
|
|
40
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
41
|
+
readonly $: Validator<number[], any, '$', 2>,
|
|
42
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
43
|
+
readonly '$.*': Validator<number, any, `$.${number}`, 2>,
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
it('has the expected type', function () {
|
|
47
|
+
expectTypeOf<T>().toEqualTypeOf(t)
|
|
48
|
+
})
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
describe('with defaults', function () {
|
|
52
|
+
type T = ValidatorsOfValues<
|
|
53
|
+
{
|
|
54
|
+
$: number[],
|
|
55
|
+
'$.*': number,
|
|
56
|
+
}
|
|
57
|
+
>
|
|
58
|
+
|
|
59
|
+
let t: {
|
|
60
|
+
readonly $: Validator<number[]>,
|
|
61
|
+
readonly '$.*': Validator<number>,
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
it('has the expected type', function () {
|
|
65
|
+
expectTypeOf<T>().toEqualTypeOf(t)
|
|
66
|
+
})
|
|
67
|
+
})
|
|
68
|
+
})
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { type Simplify } from 'type-fest'
|
|
2
|
+
|
|
3
|
+
export type FunctionalValidator<
|
|
4
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
5
|
+
V = any,
|
|
6
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
7
|
+
E = any,
|
|
8
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
9
|
+
ValuePath extends string = any,
|
|
10
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
11
|
+
Context = any,
|
|
12
|
+
> = (v: V, valuePath: ValuePath, context: Context) => E | null
|
|
13
|
+
|
|
14
|
+
export type AnnotatedValidator<
|
|
15
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
16
|
+
V = any,
|
|
17
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
18
|
+
E = any,
|
|
19
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
20
|
+
ValuePath extends string = any,
|
|
21
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
22
|
+
Context = any,
|
|
23
|
+
> = {
|
|
24
|
+
readonly validate: (v: V, valuePath: ValuePath, context: Context) => E | null,
|
|
25
|
+
readonly annotations: (valuePath: ValuePath, context: Context) => {
|
|
26
|
+
readonly required: boolean,
|
|
27
|
+
readonly readonly: boolean,
|
|
28
|
+
},
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export type Validator<
|
|
32
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
33
|
+
V = any,
|
|
34
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
35
|
+
E = any,
|
|
36
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
37
|
+
ValuePath extends string = any,
|
|
38
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
39
|
+
Context = any,
|
|
40
|
+
> = FunctionalValidator<V, E, ValuePath, Context> | AnnotatedValidator<V, E, ValuePath, Context>
|
|
41
|
+
|
|
42
|
+
export type ErrorOfValidator<V extends Validator> = V extends Validator<infer _V, infer E> ? E : never
|
|
43
|
+
|
|
44
|
+
export type ValidationError<Type extends string, Data = {}> = Simplify<
|
|
45
|
+
{
|
|
46
|
+
type: Type,
|
|
47
|
+
} & Data
|
|
48
|
+
>
|
|
49
|
+
|
|
50
|
+
export function isFunctionalValidator(v: Validator): v is FunctionalValidator {
|
|
51
|
+
return typeof v === 'function'
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function isAnnotatedValidator(v: Validator): v is AnnotatedValidator {
|
|
55
|
+
return typeof v !== 'function'
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function validate<
|
|
59
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
60
|
+
V = any,
|
|
61
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
62
|
+
E = any,
|
|
63
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
64
|
+
ValuePath extends string = any,
|
|
65
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
66
|
+
Context = any,
|
|
67
|
+
>(
|
|
68
|
+
validator: Validator<V, E, ValuePath, Context>,
|
|
69
|
+
v: V,
|
|
70
|
+
valuePath: ValuePath,
|
|
71
|
+
context: Context,
|
|
72
|
+
): E | null {
|
|
73
|
+
if (isAnnotatedValidator(validator)) {
|
|
74
|
+
return validator.validate(v, valuePath, context)
|
|
75
|
+
} else {
|
|
76
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
77
|
+
return (validator as FunctionalValidator<V, E, ValuePath, Context>)(v, valuePath, context)
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function annotations<
|
|
82
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
83
|
+
V = any,
|
|
84
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
85
|
+
E = any,
|
|
86
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
87
|
+
ValuePath extends string = any,
|
|
88
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
89
|
+
Context = any,
|
|
90
|
+
>(
|
|
91
|
+
validator: Validator<V, E, ValuePath, Context>,
|
|
92
|
+
valuePath: ValuePath,
|
|
93
|
+
context: Context,
|
|
94
|
+
) {
|
|
95
|
+
if (isAnnotatedValidator(validator)) {
|
|
96
|
+
return validator.annotations(valuePath, context)
|
|
97
|
+
} else {
|
|
98
|
+
return {
|
|
99
|
+
required: false,
|
|
100
|
+
readonly: false,
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { type AnnotatedValidator } from 'validation/validator'
|
|
2
|
+
|
|
3
|
+
export class DefinedValidator<V, E, ValuePath extends string, Context>
|
|
4
|
+
implements AnnotatedValidator<V | null | undefined, E, ValuePath, Context>
|
|
5
|
+
{
|
|
6
|
+
constructor(private readonly error: E) {
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
validate(v: V | null | undefined): E | null {
|
|
10
|
+
if (v == null) {
|
|
11
|
+
return this.error
|
|
12
|
+
}
|
|
13
|
+
return null
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
annotations() {
|
|
17
|
+
return {
|
|
18
|
+
required: true,
|
|
19
|
+
readonly: false,
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type AnnotatedValidator,
|
|
3
|
+
} from 'validation/validator'
|
|
4
|
+
|
|
5
|
+
export const MinimumStringLengthValidationErrorType = 'minimum_string_length'
|
|
6
|
+
|
|
7
|
+
export type MinimumStringLengthValidationError = {
|
|
8
|
+
type: typeof MinimumStringLengthValidationErrorType,
|
|
9
|
+
receivedLength: number,
|
|
10
|
+
minimumLength: number,
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export class MinimumStringLengthValidator<ValuePath extends string, Context>
|
|
14
|
+
implements AnnotatedValidator<string, MinimumStringLengthValidationError, ValuePath, Context>
|
|
15
|
+
{
|
|
16
|
+
constructor(private readonly minimumLength: number) {
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
validate(value: string): MinimumStringLengthValidationError | null {
|
|
20
|
+
if (value.length < this.minimumLength) {
|
|
21
|
+
return {
|
|
22
|
+
type: MinimumStringLengthValidationErrorType,
|
|
23
|
+
minimumLength: this.minimumLength,
|
|
24
|
+
receivedLength: value.length,
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return null
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
annotations() {
|
|
31
|
+
return {
|
|
32
|
+
required: true,
|
|
33
|
+
readonly: false,
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type Validator } from 'validation/validator'
|
|
2
|
+
|
|
3
|
+
export type ValidatorsOfValues<
|
|
4
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
5
|
+
FlattenedValues extends Readonly<Record<string, any>>,
|
|
6
|
+
TypePathsToValuePaths extends Readonly<Record<keyof FlattenedValues, string>> = Readonly<
|
|
7
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
8
|
+
Record<keyof FlattenedValues, any>
|
|
9
|
+
>,
|
|
10
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
11
|
+
Context = any,
|
|
12
|
+
> = {
|
|
13
|
+
readonly [
|
|
14
|
+
K in keyof FlattenedValues
|
|
15
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
16
|
+
]: Validator<FlattenedValues[K], any, TypePathsToValuePaths[K], Context>
|
|
17
|
+
}
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import { type Type } from 'types/definitions';
|
|
2
|
-
import { type Accessor, type FlattenedAccessorsOf } from 'types/flattened_accessors_of';
|
|
3
|
-
import { type ValueTypeOf } from 'types/value_type_of';
|
|
4
|
-
import { type Setter } from './flatten_value_type_to';
|
|
5
|
-
export declare function flattenAccessorsOf<T extends Type, R extends Readonly<Record<string, Accessor<any>>> = FlattenedAccessorsOf<T>>(t: T, value: ValueTypeOf<T>, setValue: Setter<ValueTypeOf<T>>): R;
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { flattenValueTypeTo, } from './flatten_value_type_to';
|
|
2
|
-
function mapAccessor(_t, value, set) {
|
|
3
|
-
return {
|
|
4
|
-
value,
|
|
5
|
-
set,
|
|
6
|
-
};
|
|
7
|
-
}
|
|
8
|
-
export function flattenAccessorsOf(t, value, setValue) {
|
|
9
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
10
|
-
return flattenValueTypeTo(t, value, setValue, mapAccessor);
|
|
11
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { flattenTypeDefsOf } from 'transformers/flatteners/flatten_type_defs_of';
|
|
2
|
-
import { booleanType, list, numberType, object, } from 'types/builders';
|
|
3
|
-
describe('flattenTypeDefsOf', function () {
|
|
4
|
-
it('flattens', function () {
|
|
5
|
-
const listTypeDef = list(numberType);
|
|
6
|
-
const structTypeDef = object()
|
|
7
|
-
.set('a', listTypeDef)
|
|
8
|
-
.set('b', booleanType);
|
|
9
|
-
const flattened = flattenTypeDefsOf(structTypeDef);
|
|
10
|
-
expect(flattened).toEqual({
|
|
11
|
-
$: structTypeDef.narrow,
|
|
12
|
-
'$.a': listTypeDef.narrow,
|
|
13
|
-
'$.a.*': numberType.narrow,
|
|
14
|
-
'$.b': booleanType.narrow,
|
|
15
|
-
});
|
|
16
|
-
});
|
|
17
|
-
});
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { type Type } from './definitions';
|
|
2
|
-
import { type FlattenedValueTypesOf } from './flattened_value_types_of';
|
|
3
|
-
export type Accessor<T = any> = {
|
|
4
|
-
readonly value: T;
|
|
5
|
-
set(v: T): void;
|
|
6
|
-
};
|
|
7
|
-
export type FlattenedAccessorsOf<T extends Type, Flattened extends Readonly<Record<string, Accessor>> = FlattenedValueTypesOf<T>> = {
|
|
8
|
-
readonly [K in keyof Flattened]: Accessor<Flattened[K]>;
|
|
9
|
-
};
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { type Type } from './definitions';
|
|
2
|
-
import { type FlattenedTypeDefsOf } from './flattened_type_defs_of';
|
|
3
|
-
import { type ValueTypeOf } from './value_type_of';
|
|
4
|
-
export type FlattenedValueTypesOf<T extends Type, SegmentOverride extends string | null = null, Flattened extends Readonly<Record<string, Type>> = FlattenedTypeDefsOf<T, SegmentOverride>> = {
|
|
5
|
-
[K in keyof Flattened]: ValueTypeOf<Flattened[K], {}>;
|
|
6
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export type JsonPathOf<Prefix extends string, Accessor extends string | number | symbol, SegmentOverride extends string | null = null> = SegmentOverride extends string ? `${Prefix}.${SegmentOverride}` : Accessor extends string | number ? `${Prefix}.${Accessor}` : never;
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { type ListTypeDef, type LiteralTypeDef, type ObjectTypeDef, type RecordTypeDef, type Type, type TypeDef, type UnionTypeDef } from './definitions';
|
|
2
|
-
export type ReadonlyTypeDefOf<T extends Type> = {
|
|
3
|
-
readonly definition: InternalReadonlyTypeDefOf<T['definition']>;
|
|
4
|
-
};
|
|
5
|
-
type InternalReadonlyTypeDefOf<T extends TypeDef> = T extends LiteralTypeDef ? InternalReadonlyTypeDefOfLiteral<T> : T extends ListTypeDef ? InternalReadonlyTypeDefOfList<T> : T extends RecordTypeDef ? InternalReadonlyTypeDefOfRecord<T> : T extends ObjectTypeDef ? InternalReadonlyTypeDefOfObject<T> : T extends UnionTypeDef ? InternalReadonlyTypeDefOfUnion<T> : never;
|
|
6
|
-
type InternalReadonlyTypeDefOfLiteral<T extends LiteralTypeDef> = T;
|
|
7
|
-
type InternalReadonlyTypeDefOfList<T extends ListTypeDef> = {
|
|
8
|
-
readonly type: T['type'];
|
|
9
|
-
readonly elements: InternalReadonlyTypeDefOf<T['elements']>;
|
|
10
|
-
};
|
|
11
|
-
type InternalReadonlyTypeDefOfRecord<T extends RecordTypeDef> = {
|
|
12
|
-
readonly type: T['type'];
|
|
13
|
-
readonly keyPrototype: T['keyPrototype'];
|
|
14
|
-
readonly valueTypeDef: undefined extends T['valueTypeDef'] ? InternalReadonlyTypeDefOf<Exclude<T['valueTypeDef'], undefined>> | undefined : InternalReadonlyTypeDefOf<T['valueTypeDef']>;
|
|
15
|
-
};
|
|
16
|
-
type InternalReadonlyTypeDefOfObject<T extends ObjectTypeDef> = T extends ObjectTypeDef<infer Fields> ? {
|
|
17
|
-
readonly type: T['type'];
|
|
18
|
-
readonly fields: {
|
|
19
|
-
readonly [K in keyof Fields]: InternalReadonlyTypeDefOf<Fields[K]>;
|
|
20
|
-
};
|
|
21
|
-
} : never;
|
|
22
|
-
type InternalReadonlyTypeDefOfUnion<T extends UnionTypeDef> = T extends UnionTypeDef<infer D, infer Unions> ? {
|
|
23
|
-
readonly type: T['type'];
|
|
24
|
-
readonly discriminator: D;
|
|
25
|
-
readonly unions: {
|
|
26
|
-
readonly [K in keyof Unions]: InternalReadonlyTypeDefOf<Unions[K]>;
|
|
27
|
-
};
|
|
28
|
-
} : never;
|
|
29
|
-
export {};
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { numberType, record, } from 'types/builders';
|
|
2
|
-
describe('FlattenedValueTypesOf', function () {
|
|
3
|
-
// note we only test a small example since most of the work is done in flatten
|
|
4
|
-
describe('record', function () {
|
|
5
|
-
const builder = record(numberType);
|
|
6
|
-
let v;
|
|
7
|
-
it('equals expected type', function () {
|
|
8
|
-
expectTypeOf(v).toEqualTypeOf();
|
|
9
|
-
});
|
|
10
|
-
});
|
|
11
|
-
});
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { type IsFieldReadonly } from '@strictly/base';
|
|
2
|
-
import { type ListTypeDef, type LiteralTypeDef, type ObjectTypeDef, type RecordTypeDef, type Type, type TypeDef, type UnionTypeDef } from './definitions';
|
|
3
|
-
export type ValueTypeOf<T, Extra = {}> = T extends Type ? InternalValueTypeOf<T['definition'], Extra> : never;
|
|
4
|
-
export type InternalValueTypeOf<F extends TypeDef, Extra> = F extends LiteralTypeDef ? InternalValueTypeOfLiteral<F> : F extends ListTypeDef ? InternalValueTypeOfList<F, Extra> : F extends RecordTypeDef ? InternalValueTypeOfRecord<F, Extra> : F extends ObjectTypeDef ? InternalValueTypeOfObject<F, Extra> : F extends UnionTypeDef ? InternalValueTypeOfUnion<F, Extra> : never;
|
|
5
|
-
type InternalValueTypeOfLiteral<F extends LiteralTypeDef> = F['valuePrototype'][number];
|
|
6
|
-
type InternalValueTypeOfList<F extends ListTypeDef, Extra> = IsFieldReadonly<F, 'elements'> extends true ? readonly InternalValueTypeOf<F['elements'], Extra>[] & Extra : InternalValueTypeOf<F['elements'], Extra>[] & Extra;
|
|
7
|
-
type InternalValueTypeOfRecord<F extends RecordTypeDef, Extra> = undefined extends F['valueTypeDef'] ? IsFieldReadonly<F, 'valueTypeDef'> extends true ? {
|
|
8
|
-
readonly [k in F['keyPrototype']]?: InternalValueTypeOf<F['valueTypeDef'], Extra>;
|
|
9
|
-
} : {
|
|
10
|
-
[k in F['keyPrototype']]?: InternalValueTypeOf<F['valueTypeDef'], Extra>;
|
|
11
|
-
} : IsFieldReadonly<F, 'valueTypeDef'> extends true ? {
|
|
12
|
-
readonly [k in F['keyPrototype']]: InternalValueTypeOf<F['valueTypeDef'], Extra>;
|
|
13
|
-
} : {
|
|
14
|
-
[k in F['keyPrototype']]: InternalValueTypeOf<F['valueTypeDef'], Extra>;
|
|
15
|
-
};
|
|
16
|
-
type InternalValueTypeOfObject<F extends ObjectTypeDef, Extra> = F extends ObjectTypeDef<infer Fields> ? {
|
|
17
|
-
[K in keyof Fields]: InternalValueTypeOf<Fields[K], Extra>;
|
|
18
|
-
} & Extra : never;
|
|
19
|
-
type InternalValueTypeOfUnion<F extends UnionTypeDef, Extra> = F extends UnionTypeDef<infer D, infer U> ? D extends string ? {
|
|
20
|
-
[K in keyof U]: InternalValueTypeOf<U[K], Extra> & Readonly<Record<D, K>>;
|
|
21
|
-
}[keyof U] : {
|
|
22
|
-
[K in keyof U]: InternalValueTypeOf<U[K], Extra>;
|
|
23
|
-
}[keyof U] : never;
|
|
24
|
-
export {};
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { flattenTypeDefsOf } from 'transformers/flatteners/flatten_type_defs_of'
|
|
2
|
-
import {
|
|
3
|
-
booleanType,
|
|
4
|
-
list,
|
|
5
|
-
numberType,
|
|
6
|
-
object,
|
|
7
|
-
} from 'types/builders'
|
|
8
|
-
|
|
9
|
-
describe('flattenTypeDefsOf', function () {
|
|
10
|
-
it('flattens', function () {
|
|
11
|
-
const listTypeDef = list(numberType)
|
|
12
|
-
const structTypeDef = object()
|
|
13
|
-
.set('a', listTypeDef)
|
|
14
|
-
.set('b', booleanType)
|
|
15
|
-
const flattened = flattenTypeDefsOf(
|
|
16
|
-
structTypeDef,
|
|
17
|
-
)
|
|
18
|
-
|
|
19
|
-
expect(flattened).toEqual({
|
|
20
|
-
$: structTypeDef.narrow,
|
|
21
|
-
'$.a': listTypeDef.narrow,
|
|
22
|
-
'$.a.*': numberType.narrow,
|
|
23
|
-
'$.b': booleanType.narrow,
|
|
24
|
-
})
|
|
25
|
-
})
|
|
26
|
-
})
|