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__/morph.test.ts
DELETED
|
@@ -1,409 +0,0 @@
|
|
|
1
|
-
import { attest } from "@arktype/attest"
|
|
2
|
-
import {
|
|
3
|
-
writeUndiscriminableMorphUnionMessage,
|
|
4
|
-
type ArkTypeError,
|
|
5
|
-
type Out
|
|
6
|
-
} from "@arktype/schema"
|
|
7
|
-
import { scope, type, type Ark, type Type } from "arktype"
|
|
8
|
-
|
|
9
|
-
describe("morph", () => {
|
|
10
|
-
it("base", () => {
|
|
11
|
-
const t = type("boolean").morph((data) => `${data}`)
|
|
12
|
-
attest<Type<(In: boolean) => Out<string>>>(t)
|
|
13
|
-
attest<string>(t.infer)
|
|
14
|
-
attest<boolean>(t.in.infer)
|
|
15
|
-
const result = t(true)
|
|
16
|
-
if (result.errors) {
|
|
17
|
-
return result.errors.throw()
|
|
18
|
-
}
|
|
19
|
-
attest<string>(result.out).equals("true")
|
|
20
|
-
attest(t("foo").errors?.summary).snap("Must be boolean (was string)")
|
|
21
|
-
attest(t.root).equals(type(["boolean", "=>", (data) => `${data}`]).root)
|
|
22
|
-
})
|
|
23
|
-
it("endomorph", () => {
|
|
24
|
-
const t = type(["boolean", "=>", (data) => !data])
|
|
25
|
-
attest<Type<(In: boolean) => Out<boolean>>>(t)
|
|
26
|
-
const result = t(true)
|
|
27
|
-
if (result.errors) {
|
|
28
|
-
return result.errors.throw()
|
|
29
|
-
}
|
|
30
|
-
attest<boolean>(result.out).equals(false)
|
|
31
|
-
})
|
|
32
|
-
// it("chained to type", () => {
|
|
33
|
-
// const t = type(["string>5", "=>", arktypes.parse.date])
|
|
34
|
-
// attest<Type<(In: string) => Out<Date>>>(t)
|
|
35
|
-
// attest(t("5/21/1993").out?.getDate()).equals(21)
|
|
36
|
-
// attest(t("foobar").errors?.summary).snap(
|
|
37
|
-
// "Must be a valid date (was 'foobar')"
|
|
38
|
-
// )
|
|
39
|
-
// })
|
|
40
|
-
it("validated output", () => {
|
|
41
|
-
const parsedUser = type("string").morph((s) => JSON.parse(s), {
|
|
42
|
-
name: "string",
|
|
43
|
-
age: "number"
|
|
44
|
-
})
|
|
45
|
-
attest<
|
|
46
|
-
Type<
|
|
47
|
-
(In: string) => Out<{
|
|
48
|
-
name: string
|
|
49
|
-
age: number
|
|
50
|
-
}>,
|
|
51
|
-
Ark
|
|
52
|
-
>
|
|
53
|
-
>(parsedUser)
|
|
54
|
-
})
|
|
55
|
-
it("any as out", () => {
|
|
56
|
-
const t = type("string", "=>", (s) => s as any)
|
|
57
|
-
attest<string>(t.in.infer)
|
|
58
|
-
attest<any>(t.infer)
|
|
59
|
-
})
|
|
60
|
-
it("never as out", () => {
|
|
61
|
-
const t = type("string", "=>", (s) => s as never)
|
|
62
|
-
attest<string>(t.in.infer)
|
|
63
|
-
attest<never>(t.infer)
|
|
64
|
-
})
|
|
65
|
-
// it("return problem", () => {
|
|
66
|
-
// const divide100By = type([
|
|
67
|
-
// "number",
|
|
68
|
-
// "=>",
|
|
69
|
-
// (n, errors) => (n === 0 ? errors.mustBe("non-zero", n, []) : 100 / n)
|
|
70
|
-
// ])
|
|
71
|
-
// attest<Type<(In: number) => Out<number>>>(divide100By)
|
|
72
|
-
// attest(divide100By(5).out).equals(20)
|
|
73
|
-
// attest(divide100By(0).errors?.summary).snap("Must be non-zero (was 0)")
|
|
74
|
-
// })
|
|
75
|
-
it("adds a problem if one is returned without being added", () => {
|
|
76
|
-
const divide100By = type([
|
|
77
|
-
"number",
|
|
78
|
-
"=>",
|
|
79
|
-
(n, errors) => {
|
|
80
|
-
if (n !== 0) {
|
|
81
|
-
return 100 / n
|
|
82
|
-
} else {
|
|
83
|
-
// errors.mustBe("non-zero")
|
|
84
|
-
// errors.byPath = {}
|
|
85
|
-
return (errors as unknown as ArkTypeError[]).pop()
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
])
|
|
89
|
-
attest(divide100By(0).errors?.summary).snap("Must be non-zero (was 0)")
|
|
90
|
-
})
|
|
91
|
-
it("at path", () => {
|
|
92
|
-
const t = type({ a: ["string", "=>", (data) => data.length] })
|
|
93
|
-
attest<Type<{ a: (In: string) => Out<number> }>>(t)
|
|
94
|
-
|
|
95
|
-
const result = t({ a: "four" })
|
|
96
|
-
if (result.errors) {
|
|
97
|
-
return result.errors.throw()
|
|
98
|
-
}
|
|
99
|
-
attest<{ a: number }>(result.out).equals({ a: 4 })
|
|
100
|
-
})
|
|
101
|
-
it("in array", () => {
|
|
102
|
-
const types = scope({
|
|
103
|
-
lengthOfString: ["string", "=>", (data) => data.length],
|
|
104
|
-
mapToLengths: "lengthOfString[]"
|
|
105
|
-
}).export()
|
|
106
|
-
attest<Type<((In: string) => Out<number>)[]>>(types.mapToLengths)
|
|
107
|
-
const result = types.mapToLengths(["1", "22", "333"])
|
|
108
|
-
if (result.errors) {
|
|
109
|
-
return result.errors.throw()
|
|
110
|
-
}
|
|
111
|
-
attest<number[]>(result.out).equals([1, 2, 3])
|
|
112
|
-
})
|
|
113
|
-
it("object inference", () => {
|
|
114
|
-
const t = type([{ a: "string" }, "=>", (data) => `${data}`])
|
|
115
|
-
attest<Type<(In: { a: string }) => Out<string>>>(t)
|
|
116
|
-
})
|
|
117
|
-
it("intersection", () => {
|
|
118
|
-
const $ = scope({
|
|
119
|
-
b: "3.14",
|
|
120
|
-
a: () => $.type("number"), //.morph((data) => `${data}`),
|
|
121
|
-
aAndB: () => $.type("a&b"),
|
|
122
|
-
bAndA: () => $.type("b&a")
|
|
123
|
-
})
|
|
124
|
-
// const types = $.compile()
|
|
125
|
-
// attest<Type<(In: 3.14) => string>>(types.aAndB)
|
|
126
|
-
// attest(types.aAndB.node).snap({
|
|
127
|
-
// number: { rules: { value: 3.14 }, morph: "(function)" }
|
|
128
|
-
// })
|
|
129
|
-
// attest<typeof types.aAndB>(types.bAndA)
|
|
130
|
-
// attest(types.bAndA.node).equals(types.aAndB.node)
|
|
131
|
-
})
|
|
132
|
-
it("object intersection", () => {
|
|
133
|
-
// const $ = scope({
|
|
134
|
-
// a: () => $.type({ a: "1" }).morph((data) => `${data}`),
|
|
135
|
-
// b: { b: "2" },
|
|
136
|
-
// c: "a&b"
|
|
137
|
-
// })
|
|
138
|
-
// const types = $.compile()
|
|
139
|
-
// attest<Type<(In: { a: 1; b: 2 }) => string>>(types.c)
|
|
140
|
-
// attest(types.c.node).snap({
|
|
141
|
-
// object: {
|
|
142
|
-
// rules: {
|
|
143
|
-
// props: {
|
|
144
|
-
// a: { number: { value: 1 } },
|
|
145
|
-
// b: { number: { value: 2 } }
|
|
146
|
-
// }
|
|
147
|
-
// },
|
|
148
|
-
// morph: "(function)"
|
|
149
|
-
// }
|
|
150
|
-
// })
|
|
151
|
-
})
|
|
152
|
-
it("union", () => {
|
|
153
|
-
const types = scope({
|
|
154
|
-
a: ["number", "=>", (data) => `${data}`],
|
|
155
|
-
b: "boolean",
|
|
156
|
-
aOrB: "a|b",
|
|
157
|
-
bOrA: "b|a"
|
|
158
|
-
}).export()
|
|
159
|
-
attest<Type<boolean | ((In: number) => Out<string>)>>(types.aOrB)
|
|
160
|
-
// attest(types.aOrB.node).snap({
|
|
161
|
-
// number: { rules: {}, morph: "(function)" },
|
|
162
|
-
// boolean: true
|
|
163
|
-
// })
|
|
164
|
-
// attest<typeof types.aOrB>(types.bOrA)
|
|
165
|
-
// attest(types.bOrA.node).equals(types.aOrB.node)
|
|
166
|
-
})
|
|
167
|
-
it("union with output", () => {
|
|
168
|
-
const t = type("number|parse.number")
|
|
169
|
-
attest<number>(t.infer)
|
|
170
|
-
})
|
|
171
|
-
it("deep intersection", () => {
|
|
172
|
-
// TODO: Fix
|
|
173
|
-
// const types = scope({
|
|
174
|
-
// a: { a: ["number>0", "=>", (data) => data + 1] },
|
|
175
|
-
// b: { a: "1" },
|
|
176
|
-
// c: "a&b"
|
|
177
|
-
// }).export()
|
|
178
|
-
// attest<Type<{ a: (In: 1) => Out<number> }>>(types.c)
|
|
179
|
-
// attest(types.c.node).snap({
|
|
180
|
-
// object: {
|
|
181
|
-
// props: {
|
|
182
|
-
// a: { number: { rules: { value: 1 }, morph: "(function)" } }
|
|
183
|
-
// }
|
|
184
|
-
// }
|
|
185
|
-
// })
|
|
186
|
-
})
|
|
187
|
-
it("deep union", () => {
|
|
188
|
-
const types = scope({
|
|
189
|
-
a: { a: ["number>0", "=>", (data) => `${data}`] },
|
|
190
|
-
b: { a: "Function" },
|
|
191
|
-
c: "a|b"
|
|
192
|
-
}).export()
|
|
193
|
-
attest<
|
|
194
|
-
Type<
|
|
195
|
-
| {
|
|
196
|
-
a: (In: number) => Out<string>
|
|
197
|
-
}
|
|
198
|
-
| {
|
|
199
|
-
a: Function
|
|
200
|
-
}
|
|
201
|
-
>
|
|
202
|
-
>(types.c)
|
|
203
|
-
|
|
204
|
-
// attest(types.c.node).snap({
|
|
205
|
-
// object: [
|
|
206
|
-
// {
|
|
207
|
-
// props: {
|
|
208
|
-
// a: {
|
|
209
|
-
// number: {
|
|
210
|
-
// rules: {
|
|
211
|
-
// range: {
|
|
212
|
-
// min: { limit: 0, comparator: ">" }
|
|
213
|
-
// }
|
|
214
|
-
// },
|
|
215
|
-
// morph: "(function)"
|
|
216
|
-
// }
|
|
217
|
-
// }
|
|
218
|
-
// }
|
|
219
|
-
// },
|
|
220
|
-
// { props: { a: "Function" } }
|
|
221
|
-
// ]
|
|
222
|
-
// })
|
|
223
|
-
})
|
|
224
|
-
it("chained reference", () => {
|
|
225
|
-
const $ = scope({
|
|
226
|
-
a: type("string").morph((s) => s.length),
|
|
227
|
-
b: () => $.type("a").morph((n) => n === 0)
|
|
228
|
-
})
|
|
229
|
-
const types = $.export()
|
|
230
|
-
attest<Type<(In: string) => Out<boolean>>>(types.b)
|
|
231
|
-
// attest(types.b.node).snap({
|
|
232
|
-
// string: { rules: {}, morph: ["(function)", "(function)"] }
|
|
233
|
-
// })
|
|
234
|
-
})
|
|
235
|
-
it("chained nested", () => {
|
|
236
|
-
const $ = scope({
|
|
237
|
-
a: type("string").morph((s) => s.length),
|
|
238
|
-
b: () => $.type({ a: "a" }).morph(({ a }) => a === 0)
|
|
239
|
-
})
|
|
240
|
-
|
|
241
|
-
const types = $.export()
|
|
242
|
-
attest<Type<(In: { a: string }) => Out<boolean>>>(types.b)
|
|
243
|
-
// attest(types.b.node).snap({
|
|
244
|
-
// object: { rules: { props: { a: "a" } }, morph: "(function)" }
|
|
245
|
-
// })
|
|
246
|
-
})
|
|
247
|
-
it("directly nested", () => {
|
|
248
|
-
const t = type([
|
|
249
|
-
{
|
|
250
|
-
a: ["string", "=>", (s: string) => s.length]
|
|
251
|
-
},
|
|
252
|
-
"=>",
|
|
253
|
-
({ a }) => a === 0
|
|
254
|
-
])
|
|
255
|
-
attest<Type<(In: { a: string }) => Out<boolean>>>(t)
|
|
256
|
-
// attest(t.node).snap({
|
|
257
|
-
// object: {
|
|
258
|
-
// rules: {
|
|
259
|
-
// props: { a: { string: { rules: {}, morph: "(function)" } } }
|
|
260
|
-
// },
|
|
261
|
-
// morph: "(function)"
|
|
262
|
-
// }
|
|
263
|
-
// })
|
|
264
|
-
})
|
|
265
|
-
it("discriminable tuple union", () => {
|
|
266
|
-
const $ = scope({
|
|
267
|
-
a: () => $.type(["string"]).morph((s) => [...s, "!"]),
|
|
268
|
-
b: ["boolean"],
|
|
269
|
-
c: () => $.type("a|b")
|
|
270
|
-
})
|
|
271
|
-
const types = $.export()
|
|
272
|
-
attest<Type<[boolean] | ((In: [string]) => Out<string[]>)>>(types.c)
|
|
273
|
-
})
|
|
274
|
-
it("double intersection", () => {
|
|
275
|
-
// attest(() => {
|
|
276
|
-
// const z = scope({
|
|
277
|
-
// a: ["boolean", "=>", (data) => `${data}`],
|
|
278
|
-
// b: ["boolean", "=>", (data) => `${data}!!!`],
|
|
279
|
-
// c: "a&b"
|
|
280
|
-
// }).compile()
|
|
281
|
-
// }).throws("Intersection of morphs results in an unsatisfiable type")
|
|
282
|
-
})
|
|
283
|
-
it("undiscriminated union", () => {
|
|
284
|
-
// TODO: fix
|
|
285
|
-
// attest(() => {
|
|
286
|
-
// scope({
|
|
287
|
-
// a: ["/.*/", "=>", (s) => s.trim()],
|
|
288
|
-
// b: "string",
|
|
289
|
-
// c: "a|b"
|
|
290
|
-
// }).export()
|
|
291
|
-
// }).throws(writeUndiscriminableMorphUnionMessage("/"))
|
|
292
|
-
})
|
|
293
|
-
it("deep double intersection", () => {
|
|
294
|
-
attest(() => {
|
|
295
|
-
// const s = scope({
|
|
296
|
-
// a: { a: ["boolean", "=>", (data) => `${data}`] },
|
|
297
|
-
// b: { a: ["boolean", "=>", (data) => `${data}!!!`] },
|
|
298
|
-
// c: "a&b"
|
|
299
|
-
// }).compile()
|
|
300
|
-
}).throws("At a: Intersection of morphs results in an unsatisfiable type")
|
|
301
|
-
})
|
|
302
|
-
it("deep undiscriminated union", () => {
|
|
303
|
-
attest(() => {
|
|
304
|
-
scope({
|
|
305
|
-
a: { a: ["string", "=>", (s) => s.trim()] },
|
|
306
|
-
b: { a: "'foo'" },
|
|
307
|
-
c: "a|b"
|
|
308
|
-
}).export()
|
|
309
|
-
}).throws(writeUndiscriminableMorphUnionMessage("/"))
|
|
310
|
-
})
|
|
311
|
-
it("deep undiscriminated reference", () => {
|
|
312
|
-
const $ = scope({
|
|
313
|
-
a: { a: ["string", "=>", (s) => s.trim()] },
|
|
314
|
-
b: { a: "boolean" },
|
|
315
|
-
c: { b: "boolean" }
|
|
316
|
-
})
|
|
317
|
-
const t = $.type("a|b")
|
|
318
|
-
attest<
|
|
319
|
-
Type<
|
|
320
|
-
| {
|
|
321
|
-
a: (In: string) => Out<string>
|
|
322
|
-
}
|
|
323
|
-
| {
|
|
324
|
-
a: boolean
|
|
325
|
-
}
|
|
326
|
-
>
|
|
327
|
-
>(t)
|
|
328
|
-
|
|
329
|
-
attest(() => {
|
|
330
|
-
scope({
|
|
331
|
-
a: { a: ["string", "=>", (s) => s.trim()] },
|
|
332
|
-
b: { b: "boolean" },
|
|
333
|
-
c: "a|b"
|
|
334
|
-
}).export()
|
|
335
|
-
}).throws(writeUndiscriminableMorphUnionMessage("/"))
|
|
336
|
-
})
|
|
337
|
-
it("array double intersection", () => {
|
|
338
|
-
// attest(() => {
|
|
339
|
-
// scope({
|
|
340
|
-
// a: { a: ["number>0", "=>", (data) => data + 1] },
|
|
341
|
-
// b: { a: ["number>0", "=>", (data) => data + 2] },
|
|
342
|
-
// c: "a[]&b[]"
|
|
343
|
-
// }).export()
|
|
344
|
-
// }).throws(
|
|
345
|
-
// "At [index]/a: Intersection of morphs results in an unsatisfiable type"
|
|
346
|
-
// )
|
|
347
|
-
})
|
|
348
|
-
it("undiscriminated morph at path", () => {
|
|
349
|
-
attest(() => {
|
|
350
|
-
scope({
|
|
351
|
-
a: { a: ["string", "=>", (s) => s.trim()] },
|
|
352
|
-
b: { b: "boolean" },
|
|
353
|
-
c: { key: "a|b" }
|
|
354
|
-
}).export()
|
|
355
|
-
}).throws(writeUndiscriminableMorphUnionMessage("key"))
|
|
356
|
-
})
|
|
357
|
-
it("helper morph intersection", () => {
|
|
358
|
-
attest(() =>
|
|
359
|
-
type("string")
|
|
360
|
-
.morph((s) => s.length)
|
|
361
|
-
.and(type("string").morph((s) => s.length))
|
|
362
|
-
).throws("Intersection of morphs results in an unsatisfiable type")
|
|
363
|
-
})
|
|
364
|
-
it("union helper undiscriminated", () => {
|
|
365
|
-
attest(() =>
|
|
366
|
-
type("string")
|
|
367
|
-
.morph((s) => s.length)
|
|
368
|
-
.or("'foo'")
|
|
369
|
-
).throws(writeUndiscriminableMorphUnionMessage("/"))
|
|
370
|
-
})
|
|
371
|
-
// it("problem not included in return", () => {
|
|
372
|
-
// const parsedInt = type([
|
|
373
|
-
// "string",
|
|
374
|
-
// "=>",
|
|
375
|
-
// (s, errors) => {
|
|
376
|
-
// const result = parseInt(s)
|
|
377
|
-
// if (Number.isNaN(result)) {
|
|
378
|
-
// return errors.mustBe("an integer string", s, [])
|
|
379
|
-
// }
|
|
380
|
-
// return result
|
|
381
|
-
// }
|
|
382
|
-
// ])
|
|
383
|
-
// attest<Type<(In: string) => Out<number>>>(parsedInt)
|
|
384
|
-
// attest(parsedInt("5").out).snap(5)
|
|
385
|
-
// attest(parsedInt("five").errors?.summary).snap(
|
|
386
|
-
// "Must be an integer string (was 'five')"
|
|
387
|
-
// )
|
|
388
|
-
// })
|
|
389
|
-
it("nullable return", () => {
|
|
390
|
-
const toNullableNumber = type(["string", "=>", (s) => s.length || null])
|
|
391
|
-
attest<Type<(In: string) => Out<number | null>>>(toNullableNumber)
|
|
392
|
-
})
|
|
393
|
-
it("undefinable return", () => {
|
|
394
|
-
const toUndefinableNumber = type([
|
|
395
|
-
"string",
|
|
396
|
-
"=>",
|
|
397
|
-
(s) => s.length || undefined
|
|
398
|
-
])
|
|
399
|
-
attest<Type<(In: string) => Out<number | undefined>>>(toUndefinableNumber)
|
|
400
|
-
})
|
|
401
|
-
it("null or undefined return", () => {
|
|
402
|
-
const toMaybeNumber = type([
|
|
403
|
-
"string",
|
|
404
|
-
"=>",
|
|
405
|
-
(s) => (s.length === 0 ? undefined : s.length === 1 ? null : s.length)
|
|
406
|
-
])
|
|
407
|
-
attest<Type<(In: string) => Out<number | null | undefined>>>(toMaybeNumber)
|
|
408
|
-
})
|
|
409
|
-
})
|
package/__tests__/narrow.test.ts
DELETED
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
import { attest } from "@arktype/attest"
|
|
2
|
-
import type { Out } from "@arktype/schema"
|
|
3
|
-
import type { equals } from "@arktype/util"
|
|
4
|
-
import { type, type Type } from "arktype"
|
|
5
|
-
|
|
6
|
-
describe("narrow", () => {
|
|
7
|
-
it("implicit problem", () => {
|
|
8
|
-
const isOdd = (n: number) => n % 2 === 1
|
|
9
|
-
const odd = type(["number", ":", isOdd])
|
|
10
|
-
attest<number>(odd.infer)
|
|
11
|
-
// attest(odd.node).equals({ number: { narrow: isOdd as any } })
|
|
12
|
-
attest(odd(1).out).equals(1)
|
|
13
|
-
attest(odd(2).errors?.summary).snap(
|
|
14
|
-
"Must be valid according to isOdd (was 2)"
|
|
15
|
-
)
|
|
16
|
-
})
|
|
17
|
-
it("implicit problem anonymous", () => {
|
|
18
|
-
const even = type(["number", ":", (n) => n % 2 === 0])
|
|
19
|
-
attest(even(1).errors?.summary).snap("Must be valid (was 1)")
|
|
20
|
-
})
|
|
21
|
-
it("explicit problem", () => {
|
|
22
|
-
const even = type([
|
|
23
|
-
"number",
|
|
24
|
-
":",
|
|
25
|
-
(n, ctx) => n % 3 === 0 || ctx.invalid({ expected: "divisible by 3" })
|
|
26
|
-
])
|
|
27
|
-
attest(even(1).errors?.summary).snap("Must be divisible by 3 (was 1)")
|
|
28
|
-
})
|
|
29
|
-
// it("problem at path", () => {
|
|
30
|
-
// type([{ s: "string" }])
|
|
31
|
-
// const abEqual = type([
|
|
32
|
-
// {
|
|
33
|
-
// a: "number",
|
|
34
|
-
// b: "number"
|
|
35
|
-
// },
|
|
36
|
-
// ":",
|
|
37
|
-
// ({ a, b }, ctx) => {
|
|
38
|
-
// if (a === b) {
|
|
39
|
-
// return true
|
|
40
|
-
// }
|
|
41
|
-
// // a, ["a"]
|
|
42
|
-
// ctx.error("equal to b")
|
|
43
|
-
// // b, ["b"]
|
|
44
|
-
// ctx.error("equal to a")
|
|
45
|
-
// return false
|
|
46
|
-
// }
|
|
47
|
-
// ])
|
|
48
|
-
// attest(abEqual({ a: 1, b: 1 }).out).equals({ a: 1, b: 1 })
|
|
49
|
-
// attest(abEqual({ a: 1, b: 2 }).errors?.summary).snap(
|
|
50
|
-
// 'a must be equal to b (was {"a":1,"b":2})\nb must be equal to a (was {"a":1,"b":2})'
|
|
51
|
-
// )
|
|
52
|
-
// })
|
|
53
|
-
it("functional predicate", () => {
|
|
54
|
-
const one = type(["number", ":", (n): n is 1 => n === 1])
|
|
55
|
-
attest<Type<1>>(one)
|
|
56
|
-
})
|
|
57
|
-
it("functional parameter inference", () => {
|
|
58
|
-
type Expected = number | boolean[]
|
|
59
|
-
const validateNumberOrBooleanList = <t>(
|
|
60
|
-
t: equals<t, Expected> extends true ? t : Expected
|
|
61
|
-
) => true
|
|
62
|
-
attest<number | boolean[]>(
|
|
63
|
-
type([
|
|
64
|
-
"number|boolean[]",
|
|
65
|
-
":",
|
|
66
|
-
(data) => validateNumberOrBooleanList(data)
|
|
67
|
-
]).infer
|
|
68
|
-
)
|
|
69
|
-
attest(() => {
|
|
70
|
-
// @ts-expect-error
|
|
71
|
-
type(["number|boolean[]", ":", (data: number | string[]) => !!data])
|
|
72
|
-
}).type.errors("Type 'boolean' is not assignable to type 'string'.")
|
|
73
|
-
})
|
|
74
|
-
it("narrow problem", () => {
|
|
75
|
-
const palindrome = type([
|
|
76
|
-
"string",
|
|
77
|
-
":",
|
|
78
|
-
(s, ctx) =>
|
|
79
|
-
s === [...s].reverse().join("") ? true : ctx.invalid("a palindrome")
|
|
80
|
-
])
|
|
81
|
-
attest<Type<string>>(palindrome)
|
|
82
|
-
attest(palindrome("dad").out).snap("dad")
|
|
83
|
-
attest(palindrome("david").errors?.summary).snap(
|
|
84
|
-
"Must be a palindrome (was 'david')"
|
|
85
|
-
)
|
|
86
|
-
})
|
|
87
|
-
it("narrows the output type of a morph", () => {
|
|
88
|
-
const t = type("string")
|
|
89
|
-
.morph((s) => s.length)
|
|
90
|
-
.narrow((n): n is 5 => n === 5)
|
|
91
|
-
attest<Type<(In: string) => Out<5>>>(t)
|
|
92
|
-
attest(t.json).snap({ domain: "string" })
|
|
93
|
-
})
|
|
94
|
-
it("expression", () => {
|
|
95
|
-
const t = type("string", ":", (s): s is `f${string}` => s[0] === "f")
|
|
96
|
-
attest<`f${string}`>(t.infer)
|
|
97
|
-
attest(t.json).snap({
|
|
98
|
-
basis: "string",
|
|
99
|
-
predicate: ["$ark.anonymousFunction8"]
|
|
100
|
-
})
|
|
101
|
-
})
|
|
102
|
-
})
|
|
@@ -1,190 +0,0 @@
|
|
|
1
|
-
// describe("node definitions", () => {
|
|
2
|
-
// describe("basis", () => {
|
|
3
|
-
// it("domain", () => {
|
|
4
|
-
// const t = node({
|
|
5
|
-
// basis: "string"
|
|
6
|
-
// })
|
|
7
|
-
// attest<TypeNode<string>>(t)
|
|
8
|
-
// })
|
|
9
|
-
// it("class", () => {
|
|
10
|
-
// const t = node({
|
|
11
|
-
// basis: Date
|
|
12
|
-
// })
|
|
13
|
-
// attest<TypeNode<Date>>(t)
|
|
14
|
-
// })
|
|
15
|
-
// it("value", () => {
|
|
16
|
-
// const t = node({
|
|
17
|
-
// basis: ["===", 3.14159]
|
|
18
|
-
// })
|
|
19
|
-
// attest<TypeNode<3.14159>>(t)
|
|
20
|
-
// })
|
|
21
|
-
// })
|
|
22
|
-
// it("optional props", () => {
|
|
23
|
-
// const t = node({
|
|
24
|
-
// basis: "object",
|
|
25
|
-
// props: {
|
|
26
|
-
// a: {
|
|
27
|
-
// value: { basis: "string" }
|
|
28
|
-
// },
|
|
29
|
-
// b: {
|
|
30
|
-
// optional: true,
|
|
31
|
-
// value: { basis: "number" }
|
|
32
|
-
// }
|
|
33
|
-
// }
|
|
34
|
-
// })
|
|
35
|
-
// attest<TypeNode<{>(t)
|
|
36
|
-
// a: string
|
|
37
|
-
// b?: boolean
|
|
38
|
-
// }>
|
|
39
|
-
// })
|
|
40
|
-
// it("arrays", () => {
|
|
41
|
-
// const t = node({
|
|
42
|
-
// basis: Array,
|
|
43
|
-
// props: [
|
|
44
|
-
// {},
|
|
45
|
-
// {
|
|
46
|
-
// key: arrayIndexInput(),
|
|
47
|
-
// value: {
|
|
48
|
-
// basis: "object",
|
|
49
|
-
// props: {
|
|
50
|
-
// name: {
|
|
51
|
-
// value: { basis: "string" }
|
|
52
|
-
// }
|
|
53
|
-
// }
|
|
54
|
-
// }
|
|
55
|
-
// }
|
|
56
|
-
// ]
|
|
57
|
-
// })
|
|
58
|
-
// attest<TypeNode<{ name: string }[]>>(t)
|
|
59
|
-
// })
|
|
60
|
-
// it("variadic tuple", () => {
|
|
61
|
-
// const t = node({
|
|
62
|
-
// basis: Array,
|
|
63
|
-
// props: [
|
|
64
|
-
// {
|
|
65
|
-
// 0: {
|
|
66
|
-
// value: { basis: "string" }
|
|
67
|
-
// },
|
|
68
|
-
// // works for numeric or string keys
|
|
69
|
-
// "1": {
|
|
70
|
-
// value: { basis: "number" }
|
|
71
|
-
// }
|
|
72
|
-
// },
|
|
73
|
-
// {
|
|
74
|
-
// key: arrayIndexInput(2),
|
|
75
|
-
// value: {
|
|
76
|
-
// basis: "symbol"
|
|
77
|
-
// }
|
|
78
|
-
// }
|
|
79
|
-
// ]
|
|
80
|
-
// })
|
|
81
|
-
// attest<TypeNode<[string, number, ...symbol[]]>>(t)
|
|
82
|
-
// })
|
|
83
|
-
// it("non-variadic tuple", () => {
|
|
84
|
-
// const t = node({
|
|
85
|
-
// basis: Array,
|
|
86
|
-
// props: {
|
|
87
|
-
// 0: {
|
|
88
|
-
// value: {
|
|
89
|
-
// basis: "object",
|
|
90
|
-
// props: {
|
|
91
|
-
// a: { value: { basis: "string" } },
|
|
92
|
-
// b: { value: { basis: "number" } }
|
|
93
|
-
// }
|
|
94
|
-
// }
|
|
95
|
-
// },
|
|
96
|
-
// 1: {
|
|
97
|
-
// value: {
|
|
98
|
-
// basis: ["===", "arktype"]
|
|
99
|
-
// }
|
|
100
|
-
// },
|
|
101
|
-
// length: {
|
|
102
|
-
// prerequisite: true,
|
|
103
|
-
// value: { basis: ["===", 2] }
|
|
104
|
-
// }
|
|
105
|
-
// }
|
|
106
|
-
// })
|
|
107
|
-
// attest<TypeNode<>(t)
|
|
108
|
-
// [
|
|
109
|
-
// {
|
|
110
|
-
// a: string
|
|
111
|
-
// b: number
|
|
112
|
-
// },
|
|
113
|
-
// "arktype"
|
|
114
|
-
// ]
|
|
115
|
-
// >
|
|
116
|
-
// })
|
|
117
|
-
// it("branches", () => {
|
|
118
|
-
// const t = node(
|
|
119
|
-
// { basis: ["===", "foo"] },
|
|
120
|
-
// { basis: ["===", "bar"] },
|
|
121
|
-
// { basis: "number" },
|
|
122
|
-
// {
|
|
123
|
-
// basis: "object",
|
|
124
|
-
// props: { a: { value: { basis: "bigint" } } }
|
|
125
|
-
// }
|
|
126
|
-
// )
|
|
127
|
-
// attest<TypeNode<number | "foo" | "bar" | { a: bigint }>>(t)
|
|
128
|
-
// })
|
|
129
|
-
// it("narrow", () => {
|
|
130
|
-
// const t = node({
|
|
131
|
-
// basis: "string",
|
|
132
|
-
// narrow: (s): s is "foo" => s === "foo"
|
|
133
|
-
// })
|
|
134
|
-
// attest<TypeNode<"foo">>(t)
|
|
135
|
-
// })
|
|
136
|
-
// it("narrow array", () => {
|
|
137
|
-
// const t = node({
|
|
138
|
-
// basis: "object",
|
|
139
|
-
// narrow: [
|
|
140
|
-
// (o): o is { a: string } => typeof o.a === "string",
|
|
141
|
-
// (o): o is { b: boolean } => typeof o.b === "boolean"
|
|
142
|
-
// ] as const
|
|
143
|
-
// })
|
|
144
|
-
// attest<TypeNode<{>(t)
|
|
145
|
-
// a: string
|
|
146
|
-
// b: boolean
|
|
147
|
-
// }>
|
|
148
|
-
// })
|
|
149
|
-
// it("morph", () => {
|
|
150
|
-
// const t = node({
|
|
151
|
-
// basis: "string",
|
|
152
|
-
// morph: (s: string) => s.length
|
|
153
|
-
// })
|
|
154
|
-
// attest<TypeNode<(In: string) => Out<number>>>(t)
|
|
155
|
-
// })
|
|
156
|
-
// it("morph list", () => {
|
|
157
|
-
// const t = node({
|
|
158
|
-
// basis: "string",
|
|
159
|
-
// morph: [(s: string) => s.length, (n: number) => ({ n })] as const
|
|
160
|
-
// })
|
|
161
|
-
// attest<TypeNode<(In: string) => Out<{ n: number }>>>(t)
|
|
162
|
-
// })
|
|
163
|
-
// it("never", () => {
|
|
164
|
-
// const t = node()
|
|
165
|
-
// attest<TypeNode<never>>(t)
|
|
166
|
-
// })
|
|
167
|
-
// it("errors on rule in wrong domain", () => {
|
|
168
|
-
// attest(() =>
|
|
169
|
-
// node({
|
|
170
|
-
// basis: "number",
|
|
171
|
-
// divisor: 5,
|
|
172
|
-
// // @ts-expect-error
|
|
173
|
-
// regex: "/.*/"
|
|
174
|
-
// })
|
|
175
|
-
// ).throws.snap(
|
|
176
|
-
// "Error: regex constraint may only be applied to a string (was number)"
|
|
177
|
-
// )
|
|
178
|
-
// })
|
|
179
|
-
// it("errors on filter literal", () => {
|
|
180
|
-
// attest(() =>
|
|
181
|
-
// node({
|
|
182
|
-
// basis: ["===", true],
|
|
183
|
-
// // @ts-expect-error
|
|
184
|
-
// narrow: (b: boolean) => b === true
|
|
185
|
-
// })
|
|
186
|
-
// ).throws(
|
|
187
|
-
// "narrow constraint may only be applied to a non-literal type (was true)"
|
|
188
|
-
// )
|
|
189
|
-
// })
|
|
190
|
-
// })
|