@strictly/define 0.0.22 → 0.0.24
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/transformers/flatteners/flatten_type_to.js +1 -1
- package/.out/transformers/flatteners/flatten_value_to.js +9 -9
- package/.out/transformers/flatteners/json_path.d.ts +1 -1
- package/.out/transformers/flatteners/json_path.js +2 -2
- package/.out/transformers/flatteners/specs/flatten_json_value_to_type_paths_of.tests.js +2 -2
- package/.out/transformers/flatteners/specs/flatten_type_to.tests.js +2 -2
- package/.out/transformers/flatteners/specs/flatten_value_to.tests.js +2 -2
- package/.out/transformers/flatteners/specs/json_paths.tests.js +2 -2
- package/.out/transformers/flatteners/specs/value_path_to_type_path.tests.js +82 -8
- package/.out/transformers/flatteners/value_path_to_type_path.js +26 -25
- package/.out/tsconfig.tsbuildinfo +1 -1
- package/.out/types/flattened_types_of_type.d.ts +11 -11
- package/.out/types/flattened_types_of_validating_type.d.ts +11 -11
- package/.out/types/paths_of_type.d.ts +7 -7
- package/.out/types/value_to_type_paths_of_type.d.ts +7 -7
- package/.turbo/turbo-build.log +8 -8
- package/.turbo/turbo-release$colon$exports.log +1 -1
- package/dist/index.cjs +52 -40
- package/dist/index.d.cts +37 -37
- package/dist/index.d.ts +37 -37
- package/dist/index.js +52 -40
- package/package.json +1 -1
- package/transformers/flatteners/flatten_type_to.ts +2 -2
- package/transformers/flatteners/flatten_value_to.ts +8 -12
- package/transformers/flatteners/json_path.ts +3 -3
- package/transformers/flatteners/specs/flatten_json_value_to_type_paths_of.tests.ts +2 -2
- package/transformers/flatteners/specs/flatten_type_to.tests.ts +2 -2
- package/transformers/flatteners/specs/flatten_value_to.tests.ts +2 -2
- package/transformers/flatteners/specs/json_paths.tests.ts +3 -3
- package/transformers/flatteners/specs/value_path_to_type_path.tests.ts +92 -8
- package/transformers/flatteners/value_path_to_type_path.ts +37 -23
- package/types/flattened_types_of_type.ts +7 -18
- package/types/flattened_types_of_validating_type.ts +7 -19
- package/types/paths_of_type.ts +16 -22
- package/types/specs/flattened_types_of_type.tests.ts +6 -6
- package/types/specs/paths_of_type.tests.ts +2 -2
- package/types/specs/value_to_type_paths_of_type.tests.ts +4 -4
- package/types/value_to_type_paths_of_type.ts +5 -12
|
@@ -194,20 +194,20 @@ describe('valuePathToTypePath', function () {
|
|
|
194
194
|
'$',
|
|
195
195
|
],
|
|
196
196
|
[
|
|
197
|
-
'
|
|
198
|
-
'
|
|
197
|
+
'$:x.a',
|
|
198
|
+
'$:x.a',
|
|
199
199
|
],
|
|
200
200
|
[
|
|
201
|
-
'
|
|
202
|
-
'
|
|
201
|
+
'$:x.b',
|
|
202
|
+
'$:x.b',
|
|
203
203
|
],
|
|
204
204
|
[
|
|
205
|
-
'
|
|
206
|
-
'
|
|
205
|
+
'$:y.b',
|
|
206
|
+
'$:y.b',
|
|
207
207
|
],
|
|
208
208
|
[
|
|
209
|
-
'
|
|
210
|
-
'
|
|
209
|
+
'$:y.c',
|
|
210
|
+
'$:y.c',
|
|
211
211
|
],
|
|
212
212
|
] as const)('it maps %s', function (from, to) {
|
|
213
213
|
const typePath = valuePathToTypePath<Paths, typeof from>(typeDef, from)
|
|
@@ -237,6 +237,90 @@ describe('valuePathToTypePath', function () {
|
|
|
237
237
|
expectTypeOf(fakeTypePath).toEqualTypeOf<'$.fake'>()
|
|
238
238
|
})
|
|
239
239
|
})
|
|
240
|
+
|
|
241
|
+
describe('nested discriminated', () => {
|
|
242
|
+
const nestedTypeDef = object().field('o', typeDef)
|
|
243
|
+
type Paths = ValueToTypePathsOfType<typeof nestedTypeDef>
|
|
244
|
+
|
|
245
|
+
describe.each([
|
|
246
|
+
[
|
|
247
|
+
'$',
|
|
248
|
+
'$',
|
|
249
|
+
],
|
|
250
|
+
[
|
|
251
|
+
'$.o',
|
|
252
|
+
'$.o',
|
|
253
|
+
],
|
|
254
|
+
[
|
|
255
|
+
'$.o:x.a',
|
|
256
|
+
'$.o:x.a',
|
|
257
|
+
],
|
|
258
|
+
[
|
|
259
|
+
'$.o:x.b',
|
|
260
|
+
'$.o:x.b',
|
|
261
|
+
],
|
|
262
|
+
[
|
|
263
|
+
'$.o:y.b',
|
|
264
|
+
'$.o:y.b',
|
|
265
|
+
],
|
|
266
|
+
[
|
|
267
|
+
'$.o:y.c',
|
|
268
|
+
'$.o:y.c',
|
|
269
|
+
],
|
|
270
|
+
] as const)('it maps %s', function (from, to) {
|
|
271
|
+
const typePath = valuePathToTypePath<Paths, typeof from>(nestedTypeDef, from)
|
|
272
|
+
|
|
273
|
+
it('maps a value path to the expected type path', function () {
|
|
274
|
+
expect(typePath).toEqual(to)
|
|
275
|
+
})
|
|
276
|
+
|
|
277
|
+
it('has expected type', function () {
|
|
278
|
+
expectTypeOf(typePath).toEqualTypeOf(to)
|
|
279
|
+
})
|
|
280
|
+
})
|
|
281
|
+
})
|
|
282
|
+
|
|
283
|
+
describe('list discriminated', () => {
|
|
284
|
+
const listTypeDef = list(typeDef)
|
|
285
|
+
type Paths = ValueToTypePathsOfType<typeof listTypeDef>
|
|
286
|
+
|
|
287
|
+
describe.each([
|
|
288
|
+
[
|
|
289
|
+
'$',
|
|
290
|
+
'$',
|
|
291
|
+
],
|
|
292
|
+
[
|
|
293
|
+
'$.0',
|
|
294
|
+
'$.*',
|
|
295
|
+
],
|
|
296
|
+
[
|
|
297
|
+
'$.0:x.a',
|
|
298
|
+
'$.*:x.a',
|
|
299
|
+
],
|
|
300
|
+
[
|
|
301
|
+
'$.0:x.b',
|
|
302
|
+
'$.*:x.b',
|
|
303
|
+
],
|
|
304
|
+
[
|
|
305
|
+
'$.99:y.b',
|
|
306
|
+
'$.*:y.b',
|
|
307
|
+
],
|
|
308
|
+
[
|
|
309
|
+
'$.1:y.c',
|
|
310
|
+
'$.*:y.c',
|
|
311
|
+
],
|
|
312
|
+
] as const)('it maps %s', function (from, to) {
|
|
313
|
+
const typePath = valuePathToTypePath<Paths, typeof from>(listTypeDef, from)
|
|
314
|
+
|
|
315
|
+
it('maps a value path to the expected type path', function () {
|
|
316
|
+
expect(typePath).toEqual(to)
|
|
317
|
+
})
|
|
318
|
+
|
|
319
|
+
it('has expected type', function () {
|
|
320
|
+
expectTypeOf(typePath).toEqualTypeOf(to)
|
|
321
|
+
})
|
|
322
|
+
})
|
|
323
|
+
})
|
|
240
324
|
})
|
|
241
325
|
})
|
|
242
326
|
})
|
|
@@ -22,22 +22,29 @@ export function valuePathToTypePath<
|
|
|
22
22
|
): ValuePathsToTypePaths[ValuePath] {
|
|
23
23
|
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
24
24
|
const valueSteps = (valuePath as string).split(/\.|\[/g)
|
|
25
|
-
|
|
25
|
+
const parts = valueSteps[0].split(':')
|
|
26
|
+
const [
|
|
27
|
+
first,
|
|
28
|
+
...qualifiers
|
|
29
|
+
] = parts
|
|
30
|
+
assertEqual(first, '$')
|
|
26
31
|
|
|
27
32
|
const typeSteps = internalJsonValuePathToTypePath(
|
|
28
33
|
typeDef,
|
|
34
|
+
qualifiers,
|
|
29
35
|
valueSteps.slice(1),
|
|
30
36
|
allowMissingPaths,
|
|
31
37
|
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
32
38
|
valuePath as string,
|
|
33
39
|
)
|
|
34
|
-
typeSteps.unshift(
|
|
40
|
+
typeSteps.unshift(valueSteps[0])
|
|
35
41
|
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
36
42
|
return typeSteps.join('.') as ValuePathsToTypePaths[ValuePath]
|
|
37
43
|
}
|
|
38
44
|
|
|
39
45
|
function internalJsonValuePathToTypePath(
|
|
40
46
|
typeDef: TypeDef,
|
|
47
|
+
qualifiers: string[],
|
|
41
48
|
valueSteps: string[],
|
|
42
49
|
allowMissingPaths: boolean,
|
|
43
50
|
originalValuePath: string,
|
|
@@ -46,9 +53,14 @@ function internalJsonValuePathToTypePath(
|
|
|
46
53
|
return []
|
|
47
54
|
}
|
|
48
55
|
const [
|
|
49
|
-
|
|
56
|
+
nextValueStepAndQualifiersString,
|
|
50
57
|
...remainingValueSteps
|
|
51
58
|
] = valueSteps
|
|
59
|
+
const nextValueStepAndQualifiers = nextValueStepAndQualifiersString.split(':')
|
|
60
|
+
const [
|
|
61
|
+
valueStep,
|
|
62
|
+
...nextQualifiers
|
|
63
|
+
] = nextValueStepAndQualifiers
|
|
52
64
|
switch (typeDef.type) {
|
|
53
65
|
case TypeDefType.Literal:
|
|
54
66
|
if (allowMissingPaths) {
|
|
@@ -58,15 +70,19 @@ function internalJsonValuePathToTypePath(
|
|
|
58
70
|
throw new PreconditionFailedError(
|
|
59
71
|
'literal should terminate path {} ({})',
|
|
60
72
|
originalValuePath,
|
|
61
|
-
|
|
73
|
+
nextValueStepAndQualifiersString,
|
|
62
74
|
)
|
|
63
75
|
}
|
|
64
76
|
case TypeDefType.List:
|
|
65
77
|
// TODO assert format of current step
|
|
66
78
|
return [
|
|
67
|
-
|
|
79
|
+
[
|
|
80
|
+
'*',
|
|
81
|
+
...nextQualifiers,
|
|
82
|
+
].join(':'),
|
|
68
83
|
...internalJsonValuePathToTypePath(
|
|
69
84
|
typeDef.elements,
|
|
85
|
+
nextQualifiers,
|
|
70
86
|
remainingValueSteps,
|
|
71
87
|
allowMissingPaths,
|
|
72
88
|
originalValuePath,
|
|
@@ -74,9 +90,13 @@ function internalJsonValuePathToTypePath(
|
|
|
74
90
|
]
|
|
75
91
|
case TypeDefType.Record:
|
|
76
92
|
return [
|
|
77
|
-
|
|
93
|
+
[
|
|
94
|
+
'*',
|
|
95
|
+
...nextQualifiers,
|
|
96
|
+
].join(':'),
|
|
78
97
|
...internalJsonValuePathToTypePath(
|
|
79
98
|
typeDef.valueTypeDef,
|
|
99
|
+
nextQualifiers,
|
|
80
100
|
remainingValueSteps,
|
|
81
101
|
allowMissingPaths,
|
|
82
102
|
originalValuePath,
|
|
@@ -92,9 +112,10 @@ function internalJsonValuePathToTypePath(
|
|
|
92
112
|
assertExists(typeDef.fields[valueStep], 'missing field in {} ({})', originalValuePath, valueStep)
|
|
93
113
|
}
|
|
94
114
|
return [
|
|
95
|
-
|
|
115
|
+
nextValueStepAndQualifiersString,
|
|
96
116
|
...internalJsonValuePathToTypePath(
|
|
97
117
|
typeDef.fields[valueStep],
|
|
118
|
+
nextQualifiers,
|
|
98
119
|
remainingValueSteps,
|
|
99
120
|
allowMissingPaths,
|
|
100
121
|
originalValuePath,
|
|
@@ -117,6 +138,7 @@ function internalJsonValuePathToTypePath(
|
|
|
117
138
|
assertExists(union, 'expected a complex union {}', originalValuePath)
|
|
118
139
|
return internalJsonValuePathToTypePath(
|
|
119
140
|
union,
|
|
141
|
+
nextQualifiers,
|
|
120
142
|
valueSteps,
|
|
121
143
|
allowMissingPaths,
|
|
122
144
|
originalValuePath,
|
|
@@ -126,8 +148,7 @@ function internalJsonValuePathToTypePath(
|
|
|
126
148
|
return []
|
|
127
149
|
}
|
|
128
150
|
} else {
|
|
129
|
-
|
|
130
|
-
if (qualifierIndex < 0) {
|
|
151
|
+
if (qualifiers.length === 0) {
|
|
131
152
|
if (allowMissingPaths) {
|
|
132
153
|
return valueSteps
|
|
133
154
|
} else {
|
|
@@ -138,25 +159,18 @@ function internalJsonValuePathToTypePath(
|
|
|
138
159
|
)
|
|
139
160
|
}
|
|
140
161
|
}
|
|
141
|
-
const qualifier = valueStep.substring(0, qualifierIndex)
|
|
142
|
-
const remainder = valueStep.substring(qualifierIndex + 1)
|
|
143
|
-
const union = assertExistsAndReturn(typeDef.unions[qualifier], 'missing union {}', qualifier)
|
|
144
162
|
const [
|
|
145
|
-
|
|
146
|
-
...
|
|
147
|
-
] =
|
|
163
|
+
qualifier,
|
|
164
|
+
...remainingQualifiers
|
|
165
|
+
] = qualifiers
|
|
166
|
+
const union = assertExistsAndReturn(typeDef.unions[qualifier], 'missing union {}', qualifier)
|
|
167
|
+
return internalJsonValuePathToTypePath(
|
|
148
168
|
union,
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
...remainingValueSteps,
|
|
152
|
-
],
|
|
169
|
+
remainingQualifiers,
|
|
170
|
+
valueSteps,
|
|
153
171
|
allowMissingPaths,
|
|
154
172
|
originalValuePath,
|
|
155
173
|
)
|
|
156
|
-
return [
|
|
157
|
-
`${qualifier}:${typeStep}`,
|
|
158
|
-
...remainingTypeSteps,
|
|
159
|
-
]
|
|
160
174
|
}
|
|
161
175
|
default:
|
|
162
176
|
throw new UnreachableError(typeDef)
|
|
@@ -23,25 +23,23 @@ export type FlattenedTypesOfType<
|
|
|
23
23
|
SegmentOverride extends string | null,
|
|
24
24
|
Path extends string = '$',
|
|
25
25
|
Depth extends number = StartingDepth,
|
|
26
|
-
> = InternalFlattenedTypeDefsOf<T['definition'], SegmentOverride, Path,
|
|
26
|
+
> = InternalFlattenedTypeDefsOf<T['definition'], SegmentOverride, Path, Depth>
|
|
27
27
|
|
|
28
28
|
type InternalFlattenedTypeDefsOf<
|
|
29
29
|
T extends TypeDef,
|
|
30
30
|
SegmentOverride extends string | null,
|
|
31
31
|
Path extends string,
|
|
32
|
-
Qualifier extends string,
|
|
33
32
|
Depth extends number,
|
|
34
33
|
> =
|
|
35
34
|
& {
|
|
36
35
|
readonly [K in Path]: Type<T>
|
|
37
36
|
}
|
|
38
|
-
& InternalFlattenedTypeDefsOfChildren<T, SegmentOverride, Path,
|
|
37
|
+
& InternalFlattenedTypeDefsOfChildren<T, SegmentOverride, Path, Depth>
|
|
39
38
|
|
|
40
39
|
type InternalFlattenedTypeDefsOfChildren<
|
|
41
40
|
T extends TypeDef,
|
|
42
41
|
SegmentOverride extends string | null,
|
|
43
42
|
Path extends string,
|
|
44
|
-
Qualifier extends string,
|
|
45
43
|
Depth extends number,
|
|
46
44
|
NextDepth extends number = Depths[Depth],
|
|
47
45
|
> =
|
|
@@ -51,9 +49,8 @@ type InternalFlattenedTypeDefsOfChildren<
|
|
|
51
49
|
: T extends LiteralTypeDef ? InternalFlattenedTypeDefsOfLiteralChildren
|
|
52
50
|
: T extends ListTypeDef ? InternalFlattenedTypeDefsOfListChildren<T, SegmentOverride, Path, NextDepth>
|
|
53
51
|
: T extends RecordTypeDef ? InternalFlattenedTypeDefsOfRecordChildren<T, SegmentOverride, Path, NextDepth>
|
|
54
|
-
: T extends ObjectTypeDef
|
|
55
|
-
|
|
56
|
-
: T extends UnionTypeDef ? InternalFlattenedTypeDefsOfUnionChildren<T, SegmentOverride, Path, Qualifier, NextDepth>
|
|
52
|
+
: T extends ObjectTypeDef ? InternalFlattenedTypeDefsOfObjectChildren<T, SegmentOverride, Path, NextDepth>
|
|
53
|
+
: T extends UnionTypeDef ? InternalFlattenedTypeDefsOfUnionChildren<T, SegmentOverride, Path, NextDepth>
|
|
57
54
|
: never
|
|
58
55
|
|
|
59
56
|
type InternalFlattenedTypeDefsOfLiteralChildren = {}
|
|
@@ -67,7 +64,6 @@ type InternalFlattenedTypeDefsOfListChildren<
|
|
|
67
64
|
T['elements'],
|
|
68
65
|
SegmentOverride,
|
|
69
66
|
PathOf<Path, number, SegmentOverride>,
|
|
70
|
-
'',
|
|
71
67
|
Depth
|
|
72
68
|
>
|
|
73
69
|
|
|
@@ -80,7 +76,6 @@ type InternalFlattenedTypeDefsOfRecordChildren<
|
|
|
80
76
|
T['valueTypeDef'],
|
|
81
77
|
SegmentOverride,
|
|
82
78
|
PathOf<Path, T['keyPrototype'], SegmentOverride>,
|
|
83
|
-
'',
|
|
84
79
|
Depth
|
|
85
80
|
>
|
|
86
81
|
|
|
@@ -88,7 +83,6 @@ type InternalFlattenedTypeDefsOfObjectChildren<
|
|
|
88
83
|
T extends ObjectTypeDef,
|
|
89
84
|
SegmentOverride extends string | null,
|
|
90
85
|
Path extends string,
|
|
91
|
-
Qualifier extends string,
|
|
92
86
|
Depth extends number,
|
|
93
87
|
> = T extends ObjectTypeDef<infer Fields> ? keyof Fields extends string ? UnionToIntersection<{
|
|
94
88
|
readonly [K in keyof Fields]-?: undefined extends Fields[K] ? InternalFlattenedTypeDefsOf<
|
|
@@ -97,15 +91,13 @@ type InternalFlattenedTypeDefsOfObjectChildren<
|
|
|
97
91
|
readonly '1': LiteralTypeDef<undefined>,
|
|
98
92
|
}>,
|
|
99
93
|
SegmentOverride,
|
|
100
|
-
PathOf<Path,
|
|
101
|
-
'',
|
|
94
|
+
PathOf<Path, K, null>,
|
|
102
95
|
Depth
|
|
103
96
|
>
|
|
104
97
|
: InternalFlattenedTypeDefsOf<
|
|
105
98
|
Exclude<Fields[K], undefined>,
|
|
106
99
|
SegmentOverride,
|
|
107
|
-
PathOf<Path,
|
|
108
|
-
'',
|
|
100
|
+
PathOf<Path, K, null>,
|
|
109
101
|
Depth
|
|
110
102
|
>
|
|
111
103
|
}[keyof Fields]>
|
|
@@ -116,14 +108,12 @@ type InternalFlattenedTypeDefsOfUnionChildren<
|
|
|
116
108
|
T extends UnionTypeDef,
|
|
117
109
|
SegmentOverride extends string | null,
|
|
118
110
|
Path extends string,
|
|
119
|
-
Qualifier extends string,
|
|
120
111
|
Depth extends number,
|
|
121
112
|
> = T extends UnionTypeDef<infer D, infer Unions> ? keyof Unions extends string ? D extends null ? UnionToIntersection<{
|
|
122
113
|
readonly [K in keyof Unions]: InternalFlattenedTypeDefsOfChildren<
|
|
123
114
|
Unions[K],
|
|
124
115
|
SegmentOverride,
|
|
125
116
|
Path,
|
|
126
|
-
'',
|
|
127
117
|
Depth
|
|
128
118
|
>
|
|
129
119
|
}[keyof Unions]>
|
|
@@ -131,8 +121,7 @@ type InternalFlattenedTypeDefsOfUnionChildren<
|
|
|
131
121
|
readonly [K in keyof Unions]: InternalFlattenedTypeDefsOfChildren<
|
|
132
122
|
Unions[K],
|
|
133
123
|
SegmentOverride,
|
|
134
|
-
Path
|
|
135
|
-
`${Qualifier}${K}:`,
|
|
124
|
+
`${Path}:${K}`,
|
|
136
125
|
Depth
|
|
137
126
|
>
|
|
138
127
|
}[keyof Unions]>
|
|
@@ -26,25 +26,23 @@ export type FlattenedTypesOfValidatingType<
|
|
|
26
26
|
SegmentOverride extends string | null,
|
|
27
27
|
Path extends string = '$',
|
|
28
28
|
Depth extends number = StartingDepth,
|
|
29
|
-
> = InternalFlattenedTypeDefsOf<T['definition'], SegmentOverride, Path,
|
|
29
|
+
> = InternalFlattenedTypeDefsOf<T['definition'], SegmentOverride, Path, Depth>
|
|
30
30
|
|
|
31
31
|
type InternalFlattenedTypeDefsOf<
|
|
32
32
|
T extends TypeDef,
|
|
33
33
|
SegmentOverride extends string | null,
|
|
34
34
|
Path extends string,
|
|
35
|
-
Qualifier extends string,
|
|
36
35
|
Depth extends number,
|
|
37
36
|
> =
|
|
38
37
|
& {
|
|
39
38
|
readonly [K in Path]: Type<T>
|
|
40
39
|
}
|
|
41
|
-
& InternalFlattenedTypeDefsOfChildren<T, SegmentOverride, Path,
|
|
40
|
+
& InternalFlattenedTypeDefsOfChildren<T, SegmentOverride, Path, Depth>
|
|
42
41
|
|
|
43
42
|
type InternalFlattenedTypeDefsOfChildren<
|
|
44
43
|
T extends TypeDef,
|
|
45
44
|
SegmentOverride extends string | null,
|
|
46
45
|
Path extends string,
|
|
47
|
-
Qualifier extends string,
|
|
48
46
|
Depth extends number,
|
|
49
47
|
NextDepth extends number = Depths[Depth],
|
|
50
48
|
> =
|
|
@@ -54,10 +52,8 @@ type InternalFlattenedTypeDefsOfChildren<
|
|
|
54
52
|
: T extends ValidatingLiteralTypeDef ? InternalFlattenedTypeDefsOfLiteralChildren
|
|
55
53
|
: T extends ValidatingListTypeDef ? InternalFlattenedTypeDefsOfListChildren<T, SegmentOverride, Path, NextDepth>
|
|
56
54
|
: T extends ValidatingRecordTypeDef ? InternalFlattenedTypeDefsOfRecordChildren<T, SegmentOverride, Path, NextDepth>
|
|
57
|
-
: T extends ValidatingObjectTypeDef
|
|
58
|
-
|
|
59
|
-
: T extends ValidatingUnionTypeDef
|
|
60
|
-
? InternalFlattenedTypeDefsOfUnionChildren<T, SegmentOverride, Path, Qualifier, NextDepth>
|
|
55
|
+
: T extends ValidatingObjectTypeDef ? InternalFlattenedTypeDefsOfObjectChildren<T, SegmentOverride, Path, NextDepth>
|
|
56
|
+
: T extends ValidatingUnionTypeDef ? InternalFlattenedTypeDefsOfUnionChildren<T, SegmentOverride, Path, NextDepth>
|
|
61
57
|
: never
|
|
62
58
|
|
|
63
59
|
type InternalFlattenedTypeDefsOfLiteralChildren = {}
|
|
@@ -71,7 +67,6 @@ type InternalFlattenedTypeDefsOfListChildren<
|
|
|
71
67
|
T['elements'],
|
|
72
68
|
SegmentOverride,
|
|
73
69
|
PathOf<Path, number, SegmentOverride>,
|
|
74
|
-
'',
|
|
75
70
|
Depth
|
|
76
71
|
>
|
|
77
72
|
|
|
@@ -84,7 +79,6 @@ type InternalFlattenedTypeDefsOfRecordChildren<
|
|
|
84
79
|
T['valueTypeDef'],
|
|
85
80
|
SegmentOverride,
|
|
86
81
|
PathOf<Path, T['keyPrototype'], SegmentOverride>,
|
|
87
|
-
'',
|
|
88
82
|
Depth
|
|
89
83
|
>
|
|
90
84
|
|
|
@@ -92,7 +86,6 @@ type InternalFlattenedTypeDefsOfObjectChildren<
|
|
|
92
86
|
T extends ValidatingObjectTypeDef,
|
|
93
87
|
SegmentOverride extends string | null,
|
|
94
88
|
Path extends string,
|
|
95
|
-
Qualifier extends string,
|
|
96
89
|
Depth extends number,
|
|
97
90
|
> = T extends ValidatingObjectTypeDef<infer _E, infer _C, infer Fields>
|
|
98
91
|
? keyof Fields extends string ? UnionToIntersection<{
|
|
@@ -111,15 +104,13 @@ type InternalFlattenedTypeDefsOfObjectChildren<
|
|
|
111
104
|
}
|
|
112
105
|
>,
|
|
113
106
|
SegmentOverride,
|
|
114
|
-
PathOf<Path,
|
|
115
|
-
'',
|
|
107
|
+
PathOf<Path, K, null>,
|
|
116
108
|
Depth
|
|
117
109
|
>
|
|
118
110
|
: InternalFlattenedTypeDefsOf<
|
|
119
111
|
Exclude<Fields[K], undefined>,
|
|
120
112
|
SegmentOverride,
|
|
121
|
-
PathOf<Path,
|
|
122
|
-
'',
|
|
113
|
+
PathOf<Path, K, null>,
|
|
123
114
|
Depth
|
|
124
115
|
>
|
|
125
116
|
}[keyof Fields]>
|
|
@@ -130,7 +121,6 @@ type InternalFlattenedTypeDefsOfUnionChildren<
|
|
|
130
121
|
T extends ValidatingUnionTypeDef,
|
|
131
122
|
SegmentOverride extends string | null,
|
|
132
123
|
Path extends string,
|
|
133
|
-
Qualifier extends string,
|
|
134
124
|
Depth extends number,
|
|
135
125
|
> = T extends ValidatingUnionTypeDef<infer _E, infer _C, infer D, infer Unions>
|
|
136
126
|
? keyof Unions extends string ? D extends null ? UnionToIntersection<{
|
|
@@ -138,7 +128,6 @@ type InternalFlattenedTypeDefsOfUnionChildren<
|
|
|
138
128
|
Unions[K],
|
|
139
129
|
SegmentOverride,
|
|
140
130
|
Path,
|
|
141
|
-
'',
|
|
142
131
|
Depth
|
|
143
132
|
>
|
|
144
133
|
}[keyof Unions]>
|
|
@@ -146,8 +135,7 @@ type InternalFlattenedTypeDefsOfUnionChildren<
|
|
|
146
135
|
readonly [K in keyof Unions]: InternalFlattenedTypeDefsOfChildren<
|
|
147
136
|
Unions[K],
|
|
148
137
|
SegmentOverride,
|
|
149
|
-
Path
|
|
150
|
-
`${Qualifier}${K}:`,
|
|
138
|
+
`${Path}:${K}`,
|
|
151
139
|
Depth
|
|
152
140
|
>
|
|
153
141
|
}[keyof Unions]>
|
package/types/paths_of_type.ts
CHANGED
|
@@ -26,24 +26,22 @@ export type InternalJsonPathsOf<
|
|
|
26
26
|
// TODO maybe depth isn't necessary here?
|
|
27
27
|
Depth extends number,
|
|
28
28
|
NextDepth extends number = Depths[Depth],
|
|
29
|
-
> = InternalJsonPathsOfChildren<F, Prefix, SegmentOverride,
|
|
29
|
+
> = InternalJsonPathsOfChildren<F, Prefix, SegmentOverride, NextDepth> | Prefix
|
|
30
30
|
|
|
31
31
|
type InternalJsonPathsOfChildren<
|
|
32
32
|
F extends TypeDef,
|
|
33
33
|
Prefix extends string,
|
|
34
34
|
SegmentOverride extends string | null,
|
|
35
|
-
Qualifier extends string,
|
|
36
35
|
Depth extends number,
|
|
37
36
|
> = Depth extends -1 ? never
|
|
38
37
|
: F extends LiteralTypeDef ? InternalJsonPathsOfLiteralChildren
|
|
39
38
|
: F extends ListTypeDef ? InternalJsonPathsOfListChildren<F, Prefix, SegmentOverride, Depth>
|
|
40
39
|
: F extends RecordTypeDef ? InternalJsonPathsOfRecordChildren<F, Prefix, SegmentOverride, Depth>
|
|
41
|
-
: F extends ObjectTypeDef ? InternalJsonPathsOfObjectChildren<F, Prefix, SegmentOverride,
|
|
40
|
+
: F extends ObjectTypeDef ? InternalJsonPathsOfObjectChildren<F, Prefix, SegmentOverride, Depth>
|
|
42
41
|
: F extends UnionTypeDef ? InternalJsonPathsOfUnionChildren<
|
|
43
42
|
F,
|
|
44
43
|
Prefix,
|
|
45
44
|
SegmentOverride,
|
|
46
|
-
Qualifier,
|
|
47
45
|
Depth
|
|
48
46
|
>
|
|
49
47
|
: never
|
|
@@ -86,28 +84,25 @@ type InternalJsonPathsOfObjectChildren<
|
|
|
86
84
|
F extends ObjectTypeDef,
|
|
87
85
|
Prefix extends string,
|
|
88
86
|
SegmentOverride extends string | null,
|
|
89
|
-
Qualifier extends string,
|
|
90
87
|
Depth extends number,
|
|
91
|
-
> = F extends ObjectTypeDef<infer Fields> ?
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
: never
|
|
88
|
+
> = F extends ObjectTypeDef<infer Fields> ? {
|
|
89
|
+
[K in keyof Fields]: InternalJsonPathsOf<
|
|
90
|
+
Fields[K],
|
|
91
|
+
PathOf<
|
|
92
|
+
Prefix,
|
|
93
|
+
K,
|
|
94
|
+
null
|
|
95
|
+
>,
|
|
96
|
+
SegmentOverride,
|
|
97
|
+
Depth
|
|
98
|
+
>
|
|
99
|
+
}[keyof Fields]
|
|
104
100
|
: never
|
|
105
101
|
|
|
106
102
|
type InternalJsonPathsOfUnionChildren<
|
|
107
103
|
F extends UnionTypeDef,
|
|
108
104
|
Prefix extends string,
|
|
109
105
|
SegmentOverride extends string | null,
|
|
110
|
-
Qualifier extends string,
|
|
111
106
|
Depth extends number,
|
|
112
107
|
> // typescript cannot infer the key in unions unless we get the value directly
|
|
113
108
|
= F extends UnionTypeDef<infer D, infer Unions> ? keyof Unions extends string ? {
|
|
@@ -117,13 +112,12 @@ type InternalJsonPathsOfUnionChildren<
|
|
|
117
112
|
// I think what we should do is have a "denormalize of", which you can then
|
|
118
113
|
// get the paths of if you want the unique paths
|
|
119
114
|
// (alt PrefixOf<Prefix, K>,)
|
|
120
|
-
Prefix
|
|
115
|
+
D extends null ? Prefix : `${Prefix}:${K}`,
|
|
121
116
|
SegmentOverride,
|
|
122
|
-
D extends null ? Qualifier : `${Qualifier}${K}:`,
|
|
123
117
|
Depth
|
|
124
118
|
>
|
|
125
119
|
}[keyof Unions]
|
|
126
120
|
// do not expose the discriminator as we cannot set this value independently
|
|
127
|
-
// | (D extends string ? JsonPathOf<Prefix, D, null
|
|
121
|
+
// | (D extends string ? JsonPathOf<Prefix, D, null> : never)
|
|
128
122
|
: never
|
|
129
123
|
: never
|
|
@@ -192,13 +192,13 @@ describe('union', function () {
|
|
|
192
192
|
|
|
193
193
|
type C = {
|
|
194
194
|
readonly $: SimplifyDeep<typeof builder._type>,
|
|
195
|
-
readonly ['
|
|
195
|
+
readonly ['$:1.a']: {
|
|
196
196
|
readonly definition: {
|
|
197
197
|
readonly type: TypeDefType.Literal,
|
|
198
198
|
readonly valuePrototype: [boolean],
|
|
199
199
|
},
|
|
200
200
|
},
|
|
201
|
-
readonly ['
|
|
201
|
+
readonly ['$:2.a']: {
|
|
202
202
|
readonly definition: {
|
|
203
203
|
readonly type: TypeDefType.Literal,
|
|
204
204
|
readonly valuePrototype: [number],
|
|
@@ -227,25 +227,25 @@ describe('union', function () {
|
|
|
227
227
|
type T = SimplifyDeep<FlattenedTypesOfType<typeof builder._type, null>>
|
|
228
228
|
type C = {
|
|
229
229
|
readonly $: SimplifyDeep<typeof builder._type>,
|
|
230
|
-
readonly ['
|
|
230
|
+
readonly ['$:1:p.a']: {
|
|
231
231
|
readonly definition: {
|
|
232
232
|
readonly type: TypeDefType.Literal,
|
|
233
233
|
readonly valuePrototype: [boolean],
|
|
234
234
|
},
|
|
235
235
|
},
|
|
236
|
-
readonly ['
|
|
236
|
+
readonly ['$:1:q.a']: {
|
|
237
237
|
readonly definition: {
|
|
238
238
|
readonly type: TypeDefType.Literal,
|
|
239
239
|
readonly valuePrototype: [string],
|
|
240
240
|
},
|
|
241
241
|
},
|
|
242
|
-
readonly ['
|
|
242
|
+
readonly ['$:2:r.b']: {
|
|
243
243
|
readonly definition: {
|
|
244
244
|
readonly type: TypeDefType.Literal,
|
|
245
245
|
readonly valuePrototype: [number],
|
|
246
246
|
},
|
|
247
247
|
},
|
|
248
|
-
readonly ['
|
|
248
|
+
readonly ['$:2:s.c']: {
|
|
249
249
|
readonly definition: {
|
|
250
250
|
readonly type: TypeDefType.Literal,
|
|
251
251
|
readonly valuePrototype: [string],
|
|
@@ -263,7 +263,7 @@ describe('PathsOfType', function () {
|
|
|
263
263
|
|
|
264
264
|
type T = PathsOfType<typeof builder>
|
|
265
265
|
|
|
266
|
-
type P = '$' | '
|
|
266
|
+
type P = '$' | '$:1.a' | '$:2.b'
|
|
267
267
|
|
|
268
268
|
it('equals expected type', function () {
|
|
269
269
|
expectTypeOf<P>().toEqualTypeOf<T>()
|
|
@@ -283,7 +283,7 @@ describe('PathsOfType', function () {
|
|
|
283
283
|
|
|
284
284
|
type T = PathsOfType<typeof builder>
|
|
285
285
|
|
|
286
|
-
type P = '$' | '
|
|
286
|
+
type P = '$' | '$:1:p.a' | '$:2:q.b'
|
|
287
287
|
|
|
288
288
|
it('equals expected type', function () {
|
|
289
289
|
expectTypeOf<P>().toEqualTypeOf<T>()
|
|
@@ -119,10 +119,10 @@ describe('ValueToTypePathsOfType', function () {
|
|
|
119
119
|
|
|
120
120
|
type C = {
|
|
121
121
|
readonly $: '$',
|
|
122
|
-
readonly ['
|
|
123
|
-
readonly ['
|
|
124
|
-
readonly ['
|
|
125
|
-
readonly ['
|
|
122
|
+
readonly ['$:1.a']: '$:1.a',
|
|
123
|
+
readonly ['$:1.b']: '$:1.b',
|
|
124
|
+
readonly ['$:2.x']: '$:2.x',
|
|
125
|
+
readonly ['$:2.y']: '$:2.y',
|
|
126
126
|
}
|
|
127
127
|
it('equals expected type', function () {
|
|
128
128
|
expectTypeOf<C>().toEqualTypeOf<T>()
|