arktype 1.1.0-dev-1 → 2.0.0-dev.0
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 +272 -0
- package/__tests__/astUtils.test.ts +17 -0
- package/__tests__/badDefinitionType.test.ts +51 -0
- package/__tests__/basis.test.ts +46 -0
- package/__tests__/bounds.test.ts +388 -0
- package/__tests__/cast.test.ts +32 -0
- package/__tests__/completions.test.ts +46 -0
- package/__tests__/config.test.ts +168 -0
- package/__tests__/cyclic.test.ts +144 -0
- package/__tests__/dateLiteral.test.ts +32 -0
- package/__tests__/declared.test.ts +123 -0
- package/__tests__/discrimination.test.ts +74 -0
- package/__tests__/divisor.test.ts +90 -0
- package/__tests__/enclosed.test.ts +93 -0
- package/__tests__/examples.test.ts +56 -0
- package/__tests__/expressions.test.ts +138 -0
- package/__tests__/generateBenchData.ts +89 -0
- package/__tests__/generated/cyclic.ts +3058 -0
- package/__tests__/generic.test.ts +294 -0
- package/__tests__/group.test.ts +72 -0
- package/__tests__/hkt.test.ts +22 -0
- package/__tests__/imports.test.ts +113 -0
- package/__tests__/instanceof.test.ts +102 -0
- package/__tests__/intersection.test.ts +104 -0
- package/__tests__/jsdoc.test.ts +64 -0
- package/__tests__/keyTraversal.test.ts +73 -0
- package/__tests__/keyof.test.ts +134 -0
- package/__tests__/keywords.test.ts +188 -0
- package/__tests__/literal.test.ts +51 -0
- package/__tests__/match.test.ts +34 -0
- package/__tests__/methods.bench.ts +22 -0
- package/__tests__/morph.test.ts +409 -0
- package/__tests__/narrow.test.ts +102 -0
- package/__tests__/nodeDefinitions.test.ts +190 -0
- package/__tests__/object.bench.ts +28 -0
- package/__tests__/objectLiteral.test.ts +300 -0
- package/__tests__/operand.bench.ts +45 -0
- package/__tests__/operator.bench.ts +130 -0
- package/__tests__/regex.test.ts +58 -0
- package/__tests__/scope.bench.ts +39 -0
- package/__tests__/scope.test.ts +140 -0
- package/__tests__/string.test.ts +48 -0
- package/__tests__/submodule.test.ts +99 -0
- package/__tests__/this.test.ts +57 -0
- package/__tests__/thunk.test.ts +80 -0
- package/__tests__/traverse.test.ts +119 -0
- package/__tests__/tsGenerics.test.ts +12 -0
- package/__tests__/typeMethods.test.ts +69 -0
- package/__tests__/typeReference.test.ts +39 -0
- package/__tests__/unenclosed.test.ts +120 -0
- package/__tests__/union.test.ts +147 -0
- package/ark.ts +91 -0
- package/main.ts +18 -0
- package/match.ts +116 -0
- package/out/ark.d.ts +52 -0
- package/out/ark.d.ts.map +1 -0
- package/out/ark.js +19 -0
- package/out/ark.js.map +1 -0
- package/out/arkConfig.d.ts +2 -0
- package/out/arkConfig.d.ts.map +1 -0
- package/out/arkConfig.js +10 -0
- package/out/arkConfig.js.map +1 -0
- package/out/main.d.ts +7 -0
- package/out/main.d.ts.map +1 -0
- package/out/main.js +4 -0
- package/out/main.js.map +1 -0
- package/out/match.d.ts +48 -0
- package/out/match.d.ts.map +1 -0
- package/out/match.js +18 -0
- package/out/match.js.map +1 -0
- package/out/parser/definition.d.ts +32 -0
- package/out/parser/definition.d.ts.map +1 -0
- package/out/parser/definition.js +32 -0
- package/out/parser/definition.js.map +1 -0
- package/out/parser/generic.d.ts +18 -0
- package/out/parser/generic.d.ts.map +1 -0
- package/out/parser/generic.js +19 -0
- package/out/parser/generic.js.map +1 -0
- package/out/parser/objectLiteral.d.ts +33 -0
- package/out/parser/objectLiteral.d.ts.map +1 -0
- package/out/parser/objectLiteral.js +66 -0
- package/out/parser/objectLiteral.js.map +1 -0
- package/out/parser/semantic/bounds.d.ts +13 -0
- package/out/parser/semantic/bounds.d.ts.map +1 -0
- package/out/parser/semantic/bounds.js +2 -0
- package/out/parser/semantic/bounds.js.map +1 -0
- package/out/parser/semantic/divisor.d.ts +9 -0
- package/out/parser/semantic/divisor.d.ts.map +1 -0
- package/out/parser/semantic/divisor.js +2 -0
- package/out/parser/semantic/divisor.js.map +1 -0
- package/out/parser/semantic/intersections.d.ts +14 -0
- package/out/parser/semantic/intersections.d.ts.map +1 -0
- package/out/parser/semantic/intersections.js +2 -0
- package/out/parser/semantic/intersections.js.map +1 -0
- package/out/parser/semantic/semantic.d.ts +33 -0
- package/out/parser/semantic/semantic.d.ts.map +1 -0
- package/out/parser/semantic/semantic.js +2 -0
- package/out/parser/semantic/semantic.js.map +1 -0
- package/out/parser/semantic/utils.d.ts +8 -0
- package/out/parser/semantic/utils.d.ts.map +1 -0
- package/out/parser/semantic/utils.js +2 -0
- package/out/parser/semantic/utils.js.map +1 -0
- package/out/parser/semantic/validate.d.ts +24 -0
- package/out/parser/semantic/validate.d.ts.map +1 -0
- package/out/parser/semantic/validate.js +2 -0
- package/out/parser/semantic/validate.js.map +1 -0
- package/out/parser/shared.d.ts +54 -0
- package/out/parser/shared.d.ts.map +1 -0
- package/out/parser/shared.js +47 -0
- package/out/parser/shared.js.map +1 -0
- package/out/parser/string/reduce/dynamic.d.ts +42 -0
- package/out/parser/string/reduce/dynamic.d.ts.map +1 -0
- package/out/parser/string/reduce/dynamic.js +133 -0
- package/out/parser/string/reduce/dynamic.js.map +1 -0
- package/out/parser/string/reduce/shared.d.ts +56 -0
- package/out/parser/string/reduce/shared.d.ts.map +1 -0
- package/out/parser/string/reduce/shared.js +26 -0
- package/out/parser/string/reduce/shared.js.map +1 -0
- package/out/parser/string/reduce/static.d.ts +176 -0
- package/out/parser/string/reduce/static.d.ts.map +1 -0
- package/out/parser/string/reduce/static.js +2 -0
- package/out/parser/string/reduce/static.js.map +1 -0
- package/out/parser/string/shift/operand/date.d.ts +11 -0
- package/out/parser/string/shift/operand/date.d.ts.map +1 -0
- package/out/parser/string/shift/operand/date.js +26 -0
- package/out/parser/string/shift/operand/date.js.map +1 -0
- package/out/parser/string/shift/operand/enclosed.d.ts +49 -0
- package/out/parser/string/shift/operand/enclosed.d.ts.map +1 -0
- package/out/parser/string/shift/operand/enclosed.js +50 -0
- package/out/parser/string/shift/operand/enclosed.js.map +1 -0
- package/out/parser/string/shift/operand/genericArgs.d.ts +29 -0
- package/out/parser/string/shift/operand/genericArgs.d.ts.map +1 -0
- package/out/parser/string/shift/operand/genericArgs.js +25 -0
- package/out/parser/string/shift/operand/genericArgs.js.map +1 -0
- package/out/parser/string/shift/operand/operand.d.ts +9 -0
- package/out/parser/string/shift/operand/operand.d.ts.map +1 -0
- package/out/parser/string/shift/operand/operand.js +17 -0
- package/out/parser/string/shift/operand/operand.js.map +1 -0
- package/out/parser/string/shift/operand/unenclosed.d.ts +34 -0
- package/out/parser/string/shift/operand/unenclosed.d.ts.map +1 -0
- package/out/parser/string/shift/operand/unenclosed.js +68 -0
- package/out/parser/string/shift/operand/unenclosed.js.map +1 -0
- package/out/parser/string/shift/operator/bounds.d.ts +33 -0
- package/out/parser/string/shift/operator/bounds.d.ts.map +1 -0
- package/out/parser/string/shift/operator/bounds.js +106 -0
- package/out/parser/string/shift/operator/bounds.js.map +1 -0
- package/out/parser/string/shift/operator/divisor.d.ts +9 -0
- package/out/parser/string/shift/operator/divisor.d.ts.map +1 -0
- package/out/parser/string/shift/operator/divisor.js +13 -0
- package/out/parser/string/shift/operator/divisor.js.map +1 -0
- package/out/parser/string/shift/operator/operator.d.ts +13 -0
- package/out/parser/string/shift/operator/operator.d.ts.map +1 -0
- package/out/parser/string/shift/operator/operator.js +29 -0
- package/out/parser/string/shift/operator/operator.js.map +1 -0
- package/out/parser/string/shift/scanner.d.ts +71 -0
- package/out/parser/string/shift/scanner.d.ts.map +1 -0
- package/out/parser/string/shift/scanner.js +99 -0
- package/out/parser/string/shift/scanner.js.map +1 -0
- package/out/parser/string/string.d.ts +24 -0
- package/out/parser/string/string.d.ts.map +1 -0
- package/out/parser/string/string.js +23 -0
- package/out/parser/string/string.js.map +1 -0
- package/out/parser/tuple.d.ts +71 -0
- package/out/parser/tuple.d.ts.map +1 -0
- package/out/parser/tuple.js +137 -0
- package/out/parser/tuple.js.map +1 -0
- package/out/scope.d.ts +159 -0
- package/out/scope.d.ts.map +1 -0
- package/out/scope.js +253 -0
- package/out/scope.js.map +1 -0
- package/out/test.d.ts +2 -0
- package/out/test.d.ts.map +1 -0
- package/out/test.js +7 -0
- package/out/test.js.map +1 -0
- package/out/tsconfig.build.tsbuildinfo +1 -0
- package/out/type.d.ts +77 -0
- package/out/type.d.ts.map +1 -0
- package/out/type.js +134 -0
- package/out/type.js.map +1 -0
- package/out/util.d.ts +19 -0
- package/out/util.d.ts.map +1 -0
- package/out/util.js +7 -0
- package/out/util.js.map +1 -0
- package/package.json +13 -10
- package/parser/definition.ts +159 -0
- package/parser/generic.ts +68 -0
- package/parser/objectLiteral.ts +197 -0
- package/parser/semantic/bounds.ts +61 -0
- package/parser/semantic/divisor.ts +13 -0
- package/parser/semantic/intersections.ts +46 -0
- package/parser/semantic/semantic.ts +144 -0
- package/parser/semantic/utils.ts +26 -0
- package/parser/semantic/validate.ts +109 -0
- package/parser/shared.ts +152 -0
- package/parser/string/reduce/dynamic.ts +196 -0
- package/parser/string/reduce/shared.ts +101 -0
- package/parser/string/reduce/static.ts +298 -0
- package/parser/string/shift/operand/date.ts +54 -0
- package/parser/string/shift/operand/enclosed.ts +118 -0
- package/parser/string/shift/operand/genericArgs.ts +127 -0
- package/parser/string/shift/operand/operand.ts +48 -0
- package/parser/string/shift/operand/unenclosed.ts +268 -0
- package/parser/string/shift/operator/bounds.ts +264 -0
- package/parser/string/shift/operator/divisor.ts +36 -0
- package/parser/string/shift/operator/operator.ts +82 -0
- package/parser/string/shift/scanner.ts +209 -0
- package/parser/string/string.ts +90 -0
- package/parser/tuple.ts +474 -0
- package/scope.ts +574 -0
- package/type.ts +347 -0
- package/util.ts +36 -0
- package/dist/main.d.ts +0 -1018
- package/dist/main.js +0 -1450
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { attest } from "@arktype/attest"
|
|
2
|
+
import { scope, type } from "arktype"
|
|
3
|
+
import { writeUnresolvableMessage } from "../parser/string/shift/operand/unenclosed.js"
|
|
4
|
+
import { writeUnboundableMessage } from "../parser/string/shift/operator/bounds.js"
|
|
5
|
+
import { writeUnexpectedCharacterMessage } from "../parser/string/shift/operator/operator.js"
|
|
6
|
+
|
|
7
|
+
describe("scope", () => {
|
|
8
|
+
it("base definition", () => {
|
|
9
|
+
const types = scope({ a: "string" }).export()
|
|
10
|
+
attest<string>(types.a.infer)
|
|
11
|
+
attest(() =>
|
|
12
|
+
// @ts-expect-error
|
|
13
|
+
scope({ a: "strong" }).export()
|
|
14
|
+
).throwsAndHasTypeError(writeUnresolvableMessage("strong"))
|
|
15
|
+
})
|
|
16
|
+
it("type definition", () => {
|
|
17
|
+
const types = scope({ a: type("string") }).export()
|
|
18
|
+
attest<string>(types.a.infer)
|
|
19
|
+
attest(() =>
|
|
20
|
+
// @ts-expect-error
|
|
21
|
+
scope({ a: type("strong") })
|
|
22
|
+
).throwsAndHasTypeError(writeUnresolvableMessage("strong"))
|
|
23
|
+
})
|
|
24
|
+
it("interdependent", () => {
|
|
25
|
+
const types = scope({
|
|
26
|
+
a: "string>5",
|
|
27
|
+
b: "email<=10",
|
|
28
|
+
c: "a&b"
|
|
29
|
+
}).export()
|
|
30
|
+
attest<string>(types.c.infer)
|
|
31
|
+
})
|
|
32
|
+
it("object array", () => {
|
|
33
|
+
const types = scope({ a: "string", b: [{ c: "a" }] }).export()
|
|
34
|
+
attest<
|
|
35
|
+
[
|
|
36
|
+
{
|
|
37
|
+
c: string
|
|
38
|
+
}
|
|
39
|
+
]
|
|
40
|
+
>(types.b.infer)
|
|
41
|
+
})
|
|
42
|
+
it("doesn't try to validate any in scope", () => {
|
|
43
|
+
// const $ = scope({ a: {} as any })
|
|
44
|
+
// attest<{ a: never }>($.infer)
|
|
45
|
+
// attest<[number, never]>($.type(["number", "a"]).infer)
|
|
46
|
+
})
|
|
47
|
+
it("infers input and output", () => {
|
|
48
|
+
const $ = scope({
|
|
49
|
+
a: ["string", "=>", (s) => s.length]
|
|
50
|
+
})
|
|
51
|
+
attest<{ a: number }>($.infer)
|
|
52
|
+
// TODO: API?
|
|
53
|
+
// attest<{ a: string }>($.in.infer)
|
|
54
|
+
})
|
|
55
|
+
it("scope.scope", () => {
|
|
56
|
+
const $ = scope({
|
|
57
|
+
a: "string"
|
|
58
|
+
})
|
|
59
|
+
const importer = $.scope({ b: "a[]" })
|
|
60
|
+
attest<{ b: string[] }>(importer.infer)
|
|
61
|
+
const t = importer.type("b")
|
|
62
|
+
attest(t.json).is(type("string[]").json)
|
|
63
|
+
})
|
|
64
|
+
it("infers its own helpers", () => {
|
|
65
|
+
const $ = scope({
|
|
66
|
+
a: () => $.type("string"),
|
|
67
|
+
b: () => $.type("number")
|
|
68
|
+
})
|
|
69
|
+
const types = $.export()
|
|
70
|
+
attest<string>(types.a.infer)
|
|
71
|
+
attest<number>(types.b.infer)
|
|
72
|
+
})
|
|
73
|
+
it("allows semantically valid helpers", () => {
|
|
74
|
+
const $ = scope({
|
|
75
|
+
n: () => $.type("number"),
|
|
76
|
+
lessThan10: () => $.type("n<10")
|
|
77
|
+
})
|
|
78
|
+
const types = $.export()
|
|
79
|
+
attest<number>(types.n.infer)
|
|
80
|
+
attest<number>(types.lessThan10.infer)
|
|
81
|
+
})
|
|
82
|
+
it("errors on helper parse error", () => {
|
|
83
|
+
attest(() => {
|
|
84
|
+
const $ = scope({
|
|
85
|
+
// @ts-expect-error
|
|
86
|
+
a: () => $.type("kung|foo")
|
|
87
|
+
})
|
|
88
|
+
$.export()
|
|
89
|
+
}).throwsAndHasTypeError(writeUnresolvableMessage("kung"))
|
|
90
|
+
})
|
|
91
|
+
it("errors on semantically invalid helper", () => {
|
|
92
|
+
attest(() => {
|
|
93
|
+
const $ = scope({
|
|
94
|
+
b: () => $.type("boolean"),
|
|
95
|
+
// @ts-expect-error
|
|
96
|
+
lessThan10: () => $.type("b<10")
|
|
97
|
+
})
|
|
98
|
+
$.export()
|
|
99
|
+
}).throwsAndHasTypeError(writeUnboundableMessage("'b'"))
|
|
100
|
+
})
|
|
101
|
+
it("errors on ridiculous unexpected alias scenario", () => {
|
|
102
|
+
attest(() =>
|
|
103
|
+
scope({
|
|
104
|
+
Unexpected: {},
|
|
105
|
+
User: {
|
|
106
|
+
// Previously, using the alias `Unexpected` allowed creating
|
|
107
|
+
// this type string which matched its own error message.
|
|
108
|
+
// @ts-expect-error
|
|
109
|
+
name: "Unexpected character 'c'"
|
|
110
|
+
}
|
|
111
|
+
}).export()
|
|
112
|
+
).throwsAndHasTypeError(writeUnexpectedCharacterMessage("c"))
|
|
113
|
+
})
|
|
114
|
+
it("autocompletion", () => {
|
|
115
|
+
attest(() => {
|
|
116
|
+
scope({
|
|
117
|
+
foobar: "string",
|
|
118
|
+
// @ts-expect-error
|
|
119
|
+
baz: "fo"
|
|
120
|
+
}).export()
|
|
121
|
+
}).type.errors(`Type '"fo"' is not assignable to type '"foobar"'`)
|
|
122
|
+
})
|
|
123
|
+
it("cross-scope reference", () => {
|
|
124
|
+
const { Apple } = scope({
|
|
125
|
+
Apple: {
|
|
126
|
+
pear: "Pear"
|
|
127
|
+
},
|
|
128
|
+
Pear: {
|
|
129
|
+
tasty: "true"
|
|
130
|
+
}
|
|
131
|
+
}).export()
|
|
132
|
+
|
|
133
|
+
const { X } = scope({
|
|
134
|
+
X: Apple
|
|
135
|
+
}).export()
|
|
136
|
+
|
|
137
|
+
const { out: data } = X({ pear: { tasty: true } })
|
|
138
|
+
attest(data).snap({ pear: { tasty: true } })
|
|
139
|
+
})
|
|
140
|
+
})
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { attest } from "@arktype/attest"
|
|
2
|
+
import { type } from "arktype"
|
|
3
|
+
import { writeUnterminatedEnclosedMessage } from "../parser/string/shift/operand/enclosed.js"
|
|
4
|
+
import {
|
|
5
|
+
writeExpressionExpectedMessage,
|
|
6
|
+
writeUnresolvableMessage
|
|
7
|
+
} from "../parser/string/shift/operand/unenclosed.js"
|
|
8
|
+
|
|
9
|
+
describe("string", () => {
|
|
10
|
+
it("errors on empty string", () => {
|
|
11
|
+
// @ts-expect-error
|
|
12
|
+
attest(() => type("")).throws(writeExpressionExpectedMessage(""))
|
|
13
|
+
})
|
|
14
|
+
it("ignores whitespace between identifiers/operators", () => {
|
|
15
|
+
const t = type(` \n string |
|
|
16
|
+
boolean [] `)
|
|
17
|
+
attest<string | boolean[]>(t.infer)
|
|
18
|
+
attest(t.json).equals(type("string|boolean[]").json)
|
|
19
|
+
})
|
|
20
|
+
it("errors on bad whitespace", () => {
|
|
21
|
+
attest(() =>
|
|
22
|
+
// @ts-expect-error
|
|
23
|
+
type("string | boo lean[]")
|
|
24
|
+
)
|
|
25
|
+
.throws(writeUnresolvableMessage("boo"))
|
|
26
|
+
.type.errors("string | boolean")
|
|
27
|
+
})
|
|
28
|
+
it("unterminated string", () => {
|
|
29
|
+
// @ts-expect-error
|
|
30
|
+
attest(() => type("'bob")).throwsAndHasTypeError(
|
|
31
|
+
writeUnterminatedEnclosedMessage("bob", "'")
|
|
32
|
+
)
|
|
33
|
+
})
|
|
34
|
+
it("shallow single autocomplete", () => {
|
|
35
|
+
// @ts-expect-error
|
|
36
|
+
attest(() => type("str")).type.errors(
|
|
37
|
+
`Argument of type '"str"' is not assignable to parameter of type '"string"'`
|
|
38
|
+
)
|
|
39
|
+
})
|
|
40
|
+
it("shallow multi autocomplete", () => {
|
|
41
|
+
// @ts-expect-error
|
|
42
|
+
attest(() => type("s")).type.errors(`"string" | "symbol" | "semver"`)
|
|
43
|
+
})
|
|
44
|
+
it("post-operator autocomplete", () => {
|
|
45
|
+
// @ts-expect-error
|
|
46
|
+
attest(() => type("string|num")).type.errors(`"string|number"`)
|
|
47
|
+
})
|
|
48
|
+
})
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { attest } from "@arktype/attest"
|
|
2
|
+
import { lazily } from "@arktype/util"
|
|
3
|
+
import { scope, type, type Ark, type Module, type Scope } from "arktype"
|
|
4
|
+
import {
|
|
5
|
+
writeMissingSubmoduleAccessMessage,
|
|
6
|
+
writeNonSubmoduleDotMessage,
|
|
7
|
+
writeUnresolvableMessage
|
|
8
|
+
} from "../parser/string/shift/operand/unenclosed.js"
|
|
9
|
+
|
|
10
|
+
const $ = lazily(() =>
|
|
11
|
+
scope({
|
|
12
|
+
a: "string",
|
|
13
|
+
b: "sub.alias",
|
|
14
|
+
sub: scope({ alias: "number" }).export()
|
|
15
|
+
})
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
describe("submodules", () => {
|
|
19
|
+
it("base", () => {
|
|
20
|
+
const types = $.export()
|
|
21
|
+
attest<
|
|
22
|
+
Module<{
|
|
23
|
+
exports: {
|
|
24
|
+
a: string
|
|
25
|
+
b: number
|
|
26
|
+
sub: Module<{
|
|
27
|
+
exports: {
|
|
28
|
+
alias: number
|
|
29
|
+
}
|
|
30
|
+
locals: {}
|
|
31
|
+
ambient: Ark
|
|
32
|
+
}>
|
|
33
|
+
}
|
|
34
|
+
locals: {}
|
|
35
|
+
ambient: Ark
|
|
36
|
+
}>
|
|
37
|
+
>(types)
|
|
38
|
+
|
|
39
|
+
attest<number>(types.sub.alias.infer)
|
|
40
|
+
const expected = type("number").json
|
|
41
|
+
attest(types.sub.alias.json).is(expected)
|
|
42
|
+
attest(types.b.json).is(expected)
|
|
43
|
+
})
|
|
44
|
+
it("non-submodule dot access", () => {
|
|
45
|
+
// @ts-expect-error
|
|
46
|
+
attest(() => $.type("b.foo")).throwsAndHasTypeError(
|
|
47
|
+
writeNonSubmoduleDotMessage("b")
|
|
48
|
+
)
|
|
49
|
+
})
|
|
50
|
+
it("thunk submodule", () => {
|
|
51
|
+
const $ = scope({
|
|
52
|
+
a: "string",
|
|
53
|
+
c: "a",
|
|
54
|
+
sub: () =>
|
|
55
|
+
$.scope({
|
|
56
|
+
foo: "a",
|
|
57
|
+
bar: "foo"
|
|
58
|
+
}).export()
|
|
59
|
+
})
|
|
60
|
+
attest<
|
|
61
|
+
Scope<{
|
|
62
|
+
exports: {
|
|
63
|
+
a: string
|
|
64
|
+
c: string
|
|
65
|
+
sub: Module<{
|
|
66
|
+
exports: {
|
|
67
|
+
foo: string
|
|
68
|
+
bar: string
|
|
69
|
+
}
|
|
70
|
+
locals: {}
|
|
71
|
+
ambient: Ark
|
|
72
|
+
}>
|
|
73
|
+
}
|
|
74
|
+
locals: {}
|
|
75
|
+
ambient: Ark
|
|
76
|
+
}>
|
|
77
|
+
>($)
|
|
78
|
+
})
|
|
79
|
+
it("no alias reference", () => {
|
|
80
|
+
// @ts-expect-error
|
|
81
|
+
attest(() => $.type("sub")).throwsAndHasTypeError(
|
|
82
|
+
writeMissingSubmoduleAccessMessage("sub")
|
|
83
|
+
)
|
|
84
|
+
})
|
|
85
|
+
it("bad alias reference", () => {
|
|
86
|
+
// @ts-expect-error
|
|
87
|
+
attest(() => $.type("sub.marine")).throwsAndHasTypeError(
|
|
88
|
+
writeUnresolvableMessage("sub.marine")
|
|
89
|
+
)
|
|
90
|
+
})
|
|
91
|
+
it("autocompletion", () => {
|
|
92
|
+
const base = scope({ foo: "true" })
|
|
93
|
+
// @ts-expect-error
|
|
94
|
+
attest(() => scope({ base, reference: "base." }).export())
|
|
95
|
+
.throws(writeUnresolvableMessage("base."))
|
|
96
|
+
.type.errors("base.foo")
|
|
97
|
+
})
|
|
98
|
+
// TODO: private aliases
|
|
99
|
+
})
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { attest } from "@arktype/attest"
|
|
2
|
+
import { scope, type } from "arktype"
|
|
3
|
+
import { writeUnresolvableMessage } from "../parser/string/shift/operand/unenclosed.js"
|
|
4
|
+
|
|
5
|
+
describe("this reference", () => {
|
|
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
|
+
})
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { attest } from "@arktype/attest"
|
|
2
|
+
import { scope, type } from "arktype"
|
|
3
|
+
import { writeBadDefinitionTypeMessage } from "../parser/definition.js"
|
|
4
|
+
import { writeUnresolvableMessage } from "../parser/string/shift/operand/unenclosed.js"
|
|
5
|
+
|
|
6
|
+
describe("thunk", () => {
|
|
7
|
+
it("thunk", () => {
|
|
8
|
+
const t = type(() => type("boolean"))
|
|
9
|
+
attest<boolean>(t.infer)
|
|
10
|
+
attest(() => {
|
|
11
|
+
// @ts-expect-error
|
|
12
|
+
type(() => type("moolean"))
|
|
13
|
+
}).throwsAndHasTypeError(writeUnresolvableMessage("moolean"))
|
|
14
|
+
})
|
|
15
|
+
it("thunks in scope", () => {
|
|
16
|
+
const $ = scope({
|
|
17
|
+
a: () => $.type({ b: "b" }),
|
|
18
|
+
b: () => $.type({ a: "string" })
|
|
19
|
+
})
|
|
20
|
+
attest<{
|
|
21
|
+
a: {
|
|
22
|
+
b: {
|
|
23
|
+
a: string
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
b: {
|
|
27
|
+
a: string
|
|
28
|
+
}
|
|
29
|
+
}>($.infer)
|
|
30
|
+
|
|
31
|
+
const types = $.export()
|
|
32
|
+
attest<{
|
|
33
|
+
b: {
|
|
34
|
+
a: string
|
|
35
|
+
}
|
|
36
|
+
}>(types.a.infer)
|
|
37
|
+
|
|
38
|
+
// attest(types.a.node).snap({ object: { props: { b: "b" } } })
|
|
39
|
+
attest<{ a: string }>(types.b.infer)
|
|
40
|
+
|
|
41
|
+
// attest(types.b.node).snap({
|
|
42
|
+
// object: { props: { a: { object: { props: { a: "string" } } } } }
|
|
43
|
+
// })
|
|
44
|
+
})
|
|
45
|
+
it("expression from thunk", () => {
|
|
46
|
+
const $ = scope({
|
|
47
|
+
a: () => $.type({ a: "string" }),
|
|
48
|
+
b: { b: "boolean" },
|
|
49
|
+
aAndB: () => $.type("a&b")
|
|
50
|
+
})
|
|
51
|
+
const types = $.export()
|
|
52
|
+
attest<{ a: string; b: boolean }>(types.aAndB.infer)
|
|
53
|
+
// attest(types.aAndB.node).snap({
|
|
54
|
+
// object: { props: { a: "string", b: "boolean" } }
|
|
55
|
+
// })
|
|
56
|
+
})
|
|
57
|
+
it("shallow thunk in type", () => {
|
|
58
|
+
const t = type(() => type("string"))
|
|
59
|
+
attest(t.json).equals(type("string").json)
|
|
60
|
+
attest<string>(t.infer)
|
|
61
|
+
})
|
|
62
|
+
it("deep thunk in type", () => {
|
|
63
|
+
const t = type({ a: () => type("string") })
|
|
64
|
+
attest(t.json).equals(type({ a: "string" }).json)
|
|
65
|
+
attest<{ a: string }>(t.infer)
|
|
66
|
+
})
|
|
67
|
+
it("non-type thunk in scope", () => {
|
|
68
|
+
const $ = scope({
|
|
69
|
+
a: () => 42
|
|
70
|
+
})
|
|
71
|
+
attest(() => $.export()).throws(writeBadDefinitionTypeMessage("number"))
|
|
72
|
+
})
|
|
73
|
+
it("parse error in thunk in scope", () => {
|
|
74
|
+
const $ = scope({
|
|
75
|
+
// @ts-expect-error
|
|
76
|
+
a: () => $.type("bad")
|
|
77
|
+
})
|
|
78
|
+
attest(() => $.export()).throws(writeUnresolvableMessage("bad"))
|
|
79
|
+
})
|
|
80
|
+
})
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { attest } from "@arktype/attest"
|
|
2
|
+
import { scope, type } from "arktype"
|
|
3
|
+
|
|
4
|
+
describe("traverse", () => {
|
|
5
|
+
it("divisible", () => {
|
|
6
|
+
const t = type("number%2")
|
|
7
|
+
attest(t(4).out).snap(4)
|
|
8
|
+
attest(t(5).errors?.summary).snap("Must be a multiple of 2 (was 5)")
|
|
9
|
+
})
|
|
10
|
+
it("range", () => {
|
|
11
|
+
const t = type("number>2")
|
|
12
|
+
attest(t(3).out).snap(3)
|
|
13
|
+
attest(t(2).errors?.summary).snap("Must be more than 2 (was 2)")
|
|
14
|
+
})
|
|
15
|
+
it("domain", () => {
|
|
16
|
+
const t = type("number")
|
|
17
|
+
attest(t(5).out).snap(5)
|
|
18
|
+
attest(t("foo").errors?.summary).snap("Must be a number (was string)")
|
|
19
|
+
})
|
|
20
|
+
it("regex", () => {
|
|
21
|
+
const t = type("/.*@arktype.io/")
|
|
22
|
+
attest(t("shawn@arktype.io").out).snap("shawn@arktype.io")
|
|
23
|
+
attest(t("shawn@hotmail.com").errors?.summary).snap(
|
|
24
|
+
"Must be a string matching /.*@arktype.io/ (was 'shawn@hotmail.com')"
|
|
25
|
+
)
|
|
26
|
+
})
|
|
27
|
+
it("required keys", () => {
|
|
28
|
+
const t = type({
|
|
29
|
+
name: "string",
|
|
30
|
+
age: "number",
|
|
31
|
+
"title?": "string"
|
|
32
|
+
})
|
|
33
|
+
attest(t({ name: "Shawn", age: 99 }).out).snap({
|
|
34
|
+
name: "Shawn",
|
|
35
|
+
age: 99
|
|
36
|
+
})
|
|
37
|
+
attest(t({ name: "Shawn" }).errors?.summary).snap("age must be defined")
|
|
38
|
+
})
|
|
39
|
+
it("customized builtin problem", () => {
|
|
40
|
+
const types = scope(
|
|
41
|
+
{ isEven: "number%2" }
|
|
42
|
+
//{
|
|
43
|
+
// TODO: Fix
|
|
44
|
+
// codes: {
|
|
45
|
+
// divisor: {
|
|
46
|
+
// mustBe: (divisor) => `a multiple of ${divisor}`,
|
|
47
|
+
// writeReason: (mustBe, was) => `${was} is not ${mustBe}!`
|
|
48
|
+
// }
|
|
49
|
+
// }
|
|
50
|
+
//}
|
|
51
|
+
).export()
|
|
52
|
+
attest(types.isEven(3).errors?.summary).snap("3 is not a multiple of 2!")
|
|
53
|
+
})
|
|
54
|
+
it("domains", () => {
|
|
55
|
+
const t = type("string|number[]")
|
|
56
|
+
attest(t([1]).out).snap([1])
|
|
57
|
+
attest(t("hello").out).snap("hello")
|
|
58
|
+
attest(t(2).errors?.summary).snap("Must be a string or an object (was 2)")
|
|
59
|
+
})
|
|
60
|
+
it("tuple length", () => {
|
|
61
|
+
const t = type(["string", "number", "string", "string[]"])
|
|
62
|
+
const data: typeof t.infer = ["foo", 5, "boo", []]
|
|
63
|
+
attest(t(data).out).equals(data)
|
|
64
|
+
attest(t(["hello"]).errors?.summary).snap("length must be 4 (was 1)")
|
|
65
|
+
})
|
|
66
|
+
it("branches", () => {
|
|
67
|
+
const t = type([{ a: "string" }, "|", { b: "boolean" }])
|
|
68
|
+
attest(t({ a: "ok" }).out).snap({ a: "ok" })
|
|
69
|
+
attest(t({ b: true }).out).snap({ b: true })
|
|
70
|
+
attest(t({}).errors?.summary).snap(
|
|
71
|
+
"a must be defined or b must be defined (was {})"
|
|
72
|
+
)
|
|
73
|
+
})
|
|
74
|
+
it("branches at path", () => {
|
|
75
|
+
const t = type({ key: [{ a: "string" }, "|", { b: "boolean" }] })
|
|
76
|
+
attest(t({ key: { a: "ok" } }).out).snap({ key: { a: "ok" } })
|
|
77
|
+
attest(t({ key: { b: true } }).out).snap({ key: { b: true } })
|
|
78
|
+
attest(t({ key: {} }).errors?.summary).snap(
|
|
79
|
+
"At key, a must be defined or b must be defined (was {})"
|
|
80
|
+
)
|
|
81
|
+
})
|
|
82
|
+
it("switch", () => {
|
|
83
|
+
const t = type([{ a: "string" }, "|", { a: "boolean" }])
|
|
84
|
+
attest(t({ a: "ok" }).out).snap({ a: "ok" })
|
|
85
|
+
attest(t({ a: true }).out).snap({ a: true })
|
|
86
|
+
// value isn't present
|
|
87
|
+
attest(t({}).errors?.summary).snap(
|
|
88
|
+
"a must be a string or boolean (was undefined)"
|
|
89
|
+
)
|
|
90
|
+
// unsatisfying value
|
|
91
|
+
attest(t({ a: 5 }).errors?.summary).snap(
|
|
92
|
+
"a must be a string or boolean (was 5)"
|
|
93
|
+
)
|
|
94
|
+
})
|
|
95
|
+
it("multiple switch", () => {
|
|
96
|
+
const types = scope({
|
|
97
|
+
a: { a: "string" },
|
|
98
|
+
b: { a: "number" },
|
|
99
|
+
c: { a: "Function" },
|
|
100
|
+
d: "a|b|c"
|
|
101
|
+
}).export()
|
|
102
|
+
attest(types.d({}).errors?.summary).snap(
|
|
103
|
+
"a must be a string, a number or an object (was undefined)"
|
|
104
|
+
)
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
it("multi", () => {
|
|
108
|
+
const naturalNumber = type("integer>0")
|
|
109
|
+
attest(naturalNumber(-1.2).errors?.summary).snap(
|
|
110
|
+
"-1.2 must be...\n• an integer\n• more than 0"
|
|
111
|
+
)
|
|
112
|
+
const naturalAtPath = type({
|
|
113
|
+
natural: naturalNumber
|
|
114
|
+
})
|
|
115
|
+
attest(naturalAtPath({ natural: -0.1 }).errors?.summary).snap(
|
|
116
|
+
"At natural, -0.1 must be...\n• an integer\n• more than 0"
|
|
117
|
+
)
|
|
118
|
+
})
|
|
119
|
+
})
|
|
@@ -0,0 +1,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,69 @@
|
|
|
1
|
+
import { attest } from "@arktype/attest"
|
|
2
|
+
import { ArkTypeError } from "@arktype/schema"
|
|
3
|
+
import { define, scope, type } from "arktype"
|
|
4
|
+
import { AssertionError } from "node:assert"
|
|
5
|
+
import { writeUnresolvableMessage } from "../parser/string/shift/operand/unenclosed.js"
|
|
6
|
+
|
|
7
|
+
describe("type methods", () => {
|
|
8
|
+
it("root discriminates", () => {
|
|
9
|
+
const t = type("string")
|
|
10
|
+
const { out, errors: errors } = t("")
|
|
11
|
+
if (errors) {
|
|
12
|
+
errors.throw()
|
|
13
|
+
} else {
|
|
14
|
+
attest<string>(out)
|
|
15
|
+
}
|
|
16
|
+
})
|
|
17
|
+
it("allows", () => {
|
|
18
|
+
const t = type("number%2")
|
|
19
|
+
const data: unknown = 4
|
|
20
|
+
if (t.allows(data)) {
|
|
21
|
+
// narrows correctly
|
|
22
|
+
attest<number>(data)
|
|
23
|
+
} else {
|
|
24
|
+
throw new Error()
|
|
25
|
+
}
|
|
26
|
+
attest(t.allows(5)).equals(false)
|
|
27
|
+
})
|
|
28
|
+
it("errors can be thrown", () => {
|
|
29
|
+
const t = type("number")
|
|
30
|
+
try {
|
|
31
|
+
attest(t("invalid").errors?.throw())
|
|
32
|
+
} catch (e) {
|
|
33
|
+
attest(e instanceof ArkTypeError).equals(true)
|
|
34
|
+
return
|
|
35
|
+
}
|
|
36
|
+
throw new AssertionError({ message: "Expected to throw" })
|
|
37
|
+
})
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
describe("scope utilities", () => {
|
|
41
|
+
describe("define", () => {
|
|
42
|
+
it("ark", () => {
|
|
43
|
+
const def = define({
|
|
44
|
+
a: "string|number",
|
|
45
|
+
b: ["boolean"],
|
|
46
|
+
c: "this"
|
|
47
|
+
})
|
|
48
|
+
attest<{ a: "string|number"; b: ["boolean"]; c: "this" }>(def)
|
|
49
|
+
})
|
|
50
|
+
it("ark error", () => {
|
|
51
|
+
// currently is a no-op, so only has type error
|
|
52
|
+
// @ts-expect-error
|
|
53
|
+
attest(define({ a: "boolean|foo" })).type.errors(
|
|
54
|
+
writeUnresolvableMessage("foo")
|
|
55
|
+
)
|
|
56
|
+
})
|
|
57
|
+
it("custom scope", () => {
|
|
58
|
+
const $ = scope({
|
|
59
|
+
a: "string[]"
|
|
60
|
+
})
|
|
61
|
+
const ok = $.define(["a[]|boolean"])
|
|
62
|
+
attest<["a[]|boolean"]>(ok)
|
|
63
|
+
// @ts-expect-error
|
|
64
|
+
attest($.define({ not: "ok" })).type.errors(
|
|
65
|
+
writeUnresolvableMessage("ok")
|
|
66
|
+
)
|
|
67
|
+
})
|
|
68
|
+
})
|
|
69
|
+
})
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { attest } from "@arktype/attest"
|
|
2
|
+
import { ark, scope, type } from "arktype"
|
|
3
|
+
import { writeUnresolvableMessage } from "../parser/string/shift/operand/unenclosed.js"
|
|
4
|
+
|
|
5
|
+
describe("type references", () => {
|
|
6
|
+
it("shallow type reference", () => {
|
|
7
|
+
const t = type(type("boolean"))
|
|
8
|
+
attest<boolean>(t.infer)
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
it("bad shallow type reference", () => {
|
|
12
|
+
attest(() => {
|
|
13
|
+
// @ts-expect-error
|
|
14
|
+
type(type("foolean"))
|
|
15
|
+
}).throwsAndHasTypeError(writeUnresolvableMessage("foolean"))
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
it("deep type reference", () => {
|
|
19
|
+
const t = type({ a: type("boolean") })
|
|
20
|
+
attest<{ a: boolean }>(t.infer)
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
it("type reference in scope", () => {
|
|
24
|
+
const a = type({ a: "string" })
|
|
25
|
+
const $ = scope({ a })
|
|
26
|
+
const types = $.export()
|
|
27
|
+
attest(types.a.json).equals(type({ a: "string" }).json)
|
|
28
|
+
attest(a.scope).is(ark)
|
|
29
|
+
attest(types.a.scope).is($)
|
|
30
|
+
attest<{ a: string }>(types.a.infer)
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
it("bad deep type reference", () => {
|
|
34
|
+
attest(() => {
|
|
35
|
+
// @ts-expect-error
|
|
36
|
+
type({ a: type("goolean") })
|
|
37
|
+
}).throwsAndHasTypeError(writeUnresolvableMessage("goolean"))
|
|
38
|
+
})
|
|
39
|
+
})
|