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.
Files changed (52) hide show
  1. package/__tests__/array.test.ts +252 -71
  2. package/__tests__/config.test.ts +75 -166
  3. package/__tests__/declared.test.ts +5 -4
  4. package/__tests__/divisor.test.ts +6 -2
  5. package/__tests__/instanceof.test.ts +1 -1
  6. package/__tests__/keyTraversal.test.ts +71 -70
  7. package/__tests__/match.bench.ts +31 -0
  8. package/__tests__/match.test.ts +216 -26
  9. package/__tests__/narrow.test.ts +3 -3
  10. package/__tests__/objectLiteral.test.ts +19 -18
  11. package/match.ts +206 -79
  12. package/out/match.d.ts +60 -31
  13. package/out/match.d.ts.map +1 -1
  14. package/out/match.js +52 -10
  15. package/out/match.js.map +1 -1
  16. package/out/parser/definition.d.ts +6 -4
  17. package/out/parser/definition.d.ts.map +1 -1
  18. package/out/parser/definition.js.map +1 -1
  19. package/out/parser/objectLiteral.d.ts.map +1 -1
  20. package/out/parser/objectLiteral.js +31 -35
  21. package/out/parser/objectLiteral.js.map +1 -1
  22. package/out/parser/semantic/semantic.d.ts +6 -7
  23. package/out/parser/semantic/semantic.d.ts.map +1 -1
  24. package/out/parser/shared.d.ts +10 -2
  25. package/out/parser/shared.d.ts.map +1 -1
  26. package/out/parser/shared.js +13 -4
  27. package/out/parser/shared.js.map +1 -1
  28. package/out/parser/string/reduce/static.d.ts +1 -1
  29. package/out/parser/string/reduce/static.d.ts.map +1 -1
  30. package/out/parser/tuple.d.ts +15 -14
  31. package/out/parser/tuple.d.ts.map +1 -1
  32. package/out/parser/tuple.js +94 -53
  33. package/out/parser/tuple.js.map +1 -1
  34. package/out/scope.d.ts +6 -11
  35. package/out/scope.d.ts.map +1 -1
  36. package/out/scope.js +7 -25
  37. package/out/scope.js.map +1 -1
  38. package/out/type.d.ts +5 -9
  39. package/out/type.d.ts.map +1 -1
  40. package/out/type.js +14 -10
  41. package/out/type.js.map +1 -1
  42. package/package.json +3 -3
  43. package/parser/definition.ts +7 -7
  44. package/parser/objectLiteral.ts +47 -51
  45. package/parser/semantic/semantic.ts +8 -9
  46. package/parser/semantic/validate.ts +1 -1
  47. package/parser/shared.ts +27 -9
  48. package/parser/string/reduce/static.ts +2 -2
  49. package/parser/tuple.ts +175 -86
  50. package/scope.ts +12 -35
  51. package/type.ts +20 -18
  52. package/parser/semantic/intersections.ts +0 -46
@@ -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
- prematureRestMessage,
7
- writeNonArrayRestMessage
6
+ multipleVariadicMesage,
7
+ writeNonArraySpreadMessage
8
8
  } from "../parser/tuple.js"
9
9
 
10
10
  describe("array", () => {
11
11
  describe("base", () => {
12
- it("base", () => {
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: "boolean", b: ["null"] }]])
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: boolean
153
+ a: bigint
117
154
  b: [null]
118
155
  }
119
156
  ]
120
157
  ]
121
158
  >(t.infer)
