arktype 2.0.0-dev.0 → 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__/scope.test.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
import { attest } from "@arktype/attest"
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { attest, contextualize } from "@arktype/attest"
|
|
2
|
+
import {
|
|
3
|
+
writeUnboundableMessage,
|
|
4
|
+
writeUnresolvableMessage
|
|
5
|
+
} from "@arktype/schema"
|
|
6
|
+
import { define, scope, type } from "arktype"
|
|
5
7
|
import { writeUnexpectedCharacterMessage } from "../parser/string/shift/operator/operator.js"
|
|
6
8
|
|
|
7
|
-
|
|
9
|
+
contextualize(() => {
|
|
8
10
|
it("base definition", () => {
|
|
9
11
|
const types = scope({ a: "string" }).export()
|
|
10
12
|
attest<string>(types.a.infer)
|
|
@@ -13,6 +15,7 @@ describe("scope", () => {
|
|
|
13
15
|
scope({ a: "strong" }).export()
|
|
14
16
|
).throwsAndHasTypeError(writeUnresolvableMessage("strong"))
|
|
15
17
|
})
|
|
18
|
+
|
|
16
19
|
it("type definition", () => {
|
|
17
20
|
const types = scope({ a: type("string") }).export()
|
|
18
21
|
attest<string>(types.a.infer)
|
|
@@ -21,6 +24,7 @@ describe("scope", () => {
|
|
|
21
24
|
scope({ a: type("strong") })
|
|
22
25
|
).throwsAndHasTypeError(writeUnresolvableMessage("strong"))
|
|
23
26
|
})
|
|
27
|
+
|
|
24
28
|
it("interdependent", () => {
|
|
25
29
|
const types = scope({
|
|
26
30
|
a: "string>5",
|
|
@@ -29,6 +33,7 @@ describe("scope", () => {
|
|
|
29
33
|
}).export()
|
|
30
34
|
attest<string>(types.c.infer)
|
|
31
35
|
})
|
|
36
|
+
|
|
32
37
|
it("object array", () => {
|
|
33
38
|
const types = scope({ a: "string", b: [{ c: "a" }] }).export()
|
|
34
39
|
attest<
|
|
@@ -39,28 +44,22 @@ describe("scope", () => {
|
|
|
39
44
|
]
|
|
40
45
|
>(types.b.infer)
|
|
41
46
|
})
|
|
47
|
+
|
|
42
48
|
it("doesn't try to validate any in scope", () => {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
49
|
+
const $ = scope({ a: {} as any })
|
|
50
|
+
attest<{ a: never }>($.infer)
|
|
51
|
+
attest<[number, never]>($.type(["number", "a"]).infer)
|
|
46
52
|
})
|
|
53
|
+
|
|
47
54
|
it("infers input and output", () => {
|
|
48
55
|
const $ = scope({
|
|
49
|
-
a: ["string", "=>",
|
|
56
|
+
a: ["string", "=>", s => s.length]
|
|
50
57
|
})
|
|
51
58
|
attest<{ a: number }>($.infer)
|
|
52
|
-
|
|
53
|
-
|
|
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)
|
|
59
|
+
|
|
60
|
+
attest<{ a: string }>($.inferIn)
|
|
63
61
|
})
|
|
62
|
+
|
|
64
63
|
it("infers its own helpers", () => {
|
|
65
64
|
const $ = scope({
|
|
66
65
|
a: () => $.type("string"),
|
|
@@ -70,6 +69,7 @@ describe("scope", () => {
|
|
|
70
69
|
attest<string>(types.a.infer)
|
|
71
70
|
attest<number>(types.b.infer)
|
|
72
71
|
})
|
|
72
|
+
|
|
73
73
|
it("allows semantically valid helpers", () => {
|
|
74
74
|
const $ = scope({
|
|
75
75
|
n: () => $.type("number"),
|
|
@@ -79,6 +79,7 @@ describe("scope", () => {
|
|
|
79
79
|
attest<number>(types.n.infer)
|
|
80
80
|
attest<number>(types.lessThan10.infer)
|
|
81
81
|
})
|
|
82
|
+
|
|
82
83
|
it("errors on helper parse error", () => {
|
|
83
84
|
attest(() => {
|
|
84
85
|
const $ = scope({
|
|
@@ -88,6 +89,7 @@ describe("scope", () => {
|
|
|
88
89
|
$.export()
|
|
89
90
|
}).throwsAndHasTypeError(writeUnresolvableMessage("kung"))
|
|
90
91
|
})
|
|
92
|
+
|
|
91
93
|
it("errors on semantically invalid helper", () => {
|
|
92
94
|
attest(() => {
|
|
93
95
|
const $ = scope({
|
|
@@ -96,8 +98,11 @@ describe("scope", () => {
|
|
|
96
98
|
lessThan10: () => $.type("b<10")
|
|
97
99
|
})
|
|
98
100
|
$.export()
|
|
99
|
-
})
|
|
101
|
+
})
|
|
102
|
+
.throws(writeUnboundableMessage("boolean"))
|
|
103
|
+
.type.errors(writeUnboundableMessage("'b'"))
|
|
100
104
|
})
|
|
105
|
+
|
|
101
106
|
it("errors on ridiculous unexpected alias scenario", () => {
|
|
102
107
|
attest(() =>
|
|
103
108
|
scope({
|
|
@@ -111,6 +116,7 @@ describe("scope", () => {
|
|
|
111
116
|
}).export()
|
|
112
117
|
).throwsAndHasTypeError(writeUnexpectedCharacterMessage("c"))
|
|
113
118
|
})
|
|
119
|
+
|
|
114
120
|
it("autocompletion", () => {
|
|
115
121
|
attest(() => {
|
|
116
122
|
scope({
|
|
@@ -120,6 +126,7 @@ describe("scope", () => {
|
|
|
120
126
|
}).export()
|
|
121
127
|
}).type.errors(`Type '"fo"' is not assignable to type '"foobar"'`)
|
|
122
128
|
})
|
|
129
|
+
|
|
123
130
|
it("cross-scope reference", () => {
|
|
124
131
|
const { Apple } = scope({
|
|
125
132
|
Apple: {
|
|
@@ -134,7 +141,234 @@ describe("scope", () => {
|
|
|
134
141
|
X: Apple
|
|
135
142
|
}).export()
|
|
136
143
|
|
|
137
|
-
const
|
|
138
|
-
attest(
|
|
144
|
+
const out = X({ pear: { tasty: true } })
|
|
145
|
+
attest(out).snap({ pear: { tasty: true } })
|
|
146
|
+
})
|
|
147
|
+
describe("define", () => {
|
|
148
|
+
it("ark", () => {
|
|
149
|
+
const def = define({
|
|
150
|
+
a: "string|number",
|
|
151
|
+
b: ["boolean"],
|
|
152
|
+
c: "this"
|
|
153
|
+
})
|
|
154
|
+
attest<{ a: "string|number"; b: ["boolean"]; c: "this" }>(def)
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
it("ark error", () => {
|
|
158
|
+
// currently is a no-op, so only has type error
|
|
159
|
+
// @ts-expect-error
|
|
160
|
+
attest(define({ a: "boolean|foo" })).type.errors(
|
|
161
|
+
writeUnresolvableMessage("foo")
|
|
162
|
+
)
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
it("custom scope", () => {
|
|
166
|
+
const $ = scope({
|
|
167
|
+
a: "string[]"
|
|
168
|
+
})
|
|
169
|
+
const ok = $.define(["a[]|boolean"])
|
|
170
|
+
attest<["a[]|boolean"]>(ok)
|
|
171
|
+
// @ts-expect-error
|
|
172
|
+
attest($.define({ not: "ok" })).type.errors(
|
|
173
|
+
writeUnresolvableMessage("ok")
|
|
174
|
+
)
|
|
175
|
+
})
|
|
176
|
+
})
|
|
177
|
+
describe("cyclic", () => {
|
|
178
|
+
it("base", () => {
|
|
179
|
+
const types = scope({ a: { b: "b" }, b: { a: "a" } }).export()
|
|
180
|
+
|
|
181
|
+
const a = {} as { b: typeof b }
|
|
182
|
+
const b = { a }
|
|
183
|
+
a.b = b
|
|
184
|
+
|
|
185
|
+
attest(types.a(a)).equals(a)
|
|
186
|
+
attest(types.a({ b: { a: { b: { a: 5 } } } }).toString()).snap(
|
|
187
|
+
"b.a.b.a must be an object (was number)"
|
|
188
|
+
)
|
|
189
|
+
|
|
190
|
+
// Type hint displays as "..." on hitting cycle (or any if "noErrorTruncation" is true)
|
|
191
|
+
attest({} as typeof types.a.infer).type.toString.snap()
|
|
192
|
+
attest({} as typeof types.b.infer.a.b.a.b.a.b.a).type.toString.snap()
|
|
193
|
+
|
|
194
|
+
// @ts-expect-error
|
|
195
|
+
attest({} as typeof types.a.infer.b.a.b.c).type.errors.snap(
|
|
196
|
+
`Property 'c' does not exist on type '{ a: { b: ...; }; }'.`
|
|
197
|
+
)
|
|
198
|
+
})
|
|
199
|
+
|
|
200
|
+
const getCyclicScope = () =>
|
|
201
|
+
scope({
|
|
202
|
+
package: {
|
|
203
|
+
name: "string",
|
|
204
|
+
"dependencies?": "package[]",
|
|
205
|
+
"contributors?": "contributor[]"
|
|
206
|
+
},
|
|
207
|
+
contributor: {
|
|
208
|
+
email: "email",
|
|
209
|
+
"packages?": "package[]"
|
|
210
|
+
}
|
|
211
|
+
})
|
|
212
|
+
|
|
213
|
+
type Package = ReturnType<typeof getCyclicScope>["infer"]["package"]
|
|
214
|
+
|
|
215
|
+
const getCyclicData = () => {
|
|
216
|
+
const packageData = {
|
|
217
|
+
name: "arktype",
|
|
218
|
+
dependencies: [{ name: "typescript" }],
|
|
219
|
+
contributors: [{ email: "david@arktype.io" }]
|
|
220
|
+
} satisfies Package
|
|
221
|
+
packageData.dependencies.push(packageData)
|
|
222
|
+
return packageData
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
it("cyclic union", () => {
|
|
226
|
+
const $ = scope({
|
|
227
|
+
a: { b: "b|false" },
|
|
228
|
+
b: { a: "a|true" }
|
|
229
|
+
})
|
|
230
|
+
attest($.infer).type.toString.snap(
|
|
231
|
+
"{ a: { b: false | { a: true | any; }; }; b: { a: true | { b: false | any; }; }; }"
|
|
232
|
+
)
|
|
233
|
+
})
|
|
234
|
+
|
|
235
|
+
it("cyclic intersection", () => {
|
|
236
|
+
const $ = scope({
|
|
237
|
+
a: { b: "b&a" },
|
|
238
|
+
b: { a: "a&b" }
|
|
239
|
+
})
|
|
240
|
+
attest($.infer).type.toString.snap(
|
|
241
|
+
"{ a: { b: { a: { b: any; a: any; }; b: any; }; }; b: { a: { b: { a: any; b: any; }; a: any; }; }; }"
|
|
242
|
+
)
|
|
243
|
+
})
|
|
244
|
+
|
|
245
|
+
it("allows valid", () => {
|
|
246
|
+
const types = getCyclicScope().export()
|
|
247
|
+
const data = getCyclicData()
|
|
248
|
+
attest(types.package(data)).snap({
|
|
249
|
+
name: "arktype",
|
|
250
|
+
dependencies: [{ name: "typescript" }, "(cycle)" as never],
|
|
251
|
+
contributors: [{ email: "david@arktype.io" }]
|
|
252
|
+
})
|
|
253
|
+
})
|
|
254
|
+
|
|
255
|
+
it("adds errors on invalid", () => {
|
|
256
|
+
const types = getCyclicScope().export()
|
|
257
|
+
const data = getCyclicData()
|
|
258
|
+
data.contributors[0].email = "ssalbdivad"
|
|
259
|
+
attest(types.package(data).toString()).snap(
|
|
260
|
+
'contributors[0].email must be a valid email (was "ssalbdivad")'
|
|
261
|
+
)
|
|
262
|
+
})
|
|
263
|
+
|
|
264
|
+
it("can include cyclic data in message", () => {
|
|
265
|
+
const data = getCyclicData()
|
|
266
|
+
const nonSelfDependent = getCyclicScope().type([
|
|
267
|
+
"package",
|
|
268
|
+
":",
|
|
269
|
+
p => !p.dependencies?.some(d => d.name === p.name)
|
|
270
|
+
])
|
|
271
|
+
attest(nonSelfDependent(data).toString()).snap(
|
|
272
|
+
'must be valid according to an anonymous predicate (was {"name":"arktype","dependencies":[{"name":"typescript"},"(cycle)"],"contributors":[{"email":"david@arktype.io"}]})'
|
|
273
|
+
)
|
|
274
|
+
})
|
|
275
|
+
|
|
276
|
+
it("union cyclic reference", () => {
|
|
277
|
+
const types = scope({
|
|
278
|
+
a: {
|
|
279
|
+
b: "b"
|
|
280
|
+
},
|
|
281
|
+
b: {
|
|
282
|
+
a: "a|3"
|
|
283
|
+
}
|
|
284
|
+
}).export()
|
|
285
|
+
attest(types.a.infer).type.toString.snap("{ b: { a: 3 | any; }")
|
|
286
|
+
|
|
287
|
+
attest(types.a.json).snap({
|
|
288
|
+
domain: "object",
|
|
289
|
+
prop: [
|
|
290
|
+
{
|
|
291
|
+
key: "b",
|
|
292
|
+
value: {
|
|
293
|
+
domain: "object",
|
|
294
|
+
prop: [{ key: "a", value: ["$a", { unit: 3 }] }]
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
]
|
|
298
|
+
})
|
|
299
|
+
|
|
300
|
+
const valid: typeof types.a.infer = { b: { a: 3 } }
|
|
301
|
+
|
|
302
|
+
attest(types.a(valid)).equals(valid)
|
|
303
|
+
|
|
304
|
+
valid.b.a = valid
|
|
305
|
+
|
|
306
|
+
// check cyclic
|
|
307
|
+
attest(types.a(valid)).equals(valid)
|
|
308
|
+
|
|
309
|
+
attest(types.a({ b: { a: { b: { a: 4 } } } }).toString()).snap(
|
|
310
|
+
'b.a.b.a must be an object or 3 (was number, 4) or b.a must be 3 (was {"b":{"a":4}})'
|
|
311
|
+
)
|
|
312
|
+
|
|
313
|
+
attest(types.b.infer).type.toString.snap("{ a: 3 | { b: any; }; }")
|
|
314
|
+
attest(types.b.json).snap({
|
|
315
|
+
domain: "object",
|
|
316
|
+
prop: [{ key: "a", value: ["$a", { unit: 3 }] }]
|
|
317
|
+
})
|
|
318
|
+
})
|
|
319
|
+
|
|
320
|
+
it("intersect cyclic reference", () => {
|
|
321
|
+
const types = scope({
|
|
322
|
+
a: {
|
|
323
|
+
b: "b"
|
|
324
|
+
},
|
|
325
|
+
b: {
|
|
326
|
+
c: "a&b"
|
|
327
|
+
}
|
|
328
|
+
}).export()
|
|
329
|
+
attest(types.a.infer).type.toString.snap()
|
|
330
|
+
attest(types.b.infer).type.toString.snap()
|
|
331
|
+
|
|
332
|
+
const expectedCyclicJson =
|
|
333
|
+
types.a.raw.firstReferenceOfKindOrThrow("alias").json
|
|
334
|
+
|
|
335
|
+
attest(types.a.json).snap({
|
|
336
|
+
domain: "object",
|
|
337
|
+
prop: [
|
|
338
|
+
{
|
|
339
|
+
key: "b",
|
|
340
|
+
value: {
|
|
341
|
+
domain: "object",
|
|
342
|
+
prop: [
|
|
343
|
+
{
|
|
344
|
+
key: "c",
|
|
345
|
+
value: expectedCyclicJson
|
|
346
|
+
}
|
|
347
|
+
]
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
]
|
|
351
|
+
})
|
|
352
|
+
const a = {} as typeof types.a.infer
|
|
353
|
+
const b = { c: {} } as typeof types.b.infer
|
|
354
|
+
a.b = b
|
|
355
|
+
b.c.b = b
|
|
356
|
+
b.c.c = b.c
|
|
357
|
+
|
|
358
|
+
attest(types.a(a)).equals(a)
|
|
359
|
+
attest(types.a({ b: { c: {} } }).toString())
|
|
360
|
+
.snap(`b.c.b must be { c: a&b } (was missing)
|
|
361
|
+
b.c.c must be a&b (was missing)`)
|
|
362
|
+
|
|
363
|
+
attest(types.b.json).snap({
|
|
364
|
+
domain: "object",
|
|
365
|
+
prop: [
|
|
366
|
+
{
|
|
367
|
+
key: "c",
|
|
368
|
+
value: expectedCyclicJson
|
|
369
|
+
}
|
|
370
|
+
]
|
|
371
|
+
})
|
|
372
|
+
})
|
|
139
373
|
})
|
|
140
374
|
})
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { attest, contextualize } from "@arktype/attest"
|
|
2
|
+
import { schema } from "@arktype/schema"
|
|
3
|
+
import { type } from "arktype"
|
|
4
|
+
|
|
5
|
+
contextualize(() => {
|
|
6
|
+
it("builtin prototypes", () => {
|
|
7
|
+
const a = type({
|
|
8
|
+
age: "number"
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
const b = type({
|
|
12
|
+
ages: a.array()
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
const c = schema(b.json as never)
|
|
16
|
+
|
|
17
|
+
attest(b.json).equals(c.json)
|
|
18
|
+
})
|
|
19
|
+
})
|
package/__tests__/string.test.ts
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
import { attest } from "@arktype/attest"
|
|
1
|
+
import { attest, contextualize } from "@arktype/attest"
|
|
2
|
+
import { writeUnresolvableMessage } from "@arktype/schema"
|
|
2
3
|
import { type } from "arktype"
|
|
3
4
|
import { writeUnterminatedEnclosedMessage } from "../parser/string/shift/operand/enclosed.js"
|
|
4
|
-
import {
|
|
5
|
-
writeExpressionExpectedMessage,
|
|
6
|
-
writeUnresolvableMessage
|
|
7
|
-
} from "../parser/string/shift/operand/unenclosed.js"
|
|
5
|
+
import { writeExpressionExpectedMessage } from "../parser/string/shift/operand/unenclosed.js"
|
|
8
6
|
|
|
9
|
-
|
|
7
|
+
contextualize(() => {
|
|
10
8
|
it("errors on empty string", () => {
|
|
11
9
|
// @ts-expect-error
|
|
12
10
|
attest(() => type("")).throws(writeExpressionExpectedMessage(""))
|
|
13
11
|
})
|
|
12
|
+
|
|
14
13
|
it("ignores whitespace between identifiers/operators", () => {
|
|
15
14
|
const t = type(` \n string |
|
|
16
|
-
|
|
15
|
+
\tboolean [] `)
|
|
17
16
|
attest<string | boolean[]>(t.infer)
|
|
18
17
|
attest(t.json).equals(type("string|boolean[]").json)
|
|
19
18
|
})
|
|
19
|
+
|
|
20
20
|
it("errors on bad whitespace", () => {
|
|
21
21
|
attest(() =>
|
|
22
22
|
// @ts-expect-error
|
|
@@ -25,22 +25,26 @@ describe("string", () => {
|
|
|
25
25
|
.throws(writeUnresolvableMessage("boo"))
|
|
26
26
|
.type.errors("string | boolean")
|
|
27
27
|
})
|
|
28
|
+
|
|
28
29
|
it("unterminated string", () => {
|
|
29
30
|
// @ts-expect-error
|
|
30
31
|
attest(() => type("'bob")).throwsAndHasTypeError(
|
|
31
32
|
writeUnterminatedEnclosedMessage("bob", "'")
|
|
32
33
|
)
|
|
33
34
|
})
|
|
35
|
+
|
|
34
36
|
it("shallow single autocomplete", () => {
|
|
35
37
|
// @ts-expect-error
|
|
36
38
|
attest(() => type("str")).type.errors(
|
|
37
39
|
`Argument of type '"str"' is not assignable to parameter of type '"string"'`
|
|
38
40
|
)
|
|
39
41
|
})
|
|
42
|
+
|
|
40
43
|
it("shallow multi autocomplete", () => {
|
|
41
44
|
// @ts-expect-error
|
|
42
45
|
attest(() => type("s")).type.errors(`"string" | "symbol" | "semver"`)
|
|
43
46
|
})
|
|
47
|
+
|
|
44
48
|
it("post-operator autocomplete", () => {
|
|
45
49
|
// @ts-expect-error
|
|
46
50
|
attest(() => type("string|num")).type.errors(`"string|number"`)
|
|
@@ -1,38 +1,27 @@
|
|
|
1
|
-
import { attest } from "@arktype/attest"
|
|
2
|
-
import { lazily } from "@arktype/util"
|
|
3
|
-
import { scope, type, type Ark, type Module, type Scope } from "arktype"
|
|
1
|
+
import { attest, contextualize } from "@arktype/attest"
|
|
4
2
|
import {
|
|
5
3
|
writeMissingSubmoduleAccessMessage,
|
|
6
4
|
writeNonSubmoduleDotMessage,
|
|
7
5
|
writeUnresolvableMessage
|
|
8
|
-
} from "
|
|
6
|
+
} from "@arktype/schema"
|
|
7
|
+
import { type Module, type Scope, scope, type } from "arktype"
|
|
9
8
|
|
|
10
|
-
|
|
11
|
-
scope({
|
|
9
|
+
contextualize(() => {
|
|
10
|
+
const $ = scope({
|
|
12
11
|
a: "string",
|
|
13
12
|
b: "sub.alias",
|
|
14
13
|
sub: scope({ alias: "number" }).export()
|
|
15
14
|
})
|
|
16
|
-
)
|
|
17
15
|
|
|
18
|
-
describe("submodules", () => {
|
|
19
16
|
it("base", () => {
|
|
20
17
|
const types = $.export()
|
|
21
18
|
attest<
|
|
22
19
|
Module<{
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
alias: number
|
|
29
|
-
}
|
|
30
|
-
locals: {}
|
|
31
|
-
ambient: Ark
|
|
32
|
-
}>
|
|
33
|
-
}
|
|
34
|
-
locals: {}
|
|
35
|
-
ambient: Ark
|
|
20
|
+
a: string
|
|
21
|
+
b: number
|
|
22
|
+
sub: Module<{
|
|
23
|
+
alias: number
|
|
24
|
+
}>
|
|
36
25
|
}>
|
|
37
26
|
>(types)
|
|
38
27
|
|
|
@@ -41,59 +30,57 @@ describe("submodules", () => {
|
|
|
41
30
|
attest(types.sub.alias.json).is(expected)
|
|
42
31
|
attest(types.b.json).is(expected)
|
|
43
32
|
})
|
|
33
|
+
|
|
44
34
|
it("non-submodule dot access", () => {
|
|
45
35
|
// @ts-expect-error
|
|
46
36
|
attest(() => $.type("b.foo")).throwsAndHasTypeError(
|
|
47
37
|
writeNonSubmoduleDotMessage("b")
|
|
48
38
|
)
|
|
49
39
|
})
|
|
40
|
+
|
|
50
41
|
it("thunk submodule", () => {
|
|
51
42
|
const $ = scope({
|
|
52
43
|
a: "string",
|
|
53
44
|
c: "a",
|
|
54
45
|
sub: () =>
|
|
55
|
-
|
|
46
|
+
scope({
|
|
47
|
+
...$.import("a", "c"),
|
|
56
48
|
foo: "a",
|
|
57
49
|
bar: "foo"
|
|
58
50
|
}).export()
|
|
59
51
|
})
|
|
60
52
|
attest<
|
|
61
53
|
Scope<{
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
bar: string
|
|
69
|
-
}
|
|
70
|
-
locals: {}
|
|
71
|
-
ambient: Ark
|
|
72
|
-
}>
|
|
73
|
-
}
|
|
74
|
-
locals: {}
|
|
75
|
-
ambient: Ark
|
|
54
|
+
a: string
|
|
55
|
+
c: string
|
|
56
|
+
sub: Module<{
|
|
57
|
+
foo: string
|
|
58
|
+
bar: string
|
|
59
|
+
}>
|
|
76
60
|
}>
|
|
77
61
|
>($)
|
|
78
62
|
})
|
|
63
|
+
|
|
79
64
|
it("no alias reference", () => {
|
|
80
65
|
// @ts-expect-error
|
|
81
66
|
attest(() => $.type("sub")).throwsAndHasTypeError(
|
|
82
67
|
writeMissingSubmoduleAccessMessage("sub")
|
|
83
68
|
)
|
|
84
69
|
})
|
|
70
|
+
|
|
85
71
|
it("bad alias reference", () => {
|
|
86
72
|
// @ts-expect-error
|
|
87
73
|
attest(() => $.type("sub.marine")).throwsAndHasTypeError(
|
|
88
74
|
writeUnresolvableMessage("sub.marine")
|
|
89
75
|
)
|
|
90
76
|
})
|
|
91
|
-
|
|
92
|
-
|
|
77
|
+
|
|
78
|
+
it("completions", () => {
|
|
79
|
+
const base = scope({ foo: "true" }).export()
|
|
93
80
|
// @ts-expect-error
|
|
94
81
|
attest(() => scope({ base, reference: "base." }).export())
|
|
95
82
|
.throws(writeUnresolvableMessage("base."))
|
|
96
|
-
.type.
|
|
83
|
+
.type.completions({ "base.": ["base.foo"] })
|
|
97
84
|
})
|
|
98
85
|
// TODO: private aliases
|
|
99
86
|
})
|