arktype 2.0.0-dev.1 → 2.0.0-dev.10
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 +270 -151
- package/__tests__/badDefinitionType.test.ts +10 -3
- package/__tests__/basis.test.ts +6 -2
- package/__tests__/bounds.test.ts +282 -352
- package/__tests__/cast.test.ts +7 -2
- package/__tests__/completions.test.ts +8 -3
- package/__tests__/config.test.ts +95 -167
- package/__tests__/dateLiteral.test.ts +8 -4
- package/__tests__/declared.test.ts +7 -6
- package/__tests__/discrimination.test.ts +1 -1
- package/__tests__/divisor.test.ts +46 -22
- package/__tests__/enclosed.test.ts +39 -25
- package/__tests__/examples.test.ts +56 -56
- package/__tests__/expressions.test.ts +184 -112
- package/__tests__/generic.test.ts +307 -288
- package/__tests__/group.test.ts +67 -54
- package/__tests__/imports.test.ts +62 -49
- package/__tests__/instanceof.test.ts +25 -23
- package/__tests__/intersection.test.ts +52 -48
- package/__tests__/keyTraversal.test.ts +77 -73
- package/__tests__/keyof.test.ts +68 -52
- package/__tests__/keywords.test.ts +164 -138
- package/__tests__/literal.test.ts +60 -21
- package/__tests__/match.bench.ts +31 -0
- package/__tests__/match.test.ts +222 -22
- package/__tests__/narrow.test.ts +52 -54
- package/__tests__/objectLiteral.test.ts +347 -205
- package/__tests__/operator.bench.ts +4 -4
- package/__tests__/pipe.test.ts +501 -0
- package/__tests__/realWorld.test.ts +63 -0
- package/__tests__/regex.test.ts +17 -7
- package/__tests__/scope.bench.ts +1 -1
- package/__tests__/scope.test.ts +257 -23
- package/__tests__/serialization.test.ts +19 -0
- package/__tests__/string.test.ts +11 -7
- package/__tests__/submodule.test.ts +26 -39
- package/__tests__/this.test.ts +83 -57
- package/__tests__/thunk.test.ts +13 -7
- package/__tests__/traverse.test.ts +76 -54
- package/__tests__/tsGenerics.test.ts +12 -12
- package/__tests__/type.test.ts +36 -0
- package/__tests__/typeReference.test.ts +7 -7
- package/__tests__/unenclosed.test.ts +116 -100
- package/__tests__/union.test.ts +171 -104
- package/ark.ts +21 -65
- package/generic.ts +69 -0
- package/main.ts +6 -18
- package/match.ts +195 -93
- package/module.ts +15 -0
- package/out/ark.d.ts +15 -39
- package/out/ark.js +9 -18
- package/out/ark.js.map +1 -1
- package/out/generic.d.ts +21 -0
- package/out/generic.js +24 -0
- package/out/generic.js.map +1 -0
- package/out/main.d.ts +6 -7
- package/out/main.js +4 -3
- package/out/main.js.map +1 -1
- package/out/match.d.ts +57 -34
- package/out/match.js +51 -17
- package/out/match.js.map +1 -1
- package/out/module.d.ts +10 -0
- package/out/module.js +3 -0
- package/out/module.js.map +1 -0
- package/out/parser/definition.d.ts +11 -13
- package/out/parser/definition.js +10 -13
- package/out/parser/definition.js.map +1 -1
- package/out/parser/generic.d.ts +5 -7
- package/out/parser/generic.js +7 -11
- package/out/parser/generic.js.map +1 -1
- package/out/parser/objectLiteral.d.ts +45 -14
- package/out/parser/objectLiteral.js +61 -52
- package/out/parser/objectLiteral.js.map +1 -1
- package/out/parser/semantic/bounds.d.ts +10 -8
- package/out/parser/semantic/bounds.js +0 -1
- package/out/parser/semantic/bounds.js.map +1 -1
- package/out/parser/semantic/divisor.d.ts +5 -7
- package/out/parser/semantic/divisor.js +0 -1
- package/out/parser/semantic/infer.d.ts +24 -0
- package/out/parser/semantic/infer.js +1 -0
- package/out/parser/semantic/infer.js.map +1 -0
- package/out/parser/semantic/utils.d.ts +3 -7
- package/out/parser/semantic/utils.js +0 -1
- package/out/parser/semantic/validate.d.ts +15 -14
- package/out/parser/semantic/validate.js +1 -1
- package/out/parser/semantic/validate.js.map +1 -1
- package/out/parser/string/reduce/dynamic.d.ts +13 -13
- package/out/parser/string/reduce/dynamic.js +31 -26
- package/out/parser/string/reduce/dynamic.js.map +1 -1
- package/out/parser/string/reduce/shared.d.ts +6 -20
- package/out/parser/string/reduce/shared.js +0 -1
- package/out/parser/string/reduce/shared.js.map +1 -1
- package/out/parser/string/reduce/static.d.ts +27 -24
- package/out/parser/string/reduce/static.js +0 -1
- package/out/parser/string/shift/operand/date.d.ts +3 -3
- package/out/parser/string/shift/operand/date.js +4 -7
- package/out/parser/string/shift/operand/date.js.map +1 -1
- package/out/parser/string/shift/operand/enclosed.d.ts +4 -19
- package/out/parser/string/shift/operand/enclosed.js +12 -13
- package/out/parser/string/shift/operand/enclosed.js.map +1 -1
- package/out/parser/string/shift/operand/genericArgs.d.ts +9 -10
- package/out/parser/string/shift/operand/genericArgs.js +6 -10
- package/out/parser/string/shift/operand/genericArgs.js.map +1 -1
- package/out/parser/string/shift/operand/operand.d.ts +3 -4
- package/out/parser/string/shift/operand/operand.js +8 -12
- package/out/parser/string/shift/operand/operand.js.map +1 -1
- package/out/parser/string/shift/operand/unenclosed.d.ts +15 -22
- package/out/parser/string/shift/operand/unenclosed.js +24 -36
- package/out/parser/string/shift/operand/unenclosed.js.map +1 -1
- package/out/parser/string/shift/operator/bounds.d.ts +9 -13
- package/out/parser/string/shift/operator/bounds.js +43 -54
- package/out/parser/string/shift/operator/bounds.js.map +1 -1
- package/out/parser/string/shift/operator/divisor.d.ts +2 -4
- package/out/parser/string/shift/operator/divisor.js +1 -3
- package/out/parser/string/shift/operator/divisor.js.map +1 -1
- package/out/parser/string/shift/operator/operator.d.ts +2 -3
- package/out/parser/string/shift/operator/operator.js +12 -19
- package/out/parser/string/shift/operator/operator.js.map +1 -1
- package/out/parser/string/shift/scanner.d.ts +2 -2
- package/out/parser/string/shift/scanner.js +14 -16
- package/out/parser/string/shift/scanner.js.map +1 -1
- package/out/parser/string/string.d.ts +8 -6
- package/out/parser/string/string.js +1 -3
- package/out/parser/string/string.js.map +1 -1
- package/out/parser/tuple.d.ts +102 -43
- package/out/parser/tuple.js +119 -87
- package/out/parser/tuple.js.map +1 -1
- package/out/scope.d.ts +43 -120
- package/out/scope.js +39 -209
- package/out/scope.js.map +1 -1
- package/out/type.d.ts +43 -64
- package/out/type.js +30 -131
- package/out/type.js.map +1 -1
- package/package.json +7 -3
- package/parser/definition.ts +77 -93
- package/parser/generic.ts +38 -40
- package/parser/objectLiteral.ts +172 -136
- package/parser/semantic/bounds.ts +33 -32
- package/parser/semantic/divisor.ts +8 -10
- package/parser/semantic/infer.ts +141 -0
- package/parser/semantic/utils.ts +12 -23
- package/parser/semantic/validate.ts +81 -73
- package/parser/string/reduce/dynamic.ts +67 -55
- package/parser/string/reduce/shared.ts +1 -3
- package/parser/string/reduce/static.ts +67 -69
- package/parser/string/shift/operand/date.ts +15 -14
- package/parser/string/shift/operand/enclosed.ts +30 -25
- package/parser/string/shift/operand/genericArgs.ts +46 -45
- package/parser/string/shift/operand/operand.ts +32 -38
- package/parser/string/shift/operand/unenclosed.ts +107 -158
- package/parser/string/shift/operator/bounds.ts +118 -112
- package/parser/string/shift/operator/divisor.ts +14 -17
- package/parser/string/shift/operator/operator.ts +35 -46
- package/parser/string/shift/scanner.ts +51 -50
- package/parser/string/string.ts +30 -35
- package/parser/tuple.ts +375 -307
- package/scope.ts +170 -453
- package/type.ts +193 -274
- package/__tests__/astUtils.test.ts +0 -17
- package/__tests__/cyclic.test.ts +0 -144
- package/__tests__/jsdoc.test.ts +0 -64
- package/__tests__/morph.test.ts +0 -409
- package/__tests__/nodeDefinitions.test.ts +0 -190
- package/__tests__/typeMethods.test.ts +0 -69
- package/out/ark.d.ts.map +0 -1
- package/out/arkConfig.d.ts +0 -2
- package/out/arkConfig.d.ts.map +0 -1
- package/out/arkConfig.js +0 -10
- package/out/arkConfig.js.map +0 -1
- package/out/main.d.ts.map +0 -1
- package/out/match.d.ts.map +0 -1
- package/out/parser/definition.d.ts.map +0 -1
- package/out/parser/generic.d.ts.map +0 -1
- package/out/parser/objectLiteral.d.ts.map +0 -1
- package/out/parser/semantic/bounds.d.ts.map +0 -1
- package/out/parser/semantic/divisor.d.ts.map +0 -1
- package/out/parser/semantic/intersections.d.ts +0 -14
- package/out/parser/semantic/intersections.d.ts.map +0 -1
- package/out/parser/semantic/intersections.js +0 -2
- package/out/parser/semantic/intersections.js.map +0 -1
- package/out/parser/semantic/semantic.d.ts +0 -33
- package/out/parser/semantic/semantic.d.ts.map +0 -1
- package/out/parser/semantic/semantic.js +0 -2
- package/out/parser/semantic/semantic.js.map +0 -1
- package/out/parser/semantic/utils.d.ts.map +0 -1
- package/out/parser/semantic/validate.d.ts.map +0 -1
- package/out/parser/shared.d.ts +0 -54
- package/out/parser/shared.d.ts.map +0 -1
- package/out/parser/shared.js +0 -47
- package/out/parser/shared.js.map +0 -1
- package/out/parser/string/reduce/dynamic.d.ts.map +0 -1
- package/out/parser/string/reduce/shared.d.ts.map +0 -1
- package/out/parser/string/reduce/static.d.ts.map +0 -1
- package/out/parser/string/shift/operand/date.d.ts.map +0 -1
- package/out/parser/string/shift/operand/enclosed.d.ts.map +0 -1
- package/out/parser/string/shift/operand/genericArgs.d.ts.map +0 -1
- package/out/parser/string/shift/operand/operand.d.ts.map +0 -1
- package/out/parser/string/shift/operand/unenclosed.d.ts.map +0 -1
- package/out/parser/string/shift/operator/bounds.d.ts.map +0 -1
- package/out/parser/string/shift/operator/divisor.d.ts.map +0 -1
- package/out/parser/string/shift/operator/operator.d.ts.map +0 -1
- package/out/parser/string/shift/scanner.d.ts.map +0 -1
- package/out/parser/string/string.d.ts.map +0 -1
- package/out/parser/tuple.d.ts.map +0 -1
- package/out/scope.d.ts.map +0 -1
- package/out/test.d.ts +0 -2
- package/out/test.d.ts.map +0 -1
- package/out/test.js +0 -7
- package/out/test.js.map +0 -1
- package/out/tsconfig.build.tsbuildinfo +0 -1
- package/out/type.d.ts.map +0 -1
- package/out/util.d.ts +0 -19
- package/out/util.d.ts.map +0 -1
- package/out/util.js +0 -7
- package/out/util.js.map +0 -1
- package/parser/semantic/intersections.ts +0 -46
- package/parser/semantic/semantic.ts +0 -144
- package/parser/shared.ts +0 -152
- package/util.ts +0 -36
package/__tests__/this.test.ts
CHANGED
|
@@ -1,57 +1,83 @@
|
|
|
1
|
-
import { attest } from "@arktype/attest"
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
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
|
-
|
|
1
|
+
// import { attest, contextualize } from "@arktype/attest"
|
|
2
|
+
// import { writeUnresolvableMessage } from "@arktype/schema"
|
|
3
|
+
// import { scope, type } from "arktype"
|
|
4
|
+
|
|
5
|
+
// contextualize(() => {
|
|
6
|
+
// it("resolves from type", () => {
|
|
7
|
+
// const disappointingGift = type({
|
|
8
|
+
// label: "string",
|
|
9
|
+
// "box?": "this"
|
|
10
|
+
// })
|
|
11
|
+
|
|
12
|
+
// type ExpectedDisappointingGift = {
|
|
13
|
+
// label: string
|
|
14
|
+
// box?: ExpectedDisappointingGift
|
|
15
|
+
// }
|
|
16
|
+
// attest<ExpectedDisappointingGift>(disappointingGift.infer)
|
|
17
|
+
// })
|
|
18
|
+
|
|
19
|
+
// it("doesn't change when rereferenced", () => {
|
|
20
|
+
// const initial = type({
|
|
21
|
+
// initial: "this"
|
|
22
|
+
// })
|
|
23
|
+
|
|
24
|
+
// const reference = type({
|
|
25
|
+
// reference: initial
|
|
26
|
+
// })
|
|
27
|
+
// type Initial = {
|
|
28
|
+
// initial: Initial
|
|
29
|
+
// }
|
|
30
|
+
// type Expected = {
|
|
31
|
+
// reference: Initial
|
|
32
|
+
// }
|
|
33
|
+
|
|
34
|
+
// attest<Expected>(reference.infer)
|
|
35
|
+
// const types = scope({
|
|
36
|
+
// initial: {
|
|
37
|
+
// initial: "initial"
|
|
38
|
+
// },
|
|
39
|
+
// reference: {
|
|
40
|
+
// reference: "initial"
|
|
41
|
+
// }
|
|
42
|
+
// }).export()
|
|
43
|
+
// attest(reference.json).equals(types.reference.json)
|
|
44
|
+
// })
|
|
45
|
+
|
|
46
|
+
// it("unresolvable in scope", () => {
|
|
47
|
+
// attest(() =>
|
|
48
|
+
// scope({
|
|
49
|
+
// disappointingGift: {
|
|
50
|
+
// label: "string",
|
|
51
|
+
// // @ts-expect-error
|
|
52
|
+
// "box?": "this"
|
|
53
|
+
// }
|
|
54
|
+
// }).export()
|
|
55
|
+
// ).throwsAndHasTypeError(writeUnresolvableMessage("this"))
|
|
56
|
+
// })
|
|
57
|
+
|
|
58
|
+
// it("root expression", () => {
|
|
59
|
+
// const t = type({ a: "string" }, "|", { b: "this" })
|
|
60
|
+
// attest(t.infer).type.toString.snap(
|
|
61
|
+
// "{ a: string; } | { b: { a: string; } | any; }"
|
|
62
|
+
// )
|
|
63
|
+
// attest(t.json).equals(type([{ a: "string" }, "|", { b: "this" }]).json)
|
|
64
|
+
// })
|
|
65
|
+
|
|
66
|
+
// // TODO: reenable
|
|
67
|
+
// it("tuple expression", () => {
|
|
68
|
+
// const t = type([{ a: "string" }, "|", { b: "this" }])
|
|
69
|
+
// attest(t.infer).type.toString.snap(
|
|
70
|
+
// "{ a: string; } | { b: { a: string; } | any; }"
|
|
71
|
+
// )
|
|
72
|
+
// const types = scope({
|
|
73
|
+
// a: {
|
|
74
|
+
// a: "string"
|
|
75
|
+
// },
|
|
76
|
+
// b: {
|
|
77
|
+
// b: "expected"
|
|
78
|
+
// },
|
|
79
|
+
// expected: "a|b"
|
|
80
|
+
// }).export()
|
|
81
|
+
// attest(t.json).equals(types.expected.json)
|
|
82
|
+
// })
|
|
83
|
+
// })
|
package/__tests__/thunk.test.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { attest } from "@arktype/attest"
|
|
1
|
+
import { attest, contextualize } from "@arktype/attest"
|
|
2
|
+
import { writeUnresolvableMessage } from "@arktype/schema"
|
|
2
3
|
import { scope, type } from "arktype"
|
|
3
4
|
import { writeBadDefinitionTypeMessage } from "../parser/definition.js"
|
|
4
|
-
import { writeUnresolvableMessage } from "../parser/string/shift/operand/unenclosed.js"
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
it("
|
|
6
|
+
contextualize(() => {
|
|
7
|
+
it("in type", () => {
|
|
8
8
|
const t = type(() => type("boolean"))
|
|
9
9
|
attest<boolean>(t.infer)
|
|
10
10
|
attest(() => {
|
|
@@ -12,7 +12,8 @@ describe("thunk", () => {
|
|
|
12
12
|
type(() => type("moolean"))
|
|
13
13
|
}).throwsAndHasTypeError(writeUnresolvableMessage("moolean"))
|
|
14
14
|
})
|
|
15
|
-
|
|
15
|
+
|
|
16
|
+
it("in scope", () => {
|
|
16
17
|
const $ = scope({
|
|
17
18
|
a: () => $.type({ b: "b" }),
|
|
18
19
|
b: () => $.type({ a: "string" })
|
|
@@ -42,6 +43,7 @@ describe("thunk", () => {
|
|
|
42
43
|
// object: { props: { a: { object: { props: { a: "string" } } } } }
|
|
43
44
|
// })
|
|
44
45
|
})
|
|
46
|
+
|
|
45
47
|
it("expression from thunk", () => {
|
|
46
48
|
const $ = scope({
|
|
47
49
|
a: () => $.type({ a: "string" }),
|
|
@@ -54,22 +56,26 @@ describe("thunk", () => {
|
|
|
54
56
|
// object: { props: { a: "string", b: "boolean" } }
|
|
55
57
|
// })
|
|
56
58
|
})
|
|
57
|
-
|
|
59
|
+
|
|
60
|
+
it("shallow in type", () => {
|
|
58
61
|
const t = type(() => type("string"))
|
|
59
62
|
attest(t.json).equals(type("string").json)
|
|
60
63
|
attest<string>(t.infer)
|
|
61
64
|
})
|
|
62
|
-
|
|
65
|
+
|
|
66
|
+
it("deep in type", () => {
|
|
63
67
|
const t = type({ a: () => type("string") })
|
|
64
68
|
attest(t.json).equals(type({ a: "string" }).json)
|
|
65
69
|
attest<{ a: string }>(t.infer)
|
|
66
70
|
})
|
|
71
|
+
|
|
67
72
|
it("non-type thunk in scope", () => {
|
|
68
73
|
const $ = scope({
|
|
69
74
|
a: () => 42
|
|
70
75
|
})
|
|
71
76
|
attest(() => $.export()).throws(writeBadDefinitionTypeMessage("number"))
|
|
72
77
|
})
|
|
78
|
+
|
|
73
79
|
it("parse error in thunk in scope", () => {
|
|
74
80
|
const $ = scope({
|
|
75
81
|
// @ts-expect-error
|
|
@@ -1,119 +1,141 @@
|
|
|
1
|
-
import { attest } from "@arktype/attest"
|
|
1
|
+
import { attest, contextualize } from "@arktype/attest"
|
|
2
2
|
import { scope, type } from "arktype"
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
contextualize(() => {
|
|
5
5
|
it("divisible", () => {
|
|
6
6
|
const t = type("number%2")
|
|
7
|
-
attest(t(4)
|
|
8
|
-
attest(t(5).
|
|
7
|
+
attest(t(4)).snap(4)
|
|
8
|
+
attest(t(5).toString()).snap("must be a multiple of 2 (was 5)")
|
|
9
9
|
})
|
|
10
|
+
|
|
10
11
|
it("range", () => {
|
|
11
12
|
const t = type("number>2")
|
|
12
|
-
attest(t(3)
|
|
13
|
-
attest(t(2).
|
|
13
|
+
attest(t(3)).snap(3)
|
|
14
|
+
attest(t(2).toString()).snap("must be more than 2 (was 2)")
|
|
14
15
|
})
|
|
16
|
+
|
|
15
17
|
it("domain", () => {
|
|
16
18
|
const t = type("number")
|
|
17
|
-
attest(t(5)
|
|
18
|
-
attest(t("foo").
|
|
19
|
+
attest(t(5)).snap(5)
|
|
20
|
+
attest(t("foo").toString()).snap("must be a number (was string)")
|
|
19
21
|
})
|
|
22
|
+
|
|
20
23
|
it("regex", () => {
|
|
21
24
|
const t = type("/.*@arktype.io/")
|
|
22
|
-
attest(t("shawn@arktype.io")
|
|
23
|
-
attest(t("shawn@hotmail.com").
|
|
24
|
-
|
|
25
|
+
attest(t("shawn@arktype.io")).snap("shawn@arktype.io")
|
|
26
|
+
attest(t("shawn@hotmail.com").toString()).snap(
|
|
27
|
+
'must be matched by .*@arktype.io (was "shawn@hotmail.com")'
|
|
25
28
|
)
|
|
26
29
|
})
|
|
30
|
+
|
|
27
31
|
it("required keys", () => {
|
|
28
32
|
const t = type({
|
|
29
33
|
name: "string",
|
|
30
34
|
age: "number",
|
|
31
35
|
"title?": "string"
|
|
32
36
|
})
|
|
33
|
-
attest(t({ name: "Shawn", age: 99 })
|
|
37
|
+
attest(t({ name: "Shawn", age: 99 })).snap({
|
|
34
38
|
name: "Shawn",
|
|
35
39
|
age: 99
|
|
36
40
|
})
|
|
37
|
-
attest(t({ name: "Shawn" }).
|
|
41
|
+
attest(t({ name: "Shawn" }).toString()).snap(
|
|
42
|
+
"age must be a number (was missing)"
|
|
43
|
+
)
|
|
38
44
|
})
|
|
45
|
+
|
|
39
46
|
it("customized builtin problem", () => {
|
|
40
47
|
const types = scope(
|
|
41
|
-
{ isEven: "number%2" }
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
// }
|
|
49
|
-
// }
|
|
50
|
-
//}
|
|
48
|
+
{ isEven: "number%2" },
|
|
49
|
+
{
|
|
50
|
+
divisor: {
|
|
51
|
+
expected: ctx => `% ${ctx.rule} !== 0`,
|
|
52
|
+
problem: ctx => `${ctx.actual} ${ctx.expected}`
|
|
53
|
+
}
|
|
54
|
+
}
|
|
51
55
|
).export()
|
|
52
|
-
attest(types.isEven(3).
|
|
56
|
+
attest(types.isEven(3).toString()).snap("3 % 2 !== 0")
|
|
53
57
|
})
|
|
58
|
+
|
|
54
59
|
it("domains", () => {
|
|
55
60
|
const t = type("string|number[]")
|
|
56
|
-
attest(t([1])
|
|
57
|
-
attest(t("hello")
|
|
58
|
-
attest(t(2).
|
|
61
|
+
attest(t([1])).snap([1])
|
|
62
|
+
attest(t("hello")).snap("hello")
|
|
63
|
+
attest(t(2).toString()).snap("must be a string or an array (was number)")
|
|
59
64
|
})
|
|
65
|
+
|
|
60
66
|
it("tuple length", () => {
|
|
61
67
|
const t = type(["string", "number", "string", "string[]"])
|
|
62
68
|
const data: typeof t.infer = ["foo", 5, "boo", []]
|
|
63
|
-
attest(t(data)
|
|
64
|
-
attest(t(["hello"]).
|
|
69
|
+
attest(t(data)).equals(data)
|
|
70
|
+
attest(t(["hello"]).toString()).snap(
|
|
71
|
+
'must be exactly length 4 (was ["hello"])'
|
|
72
|
+
)
|
|
65
73
|
})
|
|
74
|
+
|
|
66
75
|
it("branches", () => {
|
|
67
|
-
const t = type(
|
|
68
|
-
attest(t({
|
|
69
|
-
attest(t({
|
|
70
|
-
attest(t({}).
|
|
71
|
-
"
|
|
76
|
+
const t = type({ bar: "boolean" }, "|", { foo: "string" })
|
|
77
|
+
attest(t({ foo: "ok" })).snap({ foo: "ok" })
|
|
78
|
+
attest(t({ bar: true })).snap({ bar: true })
|
|
79
|
+
attest(t({}).toString()).snap(
|
|
80
|
+
"bar must be boolean (was missing) or foo must be a string (was missing)"
|
|
81
|
+
)
|
|
82
|
+
attest(t({ bar: "swapped", foo: true }).toString()).snap(
|
|
83
|
+
'bar must be boolean (was "swapped") or foo must be a string (was true)'
|
|
72
84
|
)
|
|
73
85
|
})
|
|
86
|
+
|
|
74
87
|
it("branches at path", () => {
|
|
75
88
|
const t = type({ key: [{ a: "string" }, "|", { b: "boolean" }] })
|
|
76
|
-
attest(t({ key: { a: "ok" } })
|
|
77
|
-
attest(t({ key: { b: true } })
|
|
78
|
-
attest(t({ key: {} }).
|
|
79
|
-
"
|
|
89
|
+
attest(t({ key: { a: "ok" } })).snap({ key: { a: "ok" } })
|
|
90
|
+
attest(t({ key: { b: true } })).snap({ key: { b: true } })
|
|
91
|
+
attest(t({ key: {} }).toString()).snap(
|
|
92
|
+
"key.a must be a string (was missing) or key.b must be boolean (was missing)"
|
|
80
93
|
)
|
|
81
94
|
})
|
|
95
|
+
|
|
82
96
|
it("switch", () => {
|
|
83
|
-
const t = type(
|
|
84
|
-
attest(t({ a: "ok" })
|
|
85
|
-
attest(t({ a:
|
|
97
|
+
const t = type({ a: "string" }).or({ a: "null" }).or({ a: "number" })
|
|
98
|
+
attest(t({ a: "ok" })).snap({ a: "ok" })
|
|
99
|
+
attest(t({ a: 5 })).snap({ a: 5 })
|
|
86
100
|
// value isn't present
|
|
87
|
-
attest(t({}).
|
|
88
|
-
"a must be a string or
|
|
101
|
+
attest(t({}).toString()).snap(
|
|
102
|
+
"a must be a number, a string or null (was missing)"
|
|
89
103
|
)
|
|
90
104
|
// unsatisfying value
|
|
91
|
-
attest(t({ a:
|
|
92
|
-
"a must be a string or
|
|
105
|
+
attest(t({ a: false }).toString()).snap(
|
|
106
|
+
"a must be a number, a string or null (was false)"
|
|
93
107
|
)
|
|
94
108
|
})
|
|
109
|
+
|
|
95
110
|
it("multiple switch", () => {
|
|
96
111
|
const types = scope({
|
|
97
|
-
a: {
|
|
98
|
-
b: {
|
|
99
|
-
c: {
|
|
112
|
+
a: { foo: "string" },
|
|
113
|
+
b: { foo: "number" },
|
|
114
|
+
c: { foo: "Function" },
|
|
100
115
|
d: "a|b|c"
|
|
101
116
|
}).export()
|
|
102
|
-
attest(types.d({}).
|
|
103
|
-
"
|
|
117
|
+
attest(types.d({}).toString()).snap(
|
|
118
|
+
"foo must be a function, a number or a string (was missing)"
|
|
119
|
+
)
|
|
120
|
+
attest(types.d({ foo: null }).toString()).snap(
|
|
121
|
+
"foo must be a function, a number or a string (was null)"
|
|
104
122
|
)
|
|
105
123
|
})
|
|
106
124
|
|
|
107
125
|
it("multi", () => {
|
|
108
126
|
const naturalNumber = type("integer>0")
|
|
109
|
-
attest(naturalNumber(-1.2).
|
|
110
|
-
|
|
127
|
+
attest(naturalNumber(-1.2).toString()).snap(
|
|
128
|
+
`must be...
|
|
129
|
+
• an integer
|
|
130
|
+
• more than 0`
|
|
111
131
|
)
|
|
112
132
|
const naturalAtPath = type({
|
|
113
133
|
natural: naturalNumber
|
|
114
134
|
})
|
|
115
|
-
attest(naturalAtPath({ natural: -0.1 }).
|
|
116
|
-
|
|
135
|
+
attest(naturalAtPath({ natural: -0.1 }).toString()).snap(
|
|
136
|
+
`natural must be...
|
|
137
|
+
• an integer
|
|
138
|
+
• more than 0`
|
|
117
139
|
)
|
|
118
140
|
})
|
|
119
141
|
})
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
describe("tsGenerics", () => {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
})
|
|
1
|
+
// describe("tsGenerics", () => {
|
|
2
|
+
// it("in scope", () => {
|
|
3
|
+
// const types = scope({
|
|
4
|
+
// t: "Record<string, number>"
|
|
5
|
+
// }).export()
|
|
6
|
+
// attest<Record<string, number>>(types.t.infer)
|
|
7
|
+
// })
|
|
8
|
+
// it("in module", () => {
|
|
9
|
+
// const t = arktypes.Record("string", "number")
|
|
10
|
+
// attest<Record<string, number>>(t.infer)
|
|
11
|
+
// })
|
|
12
|
+
// })
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { attest, contextualize } from "@arktype/attest"
|
|
2
|
+
import { ArkError, type } from "arktype"
|
|
3
|
+
import { AssertionError } from "node:assert"
|
|
4
|
+
|
|
5
|
+
contextualize(() => {
|
|
6
|
+
it("root discriminates", () => {
|
|
7
|
+
const t = type("string")
|
|
8
|
+
const out = t("")
|
|
9
|
+
if (out instanceof type.errors) out.throw()
|
|
10
|
+
else attest<string>(out)
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
it("allows", () => {
|
|
14
|
+
const t = type("number%2")
|
|
15
|
+
const data: unknown = 4
|
|
16
|
+
if (t.allows(data)) {
|
|
17
|
+
// narrows correctly
|
|
18
|
+
attest<number>(data)
|
|
19
|
+
} else throw new Error()
|
|
20
|
+
|
|
21
|
+
attest(t.allows(5)).equals(false)
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
// TODO: ?
|
|
25
|
+
it("errors can be thrown", () => {
|
|
26
|
+
const t = type("number")
|
|
27
|
+
try {
|
|
28
|
+
const result = t("invalid")
|
|
29
|
+
attest(result instanceof type.errors && result.throw())
|
|
30
|
+
} catch (e) {
|
|
31
|
+
attest(e instanceof ArkError).equals(true)
|
|
32
|
+
return
|
|
33
|
+
}
|
|
34
|
+
throw new AssertionError({ message: "Expected to throw" })
|
|
35
|
+
})
|
|
36
|
+
})
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { attest } from "@arktype/attest"
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { attest, contextualize } from "@arktype/attest"
|
|
2
|
+
import { writeUnresolvableMessage } from "@arktype/schema"
|
|
3
|
+
import { ambient, scope, type } from "arktype"
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
contextualize(() => {
|
|
6
6
|
it("shallow type reference", () => {
|
|
7
7
|
const t = type(type("boolean"))
|
|
8
8
|
attest<boolean>(t.infer)
|
|
@@ -24,9 +24,9 @@ describe("type references", () => {
|
|
|
24
24
|
const a = type({ a: "string" })
|
|
25
25
|
const $ = scope({ a })
|
|
26
26
|
const types = $.export()
|
|
27
|
-
attest(types.a.json).equals(
|
|
28
|
-
attest(a
|
|
29
|
-
attest(types.a
|
|
27
|
+
attest(types.a.json).equals(a.json)
|
|
28
|
+
attest(a.$.json).equals(ambient.json)
|
|
29
|
+
attest(types.a.$.json).equals($.json)
|
|
30
30
|
attest<{ a: string }>(types.a.infer)
|
|
31
31
|
})
|
|
32
32
|
|