arktype 2.0.0-dev.1 → 2.0.0-dev.11
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/out/api.d.ts +6 -0
- package/out/api.js +5 -0
- package/out/ark.d.ts +15 -39
- package/out/ark.js +9 -18
- package/out/config.d.ts +1 -0
- package/out/config.js +2 -0
- package/out/generic.d.ts +21 -0
- package/out/generic.js +24 -0
- package/out/main.d.ts +6 -7
- package/out/main.js +4 -3
- package/out/match.d.ts +57 -34
- package/out/match.js +51 -17
- package/out/module.d.ts +12 -0
- package/out/module.js +3 -0
- package/out/parser/definition.d.ts +14 -14
- package/out/parser/definition.js +10 -13
- package/out/parser/generic.d.ts +5 -7
- package/out/parser/generic.js +7 -11
- package/out/parser/objectLiteral.d.ts +44 -18
- package/out/parser/objectLiteral.js +57 -51
- package/out/parser/semantic/bounds.d.ts +10 -8
- package/out/parser/semantic/bounds.js +0 -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/utils.d.ts +3 -7
- package/out/parser/semantic/utils.js +0 -1
- package/out/parser/semantic/validate.d.ts +16 -13
- package/out/parser/semantic/validate.js +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/shared.d.ts +14 -28
- package/out/parser/string/reduce/shared.js +4 -3
- 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 +2 -2
- package/out/parser/string/shift/operand/date.js +4 -7
- package/out/parser/string/shift/operand/enclosed.d.ts +3 -4
- package/out/parser/string/shift/operand/enclosed.js +14 -14
- 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/operand.d.ts +3 -4
- package/out/parser/string/shift/operand/operand.js +8 -12
- package/out/parser/string/shift/operand/unenclosed.d.ts +16 -21
- package/out/parser/string/shift/operand/unenclosed.js +24 -36
- package/out/parser/string/shift/operator/bounds.d.ts +8 -12
- package/out/parser/string/shift/operator/bounds.js +45 -56
- package/out/parser/string/shift/operator/divisor.d.ts +1 -3
- package/out/parser/string/shift/operator/divisor.js +1 -3
- package/out/parser/string/shift/operator/operator.d.ts +1 -2
- package/out/parser/string/shift/operator/operator.js +12 -19
- package/out/parser/string/shift/scanner.d.ts +2 -2
- package/out/parser/string/shift/scanner.js +14 -16
- package/out/parser/string/string.d.ts +8 -6
- package/out/parser/string/string.js +1 -3
- package/out/parser/tuple.d.ts +102 -43
- package/out/parser/tuple.js +119 -87
- package/out/scope.d.ts +44 -119
- package/out/scope.js +39 -209
- package/out/type.d.ts +43 -64
- package/out/type.js +30 -131
- package/package.json +12 -10
- package/__tests__/array.test.ts +0 -272
- package/__tests__/astUtils.test.ts +0 -17
- package/__tests__/badDefinitionType.test.ts +0 -51
- package/__tests__/basis.test.ts +0 -46
- package/__tests__/bounds.test.ts +0 -388
- package/__tests__/cast.test.ts +0 -32
- package/__tests__/completions.test.ts +0 -46
- package/__tests__/config.test.ts +0 -168
- package/__tests__/cyclic.test.ts +0 -144
- package/__tests__/dateLiteral.test.ts +0 -32
- package/__tests__/declared.test.ts +0 -123
- package/__tests__/discrimination.test.ts +0 -74
- package/__tests__/divisor.test.ts +0 -90
- package/__tests__/enclosed.test.ts +0 -93
- package/__tests__/examples.test.ts +0 -56
- package/__tests__/expressions.test.ts +0 -138
- package/__tests__/generateBenchData.ts +0 -89
- package/__tests__/generated/cyclic.ts +0 -3058
- package/__tests__/generic.test.ts +0 -294
- package/__tests__/group.test.ts +0 -72
- package/__tests__/hkt.test.ts +0 -22
- package/__tests__/imports.test.ts +0 -113
- package/__tests__/instanceof.test.ts +0 -102
- package/__tests__/intersection.test.ts +0 -104
- package/__tests__/jsdoc.test.ts +0 -64
- package/__tests__/keyTraversal.test.ts +0 -73
- package/__tests__/keyof.test.ts +0 -134
- package/__tests__/keywords.test.ts +0 -188
- package/__tests__/literal.test.ts +0 -51
- package/__tests__/match.test.ts +0 -34
- package/__tests__/methods.bench.ts +0 -22
- package/__tests__/morph.test.ts +0 -409
- package/__tests__/narrow.test.ts +0 -102
- package/__tests__/nodeDefinitions.test.ts +0 -190
- package/__tests__/object.bench.ts +0 -28
- package/__tests__/objectLiteral.test.ts +0 -300
- package/__tests__/operand.bench.ts +0 -45
- package/__tests__/operator.bench.ts +0 -130
- package/__tests__/regex.test.ts +0 -58
- package/__tests__/scope.bench.ts +0 -39
- package/__tests__/scope.test.ts +0 -140
- package/__tests__/string.test.ts +0 -48
- package/__tests__/submodule.test.ts +0 -99
- package/__tests__/this.test.ts +0 -57
- package/__tests__/thunk.test.ts +0 -80
- package/__tests__/traverse.test.ts +0 -119
- package/__tests__/tsGenerics.test.ts +0 -12
- package/__tests__/typeMethods.test.ts +0 -69
- package/__tests__/typeReference.test.ts +0 -39
- package/__tests__/unenclosed.test.ts +0 -120
- package/__tests__/union.test.ts +0 -147
- package/ark.ts +0 -91
- package/main.ts +0 -18
- package/match.ts +0 -116
- package/out/ark.d.ts.map +0 -1
- package/out/ark.js.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/main.js.map +0 -1
- package/out/match.d.ts.map +0 -1
- package/out/match.js.map +0 -1
- package/out/parser/definition.d.ts.map +0 -1
- package/out/parser/definition.js.map +0 -1
- package/out/parser/generic.d.ts.map +0 -1
- package/out/parser/generic.js.map +0 -1
- package/out/parser/objectLiteral.d.ts.map +0 -1
- package/out/parser/objectLiteral.js.map +0 -1
- package/out/parser/semantic/bounds.d.ts.map +0 -1
- package/out/parser/semantic/bounds.js.map +0 -1
- package/out/parser/semantic/divisor.d.ts.map +0 -1
- package/out/parser/semantic/divisor.js.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/utils.js.map +0 -1
- package/out/parser/semantic/validate.d.ts.map +0 -1
- package/out/parser/semantic/validate.js.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/dynamic.js.map +0 -1
- package/out/parser/string/reduce/shared.d.ts.map +0 -1
- package/out/parser/string/reduce/shared.js.map +0 -1
- package/out/parser/string/reduce/static.d.ts.map +0 -1
- package/out/parser/string/reduce/static.js.map +0 -1
- package/out/parser/string/shift/operand/date.d.ts.map +0 -1
- package/out/parser/string/shift/operand/date.js.map +0 -1
- package/out/parser/string/shift/operand/enclosed.d.ts.map +0 -1
- package/out/parser/string/shift/operand/enclosed.js.map +0 -1
- package/out/parser/string/shift/operand/genericArgs.d.ts.map +0 -1
- package/out/parser/string/shift/operand/genericArgs.js.map +0 -1
- package/out/parser/string/shift/operand/operand.d.ts.map +0 -1
- package/out/parser/string/shift/operand/operand.js.map +0 -1
- package/out/parser/string/shift/operand/unenclosed.d.ts.map +0 -1
- package/out/parser/string/shift/operand/unenclosed.js.map +0 -1
- package/out/parser/string/shift/operator/bounds.d.ts.map +0 -1
- package/out/parser/string/shift/operator/bounds.js.map +0 -1
- package/out/parser/string/shift/operator/divisor.d.ts.map +0 -1
- package/out/parser/string/shift/operator/divisor.js.map +0 -1
- package/out/parser/string/shift/operator/operator.d.ts.map +0 -1
- package/out/parser/string/shift/operator/operator.js.map +0 -1
- package/out/parser/string/shift/scanner.d.ts.map +0 -1
- package/out/parser/string/shift/scanner.js.map +0 -1
- package/out/parser/string/string.d.ts.map +0 -1
- package/out/parser/string/string.js.map +0 -1
- package/out/parser/tuple.d.ts.map +0 -1
- package/out/parser/tuple.js.map +0 -1
- package/out/scope.d.ts.map +0 -1
- package/out/scope.js.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/type.js.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/definition.ts +0 -159
- package/parser/generic.ts +0 -68
- package/parser/objectLiteral.ts +0 -197
- package/parser/semantic/bounds.ts +0 -61
- package/parser/semantic/divisor.ts +0 -13
- package/parser/semantic/intersections.ts +0 -46
- package/parser/semantic/semantic.ts +0 -144
- package/parser/semantic/utils.ts +0 -26
- package/parser/semantic/validate.ts +0 -109
- package/parser/shared.ts +0 -152
- package/parser/string/reduce/dynamic.ts +0 -196
- package/parser/string/reduce/shared.ts +0 -101
- package/parser/string/reduce/static.ts +0 -298
- package/parser/string/shift/operand/date.ts +0 -54
- package/parser/string/shift/operand/enclosed.ts +0 -118
- package/parser/string/shift/operand/genericArgs.ts +0 -127
- package/parser/string/shift/operand/operand.ts +0 -48
- package/parser/string/shift/operand/unenclosed.ts +0 -268
- package/parser/string/shift/operator/bounds.ts +0 -264
- package/parser/string/shift/operator/divisor.ts +0 -36
- package/parser/string/shift/operator/operator.ts +0 -82
- package/parser/string/shift/scanner.ts +0 -209
- package/parser/string/string.ts +0 -90
- package/parser/tuple.ts +0 -474
- package/scope.ts +0 -574
- package/type.ts +0 -347
- package/util.ts +0 -36
package/__tests__/scope.test.ts
DELETED
|
@@ -1,140 +0,0 @@
|
|
|
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
|
-
})
|
package/__tests__/string.test.ts
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
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
|
-
})
|
|
@@ -1,99 +0,0 @@
|
|
|
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
|
-
})
|
package/__tests__/this.test.ts
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
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
|
-
})
|
package/__tests__/thunk.test.ts
DELETED
|
@@ -1,80 +0,0 @@
|
|
|
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
|
-
})
|
|
@@ -1,119 +0,0 @@
|
|
|
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
|
-
})
|
|
@@ -1,12 +0,0 @@
|
|
|
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
|
-
})
|
|
@@ -1,69 +0,0 @@
|
|
|
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
|
-
})
|
|
@@ -1,39 +0,0 @@
|
|
|
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
|
-
})
|