122
-
123
- attest(t.allows([["", 0], [{ a: true, b: [null] }]])).equals(true)
124
- attest(t.allows([["", 0], [{ a: true, b: [undefined] }]])).equals(false)
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
- describe("variadic", () => {
129
- it("spreads simple arrays", () => {
130
- const wellRested = type(["string", "...number[]"])
131
- attest<[string, ...number[]]>(wellRested.infer)
132
- attest(wellRested(["foo"]).out).equals(["foo"])
133
- attest(wellRested(["foo", 1, 2]).out).equals(["foo", 1, 2])
134
- })
135
- it("tuple expression", () => {
136
- const wellRestedTuple = type([
137
- "number",
138
- ["...", [{ a: "string" }, "[]"]]
139
- ])
140
- attest<[number, ...{ a: string }[]]>(wellRestedTuple.infer)
141
- })
142
- it("spreads array expressions", () => {
143
- const greatSpread = type([{ a: "boolean" }, "...(Date|RegExp)[]"])
144
- attest<
145
- [
146
- {
147
- a: boolean
148
- },
149
- ...(RegExp | Date)[]
150
- ]
151
- >(greatSpread.infer)
152
- })
153
- it("allows array keyword", () => {
154
- const types = scope({
155
- myArrayKeyword: "boolean[]",
156
- myVariadicKeyword: ["string", "...myArrayKeyword"]
157
- }).export()
158
- attest<[string, ...boolean[]]>(types.myVariadicKeyword.infer)
159
- })
160
- it("errors on non-array", () => {
161
- attest(() =>
162
- // @ts-expect-error
163
- type(["email", "...symbol"])
164
- ).throwsAndHasTypeError(writeNonArrayRestMessage("symbol"))
165
- attest(() =>
166
- // @ts-expect-error
167
- type(["number", ["...", "string"]])
168
- ).throwsAndHasTypeError(writeNonArrayRestMessage("string"))
169
- })
170
- it("errors on non-last element", () => {
171
- attest(() =>
172
- // @ts-expect-error
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
- type([["...", "string[]"], "number"])
178
- ).throwsAndHasTypeError(prematureRestMessage)
179
- })
216
+ ["...", "number[]"]
217
+ ])
218
+ ).throwsAndHasTypeError(multipleVariadicMesage)
180
219
  })
181
220
  })
182
221
  describe("intersection", () => {
183
222
  it("shallow array intersection", () => {
184
- const actual = type("string[]&'foo'[]").json
185
- const expected = type("'foo'[]").json
186
- attest(actual).is(expected)
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 actual = type([{ a: "string" }, "[]"]).and([
190
- { b: "number" },
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" }]).json
237
- attest(tupleAndArray.json).is(expected)
238
- attest(arrayAndTuple.json).is(expected)
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", () => {
@@ -1,168 +1,77 @@
1
+ import { attest } from "@arktype/attest"
2
+ import { scope, type } from "arktype"
3
+
1
4
  describe("config traversal", () => {
2
- // it("tuple expression", () => {
3
- // const mustBe = "a series of characters"
4
- // const types = scope({
5
- // a: ["string", ":", { mustBe }],
6
- // b: {
7
- // a: "a"
8
- // }
9
- // }).compile()
10
- // attest<string>(types.a.infer)
11
- // // attest(types.a.flat).snap([
12
- // // [
13
- // // "config",
14
- // // {
15
- // // config: [["mustBe", "a series of characters"]],
16
- // // node: "string"
17
- // // }
18
- // // ]
19
- // // ])
20
- // attest(types.a(1).errors?.summary).snap(
21
- // "Must be a series of characters (was number)"
22
- // )
23
- // attest<{ a: string }>(types.b.infer)
24
- // // attest(types.b.flat).equals([
25
- // // ["domain", "object"],
26
- // // [
27
- // // "requiredProp",
28
- // // [
29
- // // "a",
30
- // // [
31
- // // [
32
- // // "config",
33
- // // {
34
- // // config: [["mustBe", mustBe]],
35
- // // node: "string"
36
- // // }
37
- // // ]
38
- // // ]
39
- // // ]
40
- // // ]
41
- // // ])
42
- // attest(types.b({ a: true }).errors?.summary).snap(
43
- // "a must be a series of characters (was boolean)"
44
- // )
45
- // })
46
- // it("tuple expression at path", () => {
47
- // const t = type({
48
- // monster: [
49
- // "196883",
50
- // ":",
51
- // {
52
- // mustBe: "the number of dimensions in the monster group"
53
- // }
54
- // ]
55
- // })
56
- // attest<{ monster: 196883 }>(t.infer)
57
- // // attest(t.node).snap({
58
- // // object: {
59
- // // props: {
60
- // // monster: {
61
- // // node: { number: { value: 196883 } },
62
- // // config: {
63
- // // mustBe: "the number of dimensions in the monster group"
64
- // // }
65
- // // }
66
- // // }
67
- // // }
68
- // // })
69
- // // attest(t.flat).snap([
70
- // // ["domain", "object"],
71
- // // [
72
- // // "requiredProp",
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
  })