arktype 2.0.0-dev.9 → 2.0.0-rc.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/LICENSE +7 -0
- package/out/config.d.ts +25 -0
- package/out/config.js +2 -0
- package/out/generic.d.ts +154 -18
- package/out/generic.js +37 -23
- package/out/index.d.ts +7 -0
- package/out/index.js +6 -0
- package/out/intersect.d.ts +28 -0
- package/out/intersect.js +1 -0
- package/out/keys.d.ts +16 -0
- package/out/keys.js +1 -0
- package/out/keywords/ark.d.ts +33 -0
- package/out/keywords/ark.js +18 -0
- package/out/keywords/ast.d.ts +193 -0
- package/out/keywords/ast.js +2 -0
- package/out/keywords/builtin.d.ts +13 -0
- package/out/keywords/builtin.js +10 -0
- package/out/keywords/constructors/Array.d.ts +16 -0
- package/out/keywords/constructors/Array.js +19 -0
- package/out/keywords/constructors/FormData.d.ts +9 -0
- package/out/keywords/constructors/FormData.js +25 -0
- package/out/keywords/constructors/TypedArray.d.ts +15 -0
- package/out/keywords/constructors/TypedArray.js +14 -0
- package/out/keywords/constructors/constructors.d.ts +41 -0
- package/out/keywords/constructors/constructors.js +31 -0
- package/out/keywords/number/epoch.d.ts +10 -0
- package/out/keywords/number/epoch.js +26 -0
- package/out/keywords/number/integer.d.ts +3 -0
- package/out/keywords/number/integer.js +5 -0
- package/out/keywords/number/number.d.ts +26 -0
- package/out/keywords/number/number.js +9 -0
- package/out/keywords/string/alpha.d.ts +7 -0
- package/out/keywords/string/alpha.js +2 -0
- package/out/keywords/string/alphanumeric.d.ts +7 -0
- package/out/keywords/string/alphanumeric.js +2 -0
- package/out/keywords/string/capitalize.d.ts +14 -0
- package/out/keywords/string/capitalize.js +12 -0
- package/out/keywords/string/creditCard.d.ts +8 -0
- package/out/keywords/string/creditCard.js +37 -0
- package/out/keywords/string/date.d.ts +54 -0
- package/out/keywords/string/date.js +98 -0
- package/out/keywords/string/digits.d.ts +7 -0
- package/out/keywords/string/digits.js +2 -0
- package/out/keywords/string/email.d.ts +7 -0
- package/out/keywords/string/email.js +4 -0
- package/out/keywords/string/integer.d.ts +15 -0
- package/out/keywords/string/integer.js +16 -0
- package/out/keywords/string/ip.d.ts +20 -0
- package/out/keywords/string/ip.js +23 -0
- package/out/keywords/string/json.d.ts +14 -0
- package/out/keywords/string/json.js +45 -0
- package/out/keywords/string/lower.d.ts +14 -0
- package/out/keywords/string/lower.js +12 -0
- package/out/keywords/string/normalize.d.ts +72 -0
- package/out/keywords/string/normalize.js +43 -0
- package/out/keywords/string/numeric.d.ts +14 -0
- package/out/keywords/string/numeric.js +13 -0
- package/out/keywords/string/semver.d.ts +7 -0
- package/out/keywords/string/semver.js +4 -0
- package/out/keywords/string/string.d.ts +57 -0
- package/out/keywords/string/string.js +41 -0
- package/out/keywords/string/trim.d.ts +14 -0
- package/out/keywords/string/trim.js +14 -0
- package/out/keywords/string/upper.d.ts +14 -0
- package/out/keywords/string/upper.js +12 -0
- package/out/keywords/string/url.d.ts +14 -0
- package/out/keywords/string/url.js +37 -0
- package/out/keywords/string/utils.d.ts +2 -0
- package/out/keywords/string/utils.js +10 -0
- package/out/keywords/string/uuid.d.ts +38 -0
- package/out/keywords/string/uuid.js +19 -0
- package/out/keywords/ts.d.ts +53 -0
- package/out/keywords/ts.js +51 -0
- package/out/keywords/utils.d.ts +1 -0
- package/out/keywords/utils.js +2 -0
- package/out/module.d.ts +21 -9
- package/out/module.js +3 -3
- package/out/parser/definition.d.ts +20 -15
- package/out/parser/definition.js +5 -5
- package/out/parser/objectLiteral.d.ts +53 -35
- package/out/parser/objectLiteral.js +75 -50
- package/out/parser/semantic/bounds.d.ts +10 -9
- package/out/parser/semantic/default.d.ts +8 -0
- package/out/parser/semantic/default.js +1 -0
- package/out/parser/semantic/divisor.d.ts +5 -5
- package/out/parser/semantic/divisor.js +1 -0
- package/out/parser/semantic/infer.d.ts +21 -12
- package/out/parser/semantic/infer.js +1 -0
- package/out/parser/semantic/keyof.d.ts +7 -0
- package/out/parser/semantic/keyof.js +1 -0
- package/out/parser/semantic/utils.d.ts +3 -3
- package/out/parser/semantic/utils.js +1 -0
- package/out/parser/semantic/validate.d.ts +21 -17
- package/out/parser/semantic/validate.js +0 -1
- package/out/parser/string/reduce/dynamic.d.ts +16 -14
- package/out/parser/string/reduce/dynamic.js +8 -7
- package/out/parser/string/reduce/shared.d.ts +15 -15
- package/out/parser/string/reduce/shared.js +4 -2
- package/out/parser/string/reduce/static.d.ts +22 -23
- package/out/parser/string/reduce/static.js +1 -0
- package/out/parser/string/shift/operand/date.d.ts +2 -4
- package/out/parser/string/shift/operand/date.js +1 -1
- package/out/parser/string/shift/operand/enclosed.d.ts +10 -6
- package/out/parser/string/shift/operand/enclosed.js +5 -4
- package/out/parser/string/shift/operand/genericArgs.d.ts +13 -13
- package/out/parser/string/shift/operand/genericArgs.js +7 -12
- package/out/parser/string/shift/operand/operand.d.ts +8 -7
- package/out/parser/string/shift/operand/operand.js +5 -6
- package/out/parser/string/shift/operand/unenclosed.d.ts +29 -16
- package/out/parser/string/shift/operand/unenclosed.js +9 -12
- package/out/parser/string/shift/operator/bounds.d.ts +15 -20
- package/out/parser/string/shift/operator/bounds.js +11 -16
- package/out/parser/string/shift/operator/default.d.ts +19 -0
- package/out/parser/string/shift/operator/default.js +17 -0
- package/out/parser/string/shift/operator/divisor.d.ts +3 -3
- package/out/parser/string/shift/operator/divisor.js +1 -1
- package/out/parser/string/shift/operator/operator.d.ts +7 -6
- package/out/parser/string/shift/operator/operator.js +1 -1
- package/out/parser/string/shift/scanner.d.ts +26 -30
- package/out/parser/string/shift/scanner.js +23 -20
- package/out/parser/string/string.d.ts +14 -11
- package/out/parser/string/string.js +5 -2
- package/out/parser/tuple.d.ts +21 -19
- package/out/parser/tuple.js +12 -23
- package/out/scope.d.ts +102 -55
- package/out/scope.js +61 -60
- package/out/subtypes/array.d.ts +13 -0
- package/out/subtypes/array.js +1 -0
- package/out/subtypes/base.d.ts +84 -0
- package/out/subtypes/base.js +1 -0
- package/out/subtypes/date.d.ts +11 -0
- package/out/subtypes/date.js +1 -0
- package/out/subtypes/instantiate.d.ts +16 -0
- package/out/subtypes/instantiate.js +1 -0
- package/out/subtypes/morph.d.ts +13 -0
- package/out/subtypes/morph.js +1 -0
- package/out/subtypes/number.d.ts +12 -0
- package/out/subtypes/number.js +1 -0
- package/out/subtypes/object.d.ts +33 -0
- package/out/subtypes/object.js +1 -0
- package/out/subtypes/string.d.ts +13 -0
- package/out/subtypes/string.js +1 -0
- package/out/subtypes/validator.d.ts +11 -0
- package/out/subtypes/validator.js +1 -0
- package/out/type.d.ts +30 -49
- package/out/type.js +19 -14
- package/package.json +46 -45
- package/__tests__/array.test.ts +0 -391
- package/__tests__/badDefinitionType.test.ts +0 -58
- package/__tests__/basis.test.ts +0 -50
- package/__tests__/bounds.test.ts +0 -318
- package/__tests__/cast.test.ts +0 -37
- package/__tests__/completions.test.ts +0 -51
- package/__tests__/config.test.ts +0 -96
- package/__tests__/dateLiteral.test.ts +0 -36
- package/__tests__/declared.test.ts +0 -124
- package/__tests__/discrimination.test.ts +0 -74
- package/__tests__/divisor.test.ts +0 -114
- package/__tests__/enclosed.test.ts +0 -107
- package/__tests__/examples.test.ts +0 -56
- package/__tests__/expressions.test.ts +0 -210
- package/__tests__/generateBenchData.ts +0 -89
- package/__tests__/generated/cyclic.ts +0 -3058
- package/__tests__/generic.test.ts +0 -313
- package/__tests__/group.test.ts +0 -85
- package/__tests__/hkt.test.ts +0 -22
- package/__tests__/imports.test.ts +0 -126
- package/__tests__/instanceof.test.ts +0 -104
- package/__tests__/intersection.test.ts +0 -108
- package/__tests__/keyTraversal.test.ts +0 -77
- package/__tests__/keyof.test.ts +0 -150
- package/__tests__/keywords.test.ts +0 -214
- package/__tests__/literal.test.ts +0 -90
- package/__tests__/match.bench.ts +0 -31
- package/__tests__/match.test.ts +0 -234
- package/__tests__/methods.bench.ts +0 -22
- package/__tests__/narrow.test.ts +0 -100
- package/__tests__/object.bench.ts +0 -28
- package/__tests__/objectLiteral.test.ts +0 -442
- package/__tests__/operand.bench.ts +0 -45
- package/__tests__/operator.bench.ts +0 -130
- package/__tests__/pipe.test.ts +0 -501
- package/__tests__/realWorld.test.ts +0 -63
- package/__tests__/regex.test.ts +0 -68
- package/__tests__/scope.bench.ts +0 -39
- package/__tests__/scope.test.ts +0 -374
- package/__tests__/serialization.test.ts +0 -19
- package/__tests__/string.test.ts +0 -52
- package/__tests__/submodule.test.ts +0 -86
- package/__tests__/this.test.ts +0 -83
- package/__tests__/thunk.test.ts +0 -86
- package/__tests__/traverse.test.ts +0 -141
- package/__tests__/tsGenerics.test.ts +0 -12
- package/__tests__/type.test.ts +0 -36
- package/__tests__/typeReference.test.ts +0 -39
- package/__tests__/unenclosed.test.ts +0 -136
- package/__tests__/union.test.ts +0 -214
- package/ark.ts +0 -47
- package/generic.ts +0 -69
- package/main.ts +0 -6
- package/match.ts +0 -218
- package/module.ts +0 -15
- package/out/ark.d.ts +0 -28
- package/out/ark.js +0 -10
- package/out/ark.js.map +0 -1
- package/out/generic.js.map +0 -1
- package/out/main.d.ts +0 -6
- package/out/main.js +0 -5
- package/out/main.js.map +0 -1
- package/out/match.d.ts +0 -71
- package/out/match.js +0 -52
- package/out/match.js.map +0 -1
- package/out/module.js.map +0 -1
- package/out/parser/definition.js.map +0 -1
- package/out/parser/generic.d.ts +0 -16
- package/out/parser/generic.js +0 -15
- package/out/parser/generic.js.map +0 -1
- package/out/parser/objectLiteral.js.map +0 -1
- package/out/parser/semantic/bounds.js.map +0 -1
- package/out/parser/semantic/divisor.js.map +0 -1
- package/out/parser/semantic/infer.js.map +0 -1
- package/out/parser/semantic/utils.js.map +0 -1
- package/out/parser/semantic/validate.js.map +0 -1
- package/out/parser/string/reduce/dynamic.js.map +0 -1
- package/out/parser/string/reduce/shared.js.map +0 -1
- package/out/parser/string/reduce/static.js.map +0 -1
- package/out/parser/string/shift/operand/date.js.map +0 -1
- package/out/parser/string/shift/operand/enclosed.js.map +0 -1
- package/out/parser/string/shift/operand/genericArgs.js.map +0 -1
- package/out/parser/string/shift/operand/operand.js.map +0 -1
- package/out/parser/string/shift/operand/unenclosed.js.map +0 -1
- package/out/parser/string/shift/operator/bounds.js.map +0 -1
- package/out/parser/string/shift/operator/divisor.js.map +0 -1
- package/out/parser/string/shift/operator/operator.js.map +0 -1
- package/out/parser/string/shift/scanner.js.map +0 -1
- package/out/parser/string/string.js.map +0 -1
- package/out/parser/tuple.js.map +0 -1
- package/out/scope.js.map +0 -1
- package/out/type.js.map +0 -1
- package/parser/definition.ts +0 -143
- package/parser/generic.ts +0 -66
- package/parser/objectLiteral.ts +0 -233
- package/parser/semantic/bounds.ts +0 -62
- package/parser/semantic/divisor.ts +0 -11
- package/parser/semantic/infer.ts +0 -141
- package/parser/semantic/utils.ts +0 -15
- package/parser/semantic/validate.ts +0 -117
- package/parser/string/reduce/dynamic.ts +0 -208
- package/parser/string/reduce/shared.ts +0 -99
- package/parser/string/reduce/static.ts +0 -296
- package/parser/string/shift/operand/date.ts +0 -55
- package/parser/string/shift/operand/enclosed.ts +0 -123
- package/parser/string/shift/operand/genericArgs.ts +0 -128
- package/parser/string/shift/operand/operand.ts +0 -42
- package/parser/string/shift/operand/unenclosed.ts +0 -217
- package/parser/string/shift/operator/bounds.ts +0 -270
- package/parser/string/shift/operator/divisor.ts +0 -33
- package/parser/string/shift/operator/operator.ts +0 -71
- package/parser/string/shift/scanner.ts +0 -210
- package/parser/string/string.ts +0 -85
- package/parser/tuple.ts +0 -542
- package/scope.ts +0 -291
- package/type.ts +0 -266
|
@@ -1,442 +0,0 @@
|
|
|
1
|
-
import { attest, contextualize } from "@arktype/attest"
|
|
2
|
-
import {
|
|
3
|
-
writeInvalidPropertyKeyMessage,
|
|
4
|
-
writeUnboundableMessage,
|
|
5
|
-
writeUnresolvableMessage
|
|
6
|
-
} from "@arktype/schema"
|
|
7
|
-
import { printable, registeredReference } from "@arktype/util"
|
|
8
|
-
import { scope, type } from "arktype"
|
|
9
|
-
import { writeInvalidSpreadTypeMessage } from "../parser/objectLiteral.js"
|
|
10
|
-
|
|
11
|
-
contextualize(
|
|
12
|
-
"named",
|
|
13
|
-
() => {
|
|
14
|
-
it("empty", () => {
|
|
15
|
-
const o = type({})
|
|
16
|
-
attest(o.json).equals(type("object").json)
|
|
17
|
-
})
|
|
18
|
-
|
|
19
|
-
it("required", () => {
|
|
20
|
-
const o = type({ a: "string", b: "number" })
|
|
21
|
-
attest<{ a: string; b: number }>(o.infer)
|
|
22
|
-
attest(o.json).snap({
|
|
23
|
-
domain: "object",
|
|
24
|
-
prop: [
|
|
25
|
-
{ key: "a", value: "string" },
|
|
26
|
-
{ key: "b", value: "number" }
|
|
27
|
-
]
|
|
28
|
-
})
|
|
29
|
-
})
|
|
30
|
-
|
|
31
|
-
it("optional keys", () => {
|
|
32
|
-
const o = type({ "a?": "string", b: "number" })
|
|
33
|
-
attest<{ a?: string; b: number }>(o.infer)
|
|
34
|
-
attest(o.json).snap({
|
|
35
|
-
domain: "object",
|
|
36
|
-
prop: [
|
|
37
|
-
{ key: "a", optional: true, value: "string" },
|
|
38
|
-
{ key: "b", value: "number" }
|
|
39
|
-
]
|
|
40
|
-
})
|
|
41
|
-
})
|
|
42
|
-
|
|
43
|
-
it("symbol key", () => {
|
|
44
|
-
const s = Symbol()
|
|
45
|
-
const name = registeredReference(s)
|
|
46
|
-
const t = type({
|
|
47
|
-
[s]: "string"
|
|
48
|
-
})
|
|
49
|
-
attest<{ [s]: string }>(t.infer)
|
|
50
|
-
attest(t.json).snap({
|
|
51
|
-
domain: "object",
|
|
52
|
-
prop: [{ key: name, value: "string" }]
|
|
53
|
-
})
|
|
54
|
-
})
|
|
55
|
-
|
|
56
|
-
it("error in obj that has tuple that writes error at proper path", () => {
|
|
57
|
-
// @ts-expect-error
|
|
58
|
-
attest(() => type({ "a?": ["string", ["stringx", "?"]] }))
|
|
59
|
-
.throws(writeUnresolvableMessage("stringx"))
|
|
60
|
-
.type.errors.snap(
|
|
61
|
-
"Type '\"stringx\"' is not assignable to type '\"'stringx' is unresolvable \"'."
|
|
62
|
-
)
|
|
63
|
-
})
|
|
64
|
-
|
|
65
|
-
it("nested", () => {
|
|
66
|
-
const t = type({ "a?": { b: "boolean" } })
|
|
67
|
-
attest<{ a?: { b: boolean } }>(t.infer)
|
|
68
|
-
})
|
|
69
|
-
|
|
70
|
-
it("intersections", () => {
|
|
71
|
-
const a = { "a?": "string" } as const
|
|
72
|
-
const b = { b: "string" } as const
|
|
73
|
-
const c = { "c?": "string" } as const
|
|
74
|
-
const abc = type(a).and(b).and(c)
|
|
75
|
-
attest<{ a?: string; b: string; c?: string }>(abc.infer)
|
|
76
|
-
attest(abc.json).equals(type({ ...a, ...b, ...c }).json)
|
|
77
|
-
attest(abc.json).equals(type([[a, "&", b], "&", c]).json)
|
|
78
|
-
})
|
|
79
|
-
|
|
80
|
-
it("intersection", () => {
|
|
81
|
-
const t = type({ a: "number" }).and({ b: "boolean" })
|
|
82
|
-
// Should be simplified from {a: number} & {b: boolean} to {a: number, b: boolean}
|
|
83
|
-
attest(t.infer).type.toString.snap("{ a: number; b: boolean; }")
|
|
84
|
-
attest(t.json).equals(type({ a: "number", b: "boolean" }).json)
|
|
85
|
-
})
|
|
86
|
-
|
|
87
|
-
it("escaped optional token", () => {
|
|
88
|
-
const t = type({ "a\\?": "string" })
|
|
89
|
-
attest<{ "a?": string }>(t.infer)
|
|
90
|
-
})
|
|
91
|
-
|
|
92
|
-
it("traverse optional", () => {
|
|
93
|
-
const o = type({ "a?": "string" })
|
|
94
|
-
attest(o({ a: "a" })).snap({ a: "a" })
|
|
95
|
-
attest(o({})).snap({})
|
|
96
|
-
attest(o({ a: 1 }).toString()).snap("a must be a string (was number)")
|
|
97
|
-
})
|
|
98
|
-
|
|
99
|
-
// it("traverse strict optional", () => {
|
|
100
|
-
// // TODO: strict
|
|
101
|
-
// const o = type({ "a?": "string" })
|
|
102
|
-
// attest(o({ a: "a" })).snap({ a: "a" })
|
|
103
|
-
// attest(o({})).snap({})
|
|
104
|
-
// attest(o({ a: 1 }).toString()).snap("a must be a string (was number)")
|
|
105
|
-
// })
|
|
106
|
-
|
|
107
|
-
// it("multiple bad strict", () => {
|
|
108
|
-
// const t = type({ a: "string", b: "boolean" }).configure({
|
|
109
|
-
// keys: "strict"
|
|
110
|
-
// })
|
|
111
|
-
// attest(t({ a: 1, b: 2 }).toString()).snap(
|
|
112
|
-
// "a must be a string (was number)\nb must be boolean (was number)"
|
|
113
|
-
// )
|
|
114
|
-
// })
|
|
115
|
-
|
|
116
|
-
// it("optional symbol", () => {
|
|
117
|
-
// const s = Symbol()
|
|
118
|
-
// const name = reference(s)
|
|
119
|
-
// const t = type({
|
|
120
|
-
// [optional(s)]: "number"
|
|
121
|
-
// })
|
|
122
|
-
// attest<{ [s]?: number }>(t.infer)
|
|
123
|
-
// attest(t.json).equals({
|
|
124
|
-
// domain: "object",
|
|
125
|
-
// optional: [{ key: name, value: "number" }]
|
|
126
|
-
// })
|
|
127
|
-
// })
|
|
128
|
-
},
|
|
129
|
-
"spread syntax",
|
|
130
|
-
() => {
|
|
131
|
-
it("within scope", () => {
|
|
132
|
-
const s = scope({
|
|
133
|
-
user: { isAdmin: "false", name: "string" },
|
|
134
|
-
admin: { "...": "user", isAdmin: "true" }
|
|
135
|
-
}).export()
|
|
136
|
-
|
|
137
|
-
attest<{ isAdmin: true; name: string }>(s.admin.infer)
|
|
138
|
-
attest(s.admin.json).equals({
|
|
139
|
-
domain: "object",
|
|
140
|
-
prop: [
|
|
141
|
-
{ key: "isAdmin", value: { unit: true } },
|
|
142
|
-
{ key: "name", value: "string" }
|
|
143
|
-
]
|
|
144
|
-
})
|
|
145
|
-
})
|
|
146
|
-
|
|
147
|
-
it("from another `type` call", () => {
|
|
148
|
-
const user = type({ isAdmin: "false", name: "string" })
|
|
149
|
-
const admin = type({ "...": user, isAdmin: "true" })
|
|
150
|
-
|
|
151
|
-
attest<{ isAdmin: true; name: string }>(admin.infer)
|
|
152
|
-
attest(admin.json).snap({
|
|
153
|
-
domain: "object",
|
|
154
|
-
prop: [
|
|
155
|
-
{ key: "isAdmin", value: { unit: true } },
|
|
156
|
-
{ key: "name", value: "string" }
|
|
157
|
-
]
|
|
158
|
-
})
|
|
159
|
-
})
|
|
160
|
-
|
|
161
|
-
it("from an object literal", () => {
|
|
162
|
-
// no idea why you'd want to do this
|
|
163
|
-
const t = type({
|
|
164
|
-
"...": {
|
|
165
|
-
inherited: "boolean",
|
|
166
|
-
overridden: "string"
|
|
167
|
-
},
|
|
168
|
-
overridden: "number"
|
|
169
|
-
})
|
|
170
|
-
|
|
171
|
-
attest<{
|
|
172
|
-
inherited: boolean
|
|
173
|
-
overridden: number
|
|
174
|
-
}>(t.infer)
|
|
175
|
-
|
|
176
|
-
attest(t.json).snap({
|
|
177
|
-
domain: "object",
|
|
178
|
-
prop: [
|
|
179
|
-
{
|
|
180
|
-
key: "inherited",
|
|
181
|
-
value: [{ unit: false }, { unit: true }]
|
|
182
|
-
},
|
|
183
|
-
{ key: "overridden", value: "number" }
|
|
184
|
-
]
|
|
185
|
-
})
|
|
186
|
-
})
|
|
187
|
-
|
|
188
|
-
it("escaped key", () => {
|
|
189
|
-
const t = type({
|
|
190
|
-
"\\...": "string"
|
|
191
|
-
})
|
|
192
|
-
|
|
193
|
-
attest<{ "...": string }>(t.infer)
|
|
194
|
-
|
|
195
|
-
attest(t.json).snap({
|
|
196
|
-
domain: "object",
|
|
197
|
-
prop: [{ key: "...", value: "string" }]
|
|
198
|
-
})
|
|
199
|
-
})
|
|
200
|
-
|
|
201
|
-
it("with non-object", () => {
|
|
202
|
-
// @ts-expect-error
|
|
203
|
-
attest(() => type({ "...": "string" })).throwsAndHasTypeError(
|
|
204
|
-
writeInvalidSpreadTypeMessage(printable("string"))
|
|
205
|
-
)
|
|
206
|
-
})
|
|
207
|
-
|
|
208
|
-
// this is a regression test to ensure nodes are handled even if they aren't just an object
|
|
209
|
-
it("with complex type", () => {
|
|
210
|
-
const adminUser = type({
|
|
211
|
-
"...": [{ name: "string" }, "&", { isAdmin: "false" }],
|
|
212
|
-
isAdmin: "true"
|
|
213
|
-
})
|
|
214
|
-
|
|
215
|
-
attest<{ isAdmin: true; name: string }>(adminUser.infer)
|
|
216
|
-
attest(adminUser.json).snap({
|
|
217
|
-
domain: "object",
|
|
218
|
-
prop: [
|
|
219
|
-
{ key: "isAdmin", value: { unit: true } },
|
|
220
|
-
{ key: "name", value: "string" }
|
|
221
|
-
]
|
|
222
|
-
})
|
|
223
|
-
})
|
|
224
|
-
},
|
|
225
|
-
"index",
|
|
226
|
-
() => {
|
|
227
|
-
it("string index", () => {
|
|
228
|
-
const o = type({ "[string]": "string" })
|
|
229
|
-
attest<{ [x: string]: string }>(o.infer)
|
|
230
|
-
attest(o.json).snap({
|
|
231
|
-
domain: "object",
|
|
232
|
-
index: [{ key: "string", value: "string" }]
|
|
233
|
-
})
|
|
234
|
-
|
|
235
|
-
attest(o({})).equals({})
|
|
236
|
-
attest(o({ a: "a", b: "b" })).equals({ a: "a", b: "b" })
|
|
237
|
-
|
|
238
|
-
const validWithSymbol = { a: "a", [Symbol()]: null }
|
|
239
|
-
attest(validWithSymbol).equals(validWithSymbol)
|
|
240
|
-
|
|
241
|
-
attest(o({ a: 1 }).toString()).snap("a must be a string (was number)")
|
|
242
|
-
attest(o({ a: true, b: false }).toString())
|
|
243
|
-
.snap(`a must be a string (was true)
|
|
244
|
-
b must be a string (was false)`)
|
|
245
|
-
})
|
|
246
|
-
|
|
247
|
-
it("symbol index", () => {
|
|
248
|
-
const o = type({ "[symbol]": "1" })
|
|
249
|
-
attest<{ [x: symbol]: 1 }>(o.infer)
|
|
250
|
-
attest(o.json).snap({
|
|
251
|
-
domain: "object",
|
|
252
|
-
index: [{ key: "symbol", value: { unit: 1 } }]
|
|
253
|
-
})
|
|
254
|
-
|
|
255
|
-
attest(o({})).equals({})
|
|
256
|
-
|
|
257
|
-
attest(o({ a: 999 })).unknown.snap({ a: 999 })
|
|
258
|
-
|
|
259
|
-
const zildjian = Symbol()
|
|
260
|
-
const zildjianName = printable(zildjian)
|
|
261
|
-
|
|
262
|
-
// I've been dope, suspenseful with a pencil
|
|
263
|
-
// Ever since...
|
|
264
|
-
const prince = Symbol()
|
|
265
|
-
const princeName = printable(prince)
|
|
266
|
-
|
|
267
|
-
attest(o({ [zildjian]: 1, [prince]: 1 })).equals({
|
|
268
|
-
[zildjian]: 1,
|
|
269
|
-
[prince]: 1
|
|
270
|
-
})
|
|
271
|
-
|
|
272
|
-
attest({ a: 0, [zildjian]: 1 }).equals({ a: 0, [zildjian]: 1 })
|
|
273
|
-
|
|
274
|
-
attest(o({ [zildjian]: 0 }).toString()).equals(
|
|
275
|
-
`value at [${zildjianName}] must be 1 (was 0)`
|
|
276
|
-
)
|
|
277
|
-
attest(o({ [prince]: null, [zildjian]: undefined }).toString())
|
|
278
|
-
.snap(`value at [${princeName}] must be 1 (was null)
|
|
279
|
-
value at [${zildjianName}] must be 1 (was undefined)`)
|
|
280
|
-
})
|
|
281
|
-
|
|
282
|
-
it("enumerable indexed union", () => {
|
|
283
|
-
const o = type({ "['foo' | 'bar']": "string" })
|
|
284
|
-
const expected = type({ foo: "string", bar: "string" })
|
|
285
|
-
attest<typeof expected>(o)
|
|
286
|
-
attest(o.json).equals(expected.json)
|
|
287
|
-
})
|
|
288
|
-
|
|
289
|
-
it("non-enumerable indexed union", () => {
|
|
290
|
-
const o = type({ "[string | symbol]": "string" })
|
|
291
|
-
attest<{ [x: string]: string; [x: symbol]: string }>(o.infer)
|
|
292
|
-
attest(o.json).snap({
|
|
293
|
-
domain: "object",
|
|
294
|
-
index: [{ key: ["string", "symbol"], value: "string" }]
|
|
295
|
-
})
|
|
296
|
-
})
|
|
297
|
-
|
|
298
|
-
it("multiple indexed", () => {
|
|
299
|
-
const o = type({
|
|
300
|
-
"[string]": "string",
|
|
301
|
-
"[symbol]": "number"
|
|
302
|
-
})
|
|
303
|
-
attest<{ [x: string]: string; [x: symbol]: number }>(o.infer)
|
|
304
|
-
attest(o.json).snap({
|
|
305
|
-
domain: "object",
|
|
306
|
-
index: [
|
|
307
|
-
{ key: "string", value: "string" },
|
|
308
|
-
{ key: "symbol", value: "number" }
|
|
309
|
-
]
|
|
310
|
-
})
|
|
311
|
-
|
|
312
|
-
attest(o({})).equals({})
|
|
313
|
-
attest(o({ foo: "f" })).equals({ foo: "f" })
|
|
314
|
-
|
|
315
|
-
const sym = Symbol()
|
|
316
|
-
|
|
317
|
-
const symName = printable(sym)
|
|
318
|
-
|
|
319
|
-
const validWithStringsAndSymbols = {
|
|
320
|
-
str: "string",
|
|
321
|
-
[sym]: 8675309
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
attest(o(validWithStringsAndSymbols)).equals(validWithStringsAndSymbols)
|
|
325
|
-
|
|
326
|
-
attest(
|
|
327
|
-
o({
|
|
328
|
-
str: 100,
|
|
329
|
-
[sym]: "💯"
|
|
330
|
-
}).toString()
|
|
331
|
-
).snap(`str must be a string (was number)
|
|
332
|
-
value at [${symName}] must be a number (was string)`)
|
|
333
|
-
})
|
|
334
|
-
|
|
335
|
-
it("all key kinds", () => {
|
|
336
|
-
const o = type({
|
|
337
|
-
"[string]": "string",
|
|
338
|
-
required: "'foo'",
|
|
339
|
-
"optional?": "'bar'"
|
|
340
|
-
})
|
|
341
|
-
attest<{ [x: string]: string; required: "foo"; optional?: "bar" }>(
|
|
342
|
-
o.infer
|
|
343
|
-
)
|
|
344
|
-
attest(o.json).snap({
|
|
345
|
-
domain: "object",
|
|
346
|
-
prop: [
|
|
347
|
-
{ key: "optional", optional: true, value: { unit: "bar" } },
|
|
348
|
-
{ key: "required", value: { unit: "foo" } }
|
|
349
|
-
],
|
|
350
|
-
index: [{ key: "string", value: "string" }]
|
|
351
|
-
})
|
|
352
|
-
|
|
353
|
-
const valid: typeof o.infer = { required: "foo", other: "bar" }
|
|
354
|
-
attest(o(valid)).equals(valid)
|
|
355
|
-
attest(
|
|
356
|
-
o({
|
|
357
|
-
optional: "wrongString",
|
|
358
|
-
other: 0n
|
|
359
|
-
}).toString()
|
|
360
|
-
).snap(`optional must be "bar" (was "wrongString")
|
|
361
|
-
required must be "foo" (was missing)
|
|
362
|
-
other must be a string (was bigint)`)
|
|
363
|
-
})
|
|
364
|
-
|
|
365
|
-
it("index key from scope", () => {
|
|
366
|
-
const types = scope({
|
|
367
|
-
key: "symbol|'foo'|'bar'|'baz'",
|
|
368
|
-
obj: {
|
|
369
|
-
"[key]": "string"
|
|
370
|
-
}
|
|
371
|
-
}).export()
|
|
372
|
-
type Key = symbol | "foo" | "bar" | "baz"
|
|
373
|
-
attest<Key>(types.key.infer)
|
|
374
|
-
attest<Record<Key, string>>(types.obj.infer)
|
|
375
|
-
|
|
376
|
-
const expected = type({ "[symbol]": "string" }).and({
|
|
377
|
-
foo: "string",
|
|
378
|
-
bar: "string",
|
|
379
|
-
baz: "string"
|
|
380
|
-
})
|
|
381
|
-
|
|
382
|
-
attest(types.obj.json).snap(expected.json)
|
|
383
|
-
})
|
|
384
|
-
|
|
385
|
-
it("syntax error in index definition", () => {
|
|
386
|
-
attest(() =>
|
|
387
|
-
type({
|
|
388
|
-
// @ts-expect-error
|
|
389
|
-
"[unresolvable]": "string"
|
|
390
|
-
})
|
|
391
|
-
).throwsAndHasTypeError(writeUnresolvableMessage("unresolvable"))
|
|
392
|
-
})
|
|
393
|
-
|
|
394
|
-
it("does not allow syntax error message as value", () => {
|
|
395
|
-
attest(() =>
|
|
396
|
-
type({
|
|
397
|
-
// @ts-expect-error
|
|
398
|
-
"[unresolvable]": "'unresolvable' is unresolvable"
|
|
399
|
-
})
|
|
400
|
-
).throwsAndHasTypeError(writeUnresolvableMessage("unresolvable"))
|
|
401
|
-
})
|
|
402
|
-
|
|
403
|
-
it("semantic error in index definition", () => {
|
|
404
|
-
attest(() =>
|
|
405
|
-
type({
|
|
406
|
-
// @ts-expect-error
|
|
407
|
-
"[symbol<5]": "string"
|
|
408
|
-
})
|
|
409
|
-
).throwsAndHasTypeError(writeUnboundableMessage("symbol"))
|
|
410
|
-
})
|
|
411
|
-
|
|
412
|
-
it("invalid key type for index definition", () => {
|
|
413
|
-
attest(() =>
|
|
414
|
-
type({
|
|
415
|
-
// @ts-expect-error
|
|
416
|
-
"[object]": "string"
|
|
417
|
-
})
|
|
418
|
-
).throwsAndHasTypeError(writeInvalidPropertyKeyMessage("object"))
|
|
419
|
-
})
|
|
420
|
-
|
|
421
|
-
it("does not allow invalid key type error as value", () => {
|
|
422
|
-
attest(() =>
|
|
423
|
-
type({
|
|
424
|
-
// @ts-expect-error
|
|
425
|
-
"[object]":
|
|
426
|
-
"Indexed key definition 'object' must be a string, number or symbol"
|
|
427
|
-
})
|
|
428
|
-
)
|
|
429
|
-
.throws(writeUnresolvableMessage("Indexed"))
|
|
430
|
-
.type.errors(writeInvalidPropertyKeyMessage("object"))
|
|
431
|
-
})
|
|
432
|
-
|
|
433
|
-
it("escaped index", () => {
|
|
434
|
-
const o = type({ "\\[string]": "string" })
|
|
435
|
-
attest<{ "[string]": string }>(o.infer)
|
|
436
|
-
attest(o.json).snap({
|
|
437
|
-
domain: "object",
|
|
438
|
-
prop: [{ key: "[string]", value: "string" }]
|
|
439
|
-
})
|
|
440
|
-
})
|
|
441
|
-
}
|
|
442
|
-
)
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import { bench } from "@arktype/attest"
|
|
2
|
-
import { scope, type } from "arktype"
|
|
3
|
-
|
|
4
|
-
bench("single-quoted", () => {
|
|
5
|
-
const _ = type("'nineteen characters'")
|
|
6
|
-
})
|
|
7
|
-
.median([3.05, "us"])
|
|
8
|
-
.types([502, "instantiations"])
|
|
9
|
-
|
|
10
|
-
bench("double-quoted", () => {
|
|
11
|
-
const _ = type('"nineteen characters"')
|
|
12
|
-
})
|
|
13
|
-
.median([3.13, "us"])
|
|
14
|
-
.types([502, "instantiations"])
|
|
15
|
-
|
|
16
|
-
bench("regex literal", () => {
|
|
17
|
-
const _ = type("/nineteen characters/")
|
|
18
|
-
})
|
|
19
|
-
.median([4.18, "us"])
|
|
20
|
-
.types([502, "instantiations"])
|
|
21
|
-
|
|
22
|
-
bench("keyword", () => {
|
|
23
|
-
const _ = type("string")
|
|
24
|
-
})
|
|
25
|
-
.median([1.44, "us"])
|
|
26
|
-
.types([84, "instantiations"])
|
|
27
|
-
|
|
28
|
-
const $ = scope({ strung: "string" })
|
|
29
|
-
bench("alias", () => {
|
|
30
|
-
const _ = $.type("strung")
|
|
31
|
-
})
|
|
32
|
-
.median([1.54, "us"])
|
|
33
|
-
.types([725, "instantiations"])
|
|
34
|
-
|
|
35
|
-
bench("number", () => {
|
|
36
|
-
const _ = type("-98765.4321")
|
|
37
|
-
})
|
|
38
|
-
.median([4.41, "us"])
|
|
39
|
-
.types([415, "instantiations"])
|
|
40
|
-
|
|
41
|
-
bench("bigint", () => {
|
|
42
|
-
const _ = type("-987654321n")
|
|
43
|
-
})
|
|
44
|
-
.median()
|
|
45
|
-
.types()
|
|
@@ -1,130 +0,0 @@
|
|
|
1
|
-
import { bench } from "@arktype/attest"
|
|
2
|
-
import { type } from "arktype"
|
|
3
|
-
|
|
4
|
-
bench("array-string", () => {
|
|
5
|
-
const _ = type("number[]")
|
|
6
|
-
})
|
|
7
|
-
.median([3.18, "us"])
|
|
8
|
-
.types([99, "instantiations"])
|
|
9
|
-
|
|
10
|
-
bench("array-tuple", () => {
|
|
11
|
-
const _ = type(["number", "[]"])
|
|
12
|
-
})
|
|
13
|
-
.median([3.47, "us"])
|
|
14
|
-
.types([212, "instantiations"])
|
|
15
|
-
|
|
16
|
-
bench("array-chain", () => {
|
|
17
|
-
const _ = type("number").array()
|
|
18
|
-
})
|
|
19
|
-
.median([5.15, "us"])
|
|
20
|
-
.types([5, "instantiations"])
|
|
21
|
-
|
|
22
|
-
bench("union-string", () => {
|
|
23
|
-
const _ = type("number|string")
|
|
24
|
-
})
|
|
25
|
-
.median([7.07, "us"])
|
|
26
|
-
.types([777, "instantiations"])
|
|
27
|
-
|
|
28
|
-
bench("union-tuple", () => {
|
|
29
|
-
const _ = type(["number", "|", "string"])
|
|
30
|
-
})
|
|
31
|
-
.median([5.59, "us"])
|
|
32
|
-
.types([273, "instantiations"])
|
|
33
|
-
|
|
34
|
-
bench("union-chain", () => {
|
|
35
|
-
const _ = type("number").or("string")
|
|
36
|
-
})
|
|
37
|
-
.median([7.09, "us"])
|
|
38
|
-
.types([488, "instantiations"])
|
|
39
|
-
|
|
40
|
-
bench("union-10-ary", () => {
|
|
41
|
-
const _ = type("0|1|2|3|4|5|6|7|8|9")
|
|
42
|
-
})
|
|
43
|
-
.median([119.87, "us"])
|
|
44
|
-
.types([4841, "instantiations"])
|
|
45
|
-
|
|
46
|
-
bench("intersection-string", () => {
|
|
47
|
-
const _ = type("number&0")
|
|
48
|
-
})
|
|
49
|
-
.median([3.61, "us"])
|
|
50
|
-
.types([686, "instantiations"])
|
|
51
|
-
|
|
52
|
-
bench("intersection-tuple", () => {
|
|
53
|
-
const _ = type(["number", "&", "0"])
|
|
54
|
-
})
|
|
55
|
-
.median([3.16, "us"])
|
|
56
|
-
.types([474, "instantiations"])
|
|
57
|
-
|
|
58
|
-
bench("intersection-chain", () => {
|
|
59
|
-
const _ = type("number").and("0")
|
|
60
|
-
})
|
|
61
|
-
.median([4.78, "us"])
|
|
62
|
-
.types([687, "instantiations"])
|
|
63
|
-
|
|
64
|
-
bench("intersection-10-ary", () => {
|
|
65
|
-
const _ = type(
|
|
66
|
-
"unknown&unknown&unknown&unknown&unknown&unknown&unknown&unknown&unknown&unknown"
|
|
67
|
-
)
|
|
68
|
-
})
|
|
69
|
-
.median([12.61, "us"])
|
|
70
|
-
.types([4351, "instantiations"])
|
|
71
|
-
|
|
72
|
-
bench("group-shallow", () => {
|
|
73
|
-
const _ = type("string|(number[])")
|
|
74
|
-
})
|
|
75
|
-
.median([9.58, "us"])
|
|
76
|
-
.types([1080, "instantiations"])
|
|
77
|
-
|
|
78
|
-
bench("group-nested", () => {
|
|
79
|
-
const _ = type("string|(number|(boolean))[][]")
|
|
80
|
-
})
|
|
81
|
-
.median([17.22, "us"])
|
|
82
|
-
.types([3008, "instantiations"])
|
|
83
|
-
|
|
84
|
-
bench("group-deep", () => {
|
|
85
|
-
const _ = type("(0|(1|(2|(3|(4|5)[])[])[])[])[]")
|
|
86
|
-
})
|
|
87
|
-
.median([32.5, "us"])
|
|
88
|
-
.types([37638, "instantiations"])
|
|
89
|
-
|
|
90
|
-
bench("bound-single", () => {
|
|
91
|
-
const _ = type("string>5")
|
|
92
|
-
})
|
|
93
|
-
.median([3.81, "us"])
|
|
94
|
-
.types([615, "instantiations"])
|
|
95
|
-
|
|
96
|
-
bench("bound-double", () => {
|
|
97
|
-
const _ = type("-7<=integer<99")
|
|
98
|
-
})
|
|
99
|
-
.median([7.2, "us"])
|
|
100
|
-
.types([1041, "instantiations"])
|
|
101
|
-
|
|
102
|
-
bench("divisor", () => {
|
|
103
|
-
const _ = type("number%5")
|
|
104
|
-
})
|
|
105
|
-
.median([4.53, "us"])
|
|
106
|
-
.types([1023, "instantiations"])
|
|
107
|
-
|
|
108
|
-
bench("filter-tuple", () => {
|
|
109
|
-
const _ = type(["boolean", ":", b => b])
|
|
110
|
-
})
|
|
111
|
-
.median([8.04, "us"])
|
|
112
|
-
.types([583, "instantiations"])
|
|
113
|
-
|
|
114
|
-
bench("filter-chain", () => {
|
|
115
|
-
const _ = type("boolean").narrow(b => b)
|
|
116
|
-
})
|
|
117
|
-
.median([12.19, "us"])
|
|
118
|
-
.types([42, "instantiations"])
|
|
119
|
-
|
|
120
|
-
bench("morph-tuple", () => {
|
|
121
|
-
const _ = type(["boolean", "=>", b => b])
|
|
122
|
-
})
|
|
123
|
-
.median([8.96, "us"])
|
|
124
|
-
.types([562, "instantiations"])
|
|
125
|
-
|
|
126
|
-
bench("morph-chain", () => {
|
|
127
|
-
const _ = type("boolean").pipe(b => b)
|
|
128
|
-
})
|
|
129
|
-
.median([2.98, "us"])
|
|
130
|
-
.types([26, "instantiations"])
|