@strictly/define 0.0.1
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/.eslintrc.cjs +31 -0
- package/.out/index.d.ts +20 -0
- package/.out/index.js +20 -0
- package/.out/transformers/copies/copy.d.ts +4 -0
- package/.out/transformers/copies/copy.js +7 -0
- package/.out/transformers/copies/copy_to.d.ts +6 -0
- package/.out/transformers/copies/copy_to.js +91 -0
- package/.out/transformers/copies/mobx_copy.d.ts +5 -0
- package/.out/transformers/copies/mobx_copy.js +42 -0
- package/.out/transformers/copies/specs/copy_to.tests.d.ts +1 -0
- package/.out/transformers/copies/specs/copy_to.tests.js +97 -0
- package/.out/transformers/copies/specs/mobx_copy.tests.d.ts +1 -0
- package/.out/transformers/copies/specs/mobx_copy.tests.js +19 -0
- package/.out/transformers/flatteners/flatten_accessors_of.d.ts +5 -0
- package/.out/transformers/flatteners/flatten_accessors_of.js +11 -0
- package/.out/transformers/flatteners/flatten_json_value_to_type_paths_of.d.ts +3 -0
- package/.out/transformers/flatteners/flatten_json_value_to_type_paths_of.js +10 -0
- package/.out/transformers/flatteners/flatten_type_def_to.d.ts +4 -0
- package/.out/transformers/flatteners/flatten_type_def_to.js +49 -0
- package/.out/transformers/flatteners/flatten_type_defs_of.d.ts +3 -0
- package/.out/transformers/flatteners/flatten_type_defs_of.js +7 -0
- package/.out/transformers/flatteners/flatten_value_type_to.d.ts +11 -0
- package/.out/transformers/flatteners/flatten_value_type_to.js +79 -0
- package/.out/transformers/flatteners/flatten_value_types_of.d.ts +3 -0
- package/.out/transformers/flatteners/flatten_value_types_of.js +7 -0
- package/.out/transformers/flatteners/json_path.d.ts +2 -0
- package/.out/transformers/flatteners/json_path.js +14 -0
- package/.out/transformers/flatteners/specs/flatten_accessors_of.tests.d.ts +1 -0
- package/.out/transformers/flatteners/specs/flatten_accessors_of.tests.js +90 -0
- package/.out/transformers/flatteners/specs/flatten_json_value_to_type_paths_of.tests.d.ts +1 -0
- package/.out/transformers/flatteners/specs/flatten_json_value_to_type_paths_of.tests.js +94 -0
- package/.out/transformers/flatteners/specs/flatten_type_def_to.tests.d.ts +1 -0
- package/.out/transformers/flatteners/specs/flatten_type_def_to.tests.js +110 -0
- package/.out/transformers/flatteners/specs/flatten_type_defs_of.tests.d.ts +1 -0
- package/.out/transformers/flatteners/specs/flatten_type_defs_of.tests.js +17 -0
- package/.out/transformers/flatteners/specs/flatten_value_type_to.tests.d.ts +1 -0
- package/.out/transformers/flatteners/specs/flatten_value_type_to.tests.js +297 -0
- package/.out/transformers/flatteners/specs/flatten_value_types_of.tests.d.ts +1 -0
- package/.out/transformers/flatteners/specs/flatten_value_types_of.tests.js +37 -0
- package/.out/transformers/flatteners/specs/value_path_to_type_path.tests.d.ts +1 -0
- package/.out/transformers/flatteners/specs/value_path_to_type_path.tests.js +167 -0
- package/.out/transformers/flatteners/value_path_to_type_path.d.ts +2 -0
- package/.out/transformers/flatteners/value_path_to_type_path.js +96 -0
- package/.out/tsconfig.json +15 -0
- package/.out/tsconfig.tsbuildinfo +1 -0
- package/.out/tsup.config.d.ts +3 -0
- package/.out/tsup.config.js +12 -0
- package/.out/types/builders.d.ts +62 -0
- package/.out/types/builders.js +148 -0
- package/.out/types/definitions.d.ts +41 -0
- package/.out/types/definitions.js +26 -0
- package/.out/types/flattened.d.ts +2 -0
- package/.out/types/flattened.js +1 -0
- package/.out/types/flattened_accessors_of.d.ts +9 -0
- package/.out/types/flattened_accessors_of.js +1 -0
- package/.out/types/flattened_type_defs_of.d.ts +21 -0
- package/.out/types/flattened_type_defs_of.js +1 -0
- package/.out/types/flattened_value_types_of.d.ts +6 -0
- package/.out/types/flattened_value_types_of.js +1 -0
- package/.out/types/json_path_of.d.ts +1 -0
- package/.out/types/json_path_of.js +1 -0
- package/.out/types/json_paths_of.d.ts +16 -0
- package/.out/types/json_paths_of.js +1 -0
- package/.out/types/mobx_value_type_of.d.ts +11 -0
- package/.out/types/mobx_value_type_of.js +2 -0
- package/.out/types/partial_type_def_of.d.ts +40 -0
- package/.out/types/partial_type_def_of.js +1 -0
- package/.out/types/readonly_type_def_of.d.ts +29 -0
- package/.out/types/readonly_type_def_of.js +1 -0
- package/.out/types/specs/builder.tests.d.ts +1 -0
- package/.out/types/specs/builder.tests.js +93 -0
- package/.out/types/specs/flattened_accessors_of.tests.d.ts +1 -0
- package/.out/types/specs/flattened_accessors_of.tests.js +11 -0
- package/.out/types/specs/flattened_type_defs_of.tests.d.ts +1 -0
- package/.out/types/specs/flattened_type_defs_of.tests.js +71 -0
- package/.out/types/specs/flattened_value_types_of.tests.d.ts +1 -0
- package/.out/types/specs/flattened_value_types_of.tests.js +11 -0
- package/.out/types/specs/json_paths_of.tests.d.ts +1 -0
- package/.out/types/specs/json_paths_of.tests.js +199 -0
- package/.out/types/specs/partial_type_def_of.tests.d.ts +1 -0
- package/.out/types/specs/partial_type_def_of.tests.js +50 -0
- package/.out/types/specs/readonly_type_def_of.tests.d.ts +1 -0
- package/.out/types/specs/readonly_type_def_of.tests.js +55 -0
- package/.out/types/specs/strict_definitions.tests.d.ts +1 -0
- package/.out/types/specs/strict_definitions.tests.js +42 -0
- package/.out/types/specs/value_to_type_paths_of.tests.d.ts +1 -0
- package/.out/types/specs/value_to_type_paths_of.tests.js +90 -0
- package/.out/types/specs/value_type_of.tests.d.ts +1 -0
- package/.out/types/specs/value_type_of.tests.js +91 -0
- package/.out/types/specs/value_types_of_discriminated_union.tests.d.ts +1 -0
- package/.out/types/specs/value_types_of_discriminated_union.tests.js +9 -0
- package/.out/types/strict_definitions.d.ts +44 -0
- package/.out/types/strict_definitions.js +1 -0
- package/.out/types/value_to_type_paths_of.d.ts +22 -0
- package/.out/types/value_to_type_paths_of.js +1 -0
- package/.out/types/value_type_of.d.ts +24 -0
- package/.out/types/value_type_of.js +1 -0
- package/.out/types/value_types_of_discriminated_union.d.ts +9 -0
- package/.out/types/value_types_of_discriminated_union.js +1 -0
- package/.out/vitest.workspace.d.ts +2 -0
- package/.out/vitest.workspace.js +7 -0
- package/.turbo/turbo-build.log +18 -0
- package/.turbo/turbo-check-types.log +3 -0
- package/.turbo/turbo-release$colon$exports.log +3 -0
- package/README.md +12 -0
- package/dist/index.cjs +798 -0
- package/dist/index.d.cts +301 -0
- package/dist/index.d.ts +301 -0
- package/dist/index.js +775 -0
- package/index.ts +20 -0
- package/package.exports.json +18 -0
- package/package.json +45 -0
- package/transformers/copies/copy.ts +18 -0
- package/transformers/copies/copy_to.ts +226 -0
- package/transformers/copies/mobx_copy.ts +81 -0
- package/transformers/copies/specs/copy_to.tests.ts +143 -0
- package/transformers/copies/specs/mobx_copy.tests.ts +26 -0
- package/transformers/flatteners/flatten_accessors_of.ts +43 -0
- package/transformers/flatteners/flatten_json_value_to_type_paths_of.ts +39 -0
- package/transformers/flatteners/flatten_type_def_to.ts +127 -0
- package/transformers/flatteners/flatten_type_defs_of.ts +16 -0
- package/transformers/flatteners/flatten_value_type_to.ts +227 -0
- package/transformers/flatteners/flatten_value_types_of.ts +23 -0
- package/transformers/flatteners/json_path.ts +15 -0
- package/transformers/flatteners/specs/flatten_accessors_of.tests.ts +113 -0
- package/transformers/flatteners/specs/flatten_json_value_to_type_paths_of.tests.ts +115 -0
- package/transformers/flatteners/specs/flatten_type_def_to.tests.ts +146 -0
- package/transformers/flatteners/specs/flatten_type_defs_of.tests.ts +26 -0
- package/transformers/flatteners/specs/flatten_value_type_to.tests.ts +452 -0
- package/transformers/flatteners/specs/flatten_value_types_of.tests.ts +46 -0
- package/transformers/flatteners/specs/value_path_to_type_path.tests.ts +240 -0
- package/transformers/flatteners/value_path_to_type_path.ts +164 -0
- package/tsconfig.build.json +11 -0
- package/tsconfig.json +15 -0
- package/tsup.config.ts +16 -0
- package/types/builders.ts +284 -0
- package/types/definitions.ts +106 -0
- package/types/flattened.ts +5 -0
- package/types/flattened_accessors_of.ts +15 -0
- package/types/flattened_type_defs_of.ts +130 -0
- package/types/flattened_value_types_of.ts +11 -0
- package/types/json_path_of.ts +7 -0
- package/types/json_paths_of.ts +129 -0
- package/types/mobx_value_type_of.ts +16 -0
- package/types/partial_type_def_of.ts +64 -0
- package/types/readonly_type_def_of.ts +53 -0
- package/types/specs/builder.tests.ts +295 -0
- package/types/specs/flattened_accessors_of.tests.ts +27 -0
- package/types/specs/flattened_type_defs_of.tests.ts +212 -0
- package/types/specs/flattened_value_types_of.tests.ts +21 -0
- package/types/specs/json_paths_of.tests.ts +304 -0
- package/types/specs/partial_type_def_of.tests.ts +251 -0
- package/types/specs/readonly_type_def_of.tests.ts +158 -0
- package/types/specs/strict_definitions.tests.ts +184 -0
- package/types/specs/value_to_type_paths_of.tests.ts +181 -0
- package/types/specs/value_type_of.tests.ts +329 -0
- package/types/specs/value_types_of_discriminated_union.tests.ts +30 -0
- package/types/strict_definitions.ts +107 -0
- package/types/value_to_type_paths_of.ts +184 -0
- package/types/value_type_of.ts +84 -0
- package/types/value_types_of_discriminated_union.ts +14 -0
- package/vitest.workspace.ts +11 -0
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import { valuePathToTypePath } from 'transformers/flatteners/value_path_to_type_path'
|
|
2
|
+
import {
|
|
3
|
+
booleanType,
|
|
4
|
+
list,
|
|
5
|
+
numberType,
|
|
6
|
+
object,
|
|
7
|
+
record,
|
|
8
|
+
stringType,
|
|
9
|
+
union,
|
|
10
|
+
} from 'types/builders'
|
|
11
|
+
import { type ValueToTypePathsOf } from 'types/value_to_type_paths_of'
|
|
12
|
+
|
|
13
|
+
describe('valuePathToTypePath', function () {
|
|
14
|
+
describe('literal', function () {
|
|
15
|
+
const typeDef = numberType
|
|
16
|
+
type JsonPaths = ValueToTypePathsOf<typeof typeDef>
|
|
17
|
+
|
|
18
|
+
const typePath = valuePathToTypePath<JsonPaths, '$'>(typeDef, '$')
|
|
19
|
+
|
|
20
|
+
it('maps a value path to the expected type path', function () {
|
|
21
|
+
expect(typePath).toEqual('$')
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
it('has expected type', function () {
|
|
25
|
+
expectTypeOf(typePath).toEqualTypeOf<'$'>()
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
describe('fake subpath', function () {
|
|
29
|
+
const fakeTypePath = valuePathToTypePath<{
|
|
30
|
+
'$.fake': '$.fake',
|
|
31
|
+
}, '$.fake'>(typeDef, '$.fake', true)
|
|
32
|
+
|
|
33
|
+
it('maps a value path to the expected type path', function () {
|
|
34
|
+
expect(fakeTypePath).toEqual('$.fake')
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
it('has expected type', function () {
|
|
38
|
+
expectTypeOf(fakeTypePath).toEqualTypeOf<'$.fake'>()
|
|
39
|
+
})
|
|
40
|
+
})
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
describe('list', function () {
|
|
44
|
+
const typeDef = list(numberType)
|
|
45
|
+
type JsonPaths = ValueToTypePathsOf<typeof typeDef>
|
|
46
|
+
|
|
47
|
+
describe.each([
|
|
48
|
+
[
|
|
49
|
+
'$',
|
|
50
|
+
'$',
|
|
51
|
+
],
|
|
52
|
+
[
|
|
53
|
+
'$.0',
|
|
54
|
+
'$.*',
|
|
55
|
+
],
|
|
56
|
+
] as const)('it maps "%s"', function (from, to) {
|
|
57
|
+
const typePath = valuePathToTypePath<JsonPaths, typeof from>(typeDef, from)
|
|
58
|
+
|
|
59
|
+
it('maps a value path to the expected type path', function () {
|
|
60
|
+
expect(typePath).toEqual(to)
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
it('has expected type', function () {
|
|
64
|
+
expectTypeOf(typePath).toEqualTypeOf(to)
|
|
65
|
+
})
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
describe('fake subpath', function () {
|
|
69
|
+
const fakeTypePath = valuePathToTypePath<
|
|
70
|
+
JsonPaths & {
|
|
71
|
+
[_: `$.${number}.fake`]: '$.*.fake',
|
|
72
|
+
},
|
|
73
|
+
'$.0.fake'
|
|
74
|
+
>(typeDef, '$.0.fake', true)
|
|
75
|
+
|
|
76
|
+
it('maps a value path to the expected type path', function () {
|
|
77
|
+
expect(fakeTypePath).toEqual('$.*.fake')
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
it('has expected type', function () {
|
|
81
|
+
expectTypeOf(fakeTypePath).toEqualTypeOf<'$.*.fake'>()
|
|
82
|
+
})
|
|
83
|
+
})
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
describe('record', function () {
|
|
87
|
+
type Key = 'a' | 'b'
|
|
88
|
+
const typeDef = record<typeof numberType, Key>(numberType)
|
|
89
|
+
type JsonPaths = ValueToTypePathsOf<typeof typeDef>
|
|
90
|
+
|
|
91
|
+
describe.each([
|
|
92
|
+
[
|
|
93
|
+
'$',
|
|
94
|
+
'$',
|
|
95
|
+
],
|
|
96
|
+
[
|
|
97
|
+
'$.a',
|
|
98
|
+
'$.*',
|
|
99
|
+
],
|
|
100
|
+
[
|
|
101
|
+
'$.b',
|
|
102
|
+
'$.*',
|
|
103
|
+
],
|
|
104
|
+
] as const)('it maps "%s"', function (from, to) {
|
|
105
|
+
const typePath = valuePathToTypePath<JsonPaths, typeof from>(typeDef, from)
|
|
106
|
+
|
|
107
|
+
it('maps a value path to the expected type path', function () {
|
|
108
|
+
expect(typePath).toEqual(to)
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
it('has expected type', function () {
|
|
112
|
+
expectTypeOf(typePath).toEqualTypeOf(to)
|
|
113
|
+
})
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
describe('fake subpath', function () {
|
|
117
|
+
const fakeTypePath = valuePathToTypePath<
|
|
118
|
+
JsonPaths & {
|
|
119
|
+
'$.a.fake': '$.*.fake',
|
|
120
|
+
'$.b.fake': '$.*.fake',
|
|
121
|
+
},
|
|
122
|
+
'$.a.fake'
|
|
123
|
+
>(typeDef, '$.a.fake', true)
|
|
124
|
+
|
|
125
|
+
it('maps a value path to the expected type path', function () {
|
|
126
|
+
expect(fakeTypePath).toEqual('$.*.fake')
|
|
127
|
+
})
|
|
128
|
+
|
|
129
|
+
it('has expected type', function () {
|
|
130
|
+
expectTypeOf(fakeTypePath).toEqualTypeOf<'$.*.fake'>()
|
|
131
|
+
})
|
|
132
|
+
})
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
describe('object', function () {
|
|
136
|
+
const typeDef = object().set('a', numberType).set('b', booleanType)
|
|
137
|
+
type JsonPaths = ValueToTypePathsOf<typeof typeDef>
|
|
138
|
+
|
|
139
|
+
describe.each([
|
|
140
|
+
[
|
|
141
|
+
'$',
|
|
142
|
+
'$',
|
|
143
|
+
],
|
|
144
|
+
[
|
|
145
|
+
'$.a',
|
|
146
|
+
'$.a',
|
|
147
|
+
],
|
|
148
|
+
[
|
|
149
|
+
'$.b',
|
|
150
|
+
'$.b',
|
|
151
|
+
],
|
|
152
|
+
] as const)('it maps %s', function (from, to) {
|
|
153
|
+
const typePath = valuePathToTypePath<JsonPaths, typeof from>(typeDef, from)
|
|
154
|
+
|
|
155
|
+
it('maps a value path to the expected type path', function () {
|
|
156
|
+
expect(typePath).toEqual(to)
|
|
157
|
+
})
|
|
158
|
+
|
|
159
|
+
it('has expected type', function () {
|
|
160
|
+
expectTypeOf(typePath).toEqualTypeOf(to)
|
|
161
|
+
})
|
|
162
|
+
})
|
|
163
|
+
|
|
164
|
+
describe('fake field', function () {
|
|
165
|
+
const fakeTypePath = valuePathToTypePath<
|
|
166
|
+
JsonPaths & {
|
|
167
|
+
'$.fake': '$.fake',
|
|
168
|
+
},
|
|
169
|
+
'$.fake'
|
|
170
|
+
>(typeDef, '$.fake', true)
|
|
171
|
+
|
|
172
|
+
it('maps a value path to the expected type path', function () {
|
|
173
|
+
expect(fakeTypePath).toEqual('$.fake')
|
|
174
|
+
})
|
|
175
|
+
|
|
176
|
+
it('has expected type', function () {
|
|
177
|
+
expectTypeOf(fakeTypePath).toEqualTypeOf<'$.fake'>()
|
|
178
|
+
})
|
|
179
|
+
})
|
|
180
|
+
})
|
|
181
|
+
|
|
182
|
+
describe('union', function () {
|
|
183
|
+
describe('discriminated', function () {
|
|
184
|
+
const typeDef = union('w')
|
|
185
|
+
.add('x', object().set('a', numberType).set('b', booleanType))
|
|
186
|
+
.add('y', object().set('b', stringType).set('c', booleanType))
|
|
187
|
+
type JsonPaths = ValueToTypePathsOf<typeof typeDef>
|
|
188
|
+
|
|
189
|
+
describe.each([
|
|
190
|
+
[
|
|
191
|
+
'$',
|
|
192
|
+
'$',
|
|
193
|
+
],
|
|
194
|
+
[
|
|
195
|
+
'$.x:a',
|
|
196
|
+
'$.x:a',
|
|
197
|
+
],
|
|
198
|
+
[
|
|
199
|
+
'$.x:b',
|
|
200
|
+
'$.x:b',
|
|
201
|
+
],
|
|
202
|
+
[
|
|
203
|
+
'$.y:b',
|
|
204
|
+
'$.y:b',
|
|
205
|
+
],
|
|
206
|
+
[
|
|
207
|
+
'$.y:c',
|
|
208
|
+
'$.y:c',
|
|
209
|
+
],
|
|
210
|
+
] as const)('it maps %s', function (from, to) {
|
|
211
|
+
const typePath = valuePathToTypePath<JsonPaths, typeof from>(typeDef, from)
|
|
212
|
+
|
|
213
|
+
it('maps a value path to the expected type path', function () {
|
|
214
|
+
expect(typePath).toEqual(to)
|
|
215
|
+
})
|
|
216
|
+
|
|
217
|
+
it('has expected type', function () {
|
|
218
|
+
expectTypeOf(typePath).toEqualTypeOf(to)
|
|
219
|
+
})
|
|
220
|
+
})
|
|
221
|
+
|
|
222
|
+
describe('fake', function () {
|
|
223
|
+
const fakeTypePath = valuePathToTypePath<
|
|
224
|
+
JsonPaths & {
|
|
225
|
+
'$.fake': '$.fake',
|
|
226
|
+
},
|
|
227
|
+
'$.fake'
|
|
228
|
+
>(typeDef, '$.fake', true)
|
|
229
|
+
|
|
230
|
+
it('maps a value path to the expected type path', function () {
|
|
231
|
+
expect(fakeTypePath).toEqual('$.fake')
|
|
232
|
+
})
|
|
233
|
+
|
|
234
|
+
it('has expected type', function () {
|
|
235
|
+
expectTypeOf(fakeTypePath).toEqualTypeOf<'$.fake'>()
|
|
236
|
+
})
|
|
237
|
+
})
|
|
238
|
+
})
|
|
239
|
+
})
|
|
240
|
+
})
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import {
|
|
2
|
+
assertEqual,
|
|
3
|
+
assertExists,
|
|
4
|
+
assertExistsAndReturn,
|
|
5
|
+
PreconditionFailedError,
|
|
6
|
+
reduce,
|
|
7
|
+
UnreachableError,
|
|
8
|
+
} from '@strictly/base'
|
|
9
|
+
import {
|
|
10
|
+
type Type,
|
|
11
|
+
type TypeDef,
|
|
12
|
+
TypeDefType,
|
|
13
|
+
} from 'types/definitions'
|
|
14
|
+
|
|
15
|
+
export function valuePathToTypePath<
|
|
16
|
+
JsonPaths extends Record<string, string>,
|
|
17
|
+
ValuePath extends keyof JsonPaths,
|
|
18
|
+
>(
|
|
19
|
+
{ definition: typeDef }: Type,
|
|
20
|
+
valuePath: ValuePath,
|
|
21
|
+
allowMissingPaths: boolean = false,
|
|
22
|
+
): JsonPaths[ValuePath] {
|
|
23
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
24
|
+
const valueSteps = (valuePath as string).split(/\.|\[/g)
|
|
25
|
+
assertEqual(valueSteps[0], '$')
|
|
26
|
+
|
|
27
|
+
const typeSteps = internalJsonValuePathToTypePath(
|
|
28
|
+
typeDef,
|
|
29
|
+
valueSteps.slice(1),
|
|
30
|
+
allowMissingPaths,
|
|
31
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
32
|
+
valuePath as string,
|
|
33
|
+
)
|
|
34
|
+
typeSteps.unshift('$')
|
|
35
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
36
|
+
return typeSteps.join('.') as JsonPaths[ValuePath]
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function internalJsonValuePathToTypePath(
|
|
40
|
+
typeDef: TypeDef,
|
|
41
|
+
valueSteps: string[],
|
|
42
|
+
allowMissingPaths: boolean,
|
|
43
|
+
originalValuePath: string,
|
|
44
|
+
): string[] {
|
|
45
|
+
if (valueSteps.length === 0) {
|
|
46
|
+
return []
|
|
47
|
+
}
|
|
48
|
+
const [
|
|
49
|
+
valueStep,
|
|
50
|
+
...remainingValueSteps
|
|
51
|
+
] = valueSteps
|
|
52
|
+
switch (typeDef.type) {
|
|
53
|
+
case TypeDefType.Literal:
|
|
54
|
+
if (allowMissingPaths) {
|
|
55
|
+
// fake it
|
|
56
|
+
return valueSteps
|
|
57
|
+
} else {
|
|
58
|
+
throw new PreconditionFailedError(
|
|
59
|
+
'literal should terminate path {} ({})',
|
|
60
|
+
originalValuePath,
|
|
61
|
+
valueStep,
|
|
62
|
+
)
|
|
63
|
+
}
|
|
64
|
+
case TypeDefType.List:
|
|
65
|
+
// TODO assert format of current step
|
|
66
|
+
return [
|
|
67
|
+
'*',
|
|
68
|
+
...internalJsonValuePathToTypePath(
|
|
69
|
+
typeDef.elements,
|
|
70
|
+
remainingValueSteps,
|
|
71
|
+
allowMissingPaths,
|
|
72
|
+
originalValuePath,
|
|
73
|
+
),
|
|
74
|
+
]
|
|
75
|
+
case TypeDefType.Record:
|
|
76
|
+
return [
|
|
77
|
+
'*',
|
|
78
|
+
...internalJsonValuePathToTypePath(
|
|
79
|
+
typeDef.valueTypeDef,
|
|
80
|
+
remainingValueSteps,
|
|
81
|
+
allowMissingPaths,
|
|
82
|
+
originalValuePath,
|
|
83
|
+
),
|
|
84
|
+
]
|
|
85
|
+
case TypeDefType.Object:
|
|
86
|
+
if (allowMissingPaths) {
|
|
87
|
+
if (typeDef.fields[valueStep] == null) {
|
|
88
|
+
// fake it
|
|
89
|
+
return valueSteps
|
|
90
|
+
}
|
|
91
|
+
} else {
|
|
92
|
+
assertExists(typeDef.fields[valueStep], 'missing field in {} ({})', originalValuePath, valueStep)
|
|
93
|
+
}
|
|
94
|
+
return [
|
|
95
|
+
valueStep,
|
|
96
|
+
...internalJsonValuePathToTypePath(
|
|
97
|
+
typeDef.fields[valueStep],
|
|
98
|
+
remainingValueSteps,
|
|
99
|
+
allowMissingPaths,
|
|
100
|
+
originalValuePath,
|
|
101
|
+
),
|
|
102
|
+
]
|
|
103
|
+
case TypeDefType.Union:
|
|
104
|
+
if (typeDef.discriminator == null) {
|
|
105
|
+
if (remainingValueSteps.length > 0) {
|
|
106
|
+
// find the non-literal typedef
|
|
107
|
+
const union = reduce<string, TypeDef, null | TypeDef>(
|
|
108
|
+
typeDef.unions,
|
|
109
|
+
function (acc, _k, v) {
|
|
110
|
+
if (v.type !== TypeDefType.Literal || v.type === TypeDefType.Literal && v.valuePrototype == null) {
|
|
111
|
+
return v
|
|
112
|
+
}
|
|
113
|
+
return acc
|
|
114
|
+
},
|
|
115
|
+
null,
|
|
116
|
+
)
|
|
117
|
+
assertExists(union, 'expected a complex union {}', originalValuePath)
|
|
118
|
+
return internalJsonValuePathToTypePath(
|
|
119
|
+
union,
|
|
120
|
+
valueSteps,
|
|
121
|
+
allowMissingPaths,
|
|
122
|
+
originalValuePath,
|
|
123
|
+
)
|
|
124
|
+
} else {
|
|
125
|
+
// doesn't really matter
|
|
126
|
+
return []
|
|
127
|
+
}
|
|
128
|
+
} else {
|
|
129
|
+
const qualifierIndex = valueStep.indexOf(':')
|
|
130
|
+
if (qualifierIndex < 0) {
|
|
131
|
+
if (allowMissingPaths) {
|
|
132
|
+
return valueSteps
|
|
133
|
+
} else {
|
|
134
|
+
throw new PreconditionFailedError(
|
|
135
|
+
'mismatched qualifiers in {} (at {})',
|
|
136
|
+
originalValuePath,
|
|
137
|
+
valueStep,
|
|
138
|
+
)
|
|
139
|
+
}
|
|
140
|
+
}
|
|
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
|
+
const [
|
|
145
|
+
typeStep,
|
|
146
|
+
...remainingTypeSteps
|
|
147
|
+
] = internalJsonValuePathToTypePath(
|
|
148
|
+
union,
|
|
149
|
+
[
|
|
150
|
+
remainder,
|
|
151
|
+
...remainingValueSteps,
|
|
152
|
+
],
|
|
153
|
+
allowMissingPaths,
|
|
154
|
+
originalValuePath,
|
|
155
|
+
)
|
|
156
|
+
return [
|
|
157
|
+
`${qualifier}:${typeStep}`,
|
|
158
|
+
...remainingTypeSteps,
|
|
159
|
+
]
|
|
160
|
+
}
|
|
161
|
+
default:
|
|
162
|
+
throw new UnreachableError(typeDef)
|
|
163
|
+
}
|
|
164
|
+
}
|
package/tsconfig.json
ADDED
package/tsup.config.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import {
|
|
2
|
+
defineConfig,
|
|
3
|
+
type Options,
|
|
4
|
+
} from 'tsup'
|
|
5
|
+
|
|
6
|
+
export default defineConfig((options: Options) => ({
|
|
7
|
+
entry: ['index.ts'],
|
|
8
|
+
tsconfig: './tsconfig.build.json',
|
|
9
|
+
clean: false,
|
|
10
|
+
dts: true,
|
|
11
|
+
format: [
|
|
12
|
+
'cjs',
|
|
13
|
+
'esm',
|
|
14
|
+
],
|
|
15
|
+
...options,
|
|
16
|
+
}))
|