arktype 2.0.0-dev.1 → 2.0.0-dev.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/__tests__/array.test.ts +252 -71
- package/__tests__/config.test.ts +75 -166
- package/__tests__/declared.test.ts +5 -4
- package/__tests__/divisor.test.ts +6 -2
- package/__tests__/instanceof.test.ts +1 -1
- package/__tests__/keyTraversal.test.ts +71 -70
- package/__tests__/match.bench.ts +31 -0
- package/__tests__/match.test.ts +216 -26
- package/__tests__/narrow.test.ts +3 -3
- package/__tests__/objectLiteral.test.ts +19 -18
- package/match.ts +206 -79
- package/out/match.d.ts +60 -31
- package/out/match.d.ts.map +1 -1
- package/out/match.js +52 -10
- package/out/match.js.map +1 -1
- package/out/parser/definition.d.ts +6 -4
- package/out/parser/definition.d.ts.map +1 -1
- package/out/parser/definition.js.map +1 -1
- package/out/parser/objectLiteral.d.ts.map +1 -1
- package/out/parser/objectLiteral.js +31 -35
- package/out/parser/objectLiteral.js.map +1 -1
- package/out/parser/semantic/semantic.d.ts +6 -7
- package/out/parser/semantic/semantic.d.ts.map +1 -1
- package/out/parser/shared.d.ts +10 -2
- package/out/parser/shared.d.ts.map +1 -1
- package/out/parser/shared.js +13 -4
- package/out/parser/shared.js.map +1 -1
- package/out/parser/string/reduce/static.d.ts +1 -1
- package/out/parser/string/reduce/static.d.ts.map +1 -1
- package/out/parser/tuple.d.ts +15 -14
- package/out/parser/tuple.d.ts.map +1 -1
- package/out/parser/tuple.js +94 -53
- package/out/parser/tuple.js.map +1 -1
- package/out/scope.d.ts +6 -11
- package/out/scope.d.ts.map +1 -1
- package/out/scope.js +7 -25
- package/out/scope.js.map +1 -1
- package/out/type.d.ts +5 -9
- package/out/type.d.ts.map +1 -1
- package/out/type.js +14 -10
- package/out/type.js.map +1 -1
- package/package.json +3 -3
- package/parser/definition.ts +7 -7
- package/parser/objectLiteral.ts +47 -51
- package/parser/semantic/semantic.ts +8 -9
- package/parser/semantic/validate.ts +1 -1
- package/parser/shared.ts +27 -9
- package/parser/string/reduce/static.ts +2 -2
- package/parser/tuple.ts +175 -86
- package/scope.ts +12 -35
- package/type.ts +20 -18
- package/parser/semantic/intersections.ts +0 -46
package/__tests__/array.test.ts
CHANGED
|
@@ -3,27 +3,44 @@ import { scope, type } from "arktype"
|
|
|
3
3
|
import { writeUnresolvableMessage } from "../parser/string/shift/operand/unenclosed.js"
|
|
4
4
|
import { incompleteArrayTokenMessage } from "../parser/string/shift/operator/operator.js"
|
|
5
5
|
import {
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
multipleVariadicMesage,
|
|
7
|
+
writeNonArraySpreadMessage
|
|
8
8
|
} from "../parser/tuple.js"
|
|
9
9
|
|
|
10
10
|
describe("array", () => {
|
|
11
11
|
describe("base", () => {
|
|
12
|
-
it("
|
|
12
|
+
it("allows and apply", () => {
|
|
13
13
|
const t = type("string[]")
|
|
14
14
|
attest<string[]>(t.infer)
|
|
15
15
|
attest(t.allows([])).equals(true)
|
|
16
|
+
attest(t([]).out).snap([])
|
|
16
17
|
attest(t.allows(["foo", "bar"])).equals(true)
|
|
18
|
+
attest(t(["foo", "bar"]).out).snap(["foo", "bar"])
|
|
17
19
|
attest(t.allows(["foo", "bar", 5])).equals(false)
|
|
20
|
+
attest(t(["foo", "bar", 5]).errors?.summary).snap(
|
|
21
|
+
"Value at [2] must be a string (was number)"
|
|
22
|
+
)
|
|
18
23
|
attest(t.allows([5, "foo", "bar"])).equals(false)
|
|
24
|
+
attest(t([5, "foo", "bar"]).errors?.summary).snap(
|
|
25
|
+
"Value at [0] must be a string (was number)"
|
|
26
|
+
)
|
|
19
27
|
})
|
|
28
|
+
|
|
20
29
|
it("nested", () => {
|
|
21
30
|
const t = type("string[][]")
|
|
22
31
|
attest<string[][]>(t.infer)
|
|
23
32
|
attest(t.allows([])).equals(true)
|
|
33
|
+
attest(t([]).out).snap([])
|
|
24
34
|
attest(t.allows([["foo"]])).equals(true)
|
|
35
|
+
attest(t([["foo"]]).out).snap([["foo"]])
|
|
25
36
|
attest(t.allows(["foo"])).equals(false)
|
|
37
|
+
attest(t(["foo"]).errors?.summary).snap(
|
|
38
|
+
"Value at [0] must be an array (was string)"
|
|
39
|
+
)
|
|
26
40
|
attest(t.allows([["foo", 5]])).equals(false)
|
|
41
|
+
attest(t([["foo", 5]]).errors?.summary).snap(
|
|
42
|
+
"Value at [0][1] must be a string (was number)"
|
|
43
|
+
)
|
|
27
44
|
})
|
|
28
45
|
|
|
29
46
|
it("tuple expression", () => {
|
|
@@ -31,6 +48,7 @@ describe("array", () => {
|
|
|
31
48
|
attest<string[]>(t.infer)
|
|
32
49
|
attest(t.json).equals(type("string[]").json)
|
|
33
50
|
})
|
|
51
|
+
|
|
34
52
|
describe("optional tuple literals", () => {
|
|
35
53
|
it("string optional", () => {
|
|
36
54
|
const t = type(["string?"])
|
|
@@ -91,12 +109,23 @@ describe("array", () => {
|
|
|
91
109
|
const t = type(["string", "number"])
|
|
92
110
|
attest<[string, number]>(t.infer)
|
|
93
111
|
attest(t.allows(["", 0])).equals(true)
|
|
112
|
+
attest(t(["", 0]).out).snap(["", 0])
|
|
94
113
|
attest(t.allows([true, 0])).equals(false)
|
|
114
|
+
attest(t([true, 0]).errors?.summary).snap(
|
|
115
|
+
"Value at [0] must be a string (was boolean)"
|
|
116
|
+
)
|
|
95
117
|
attest(t.allows([0, false])).equals(false)
|
|
118
|
+
attest(t([0, false]).errors?.summary)
|
|
119
|
+
.snap(`Value at [0] must be a string (was number)
|
|
120
|
+
Value at [1] must be a number (was boolean)`)
|
|
96
121
|
// too short
|
|
97
122
|
attest(t.allows([""])).equals(false)
|
|
123
|
+
attest(t([""]).errors?.summary).snap("Must be at least length 2 (was 1)")
|
|
98
124
|
// too long
|
|
99
125
|
attest(t.allows(["", 0, 1])).equals(false)
|
|
126
|
+
attest(t(["", 0, 1]).errors?.summary).snap(
|
|
127
|
+
"Must be at most length 2 (was 3)"
|
|
128
|
+
)
|
|
100
129
|
// non-array
|
|
101
130
|
attest(
|
|
102
131
|
t.allows({
|
|
@@ -105,93 +134,100 @@ describe("array", () => {
|
|
|
105
134
|
1: 0
|
|
106
135
|
})
|
|
107
136
|
).equals(false)
|
|
137
|
+
attest(
|
|
138
|
+
t({
|
|
139
|
+
length: 2,
|
|
140
|
+
0: "",
|
|
141
|
+
1: 0
|
|
142
|
+
}).errors?.summary
|
|
143
|
+
).snap("Must be an array (was object)")
|
|
108
144
|
})
|
|
145
|
+
|
|
109
146
|
it("nested", () => {
|
|
110
|
-
const t = type([["string", "number"], [{ a: "
|
|
147
|
+
const t = type([["string", "number"], [{ a: "bigint", b: ["null"] }]])
|
|
111
148
|
attest<
|
|
112
149
|
[
|
|
113
150
|
[string, number],
|
|
114
151
|
[
|
|
115
152
|
{
|
|
116
|
-
a:
|
|
153
|
+
a: bigint
|
|
117
154
|
b: [null]
|
|
118
155
|
}
|
|
119
156
|
]
|
|
120
157
|
]
|
|
121
158
|
>(t.infer)
|
|
122
|
-
|
|
123
|
-
attest(t.allows(
|
|
124
|
-
attest(t
|
|
159
|
+
const valid: typeof t.infer = [["", 0], [{ a: 0n, b: [null] }]]
|
|
160
|
+
attest(t.allows(valid)).equals(true)
|
|
161
|
+
attest(t(valid).out).equals(valid)
|
|
162
|
+
const invalid = [["", 0], [{ a: 0n, b: [undefined] }]]
|
|
163
|
+
attest(t.allows(invalid)).equals(false)
|
|
164
|
+
attest(t(invalid).errors?.summary).snap(
|
|
165
|
+
"Value at [1][0].b[0] must be null (was undefined)"
|
|
166
|
+
)
|
|
125
167
|
})
|
|
126
168
|
})
|
|
127
169
|
describe("variadic tuple", () => {
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
})
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
type(["...number[]", "string"])
|
|
174
|
-
).throwsAndHasTypeError(prematureRestMessage)
|
|
175
|
-
attest(() =>
|
|
170
|
+
it("spreads simple arrays", () => {
|
|
171
|
+
const wellRested = type(["string", "...number[]"])
|
|
172
|
+
attest<[string, ...number[]]>(wellRested.infer)
|
|
173
|
+
attest(wellRested(["foo"]).out).equals(["foo"])
|
|
174
|
+
attest(wellRested(["foo", 1, 2]).out).equals(["foo", 1, 2])
|
|
175
|
+
})
|
|
176
|
+
it("tuple expression", () => {
|
|
177
|
+
const wellRestedTuple = type(["number", ["...", [{ a: "string" }, "[]"]]])
|
|
178
|
+
attest<[number, ...{ a: string }[]]>(wellRestedTuple.infer)
|
|
179
|
+
})
|
|
180
|
+
it("spreads array expressions", () => {
|
|
181
|
+
const greatSpread = type(["0", "...(Date|RegExp)[]"])
|
|
182
|
+
attest<[0, ...(RegExp | Date)[]]>(greatSpread.infer)
|
|
183
|
+
})
|
|
184
|
+
it("distributes spread unions", () => {
|
|
185
|
+
const t = type(["1", "...(Date[] | RegExp[])"])
|
|
186
|
+
attest<[1, ...(Date[] | RegExp[])]>(t.infer)
|
|
187
|
+
const expected = type(["1", "...Date[]"]).or(["1", "...RegExp[]"])
|
|
188
|
+
attest(t.json).equals(expected.json)
|
|
189
|
+
})
|
|
190
|
+
it("allows array keyword", () => {
|
|
191
|
+
const types = scope({
|
|
192
|
+
myArrayKeyword: "boolean[]",
|
|
193
|
+
myVariadicKeyword: ["string", "...myArrayKeyword"]
|
|
194
|
+
}).export()
|
|
195
|
+
attest<[string, ...boolean[]]>(types.myVariadicKeyword.infer)
|
|
196
|
+
})
|
|
197
|
+
it("errors on non-array", () => {
|
|
198
|
+
attest(() =>
|
|
199
|
+
// @ts-expect-error
|
|
200
|
+
type(["email", "...symbol"])
|
|
201
|
+
).throwsAndHasTypeError(writeNonArraySpreadMessage("a symbol"))
|
|
202
|
+
attest(() =>
|
|
203
|
+
// @ts-expect-error
|
|
204
|
+
type(["number", ["...", "string"]])
|
|
205
|
+
).throwsAndHasTypeError(writeNonArraySpreadMessage("a string"))
|
|
206
|
+
})
|
|
207
|
+
it("errors on multiple variadic", () => {
|
|
208
|
+
attest(() =>
|
|
209
|
+
// @ts-expect-error
|
|
210
|
+
type(["...number[]", "...string[]"])
|
|
211
|
+
).throwsAndHasTypeError(multipleVariadicMesage)
|
|
212
|
+
attest(() =>
|
|
213
|
+
type([
|
|
214
|
+
["...", "string[]"],
|
|
176
215
|
// @ts-expect-error
|
|
177
|
-
|
|
178
|
-
)
|
|
179
|
-
|
|
216
|
+
["...", "number[]"]
|
|
217
|
+
])
|
|
218
|
+
).throwsAndHasTypeError(multipleVariadicMesage)
|
|
180
219
|
})
|
|
181
220
|
})
|
|
182
221
|
describe("intersection", () => {
|
|
183
222
|
it("shallow array intersection", () => {
|
|
184
|
-
const
|
|
185
|
-
const expected = type("'foo'[]")
|
|
186
|
-
attest(
|
|
223
|
+
const t = type("string[]&'foo'[]")
|
|
224
|
+
const expected = type("'foo'[]")
|
|
225
|
+
attest(t.json).equals(expected.json)
|
|
187
226
|
})
|
|
188
227
|
it("deep array intersection", () => {
|
|
189
|
-
const
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
]).json
|
|
193
|
-
const expected = type([{ a: "string", b: "number" }, "[]"]).json
|
|
194
|
-
attest(actual).is(expected)
|
|
228
|
+
const t = type([{ a: "string" }, "[]"]).and([{ b: "number" }, "[]"])
|
|
229
|
+
const expected = type([{ a: "string", b: "number" }, "[]"])
|
|
230
|
+
attest(t.json).equals(expected.json)
|
|
195
231
|
})
|
|
196
232
|
it("tuple intersection", () => {
|
|
197
233
|
const t = type([[{ a: "string" }], "&", [{ b: "boolean" }]])
|
|
@@ -203,6 +239,7 @@ describe("array", () => {
|
|
|
203
239
|
}
|
|
204
240
|
]
|
|
205
241
|
>(t.infer)
|
|
242
|
+
const expected = type([{ a: "string", b: "boolean" }])
|
|
206
243
|
})
|
|
207
244
|
it("tuple and array", () => {
|
|
208
245
|
const tupleAndArray = type([
|
|
@@ -233,9 +270,9 @@ describe("array", () => {
|
|
|
233
270
|
]
|
|
234
271
|
>(arrayAndTuple.infer)
|
|
235
272
|
|
|
236
|
-
const expected = type([{ a: "string", b: "boolean" }])
|
|
237
|
-
attest(tupleAndArray.json).
|
|
238
|
-
attest(arrayAndTuple.json).
|
|
273
|
+
const expected = type([{ a: "string", b: "boolean" }])
|
|
274
|
+
attest(tupleAndArray.json).equals(expected.json)
|
|
275
|
+
attest(arrayAndTuple.json).equals(expected.json)
|
|
239
276
|
})
|
|
240
277
|
it("variadic and tuple", () => {
|
|
241
278
|
const b = type([{ b: "boolean" }, "[]"])
|
|
@@ -259,6 +296,150 @@ describe("array", () => {
|
|
|
259
296
|
attest<typeof expected.infer>(t.infer)
|
|
260
297
|
attest(t.json).equals(expected.json)
|
|
261
298
|
})
|
|
299
|
+
// based on the equivalent type-level test from @arktype/util
|
|
300
|
+
it("kitchen sink", () => {
|
|
301
|
+
const l = type([
|
|
302
|
+
{ a: "0" },
|
|
303
|
+
[{ b: "1" }, "?"],
|
|
304
|
+
[{ c: "2" }, "?"],
|
|
305
|
+
["...", [{ d: "3" }, "[]"]]
|
|
306
|
+
])
|
|
307
|
+
const r = type([
|
|
308
|
+
[{ e: "4" }, "?"],
|
|
309
|
+
[{ f: "5" }, "?"],
|
|
310
|
+
["...", [{ g: "6" }, "[]"]]
|
|
311
|
+
])
|
|
312
|
+
const result = l.and(r)
|
|
313
|
+
|
|
314
|
+
const expected = type([
|
|
315
|
+
{ a: "0", e: "4" },
|
|
316
|
+
[{ b: "1", f: "5" }, "?"],
|
|
317
|
+
[{ c: "2", g: "6" }, "?"],
|
|
318
|
+
["...", [{ d: "3", g: "6" }, "[]"]]
|
|
319
|
+
])
|
|
320
|
+
|
|
321
|
+
attest<typeof expected>(result)
|
|
322
|
+
attest(result.json).equals(expected.json)
|
|
323
|
+
})
|
|
324
|
+
it("prefix and postfix", () => {
|
|
325
|
+
const l = type([["...", [{ a: "0" }, "[]"]], { b: "0" }, { c: "0" }])
|
|
326
|
+
const r = type([{ x: "0" }, { y: "0" }, ["...", [{ z: "0" }, "[]"]]])
|
|
327
|
+
|
|
328
|
+
// currently getting this "Expected" result at a type-level incurs
|
|
329
|
+
// too high a performance cost for such a niche intersection.
|
|
330
|
+
type Expected =
|
|
331
|
+
| [
|
|
332
|
+
{ a: 0; x: 0 },
|
|
333
|
+
{ a: 0; y: 0 },
|
|
334
|
+
...{ a: 0; z: 0 }[],
|
|
335
|
+
{ b: 0; z: 0 },
|
|
336
|
+
{ c: 0; z: 0 }
|
|
337
|
+
]
|
|
338
|
+
| [{ a: 0; x: 0 }, { b: 0; y: 0 }, { c: 0; z: 0 }]
|
|
339
|
+
| [{ b: 0; x: 0 }, { c: 0; y: 0 }]
|
|
340
|
+
|
|
341
|
+
const result = l.and(r)
|
|
342
|
+
attest(result.json).snap([
|
|
343
|
+
{
|
|
344
|
+
basis: "Array",
|
|
345
|
+
maxLength: 2,
|
|
346
|
+
minLength: 2,
|
|
347
|
+
sequence: {
|
|
348
|
+
prefix: [
|
|
349
|
+
{
|
|
350
|
+
basis: "object",
|
|
351
|
+
required: [
|
|
352
|
+
{ key: "b", value: { unit: 0 } },
|
|
353
|
+
{ key: "x", value: { unit: 0 } }
|
|
354
|
+
]
|
|
355
|
+
},
|
|
356
|
+
{
|
|
357
|
+
basis: "object",
|
|
358
|
+
required: [
|
|
359
|
+
{ key: "c", value: { unit: 0 } },
|
|
360
|
+
{ key: "y", value: { unit: 0 } }
|
|
361
|
+
]
|
|
362
|
+
}
|
|
363
|
+
]
|
|
364
|
+
}
|
|
365
|
+
},
|
|
366
|
+
{
|
|
367
|
+
basis: "Array",
|
|
368
|
+
maxLength: 3,
|
|
369
|
+
minLength: 3,
|
|
370
|
+
sequence: {
|
|
371
|
+
prefix: [
|
|
372
|
+
{
|
|
373
|
+
basis: "object",
|
|
374
|
+
required: [
|
|
375
|
+
{ key: "a", value: { unit: 0 } },
|
|
376
|
+
{ key: "x", value: { unit: 0 } }
|
|
377
|
+
]
|
|
378
|
+
},
|
|
379
|
+
{
|
|
380
|
+
basis: "object",
|
|
381
|
+
required: [
|
|
382
|
+
{ key: "b", value: { unit: 0 } },
|
|
383
|
+
{ key: "y", value: { unit: 0 } }
|
|
384
|
+
]
|
|
385
|
+
},
|
|
386
|
+
{
|
|
387
|
+
basis: "object",
|
|
388
|
+
required: [
|
|
389
|
+
{ key: "c", value: { unit: 0 } },
|
|
390
|
+
{ key: "z", value: { unit: 0 } }
|
|
391
|
+
]
|
|
392
|
+
}
|
|
393
|
+
]
|
|
394
|
+
}
|
|
395
|
+
},
|
|
396
|
+
{
|
|
397
|
+
basis: "Array",
|
|
398
|
+
minLength: 4,
|
|
399
|
+
sequence: {
|
|
400
|
+
postfix: [
|
|
401
|
+
{
|
|
402
|
+
basis: "object",
|
|
403
|
+
required: [
|
|
404
|
+
{ key: "b", value: { unit: 0 } },
|
|
405
|
+
{ key: "z", value: { unit: 0 } }
|
|
406
|
+
]
|
|
407
|
+
},
|
|
408
|
+
{
|
|
409
|
+
basis: "object",
|
|
410
|
+
required: [
|
|
411
|
+
{ key: "c", value: { unit: 0 } },
|
|
412
|
+
{ key: "z", value: { unit: 0 } }
|
|
413
|
+
]
|
|
414
|
+
}
|
|
415
|
+
],
|
|
416
|
+
prefix: [
|
|
417
|
+
{
|
|
418
|
+
basis: "object",
|
|
419
|
+
required: [
|
|
420
|
+
{ key: "a", value: { unit: 0 } },
|
|
421
|
+
{ key: "x", value: { unit: 0 } }
|
|
422
|
+
]
|
|
423
|
+
},
|
|
424
|
+
{
|
|
425
|
+
basis: "object",
|
|
426
|
+
required: [
|
|
427
|
+
{ key: "a", value: { unit: 0 } },
|
|
428
|
+
{ key: "y", value: { unit: 0 } }
|
|
429
|
+
]
|
|
430
|
+
}
|
|
431
|
+
],
|
|
432
|
+
variadic: {
|
|
433
|
+
basis: "object",
|
|
434
|
+
required: [
|
|
435
|
+
{ key: "a", value: { unit: 0 } },
|
|
436
|
+
{ key: "z", value: { unit: 0 } }
|
|
437
|
+
]
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
])
|
|
442
|
+
})
|
|
262
443
|
})
|
|
263
444
|
// TODO: reenable
|
|
264
445
|
// describe("traversal", () => {
|
package/__tests__/config.test.ts
CHANGED
|
@@ -1,168 +1,77 @@
|
|
|
1
|
+
import { attest } from "@arktype/attest"
|
|
2
|
+
import { scope, type } from "arktype"
|
|
3
|
+
|
|
1
4
|
describe("config traversal", () => {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
// // "monster",
|
|
75
|
-
// // [
|
|
76
|
-
// // [
|
|
77
|
-
// // "config",
|
|
78
|
-
// // {
|
|
79
|
-
// // config: [
|
|
80
|
-
// // [
|
|
81
|
-
// // "mustBe",
|
|
82
|
-
// // "the number of dimensions in the monster group"
|
|
83
|
-
// // ]
|
|
84
|
-
// // ],
|
|
85
|
-
// // node: [["value", 196883]]
|
|
86
|
-
// // }
|
|
87
|
-
// // ]
|
|
88
|
-
// // ]
|
|
89
|
-
// // ]
|
|
90
|
-
// // ]
|
|
91
|
-
// // ])
|
|
92
|
-
// attest(t({ monster: 196882 }).errors?.summary).snap(
|
|
93
|
-
// "monster must be the number of dimensions in the monster group (was 196882)"
|
|
94
|
-
// )
|
|
95
|
-
// })
|
|
96
|
-
// it("anonymous type config", () => {
|
|
97
|
-
// const t = type(type("true", { mustBe: "unfalse" }))
|
|
98
|
-
// attest<true>(t.infer)
|
|
99
|
-
// // attest(t.flat).snap([
|
|
100
|
-
// // [
|
|
101
|
-
// // "config",
|
|
102
|
-
// // { config: [["mustBe", "unfalse"]], node: [["value", true]] }
|
|
103
|
-
// // ]
|
|
104
|
-
// // ])
|
|
105
|
-
// attest(t(false).errors?.summary).snap("Must be unfalse (was false)")
|
|
106
|
-
// })
|
|
107
|
-
// it("anonymous type config at path", () => {
|
|
108
|
-
// const unfalse = type("true", { mustBe: "unfalse" })
|
|
109
|
-
// const t = type({ myKey: unfalse })
|
|
110
|
-
// // attest(t.flat).snap([
|
|
111
|
-
// // ["domain", "object"],
|
|
112
|
-
// // [
|
|
113
|
-
// // "requiredProp",
|
|
114
|
-
// // [
|
|
115
|
-
// // "myKey",
|
|
116
|
-
// // [
|
|
117
|
-
// // [
|
|
118
|
-
// // "config",
|
|
119
|
-
// // {
|
|
120
|
-
// // config: [["mustBe", "unfalse"]],
|
|
121
|
-
// // node: [["value", true]]
|
|
122
|
-
// // }
|
|
123
|
-
// // ]
|
|
124
|
-
// // ]
|
|
125
|
-
// // ]
|
|
126
|
-
// // ]
|
|
127
|
-
// // ])
|
|
128
|
-
// attest(t({ myKey: "500" }).errors?.summary).snap(
|
|
129
|
-
// "myKey must be unfalse (was '500')"
|
|
130
|
-
// )
|
|
131
|
-
// // config only applies within myKey
|
|
132
|
-
// attest(t({ yourKey: "500" }).errors?.summary).snap(
|
|
133
|
-
// "myKey must be defined"
|
|
134
|
-
// )
|
|
135
|
-
// })
|
|
136
|
-
// it("anonymous type thunk", () => {
|
|
137
|
-
// const t = type(() => type("false", { mustBe: "untrue" }))
|
|
138
|
-
// attest<false>(t.infer)
|
|
139
|
-
// // attest(t.flat).snap([
|
|
140
|
-
// // [
|
|
141
|
-
// // "config",
|
|
142
|
-
// // { config: [["mustBe", "untrue"]], node: [["value", false]] }
|
|
143
|
-
// // ]
|
|
144
|
-
// // ])
|
|
145
|
-
// })
|
|
146
|
-
// it("anonymous type thunk at path", () => {
|
|
147
|
-
// const t = type({ myKey: () => type("false", { mustBe: "untrue" }) })
|
|
148
|
-
// attest<{ myKey: false }>(t.infer)
|
|
149
|
-
// // attest(t.flat).snap([
|
|
150
|
-
// // ["domain", "object"],
|
|
151
|
-
// // [
|
|
152
|
-
// // "requiredProp",
|
|
153
|
-
// // [
|
|
154
|
-
// // "myKey",
|
|
155
|
-
// // [
|
|
156
|
-
// // [
|
|
157
|
-
// // "config",
|
|
158
|
-
// // {
|
|
159
|
-
// // config: [["mustBe", "untrue"]],
|
|
160
|
-
// // node: [["value", false]]
|
|
161
|
-
// // }
|
|
162
|
-
// // ]
|
|
163
|
-
// // ]
|
|
164
|
-
// // ]
|
|
165
|
-
// // ]
|
|
166
|
-
// // ])
|
|
167
|
-
// })
|
|
5
|
+
it("tuple expression", () => {
|
|
6
|
+
const description = "a series of characters"
|
|
7
|
+
const types = scope({
|
|
8
|
+
a: ["string", "@", description],
|
|
9
|
+
b: {
|
|
10
|
+
a: "a"
|
|
11
|
+
}
|
|
12
|
+
}).export()
|
|
13
|
+
attest<string>(types.a.infer)
|
|
14
|
+
attest(types.a.description).equals(description)
|
|
15
|
+
attest(types.a(1).errors?.summary).snap(
|
|
16
|
+
"Must be a series of characters (was number)"
|
|
17
|
+
)
|
|
18
|
+
attest<{ a: string }>(types.b.infer)
|
|
19
|
+
attest(types.b({ a: true }).errors?.summary).snap(
|
|
20
|
+
"a must be a series of characters (was boolean)"
|
|
21
|
+
)
|
|
22
|
+
})
|
|
23
|
+
it("tuple expression at path", () => {
|
|
24
|
+
const description = "the number of dimensions in the monster group"
|
|
25
|
+
const t = type({
|
|
26
|
+
monster: ["196883", "@", description]
|
|
27
|
+
})
|
|
28
|
+
attest<{ monster: 196883 }>(t.infer)
|
|
29
|
+
attest(t.description).equals(description)
|
|
30
|
+
attest(t({ monster: 196882 }).errors?.summary).snap(
|
|
31
|
+
"monster must be the number of dimensions in the monster group (was 196882)"
|
|
32
|
+
)
|
|
33
|
+
})
|
|
34
|
+
it("anonymous type config", () => {
|
|
35
|
+
const t = type(type("true", "@", { description: "unfalse" }))
|
|
36
|
+
attest<true>(t.infer)
|
|
37
|
+
attest(t(false).errors?.summary).snap("Must be unfalse (was false)")
|
|
38
|
+
})
|
|
39
|
+
it("anonymous type config at path", () => {
|
|
40
|
+
const unfalse = type("true", "@", { description: "unfalse" })
|
|
41
|
+
const t = type({ myKey: unfalse })
|
|
42
|
+
attest(t({ myKey: "500" }).errors?.summary).snap(
|
|
43
|
+
`myKey must be unfalse (was "500")`
|
|
44
|
+
)
|
|
45
|
+
})
|
|
46
|
+
it("anonymous type thunk", () => {
|
|
47
|
+
const t = type(() => type("false", "@", { description: "untrue" }))
|
|
48
|
+
attest<false>(t.infer)
|
|
49
|
+
attest(t.description).snap("untrue")
|
|
50
|
+
})
|
|
51
|
+
it("anonymous type thunk at path", () => {
|
|
52
|
+
const t = type({
|
|
53
|
+
myKey: () => type("false", "@", { description: "untrue" })
|
|
54
|
+
})
|
|
55
|
+
attest<{ myKey: false }>(t.infer)
|
|
56
|
+
attest(t({ myKey: true }).errors?.summary).snap(
|
|
57
|
+
"myKey must be untrue (was true)"
|
|
58
|
+
)
|
|
59
|
+
})
|
|
60
|
+
it("applies config to shallow descendants", () => {
|
|
61
|
+
const user = type({
|
|
62
|
+
name: "string",
|
|
63
|
+
age: "number"
|
|
64
|
+
}).describe("a valid user")
|
|
65
|
+
|
|
66
|
+
// should give the original error at a path
|
|
67
|
+
attest(
|
|
68
|
+
user({
|
|
69
|
+
name: "david",
|
|
70
|
+
age: true
|
|
71
|
+
}).errors?.summary
|
|
72
|
+
).snap("age must be a number (was boolean)")
|
|
73
|
+
|
|
74
|
+
// should give the shallow custom error
|
|
75
|
+
attest(user(null).errors?.summary).snap("Must be a valid user (was null)")
|
|
76
|
+
})
|
|
168
77
|
})
|