arktype 1.1.0-dev-2 → 2.0.0-dev.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/__tests__/array.test.ts +272 -0
- package/__tests__/astUtils.test.ts +17 -0
- package/__tests__/badDefinitionType.test.ts +51 -0
- package/__tests__/basis.test.ts +46 -0
- package/__tests__/bounds.test.ts +388 -0
- package/__tests__/cast.test.ts +32 -0
- package/__tests__/completions.test.ts +46 -0
- package/__tests__/config.test.ts +168 -0
- package/__tests__/cyclic.test.ts +144 -0
- package/__tests__/dateLiteral.test.ts +32 -0
- package/__tests__/declared.test.ts +123 -0
- package/__tests__/discrimination.test.ts +74 -0
- package/__tests__/divisor.test.ts +90 -0
- package/__tests__/enclosed.test.ts +93 -0
- package/__tests__/examples.test.ts +56 -0
- package/__tests__/expressions.test.ts +138 -0
- package/__tests__/generateBenchData.ts +89 -0
- package/__tests__/generated/cyclic.ts +3058 -0
- package/__tests__/generic.test.ts +294 -0
- package/__tests__/group.test.ts +72 -0
- package/__tests__/hkt.test.ts +22 -0
- package/__tests__/imports.test.ts +113 -0
- package/__tests__/instanceof.test.ts +102 -0
- package/__tests__/intersection.test.ts +104 -0
- package/__tests__/jsdoc.test.ts +64 -0
- package/__tests__/keyTraversal.test.ts +73 -0
- package/__tests__/keyof.test.ts +134 -0
- package/__tests__/keywords.test.ts +188 -0
- package/__tests__/literal.test.ts +51 -0
- package/__tests__/match.test.ts +34 -0
- package/__tests__/methods.bench.ts +22 -0
- package/__tests__/morph.test.ts +409 -0
- package/__tests__/narrow.test.ts +102 -0
- package/__tests__/nodeDefinitions.test.ts +190 -0
- package/__tests__/object.bench.ts +28 -0
- package/__tests__/objectLiteral.test.ts +300 -0
- package/__tests__/operand.bench.ts +45 -0
- package/__tests__/operator.bench.ts +130 -0
- package/__tests__/regex.test.ts +58 -0
- package/__tests__/scope.bench.ts +39 -0
- package/__tests__/scope.test.ts +140 -0
- package/__tests__/string.test.ts +48 -0
- package/__tests__/submodule.test.ts +99 -0
- package/__tests__/this.test.ts +57 -0
- package/__tests__/thunk.test.ts +80 -0
- package/__tests__/traverse.test.ts +119 -0
- package/__tests__/tsGenerics.test.ts +12 -0
- package/__tests__/typeMethods.test.ts +69 -0
- package/__tests__/typeReference.test.ts +39 -0
- package/__tests__/unenclosed.test.ts +120 -0
- package/__tests__/union.test.ts +147 -0
- package/ark.ts +91 -0
- package/main.ts +7 -9
- package/match.ts +47 -68
- package/out/ark.d.ts +52 -0
- package/out/ark.d.ts.map +1 -0
- package/out/ark.js +19 -0
- package/out/ark.js.map +1 -0
- package/out/arkConfig.d.ts +2 -0
- package/out/arkConfig.d.ts.map +1 -0
- package/out/arkConfig.js +10 -0
- package/out/arkConfig.js.map +1 -0
- package/out/main.d.ts +7 -0
- package/out/main.d.ts.map +1 -0
- package/out/main.js +4 -0
- package/out/main.js.map +1 -0
- package/out/match.d.ts +48 -0
- package/out/match.d.ts.map +1 -0
- package/{dist → out}/match.js +0 -5
- package/out/match.js.map +1 -0
- package/{dist → out}/parser/definition.d.ts +3 -3
- package/out/parser/definition.d.ts.map +1 -0
- package/{dist → out}/parser/definition.js +8 -5
- package/out/parser/definition.js.map +1 -0
- package/out/parser/generic.d.ts +18 -0
- package/out/parser/generic.d.ts.map +1 -0
- package/out/parser/generic.js +19 -0
- package/out/parser/generic.js.map +1 -0
- package/{dist → out}/parser/objectLiteral.d.ts +14 -5
- package/out/parser/objectLiteral.d.ts.map +1 -0
- package/out/parser/objectLiteral.js +66 -0
- package/out/parser/objectLiteral.js.map +1 -0
- package/out/parser/semantic/bounds.d.ts +13 -0
- package/out/parser/semantic/bounds.d.ts.map +1 -0
- package/{dist → out}/parser/semantic/bounds.js.map +1 -1
- package/{dist → out}/parser/semantic/divisor.d.ts +2 -2
- package/{dist → out}/parser/semantic/divisor.d.ts.map +1 -1
- package/{dist → out}/parser/semantic/intersections.d.ts +1 -2
- package/out/parser/semantic/intersections.d.ts.map +1 -0
- package/out/parser/semantic/semantic.d.ts +33 -0
- package/out/parser/semantic/semantic.d.ts.map +1 -0
- package/{dist → out}/parser/semantic/utils.d.ts +1 -1
- package/{dist → out}/parser/semantic/utils.d.ts.map +1 -1
- package/{dist → out}/parser/semantic/validate.d.ts +3 -3
- package/out/parser/semantic/validate.d.ts.map +1 -0
- package/{dist → out}/parser/semantic/validate.js.map +1 -1
- package/{dist → out}/parser/shared.d.ts +7 -1
- package/out/parser/shared.d.ts.map +1 -0
- package/{dist → out}/parser/shared.js +7 -1
- package/out/parser/shared.js.map +1 -0
- package/{dist → out}/parser/string/reduce/dynamic.d.ts +11 -9
- package/out/parser/string/reduce/dynamic.d.ts.map +1 -0
- package/{dist → out}/parser/string/reduce/dynamic.js +13 -2
- package/out/parser/string/reduce/dynamic.js.map +1 -0
- package/{dist → out}/parser/string/reduce/shared.d.ts +29 -2
- package/{dist → out}/parser/string/reduce/shared.d.ts.map +1 -1
- package/{dist → out}/parser/string/reduce/shared.js +20 -1
- package/out/parser/string/reduce/shared.js.map +1 -0
- package/{dist → out}/parser/string/reduce/static.d.ts +1 -2
- package/out/parser/string/reduce/static.d.ts.map +1 -0
- package/{dist → out}/parser/string/shift/operand/date.d.ts +3 -3
- package/out/parser/string/shift/operand/date.d.ts.map +1 -0
- package/{dist → out}/parser/string/shift/operand/date.js.map +1 -1
- package/{dist → out}/parser/string/shift/operand/enclosed.d.ts +2 -2
- package/{dist → out}/parser/string/shift/operand/enclosed.d.ts.map +1 -1
- package/{dist → out}/parser/string/shift/operand/enclosed.js +4 -4
- package/out/parser/string/shift/operand/enclosed.js.map +1 -0
- package/out/parser/string/shift/operand/genericArgs.d.ts +29 -0
- package/out/parser/string/shift/operand/genericArgs.d.ts.map +1 -0
- package/out/parser/string/shift/operand/genericArgs.js +25 -0
- package/out/parser/string/shift/operand/genericArgs.js.map +1 -0
- package/{dist → out}/parser/string/shift/operand/operand.d.ts.map +1 -1
- package/{dist → out}/parser/string/shift/operand/operand.js.map +1 -1
- package/{dist → out}/parser/string/shift/operand/unenclosed.d.ts +7 -9
- package/out/parser/string/shift/operand/unenclosed.d.ts.map +1 -0
- package/{dist → out}/parser/string/shift/operand/unenclosed.js +10 -10
- package/out/parser/string/shift/operand/unenclosed.js.map +1 -0
- package/{dist → out}/parser/string/shift/operator/bounds.d.ts +11 -33
- package/out/parser/string/shift/operator/bounds.d.ts.map +1 -0
- package/out/parser/string/shift/operator/bounds.js +106 -0
- package/out/parser/string/shift/operator/bounds.js.map +1 -0
- package/{dist → out}/parser/string/shift/operator/divisor.js.map +1 -1
- package/out/parser/string/shift/operator/operator.d.ts.map +1 -0
- package/{dist → out}/parser/string/shift/operator/operator.js.map +1 -1
- package/{dist → out}/parser/string/shift/scanner.d.ts +1 -1
- package/{dist → out}/parser/string/shift/scanner.d.ts.map +1 -1
- package/out/parser/string/shift/scanner.js.map +1 -0
- package/{dist → out}/parser/string/string.d.ts +9 -11
- package/out/parser/string/string.d.ts.map +1 -0
- package/out/parser/string/string.js +23 -0
- package/out/parser/string/string.js.map +1 -0
- package/{dist → out}/parser/tuple.d.ts +9 -11
- package/out/parser/tuple.d.ts.map +1 -0
- package/{dist → out}/parser/tuple.js +17 -14
- package/out/parser/tuple.js.map +1 -0
- package/{dist → out}/scope.d.ts +16 -16
- package/out/scope.d.ts.map +1 -0
- package/{dist → out}/scope.js +27 -22
- package/out/scope.js.map +1 -0
- package/out/test.d.ts +2 -0
- package/out/test.d.ts.map +1 -0
- package/out/test.js +7 -0
- package/out/test.js.map +1 -0
- package/out/tsconfig.build.tsbuildinfo +1 -0
- package/{dist → out}/type.d.ts +23 -47
- package/out/type.d.ts.map +1 -0
- package/{dist → out}/type.js +27 -31
- package/out/type.js.map +1 -0
- package/out/util.d.ts +19 -0
- package/out/util.d.ts.map +1 -0
- package/out/util.js +7 -0
- package/out/util.js.map +1 -0
- package/package.json +13 -15
- package/parser/definition.ts +58 -55
- package/parser/generic.ts +17 -157
- package/parser/objectLiteral.ts +107 -30
- package/parser/semantic/bounds.ts +22 -24
- package/parser/semantic/divisor.ts +2 -2
- package/parser/semantic/intersections.ts +17 -18
- package/parser/semantic/semantic.ts +80 -43
- package/parser/semantic/utils.ts +7 -7
- package/parser/semantic/validate.ts +40 -36
- package/parser/shared.ts +28 -17
- package/parser/string/reduce/dynamic.ts +26 -11
- package/parser/string/reduce/shared.ts +35 -7
- package/parser/string/reduce/static.ts +20 -22
- package/parser/string/shift/operand/date.ts +1 -4
- package/parser/string/shift/operand/enclosed.ts +5 -6
- package/parser/string/shift/operand/genericArgs.ts +127 -0
- package/parser/string/shift/operand/operand.ts +21 -21
- package/parser/string/shift/operand/unenclosed.ts +54 -68
- package/parser/string/shift/operator/bounds.ts +99 -64
- package/parser/string/shift/operator/operator.ts +31 -31
- package/parser/string/shift/scanner.ts +7 -7
- package/parser/string/string.ts +29 -32
- package/parser/tuple.ts +87 -88
- package/scope.ts +81 -80
- package/type.ts +76 -132
- package/util.ts +36 -0
- package/dist/main.d.ts +0 -11
- package/dist/main.d.ts.map +0 -1
- package/dist/main.js +0 -8
- package/dist/main.js.map +0 -1
- package/dist/match.d.ts +0 -46
- package/dist/match.d.ts.map +0 -1
- package/dist/match.js.map +0 -1
- package/dist/parser/definition.d.ts.map +0 -1
- package/dist/parser/definition.js.map +0 -1
- package/dist/parser/generic.d.ts +0 -44
- package/dist/parser/generic.d.ts.map +0 -1
- package/dist/parser/generic.js +0 -46
- package/dist/parser/generic.js.map +0 -1
- package/dist/parser/objectLiteral.d.ts.map +0 -1
- package/dist/parser/objectLiteral.js +0 -38
- package/dist/parser/objectLiteral.js.map +0 -1
- package/dist/parser/semantic/bounds.d.ts +0 -13
- package/dist/parser/semantic/bounds.d.ts.map +0 -1
- package/dist/parser/semantic/intersections.d.ts.map +0 -1
- package/dist/parser/semantic/semantic.d.ts +0 -22
- package/dist/parser/semantic/semantic.d.ts.map +0 -1
- package/dist/parser/semantic/validate.d.ts.map +0 -1
- package/dist/parser/shared.d.ts.map +0 -1
- package/dist/parser/shared.js.map +0 -1
- package/dist/parser/string/reduce/dynamic.d.ts.map +0 -1
- package/dist/parser/string/reduce/dynamic.js.map +0 -1
- package/dist/parser/string/reduce/shared.js.map +0 -1
- package/dist/parser/string/reduce/static.d.ts.map +0 -1
- package/dist/parser/string/shift/operand/date.d.ts.map +0 -1
- package/dist/parser/string/shift/operand/enclosed.js.map +0 -1
- package/dist/parser/string/shift/operand/unenclosed.d.ts.map +0 -1
- package/dist/parser/string/shift/operand/unenclosed.js.map +0 -1
- package/dist/parser/string/shift/operator/bounds.d.ts.map +0 -1
- package/dist/parser/string/shift/operator/bounds.js +0 -95
- package/dist/parser/string/shift/operator/bounds.js.map +0 -1
- package/dist/parser/string/shift/operator/operator.d.ts.map +0 -1
- package/dist/parser/string/shift/scanner.js.map +0 -1
- package/dist/parser/string/string.d.ts.map +0 -1
- package/dist/parser/string/string.js +0 -31
- package/dist/parser/string/string.js.map +0 -1
- package/dist/parser/tuple.d.ts.map +0 -1
- package/dist/parser/tuple.js.map +0 -1
- package/dist/scope.d.ts.map +0 -1
- package/dist/scope.js.map +0 -1
- package/dist/scopes/ark.d.ts +0 -38
- package/dist/scopes/ark.d.ts.map +0 -1
- package/dist/scopes/ark.js +0 -24
- package/dist/scopes/ark.js.map +0 -1
- package/dist/scopes/jsObjects.d.ts +0 -19
- package/dist/scopes/jsObjects.d.ts.map +0 -1
- package/dist/scopes/jsObjects.js +0 -15
- package/dist/scopes/jsObjects.js.map +0 -1
- package/dist/scopes/parsing.d.ts +0 -18
- package/dist/scopes/parsing.d.ts.map +0 -1
- package/dist/scopes/parsing.js +0 -61
- package/dist/scopes/parsing.js.map +0 -1
- package/dist/scopes/tsGenerics.d.ts +0 -17
- package/dist/scopes/tsGenerics.d.ts.map +0 -1
- package/dist/scopes/tsGenerics.js +0 -13
- package/dist/scopes/tsGenerics.js.map +0 -1
- package/dist/scopes/tsKeywords.d.ts +0 -24
- package/dist/scopes/tsKeywords.d.ts.map +0 -1
- package/dist/scopes/tsKeywords.js +0 -20
- package/dist/scopes/tsKeywords.js.map +0 -1
- package/dist/scopes/utils/creditCard.d.ts +0 -3
- package/dist/scopes/utils/creditCard.d.ts.map +0 -1
- package/dist/scopes/utils/creditCard.js +0 -36
- package/dist/scopes/utils/creditCard.js.map +0 -1
- package/dist/scopes/utils/date.d.ts +0 -24
- package/dist/scopes/utils/date.d.ts.map +0 -1
- package/dist/scopes/utils/date.js +0 -53
- package/dist/scopes/utils/date.js.map +0 -1
- package/dist/scopes/validation.d.ts +0 -20
- package/dist/scopes/validation.d.ts.map +0 -1
- package/dist/scopes/validation.js +0 -57
- package/dist/scopes/validation.js.map +0 -1
- package/dist/type.d.ts.map +0 -1
- package/dist/type.js.map +0 -1
- package/scopes/ark.ts +0 -64
- package/scopes/jsObjects.ts +0 -29
- package/scopes/parsing.ts +0 -81
- package/scopes/tsGenerics.ts +0 -28
- package/scopes/tsKeywords.ts +0 -39
- package/scopes/utils/creditCard.ts +0 -37
- package/scopes/utils/date.ts +0 -108
- package/scopes/validation.ts +0 -81
- /package/{dist → out}/parser/semantic/bounds.js +0 -0
- /package/{dist → out}/parser/semantic/divisor.js +0 -0
- /package/{dist → out}/parser/semantic/divisor.js.map +0 -0
- /package/{dist → out}/parser/semantic/intersections.js +0 -0
- /package/{dist → out}/parser/semantic/intersections.js.map +0 -0
- /package/{dist → out}/parser/semantic/semantic.js +0 -0
- /package/{dist → out}/parser/semantic/semantic.js.map +0 -0
- /package/{dist → out}/parser/semantic/utils.js +0 -0
- /package/{dist → out}/parser/semantic/utils.js.map +0 -0
- /package/{dist → out}/parser/semantic/validate.js +0 -0
- /package/{dist → out}/parser/string/reduce/static.js +0 -0
- /package/{dist → out}/parser/string/reduce/static.js.map +0 -0
- /package/{dist → out}/parser/string/shift/operand/date.js +0 -0
- /package/{dist → out}/parser/string/shift/operand/operand.d.ts +0 -0
- /package/{dist → out}/parser/string/shift/operand/operand.js +0 -0
- /package/{dist → out}/parser/string/shift/operator/divisor.d.ts +0 -0
- /package/{dist → out}/parser/string/shift/operator/divisor.d.ts.map +0 -0
- /package/{dist → out}/parser/string/shift/operator/divisor.js +0 -0
- /package/{dist → out}/parser/string/shift/operator/operator.d.ts +0 -0
- /package/{dist → out}/parser/string/shift/operator/operator.js +0 -0
- /package/{dist → out}/parser/string/shift/scanner.js +0 -0
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { attest } from "@arktype/attest"
|
|
2
|
+
import { writeIndivisibleMessage } from "@arktype/schema"
|
|
3
|
+
import { type } from "arktype"
|
|
4
|
+
import {
|
|
5
|
+
writeMissingRightOperandMessage,
|
|
6
|
+
writeUnresolvableMessage
|
|
7
|
+
} from "../parser/string/shift/operand/unenclosed.js"
|
|
8
|
+
|
|
9
|
+
describe("intersection", () => {
|
|
10
|
+
it("two types", () => {
|
|
11
|
+
const t = type("boolean&true")
|
|
12
|
+
attest<true>(t.infer)
|
|
13
|
+
attest(t.json).is(type("true").json)
|
|
14
|
+
})
|
|
15
|
+
it("intersection parsed before union", () => {
|
|
16
|
+
// Should be parsed as:
|
|
17
|
+
// 1. "0" | ("1"&"string") | "2"
|
|
18
|
+
// 2. "0" | "1" | "2"
|
|
19
|
+
const t = type("'0'|'1'&string|'2'")
|
|
20
|
+
attest<"0" | "1" | "2">(t.infer)
|
|
21
|
+
attest(t.json).equals(type("===", "0", "1", "2").json)
|
|
22
|
+
})
|
|
23
|
+
it("tuple expression", () => {
|
|
24
|
+
const t = type([{ a: "string" }, "&", { b: "number" }])
|
|
25
|
+
attest<{ a: string; b: number }>(t.infer)
|
|
26
|
+
attest(t.json).equals(type({ a: "string", b: "number" }).json)
|
|
27
|
+
})
|
|
28
|
+
it("several types", () => {
|
|
29
|
+
const t = type("unknown&boolean&false")
|
|
30
|
+
attest<false>(t.infer)
|
|
31
|
+
attest(t.json).equals(type("false").json)
|
|
32
|
+
})
|
|
33
|
+
it("method", () => {
|
|
34
|
+
const t = type({ a: "string" }).and({ b: "boolean" })
|
|
35
|
+
attest<{ a: string; b: boolean }>(t.infer)
|
|
36
|
+
attest(t.json).equals(type({ a: "string", b: "boolean" }).json)
|
|
37
|
+
})
|
|
38
|
+
it("chained deep intersections", () => {
|
|
39
|
+
const b = type({ b: "boolean" }, "=>", (o) => [o.b])
|
|
40
|
+
const t = type({
|
|
41
|
+
a: ["string", "=>", (s) => s.length]
|
|
42
|
+
})
|
|
43
|
+
.and({
|
|
44
|
+
// unable to inline this due to:
|
|
45
|
+
// https://github.com/arktypeio/arktype/issues/806
|
|
46
|
+
b
|
|
47
|
+
})
|
|
48
|
+
.and({
|
|
49
|
+
b: { b: "true" },
|
|
50
|
+
c: "'hello'"
|
|
51
|
+
})
|
|
52
|
+
attest<{
|
|
53
|
+
a: string
|
|
54
|
+
b: {
|
|
55
|
+
b: true
|
|
56
|
+
}
|
|
57
|
+
c: "hello"
|
|
58
|
+
}>(t.in.infer)
|
|
59
|
+
|
|
60
|
+
attest<{ a: number; b: boolean[]; c: "hello" }>(t.infer)
|
|
61
|
+
})
|
|
62
|
+
describe("errors", () => {
|
|
63
|
+
it("bad reference", () => {
|
|
64
|
+
// @ts-expect-error
|
|
65
|
+
attest(() => type("boolean&tru"))
|
|
66
|
+
.throws(writeUnresolvableMessage("tru"))
|
|
67
|
+
.type.errors("boolean&true")
|
|
68
|
+
})
|
|
69
|
+
it("double and", () => {
|
|
70
|
+
// @ts-expect-error
|
|
71
|
+
attest(() => type("boolean&&true")).throws(
|
|
72
|
+
writeMissingRightOperandMessage("&", "&true")
|
|
73
|
+
)
|
|
74
|
+
})
|
|
75
|
+
it("implicit never", () => {
|
|
76
|
+
attest(() => type("string&number")).throws(
|
|
77
|
+
"Intersection of a string and a number results in an unsatisfiable type"
|
|
78
|
+
)
|
|
79
|
+
})
|
|
80
|
+
it("left semantic error", () => {
|
|
81
|
+
// @ts-expect-error
|
|
82
|
+
attest(() => type("string%2&'foo'")).throwsAndHasTypeError(
|
|
83
|
+
writeIndivisibleMessage("string")
|
|
84
|
+
)
|
|
85
|
+
})
|
|
86
|
+
it("right semantic error", () => {
|
|
87
|
+
// @ts-expect-error
|
|
88
|
+
attest(() => type("'foo'&string%2")).throwsAndHasTypeError(
|
|
89
|
+
writeIndivisibleMessage("string")
|
|
90
|
+
)
|
|
91
|
+
})
|
|
92
|
+
it("chained validation", () => {
|
|
93
|
+
attest(() =>
|
|
94
|
+
// @ts-expect-error
|
|
95
|
+
type({ a: "string" }).and({ b: "what" })
|
|
96
|
+
).throwsAndHasTypeError(writeUnresolvableMessage("what"))
|
|
97
|
+
})
|
|
98
|
+
it("at path", () => {
|
|
99
|
+
attest(() => type({ a: "string" }).and({ a: "number" })).throws(
|
|
100
|
+
"Intersection at a of a string and a number results in an unsatisfiable type"
|
|
101
|
+
)
|
|
102
|
+
})
|
|
103
|
+
})
|
|
104
|
+
})
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { scope, type } from "arktype"
|
|
2
|
+
|
|
3
|
+
const ab = { a: "1", b: 1 }
|
|
4
|
+
|
|
5
|
+
describe("JSDoc", () => {
|
|
6
|
+
// Note: `<#` are not checked, they are for understandability
|
|
7
|
+
it("objects keep JSDoc", () => {
|
|
8
|
+
type({
|
|
9
|
+
/** JSDoc */
|
|
10
|
+
a: "string"
|
|
11
|
+
}).assert(ab).a // <# JSDoc
|
|
12
|
+
})
|
|
13
|
+
it("intersections keep JSDoc", () => {
|
|
14
|
+
type([
|
|
15
|
+
{
|
|
16
|
+
/** JSDoc */
|
|
17
|
+
a: "string"
|
|
18
|
+
},
|
|
19
|
+
"&",
|
|
20
|
+
{
|
|
21
|
+
b: "number"
|
|
22
|
+
}
|
|
23
|
+
]).assert(ab).a // <# JSDoc
|
|
24
|
+
|
|
25
|
+
type([
|
|
26
|
+
{
|
|
27
|
+
/** First */
|
|
28
|
+
a: "string"
|
|
29
|
+
},
|
|
30
|
+
"&",
|
|
31
|
+
{
|
|
32
|
+
/** Second */
|
|
33
|
+
a: "string"
|
|
34
|
+
}
|
|
35
|
+
]).assert(ab).a // <# First
|
|
36
|
+
|
|
37
|
+
scope({
|
|
38
|
+
a: {
|
|
39
|
+
/** JSDoc */
|
|
40
|
+
a: "string"
|
|
41
|
+
},
|
|
42
|
+
b: {
|
|
43
|
+
b: "number"
|
|
44
|
+
},
|
|
45
|
+
ab: "a & b"
|
|
46
|
+
})
|
|
47
|
+
.export()
|
|
48
|
+
.ab.assert(ab).a // <# JSDoc
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
it("works with module scopes", () => {
|
|
52
|
+
scope({
|
|
53
|
+
/** JSDoc */
|
|
54
|
+
a: "string"
|
|
55
|
+
}).export().a // <# JSDoc
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
it("doesn't work with optional keys", () => {
|
|
59
|
+
type({
|
|
60
|
+
/** JSDoc */
|
|
61
|
+
"a?": "string"
|
|
62
|
+
}).assert(ab).a // <# ---
|
|
63
|
+
})
|
|
64
|
+
})
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { attest } from "@arktype/attest"
|
|
2
|
+
import { type } from "arktype"
|
|
3
|
+
|
|
4
|
+
describe("key traversal", () => {
|
|
5
|
+
const getExtraneousB = () => ({ a: "ok", b: "why?" })
|
|
6
|
+
it("loose by default", () => {
|
|
7
|
+
const t = type({
|
|
8
|
+
a: "string"
|
|
9
|
+
})
|
|
10
|
+
const dataWithExtraneousB = getExtraneousB()
|
|
11
|
+
attest(t(dataWithExtraneousB).out).equals(dataWithExtraneousB)
|
|
12
|
+
})
|
|
13
|
+
it("invalid union", () => {
|
|
14
|
+
const o = type([{ a: "string" }, "|", { b: "boolean" }]).configure({
|
|
15
|
+
keys: "strict"
|
|
16
|
+
})
|
|
17
|
+
attest(o({ a: 2 }).errors?.summary).snap(
|
|
18
|
+
'a must be a string or removed (was {"a":2})'
|
|
19
|
+
)
|
|
20
|
+
})
|
|
21
|
+
it("distilled type", () => {
|
|
22
|
+
const t = type({
|
|
23
|
+
a: "string"
|
|
24
|
+
}).configure({ keys: "distilled" })
|
|
25
|
+
attest(t({ a: "ok" }).out).equals({ a: "ok" })
|
|
26
|
+
attest(t(getExtraneousB()).out).snap({ a: "ok" })
|
|
27
|
+
})
|
|
28
|
+
it("distilled array", () => {
|
|
29
|
+
const o = type({ a: "email[]" }).configure({
|
|
30
|
+
keys: "distilled"
|
|
31
|
+
})
|
|
32
|
+
attest(o({ a: ["shawn@arktype.io"] }).out).snap({
|
|
33
|
+
a: ["shawn@arktype.io"]
|
|
34
|
+
})
|
|
35
|
+
attest(o({ a: ["notAnEmail"] }).errors?.summary).snap(
|
|
36
|
+
"a/0 must be a valid email (was 'notAnEmail')"
|
|
37
|
+
)
|
|
38
|
+
// can handle missing keys
|
|
39
|
+
attest(o({ b: ["shawn"] }).errors?.summary).snap("a must be defined")
|
|
40
|
+
})
|
|
41
|
+
it("distilled union", () => {
|
|
42
|
+
const o = type([{ a: "string" }, "|", { b: "boolean" }]).configure({
|
|
43
|
+
keys: "distilled"
|
|
44
|
+
})
|
|
45
|
+
// can distill to first branch
|
|
46
|
+
attest(o({ a: "to", z: "bra" }).out).snap({ a: "to" })
|
|
47
|
+
// can distill to second branch
|
|
48
|
+
attest(o({ b: true, c: false }).out).snap({ b: true })
|
|
49
|
+
// can handle missing keys
|
|
50
|
+
attest(o({ a: 2 }).errors?.summary).snap(
|
|
51
|
+
'a must be a string or b must be defined (was {"a":2})'
|
|
52
|
+
)
|
|
53
|
+
})
|
|
54
|
+
it("strict type", () => {
|
|
55
|
+
const t = type({
|
|
56
|
+
a: "string"
|
|
57
|
+
}).configure({ keys: "strict" })
|
|
58
|
+
attest(t({ a: "ok" }).out).equals({ a: "ok" })
|
|
59
|
+
attest(t(getExtraneousB()).errors?.summary).snap("b must be removed")
|
|
60
|
+
})
|
|
61
|
+
it("strict array", () => {
|
|
62
|
+
const o = type({ a: "string[]" }).configure({
|
|
63
|
+
keys: "strict"
|
|
64
|
+
})
|
|
65
|
+
attest(o({ a: ["shawn"] }).out).snap({ a: ["shawn"] })
|
|
66
|
+
attest(o({ a: [2] }).errors?.summary).snap(
|
|
67
|
+
"a/0 must be a string (was number)"
|
|
68
|
+
)
|
|
69
|
+
attest(o({ b: ["shawn"] }).errors?.summary).snap(
|
|
70
|
+
"b must be removed\na must be defined"
|
|
71
|
+
)
|
|
72
|
+
})
|
|
73
|
+
})
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { attest } from "@arktype/attest"
|
|
2
|
+
import { schema } from "@arktype/schema"
|
|
3
|
+
import { type } from "arktype"
|
|
4
|
+
import {
|
|
5
|
+
writeMissingRightOperandMessage,
|
|
6
|
+
writeUnresolvableMessage
|
|
7
|
+
} from "../parser/string/shift/operand/unenclosed.js"
|
|
8
|
+
|
|
9
|
+
describe("keyof", () => {
|
|
10
|
+
it("autocompletion", () => {
|
|
11
|
+
// @ts-expect-error
|
|
12
|
+
attest(() => type("k")).type.errors("keyof ")
|
|
13
|
+
})
|
|
14
|
+
it("root expression", () => {
|
|
15
|
+
const t = type("keyof", "Date")
|
|
16
|
+
attest<keyof Date>(t.infer)
|
|
17
|
+
attest(t.json).equals(schema(Date).keyof().json)
|
|
18
|
+
})
|
|
19
|
+
it("primitive", () => {
|
|
20
|
+
const t = type("keyof bigint")
|
|
21
|
+
attest<keyof bigint>(t.infer)
|
|
22
|
+
const expected = schema.units(
|
|
23
|
+
"toLocaleString",
|
|
24
|
+
"toString",
|
|
25
|
+
"valueOf",
|
|
26
|
+
Symbol.toStringTag
|
|
27
|
+
)
|
|
28
|
+
attest(t.json).equals(expected.json)
|
|
29
|
+
})
|
|
30
|
+
it("object literal", () => {
|
|
31
|
+
const t = type({ a: "123", b: "123" }).keyof()
|
|
32
|
+
attest<"a" | "b">(t.infer)
|
|
33
|
+
attest(t.json).equals(type("'a'|'b'").json)
|
|
34
|
+
})
|
|
35
|
+
it("overlapping union", () => {
|
|
36
|
+
const t = type({ a: "number", b: "boolean" })
|
|
37
|
+
.or({ b: "number", c: "string" })
|
|
38
|
+
.keyof()
|
|
39
|
+
attest<"b">(t.infer)
|
|
40
|
+
attest(t.json).equals(type("'b'").json)
|
|
41
|
+
})
|
|
42
|
+
it("non-overlapping union", () => {
|
|
43
|
+
attest(() => type({ a: "number" }).or({ b: "number" }).keyof()).throws(
|
|
44
|
+
'Intersection of "a" and "b" results in an unsatisfiable type'
|
|
45
|
+
)
|
|
46
|
+
})
|
|
47
|
+
it("tuple expression", () => {
|
|
48
|
+
const t = type(["keyof", { a: "string" }])
|
|
49
|
+
attest<"a">(t.infer)
|
|
50
|
+
attest(t.json).equals(schema.units("a").json)
|
|
51
|
+
})
|
|
52
|
+
it("union including non-object", () => {
|
|
53
|
+
attest(() => type({ a: "number" }).or("boolean").keyof()).throws(
|
|
54
|
+
'Intersection of "toString" or "valueOf" and "a" results in an unsatisfiable type'
|
|
55
|
+
)
|
|
56
|
+
})
|
|
57
|
+
it("unsatisfiable", () => {
|
|
58
|
+
attest(() => type("keyof undefined")).throws(
|
|
59
|
+
"Intersection of unknown and never results in an unsatisfiable type"
|
|
60
|
+
)
|
|
61
|
+
})
|
|
62
|
+
it("multiple keyofs", () => {
|
|
63
|
+
const t = type("keyof keyof string")
|
|
64
|
+
attest<"toString" | "valueOf">(t.infer)
|
|
65
|
+
attest(t.json).equals(schema.units("toString", "valueOf").json)
|
|
66
|
+
})
|
|
67
|
+
it("groupable", () => {
|
|
68
|
+
const t = type("(keyof symbol & string)[]")
|
|
69
|
+
attest<("toString" | "valueOf" | "description")[]>(t.infer)
|
|
70
|
+
attest(t.json).equals(
|
|
71
|
+
schema.units("toString", "valueOf", "description").array().json
|
|
72
|
+
)
|
|
73
|
+
})
|
|
74
|
+
it("intersection precedence", () => {
|
|
75
|
+
const t = type("keyof symbol & symbol")
|
|
76
|
+
attest<typeof Symbol.toStringTag | typeof Symbol.toPrimitive>(t.infer)
|
|
77
|
+
attest(t.json).is(schema.units(Symbol.toStringTag, Symbol.toPrimitive).json)
|
|
78
|
+
})
|
|
79
|
+
it("union precedence", () => {
|
|
80
|
+
const t = type("keyof boolean | number")
|
|
81
|
+
attest<"valueOf" | number>(t.infer)
|
|
82
|
+
// for some reason TS doesn't include toString as a keyof boolean?
|
|
83
|
+
// given it is included in keyof string, it seems like an anomaly, but we include it
|
|
84
|
+
attest(t.json).equals(type("number | 'valueOf' | 'toString'").json)
|
|
85
|
+
})
|
|
86
|
+
it("missing operand", () => {
|
|
87
|
+
// @ts-expect-error
|
|
88
|
+
attest(() => type("keyof "))
|
|
89
|
+
.throws(writeMissingRightOperandMessage("keyof", ""))
|
|
90
|
+
// it tries to autocomplete, so this is just a possible completion that would be included
|
|
91
|
+
.type.errors("keyof bigint")
|
|
92
|
+
})
|
|
93
|
+
it("invalid operand", () => {
|
|
94
|
+
// @ts-expect-error
|
|
95
|
+
attest(() => type("keyof nope")).throwsAndHasTypeError(
|
|
96
|
+
writeUnresolvableMessage("nope")
|
|
97
|
+
)
|
|
98
|
+
})
|
|
99
|
+
// TODO: numeric
|
|
100
|
+
it("array", () => {
|
|
101
|
+
const t = type("keyof string[]")
|
|
102
|
+
attest<keyof string[]>(t.infer)
|
|
103
|
+
// the array prototype has many items and they vary based on the JS
|
|
104
|
+
// flavor we're running in, so just check that the indices from the type
|
|
105
|
+
// and one prototype key are present as a heuristic
|
|
106
|
+
t.assert("0")
|
|
107
|
+
t.assert("354")
|
|
108
|
+
t.assert("map")
|
|
109
|
+
t.assert(Symbol.iterator)
|
|
110
|
+
attest(() => t.assert("0.1")).throws.snap(
|
|
111
|
+
'TypeError: / must be a string matching /^(?:0|(?:[1-9]\\d*))$/, "length", "at", "concat", "copyWithin", "fill", "find", "findIndex", "lastIndexOf", "pop", "push", "reverse", "shift", "unshift", "slice", "sort", "splice", "includes", "indexOf", "join", "keys", "entries", "values", "forEach", "filter", "flat", "flatMap", "map", "every", "some", "reduce", "reduceRight", "toLocaleString", "toString", "findLast", "findLastIndex", (symbol Symbol.iterator) or (symbol Symbol.unscopables) (was "0.1")'
|
|
112
|
+
)
|
|
113
|
+
attest(() => t.assert("-1")).throws.snap(
|
|
114
|
+
'TypeError: / must be a string matching /^(?:0|(?:[1-9]\\d*))$/, "length", "at", "concat", "copyWithin", "fill", "find", "findIndex", "lastIndexOf", "pop", "push", "reverse", "shift", "unshift", "slice", "sort", "splice", "includes", "indexOf", "join", "keys", "entries", "values", "forEach", "filter", "flat", "flatMap", "map", "every", "some", "reduce", "reduceRight", "toLocaleString", "toString", "findLast", "findLastIndex", (symbol Symbol.iterator) or (symbol Symbol.unscopables) (was "-1")'
|
|
115
|
+
)
|
|
116
|
+
})
|
|
117
|
+
it("tuple", () => {
|
|
118
|
+
const t = type(["keyof", ["string", "number"]])
|
|
119
|
+
attest<keyof [string, number]>(t.infer)
|
|
120
|
+
t.assert("1")
|
|
121
|
+
t.assert("map")
|
|
122
|
+
attest(() => t.assert("2")).throws.snap(
|
|
123
|
+
'TypeError: / must be "length", "0", "1", "at", "concat", "copyWithin", "fill", "find", "findIndex", "lastIndexOf", "pop", "push", "reverse", "shift", "unshift", "slice", "sort", "splice", "includes", "indexOf", "join", "keys", "entries", "values", "forEach", "filter", "flat", "flatMap", "map", "every", "some", "reduce", "reduceRight", "toLocaleString", "toString", "findLast", "findLastIndex", (symbol Symbol.iterator) or (symbol Symbol.unscopables) (was "2")'
|
|
124
|
+
)
|
|
125
|
+
})
|
|
126
|
+
it("wellFormedNonNegativeInteger intersection", () => {
|
|
127
|
+
const a = type([{ "1": "'foo'" }, "&", "string[]"])
|
|
128
|
+
const t = type("keyof", a)
|
|
129
|
+
// TODO should still include wellFormed
|
|
130
|
+
attest(t.root.toString()).snap(
|
|
131
|
+
'"1" or "at" or "concat" or "copyWithin" or "entries" or "every" or "fill" or "filter" or "find" or "findIndex" or "findLast" or "findLastIndex" or "flat" or "flatMap" or "forEach" or "includes" or "indexOf" or "join" or "keys" or "lastIndexOf" or "length" or "map" or "pop" or "push" or "reduce" or "reduceRight" or "reverse" or "shift" or "slice" or "some" or "sort" or "splice" or "toLocaleString" or "toString" or "unshift" or "values" or (symbol Symbol.iterator) or (symbol Symbol.unscopables) or /^(?:0|(?:[1-9]\\d*))$/'
|
|
132
|
+
)
|
|
133
|
+
})
|
|
134
|
+
})
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import { attest } from "@arktype/attest"
|
|
2
|
+
import { schema } from "@arktype/schema"
|
|
3
|
+
import { type } from "arktype"
|
|
4
|
+
|
|
5
|
+
describe("keywords", () => {
|
|
6
|
+
describe("jsObjects", () => {
|
|
7
|
+
it("Function", () => {
|
|
8
|
+
// should not be treated as a morph
|
|
9
|
+
attest<Function>(type("Function").infer)
|
|
10
|
+
})
|
|
11
|
+
it("Date", () => {
|
|
12
|
+
// should not expand built-in classes
|
|
13
|
+
attest(type("Date").infer).type.toString.snap("Date")
|
|
14
|
+
})
|
|
15
|
+
})
|
|
16
|
+
describe("tsKeywords", () => {
|
|
17
|
+
it("string strings", () => {
|
|
18
|
+
/**
|
|
19
|
+
* In honor of @ark-expect-beta aka log(n):
|
|
20
|
+
* - Zirco author https://github.com/zirco-lang/zrc
|
|
21
|
+
* - Shameless Rust stan
|
|
22
|
+
* - Occasional user of ArkType libraries
|
|
23
|
+
* - Frequent user of ArkType Discord
|
|
24
|
+
* - Universally renowned two-finger speed typist
|
|
25
|
+
*/
|
|
26
|
+
const string = type("string")
|
|
27
|
+
attest<string>(string.infer)
|
|
28
|
+
attest(string("string").out).snap("string")
|
|
29
|
+
})
|
|
30
|
+
it("any", () => {
|
|
31
|
+
const any = type("any")
|
|
32
|
+
// equivalent to unknown at runtime
|
|
33
|
+
attest(any.json).equals(type("unknown").json)
|
|
34
|
+
// inferred as any
|
|
35
|
+
attest<any>(any.infer)
|
|
36
|
+
})
|
|
37
|
+
it("boolean", () => {
|
|
38
|
+
const boolean = type("boolean")
|
|
39
|
+
attest<boolean>(boolean.infer)
|
|
40
|
+
// should be simplified to simple checks for true and false literals
|
|
41
|
+
attest(boolean.json).equals(schema({ unit: false }, { unit: true }).json)
|
|
42
|
+
// TODO:
|
|
43
|
+
// attest(boolean.json).snap(`if( $arkRoot !== false && $arkRoot !== true) {
|
|
44
|
+
// return false
|
|
45
|
+
// }`)
|
|
46
|
+
})
|
|
47
|
+
it("never", () => {
|
|
48
|
+
const never = type("never")
|
|
49
|
+
attest<never>(never.infer)
|
|
50
|
+
// should be equivalent to a zero-branch union
|
|
51
|
+
attest(never.json).equals(schema().json)
|
|
52
|
+
})
|
|
53
|
+
it("unknown", () => {
|
|
54
|
+
// should be equivalent to an unconstrained predicate
|
|
55
|
+
attest(type("unknown").json).equals(schema({}).json)
|
|
56
|
+
})
|
|
57
|
+
it("void", () => {
|
|
58
|
+
const t = type("void")
|
|
59
|
+
attest<void>(t.infer)
|
|
60
|
+
//should be treated as undefined at runtime
|
|
61
|
+
attest(t.json).equals(type("undefined").json)
|
|
62
|
+
})
|
|
63
|
+
})
|
|
64
|
+
// describe("validation", () => {
|
|
65
|
+
// it("integer", () => {
|
|
66
|
+
// const integer = type("integer")
|
|
67
|
+
// attest(integer(123).out).equals(123)
|
|
68
|
+
// attest(integer("123").errors?.summary).equals(
|
|
69
|
+
// "Must be a number (was string)"
|
|
70
|
+
// )
|
|
71
|
+
// attest(integer(12.12).errors?.summary).equals(
|
|
72
|
+
// "Must be an integer (was 12.12)"
|
|
73
|
+
// )
|
|
74
|
+
// })
|
|
75
|
+
// it("alpha", () => {
|
|
76
|
+
// const alpha = type("alpha")
|
|
77
|
+
// attest(alpha("user").out).equals("user")
|
|
78
|
+
// attest(alpha("user123").errors?.summary).equals(
|
|
79
|
+
// "Must be only letters (was 'user123')"
|
|
80
|
+
// )
|
|
81
|
+
// })
|
|
82
|
+
// it("alphanumeric", () => {
|
|
83
|
+
// const alphanumeric = type("alphanumeric")
|
|
84
|
+
// attest(alphanumeric("user123").out).equals("user123")
|
|
85
|
+
// attest(alphanumeric("user").out).equals("user")
|
|
86
|
+
// attest(alphanumeric("123").out).equals("123")
|
|
87
|
+
// attest(alphanumeric("abc@123").errors?.summary).equals(
|
|
88
|
+
// "Must be only letters and digits (was 'abc@123')"
|
|
89
|
+
// )
|
|
90
|
+
// })
|
|
91
|
+
// it("lowercase", () => {
|
|
92
|
+
// const lowercase = type("lowercase")
|
|
93
|
+
// attest(lowercase("var").out).equals("var")
|
|
94
|
+
// attest(lowercase("newVar").errors?.summary).equals(
|
|
95
|
+
// "Must be only lowercase letters (was 'newVar')"
|
|
96
|
+
// )
|
|
97
|
+
// })
|
|
98
|
+
// it("uppercase", () => {
|
|
99
|
+
// const uppercase = type("uppercase")
|
|
100
|
+
// attest(uppercase("VAR").out).equals("VAR")
|
|
101
|
+
// attest(uppercase("CONST_VAR").errors?.summary).equals(
|
|
102
|
+
// "Must be only uppercase letters (was 'CONST_VAR')"
|
|
103
|
+
// )
|
|
104
|
+
// attest(uppercase("myVar").errors?.summary).equals(
|
|
105
|
+
// "Must be only uppercase letters (was 'myVar')"
|
|
106
|
+
// )
|
|
107
|
+
// })
|
|
108
|
+
// it("email", () => {
|
|
109
|
+
// const email = type("email")
|
|
110
|
+
// attest(email("shawn@mail.com").out).equals("shawn@mail.com")
|
|
111
|
+
// attest(email("shawn@email").errors?.summary).equals(
|
|
112
|
+
// "Must be a valid email (was 'shawn@email')"
|
|
113
|
+
// )
|
|
114
|
+
// })
|
|
115
|
+
// it("uuid", () => {
|
|
116
|
+
// const uuid = type("uuid")
|
|
117
|
+
// attest(uuid("f70b8242-dd57-4e6b-b0b7-649d997140a0").out).equals(
|
|
118
|
+
// "f70b8242-dd57-4e6b-b0b7-649d997140a0"
|
|
119
|
+
// )
|
|
120
|
+
// attest(uuid("1234").errors?.summary).equals(
|
|
121
|
+
// "Must be a valid UUID (was '1234')"
|
|
122
|
+
// )
|
|
123
|
+
// })
|
|
124
|
+
// it("parsedNumber", () => {
|
|
125
|
+
// const parsedNumber = type("parsedNumber")
|
|
126
|
+
// attest(parsedNumber("5").out).equals(5)
|
|
127
|
+
// attest(parsedNumber("5.5").out).equals(5.5)
|
|
128
|
+
// attest(parsedNumber("five").errors?.summary).equals(
|
|
129
|
+
// "Must be a well-formed numeric string (was 'five')"
|
|
130
|
+
// )
|
|
131
|
+
// })
|
|
132
|
+
// it("parsedInteger", () => {
|
|
133
|
+
// const parsedInteger = type("parsedInteger")
|
|
134
|
+
// attest(parsedInteger("5").out).equals(5)
|
|
135
|
+
// attest(parsedInteger("5.5").errors?.summary).equals(
|
|
136
|
+
// "Must be a well-formed integer string (was '5.5')"
|
|
137
|
+
// )
|
|
138
|
+
// attest(parsedInteger("five").errors?.summary).equals(
|
|
139
|
+
// "Must be a well-formed integer string (was 'five')"
|
|
140
|
+
// )
|
|
141
|
+
// attest(parsedInteger(5).errors?.summary).equals(
|
|
142
|
+
// "Must be a string (was number)"
|
|
143
|
+
// )
|
|
144
|
+
// attest(parsedInteger("9007199254740992").errors?.summary).equals(
|
|
145
|
+
// "Must be an integer in the range Number.MIN_SAFE_INTEGER to Number.MAX_SAFE_INTEGER (was '9007199254740992')"
|
|
146
|
+
// )
|
|
147
|
+
// })
|
|
148
|
+
// it("parsedDate", () => {
|
|
149
|
+
// const parsedDate = type("parsedDate")
|
|
150
|
+
// attest(parsedDate("5/21/1993").out?.toDateString()).equals(
|
|
151
|
+
// "Fri May 21 1993"
|
|
152
|
+
// )
|
|
153
|
+
// attest(parsedDate("foo").errors?.summary).equals(
|
|
154
|
+
// "Must be a valid date (was 'foo')"
|
|
155
|
+
// )
|
|
156
|
+
// attest(parsedDate(5).errors?.summary).equals(
|
|
157
|
+
// "Must be a string (was number)"
|
|
158
|
+
// )
|
|
159
|
+
// })
|
|
160
|
+
// it("json", () => {
|
|
161
|
+
// const json = type("json")
|
|
162
|
+
// attest(json('{"a": "hello"}').out).equals({ a: "hello" })
|
|
163
|
+
// attest(json(123).errors?.summary).equals(
|
|
164
|
+
// "Must be a JSON-parsable string (was number)"
|
|
165
|
+
// )
|
|
166
|
+
// })
|
|
167
|
+
// it("credit card", () => {
|
|
168
|
+
// const validCC = "5489582921773376"
|
|
169
|
+
// attest(ark.creditCard(validCC).out).equals(validCC)
|
|
170
|
+
// // Regex validation
|
|
171
|
+
// attest(ark.creditCard("0".repeat(16)).errors?.summary).equals(
|
|
172
|
+
// "Must be a valid credit card number (was '0000000000000000')"
|
|
173
|
+
// )
|
|
174
|
+
// // Luhn validation
|
|
175
|
+
// attest(
|
|
176
|
+
// ark.creditCard(validCC.slice(0, -1) + "0").errors?.summary
|
|
177
|
+
// ).equals(
|
|
178
|
+
// "Must be a valid credit card number (was '5489582921773370')"
|
|
179
|
+
// )
|
|
180
|
+
// })
|
|
181
|
+
// it("semver", () => {
|
|
182
|
+
// attest(ark.semver("1.0.0").out).equals("1.0.0")
|
|
183
|
+
// attest(ark.semver("-1.0.0").errors?.summary).equals(
|
|
184
|
+
// "Must be a valid semantic version (see https://semver.org/) (was '-1.0.0')"
|
|
185
|
+
// )
|
|
186
|
+
// })
|
|
187
|
+
// })
|
|
188
|
+
})
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { attest } from "@arktype/attest"
|
|
2
|
+
import { schema } from "@arktype/schema"
|
|
3
|
+
import { type } from "arktype"
|
|
4
|
+
|
|
5
|
+
describe("literal", () => {
|
|
6
|
+
describe("tuple expression", () => {
|
|
7
|
+
it("literal", () => {
|
|
8
|
+
const t = type(["===", 5])
|
|
9
|
+
attest<5>(t.infer)
|
|
10
|
+
attest(t.json).equals(type("5").json)
|
|
11
|
+
})
|
|
12
|
+
it("non-serializable", () => {
|
|
13
|
+
const s = Symbol()
|
|
14
|
+
const t = type(["===", s])
|
|
15
|
+
attest<symbol>(t.infer)
|
|
16
|
+
attest(t(s).out).equals(s)
|
|
17
|
+
attest(t("test").errors?.summary).snap(
|
|
18
|
+
'Must be (symbol anonymous) (was "test")'
|
|
19
|
+
)
|
|
20
|
+
})
|
|
21
|
+
it("branches", () => {
|
|
22
|
+
const o = { ark: true }
|
|
23
|
+
const s = Symbol()
|
|
24
|
+
const t = type(["===", true, "foo", 5, 1n, null, undefined, o, s])
|
|
25
|
+
attest<
|
|
26
|
+
true | "foo" | 5 | 1n | null | undefined | { ark: boolean } | typeof s
|
|
27
|
+
>(t.infer)
|
|
28
|
+
attest(t.json).equals(
|
|
29
|
+
schema.units(true, "foo", 5, 1n, null, undefined, o, s).json
|
|
30
|
+
)
|
|
31
|
+
})
|
|
32
|
+
})
|
|
33
|
+
describe("root expression", () => {
|
|
34
|
+
it("single", () => {
|
|
35
|
+
const t = type("===", true)
|
|
36
|
+
attest<true>(t.infer)
|
|
37
|
+
attest(t.json).equals(type("true").json)
|
|
38
|
+
})
|
|
39
|
+
it("branches", () => {
|
|
40
|
+
const o = { ark: true }
|
|
41
|
+
const s = Symbol()
|
|
42
|
+
const t = type("===", "foo", 5, true, null, 1n, undefined, o, s)
|
|
43
|
+
attest<
|
|
44
|
+
true | "foo" | 5 | 1n | null | undefined | { ark: boolean } | typeof s
|
|
45
|
+
>(t.infer)
|
|
46
|
+
attest(t.json).equals(
|
|
47
|
+
schema.units(true, "foo", 5, 1n, null, undefined, o, s).json
|
|
48
|
+
)
|
|
49
|
+
})
|
|
50
|
+
})
|
|
51
|
+
})
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// import { match } from "arktype"
|
|
2
|
+
|
|
3
|
+
// const matcher = match({
|
|
4
|
+
// boolean: (b) => !b,
|
|
5
|
+
// semver: (s) => s.length
|
|
6
|
+
// }).when({ condition: "true" }, (data) => {
|
|
7
|
+
// return data.condition
|
|
8
|
+
// })
|
|
9
|
+
|
|
10
|
+
// const greeting = match({
|
|
11
|
+
// "integer<12": () => "Good morning",
|
|
12
|
+
// "integer<18": () => "Good afternoon",
|
|
13
|
+
// default: () => "Good evening"
|
|
14
|
+
// })
|
|
15
|
+
|
|
16
|
+
// const result = greeting(5)
|
|
17
|
+
|
|
18
|
+
// const base = matcher({} as unknown) //=>?
|
|
19
|
+
|
|
20
|
+
// const oneResult = matcher({} as object) //=>?
|
|
21
|
+
|
|
22
|
+
// const twoResults = matcher({} as boolean | string) //=>?
|
|
23
|
+
|
|
24
|
+
// const validMatcher = match({
|
|
25
|
+
// // ^?
|
|
26
|
+
// number: (data) => data,
|
|
27
|
+
// "string|unknown[]": (data) => data.length
|
|
28
|
+
// })
|
|
29
|
+
|
|
30
|
+
// const invalidMatcher = match({
|
|
31
|
+
// // ^?
|
|
32
|
+
// "string|numbr": (data) => data,
|
|
33
|
+
// "1<boolean<5": (data) => data
|
|
34
|
+
// })
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { bench } from "@arktype/attest"
|
|
2
|
+
import { type } from "arktype"
|
|
3
|
+
|
|
4
|
+
bench("inline definition", () => {
|
|
5
|
+
const _ = type({
|
|
6
|
+
a: "string"
|
|
7
|
+
})
|
|
8
|
+
}).types([194, "instantiations"])
|
|
9
|
+
|
|
10
|
+
bench("referenced type", () => {
|
|
11
|
+
const a = type("string")
|
|
12
|
+
const _ = type({
|
|
13
|
+
a
|
|
14
|
+
})
|
|
15
|
+
}).types([138, "instantiations"])
|
|
16
|
+
|
|
17
|
+
// https://github.com/arktypeio/arktype/issues/787
|
|
18
|
+
bench("inline reference", () => {
|
|
19
|
+
const _ = type({
|
|
20
|
+
a: type("string")
|
|
21
|
+
})
|
|
22
|
+
}).types([5853, "instantiations"])
|