arktype 1.1.0-dev-1 → 1.1.0-dev-2
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/dist/main.d.ts +11 -1018
- package/dist/main.d.ts.map +1 -0
- package/dist/main.js +8 -1450
- package/dist/main.js.map +1 -0
- package/dist/match.d.ts +46 -0
- package/dist/match.d.ts.map +1 -0
- package/dist/match.js +23 -0
- package/dist/match.js.map +1 -0
- package/dist/parser/definition.d.ts +32 -0
- package/dist/parser/definition.d.ts.map +1 -0
- package/dist/parser/definition.js +29 -0
- package/dist/parser/definition.js.map +1 -0
- package/dist/parser/generic.d.ts +44 -0
- package/dist/parser/generic.d.ts.map +1 -0
- package/dist/parser/generic.js +46 -0
- package/dist/parser/generic.js.map +1 -0
- package/dist/parser/objectLiteral.d.ts +24 -0
- package/dist/parser/objectLiteral.d.ts.map +1 -0
- package/dist/parser/objectLiteral.js +38 -0
- package/dist/parser/objectLiteral.js.map +1 -0
- package/dist/parser/semantic/bounds.d.ts +13 -0
- package/dist/parser/semantic/bounds.d.ts.map +1 -0
- package/dist/parser/semantic/bounds.js +2 -0
- package/dist/parser/semantic/bounds.js.map +1 -0
- package/dist/parser/semantic/divisor.d.ts +9 -0
- package/dist/parser/semantic/divisor.d.ts.map +1 -0
- package/dist/parser/semantic/divisor.js +2 -0
- package/dist/parser/semantic/divisor.js.map +1 -0
- package/dist/parser/semantic/intersections.d.ts +15 -0
- package/dist/parser/semantic/intersections.d.ts.map +1 -0
- package/dist/parser/semantic/intersections.js +2 -0
- package/dist/parser/semantic/intersections.js.map +1 -0
- package/dist/parser/semantic/semantic.d.ts +22 -0
- package/dist/parser/semantic/semantic.d.ts.map +1 -0
- package/dist/parser/semantic/semantic.js +2 -0
- package/dist/parser/semantic/semantic.js.map +1 -0
- package/dist/parser/semantic/utils.d.ts +8 -0
- package/dist/parser/semantic/utils.d.ts.map +1 -0
- package/dist/parser/semantic/utils.js +2 -0
- package/dist/parser/semantic/utils.js.map +1 -0
- package/dist/parser/semantic/validate.d.ts +24 -0
- package/dist/parser/semantic/validate.d.ts.map +1 -0
- package/dist/parser/semantic/validate.js +2 -0
- package/dist/parser/semantic/validate.js.map +1 -0
- package/dist/parser/shared.d.ts +48 -0
- package/dist/parser/shared.d.ts.map +1 -0
- package/dist/parser/shared.js +41 -0
- package/dist/parser/shared.js.map +1 -0
- package/dist/parser/string/reduce/dynamic.d.ts +40 -0
- package/dist/parser/string/reduce/dynamic.d.ts.map +1 -0
- package/dist/parser/string/reduce/dynamic.js +122 -0
- package/dist/parser/string/reduce/dynamic.js.map +1 -0
- package/dist/parser/string/reduce/shared.d.ts +29 -0
- package/dist/parser/string/reduce/shared.d.ts.map +1 -0
- package/dist/parser/string/reduce/shared.js +7 -0
- package/dist/parser/string/reduce/shared.js.map +1 -0
- package/dist/parser/string/reduce/static.d.ts +177 -0
- package/dist/parser/string/reduce/static.d.ts.map +1 -0
- package/dist/parser/string/reduce/static.js +2 -0
- package/dist/parser/string/reduce/static.js.map +1 -0
- package/dist/parser/string/shift/operand/date.d.ts +11 -0
- package/dist/parser/string/shift/operand/date.d.ts.map +1 -0
- package/dist/parser/string/shift/operand/date.js +26 -0
- package/dist/parser/string/shift/operand/date.js.map +1 -0
- package/dist/parser/string/shift/operand/enclosed.d.ts +49 -0
- package/dist/parser/string/shift/operand/enclosed.d.ts.map +1 -0
- package/dist/parser/string/shift/operand/enclosed.js +50 -0
- package/dist/parser/string/shift/operand/enclosed.js.map +1 -0
- package/dist/parser/string/shift/operand/operand.d.ts +9 -0
- package/dist/parser/string/shift/operand/operand.d.ts.map +1 -0
- package/dist/parser/string/shift/operand/operand.js +17 -0
- package/dist/parser/string/shift/operand/operand.js.map +1 -0
- package/dist/parser/string/shift/operand/unenclosed.d.ts +36 -0
- package/dist/parser/string/shift/operand/unenclosed.d.ts.map +1 -0
- package/dist/parser/string/shift/operand/unenclosed.js +68 -0
- package/dist/parser/string/shift/operand/unenclosed.js.map +1 -0
- package/dist/parser/string/shift/operator/bounds.d.ts +55 -0
- package/dist/parser/string/shift/operator/bounds.d.ts.map +1 -0
- package/dist/parser/string/shift/operator/bounds.js +95 -0
- package/dist/parser/string/shift/operator/bounds.js.map +1 -0
- package/dist/parser/string/shift/operator/divisor.d.ts +9 -0
- package/dist/parser/string/shift/operator/divisor.d.ts.map +1 -0
- package/dist/parser/string/shift/operator/divisor.js +13 -0
- package/dist/parser/string/shift/operator/divisor.js.map +1 -0
- package/dist/parser/string/shift/operator/operator.d.ts +13 -0
- package/dist/parser/string/shift/operator/operator.d.ts.map +1 -0
- package/dist/parser/string/shift/operator/operator.js +29 -0
- package/dist/parser/string/shift/operator/operator.js.map +1 -0
- package/dist/parser/string/shift/scanner.d.ts +71 -0
- package/dist/parser/string/shift/scanner.d.ts.map +1 -0
- package/dist/parser/string/shift/scanner.js +99 -0
- package/dist/parser/string/shift/scanner.js.map +1 -0
- package/dist/parser/string/string.d.ts +26 -0
- package/dist/parser/string/string.d.ts.map +1 -0
- package/dist/parser/string/string.js +31 -0
- package/dist/parser/string/string.js.map +1 -0
- package/dist/parser/tuple.d.ts +73 -0
- package/dist/parser/tuple.d.ts.map +1 -0
- package/dist/parser/tuple.js +134 -0
- package/dist/parser/tuple.js.map +1 -0
- package/dist/scope.d.ts +159 -0
- package/dist/scope.d.ts.map +1 -0
- package/dist/scope.js +248 -0
- package/dist/scope.js.map +1 -0
- package/dist/scopes/ark.d.ts +38 -0
- package/dist/scopes/ark.d.ts.map +1 -0
- package/dist/scopes/ark.js +24 -0
- package/dist/scopes/ark.js.map +1 -0
- package/dist/scopes/jsObjects.d.ts +19 -0
- package/dist/scopes/jsObjects.d.ts.map +1 -0
- package/dist/scopes/jsObjects.js +15 -0
- package/dist/scopes/jsObjects.js.map +1 -0
- package/dist/scopes/parsing.d.ts +18 -0
- package/dist/scopes/parsing.d.ts.map +1 -0
- package/dist/scopes/parsing.js +61 -0
- package/dist/scopes/parsing.js.map +1 -0
- package/dist/scopes/tsGenerics.d.ts +17 -0
- package/dist/scopes/tsGenerics.d.ts.map +1 -0
- package/dist/scopes/tsGenerics.js +13 -0
- package/dist/scopes/tsGenerics.js.map +1 -0
- package/dist/scopes/tsKeywords.d.ts +24 -0
- package/dist/scopes/tsKeywords.d.ts.map +1 -0
- package/dist/scopes/tsKeywords.js +20 -0
- package/dist/scopes/tsKeywords.js.map +1 -0
- package/dist/scopes/utils/creditCard.d.ts +3 -0
- package/dist/scopes/utils/creditCard.d.ts.map +1 -0
- package/dist/scopes/utils/creditCard.js +36 -0
- package/dist/scopes/utils/creditCard.js.map +1 -0
- package/dist/scopes/utils/date.d.ts +24 -0
- package/dist/scopes/utils/date.d.ts.map +1 -0
- package/dist/scopes/utils/date.js +53 -0
- package/dist/scopes/utils/date.js.map +1 -0
- package/dist/scopes/validation.d.ts +20 -0
- package/dist/scopes/validation.d.ts.map +1 -0
- package/dist/scopes/validation.js +57 -0
- package/dist/scopes/validation.js.map +1 -0
- package/dist/type.d.ts +101 -0
- package/dist/type.d.ts.map +1 -0
- package/dist/type.js +138 -0
- package/dist/type.js.map +1 -0
- package/main.ts +20 -0
- package/match.ts +137 -0
- package/package.json +12 -7
- package/parser/definition.ts +156 -0
- package/parser/generic.ts +208 -0
- package/parser/objectLiteral.ts +120 -0
- package/parser/semantic/bounds.ts +63 -0
- package/parser/semantic/divisor.ts +13 -0
- package/parser/semantic/intersections.ts +47 -0
- package/parser/semantic/semantic.ts +107 -0
- package/parser/semantic/utils.ts +26 -0
- package/parser/semantic/validate.ts +105 -0
- package/parser/shared.ts +141 -0
- package/parser/string/reduce/dynamic.ts +181 -0
- package/parser/string/reduce/shared.ts +73 -0
- package/parser/string/reduce/static.ts +300 -0
- package/parser/string/shift/operand/date.ts +57 -0
- package/parser/string/shift/operand/enclosed.ts +119 -0
- package/parser/string/shift/operand/operand.ts +48 -0
- package/parser/string/shift/operand/unenclosed.ts +282 -0
- package/parser/string/shift/operator/bounds.ts +229 -0
- package/parser/string/shift/operator/divisor.ts +36 -0
- package/parser/string/shift/operator/operator.ts +82 -0
- package/parser/string/shift/scanner.ts +209 -0
- package/parser/string/string.ts +93 -0
- package/parser/tuple.ts +475 -0
- package/scope.ts +573 -0
- package/scopes/ark.ts +64 -0
- package/scopes/jsObjects.ts +29 -0
- package/scopes/parsing.ts +81 -0
- package/scopes/tsGenerics.ts +28 -0
- package/scopes/tsKeywords.ts +39 -0
- package/scopes/utils/creditCard.ts +37 -0
- package/scopes/utils/date.ts +108 -0
- package/scopes/validation.ts +81 -0
- package/type.ts +403 -0
package/match.ts
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import type { Morph } from "@arktype/schema"
|
|
2
|
+
import type {
|
|
3
|
+
conform,
|
|
4
|
+
entryOf,
|
|
5
|
+
ErrorMessage,
|
|
6
|
+
evaluate,
|
|
7
|
+
Fn,
|
|
8
|
+
isDisjoint,
|
|
9
|
+
join,
|
|
10
|
+
paramsOf,
|
|
11
|
+
replaceKey,
|
|
12
|
+
returnOf,
|
|
13
|
+
unionToTuple,
|
|
14
|
+
valueOf
|
|
15
|
+
} from "@arktype/util"
|
|
16
|
+
import type { Scope } from "./scope.js"
|
|
17
|
+
import { Type, type inferTypeRoot, type validateTypeRoot } from "./type.js"
|
|
18
|
+
|
|
19
|
+
type cedille = "¸"
|
|
20
|
+
|
|
21
|
+
type serializedWhentry<
|
|
22
|
+
k extends string,
|
|
23
|
+
v extends string
|
|
24
|
+
> = `[${k}${cedille}${v}]`
|
|
25
|
+
|
|
26
|
+
type parseWhentryKey<
|
|
27
|
+
s extends string,
|
|
28
|
+
$,
|
|
29
|
+
result = {}
|
|
30
|
+
> = s extends `${serializedWhentry<infer k, infer v>}${cedille}${infer tail}`
|
|
31
|
+
? validateTypeRoot<v, $> extends ErrorMessage<infer message>
|
|
32
|
+
? ErrorMessage<message>
|
|
33
|
+
: parseWhentryKey<tail, $, result & { [_ in k]: inferTypeRoot<v, $> }>
|
|
34
|
+
: s extends serializedWhentry<infer k, infer v>
|
|
35
|
+
? validateTypeRoot<v, $> extends ErrorMessage<infer message>
|
|
36
|
+
? ErrorMessage<message>
|
|
37
|
+
: evaluate<result & { [_ in k]: inferTypeRoot<v, $> }>
|
|
38
|
+
: validateTypeRoot<s, $> extends ErrorMessage<infer message>
|
|
39
|
+
? ErrorMessage<message>
|
|
40
|
+
: never
|
|
41
|
+
|
|
42
|
+
export type WhenParser<$> = <const def>(
|
|
43
|
+
def: validateTypeRoot<def, $>
|
|
44
|
+
) => join<
|
|
45
|
+
unionToTuple<`[${join<conform<entryOf<def>, [string, string]>, cedille>}]`>,
|
|
46
|
+
cedille
|
|
47
|
+
>
|
|
48
|
+
|
|
49
|
+
export const createWhenParser = <$>(scope: Scope): WhenParser<$> => {
|
|
50
|
+
const parser = (def: unknown) => new Type(def, scope).alias
|
|
51
|
+
return parser as never
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
type MatchContext = {
|
|
55
|
+
inConstraint: unknown
|
|
56
|
+
outConstraint: unknown
|
|
57
|
+
thens: readonly Fn[]
|
|
58
|
+
$: unknown
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
type validateCases<cases, ctx extends MatchContext> = {
|
|
62
|
+
// adding keyof $ explicitly provides key completions for aliases
|
|
63
|
+
[k in keyof cases | keyof ctx["$"]]?: k extends validateTypeRoot<k, ctx["$"]>
|
|
64
|
+
? (
|
|
65
|
+
In: ctx["inConstraint"] & inferTypeRoot<k, ctx["$"]>
|
|
66
|
+
) => ctx["outConstraint"]
|
|
67
|
+
: parseWhentryKey<k & string, ctx["$"]> extends ErrorMessage<infer message>
|
|
68
|
+
? ErrorMessage<message>
|
|
69
|
+
: (In: parseWhentryKey<k & string, ctx["$"]>) => ctx["outConstraint"]
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export type MatchParser<$> = {
|
|
73
|
+
<In = unknown, Out = unknown>(): ChainableMatchParser<{
|
|
74
|
+
inConstraint: In
|
|
75
|
+
outConstraint: Out
|
|
76
|
+
thens: []
|
|
77
|
+
$: $
|
|
78
|
+
}>
|
|
79
|
+
} & CaseMatchParser<{
|
|
80
|
+
inConstraint: unknown
|
|
81
|
+
outConstraint: unknown
|
|
82
|
+
thens: []
|
|
83
|
+
$: $
|
|
84
|
+
}>
|
|
85
|
+
|
|
86
|
+
export type CaseMatchParser<ctx extends MatchContext> = <cases>(
|
|
87
|
+
def: conform<cases, validateCases<cases, ctx>>
|
|
88
|
+
) => ChainableMatchParser<
|
|
89
|
+
replaceKey<ctx, "thens", [...ctx["thens"], ...unionToTuple<valueOf<cases>>]>
|
|
90
|
+
>
|
|
91
|
+
|
|
92
|
+
export type WhenMatchParser<ctx extends MatchContext> = <
|
|
93
|
+
def,
|
|
94
|
+
then extends (
|
|
95
|
+
In: ctx["inConstraint"] & inferTypeRoot<def, ctx["$"]>
|
|
96
|
+
) => ctx["outConstraint"]
|
|
97
|
+
>(
|
|
98
|
+
when: validateTypeRoot<def, ctx["$"]>,
|
|
99
|
+
then: then
|
|
100
|
+
) => ChainableMatchParser<replaceKey<ctx, "thens", [...ctx["thens"], then]>>
|
|
101
|
+
|
|
102
|
+
export type MatchInvokation<ctx extends MatchContext> = <
|
|
103
|
+
data extends ctx["inConstraint"]
|
|
104
|
+
>(
|
|
105
|
+
In: data
|
|
106
|
+
) => {
|
|
107
|
+
[i in Extract<keyof ctx["thens"], `${number}`>]: isDisjoint<
|
|
108
|
+
data,
|
|
109
|
+
paramsOf<ctx["thens"][i]>[0]
|
|
110
|
+
> extends true
|
|
111
|
+
? never
|
|
112
|
+
: returnOf<ctx["thens"][i]>
|
|
113
|
+
}[Extract<keyof ctx["thens"], `${number}`>]
|
|
114
|
+
|
|
115
|
+
export type ChainableMatchParser<ctx extends MatchContext> =
|
|
116
|
+
MatchInvokation<ctx> & {
|
|
117
|
+
cases: CaseMatchParser<ctx>
|
|
118
|
+
when: WhenMatchParser<ctx>
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export const createMatchParser = <$>(scope: Scope): MatchParser<$> => {
|
|
122
|
+
// TODO: move to match node, discrimination
|
|
123
|
+
const parser = (cases: Record<string, Morph>) => {
|
|
124
|
+
const caseArray = Object.entries(cases).map(([def, morph]) => ({
|
|
125
|
+
when: new Type(def, scope).allows,
|
|
126
|
+
then: morph
|
|
127
|
+
}))
|
|
128
|
+
return (data: unknown) => {
|
|
129
|
+
for (const c of caseArray) {
|
|
130
|
+
if (c.when(data)) {
|
|
131
|
+
return c.then(data, {} as never)
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return parser as never
|
|
137
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "arktype",
|
|
3
3
|
"description": "TypeScript's 1:1 validator, optimized from editor to runtime",
|
|
4
|
-
"version": "1.1.0-dev-
|
|
4
|
+
"version": "1.1.0-dev-2",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "David Blass",
|
|
@@ -16,22 +16,27 @@
|
|
|
16
16
|
"exports": {
|
|
17
17
|
".": {
|
|
18
18
|
"arktype-repo": "./main.js",
|
|
19
|
-
"
|
|
19
|
+
"types": "./dist/main.d.ts",
|
|
20
|
+
"default": "./dist/main.js"
|
|
20
21
|
},
|
|
21
22
|
"./internal/*": {
|
|
22
23
|
"arktype-repo": "./*",
|
|
23
|
-
"
|
|
24
|
+
"types": "./dist/*",
|
|
25
|
+
"default": "./dist/*"
|
|
24
26
|
}
|
|
25
27
|
},
|
|
26
28
|
"files": [
|
|
27
|
-
"dist"
|
|
29
|
+
"dist",
|
|
30
|
+
"**/*.ts",
|
|
31
|
+
"!**/*.tsBuildInfo",
|
|
32
|
+
"!__tests__"
|
|
28
33
|
],
|
|
29
34
|
"scripts": {
|
|
30
|
-
"build": "
|
|
35
|
+
"build": "tsc --build ./tsconfig.build.json",
|
|
31
36
|
"test": "ts ../repo/testPackage.ts"
|
|
32
37
|
},
|
|
33
38
|
"dependencies": {
|
|
34
|
-
"@arktype/util": "0.0.
|
|
35
|
-
"@arktype/schema": "0.0.
|
|
39
|
+
"@arktype/util": "0.0.5",
|
|
40
|
+
"@arktype/schema": "0.0.2"
|
|
36
41
|
}
|
|
37
42
|
}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { isNode, node, type Root } from "@arktype/schema"
|
|
2
|
+
import {
|
|
3
|
+
isThunk,
|
|
4
|
+
objectKindOf,
|
|
5
|
+
stringify,
|
|
6
|
+
throwParseError,
|
|
7
|
+
type defined,
|
|
8
|
+
type Dict,
|
|
9
|
+
type equals,
|
|
10
|
+
type ErrorMessage,
|
|
11
|
+
type evaluate,
|
|
12
|
+
type isAny,
|
|
13
|
+
type isUnknown,
|
|
14
|
+
type List,
|
|
15
|
+
type objectKindOrDomainOf,
|
|
16
|
+
type optionalKeyOf,
|
|
17
|
+
type Primitive,
|
|
18
|
+
type requiredKeyOf
|
|
19
|
+
} from "@arktype/util"
|
|
20
|
+
import type { ParseContext } from "../scope.js"
|
|
21
|
+
import type { type } from "../scopes/ark.js"
|
|
22
|
+
import { Type } from "../type.js"
|
|
23
|
+
import {
|
|
24
|
+
parseObjectLiteral,
|
|
25
|
+
type inferObjectLiteral,
|
|
26
|
+
type validateObjectLiteral
|
|
27
|
+
} from "./objectLiteral.js"
|
|
28
|
+
import type { validateString } from "./semantic/validate.js"
|
|
29
|
+
import type { BaseCompletions, inferString } from "./string/string.js"
|
|
30
|
+
import {
|
|
31
|
+
parseTuple,
|
|
32
|
+
type inferTuple,
|
|
33
|
+
type TupleExpression,
|
|
34
|
+
type validateTuple
|
|
35
|
+
} from "./tuple.js"
|
|
36
|
+
|
|
37
|
+
export const parseObject = (def: object, ctx: ParseContext): Root => {
|
|
38
|
+
const objectKind = objectKindOf(def)
|
|
39
|
+
switch (objectKind) {
|
|
40
|
+
case undefined:
|
|
41
|
+
if (isNode(def)) {
|
|
42
|
+
return def as Root
|
|
43
|
+
}
|
|
44
|
+
return parseObjectLiteral(def as Dict, ctx)
|
|
45
|
+
case "Array":
|
|
46
|
+
return parseTuple(def as List, ctx)
|
|
47
|
+
case "RegExp":
|
|
48
|
+
return node({ basis: "string", pattern: def as RegExp })
|
|
49
|
+
case "Function":
|
|
50
|
+
const resolvedDef = isThunk(def) ? def() : def
|
|
51
|
+
if (resolvedDef instanceof Type) {
|
|
52
|
+
return resolvedDef.root
|
|
53
|
+
}
|
|
54
|
+
return throwParseError(writeBadDefinitionTypeMessage("Function"))
|
|
55
|
+
default:
|
|
56
|
+
return throwParseError(
|
|
57
|
+
writeBadDefinitionTypeMessage(objectKind ?? stringify(def))
|
|
58
|
+
)
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export type inferDefinition<def, $, args> = isAny<def> extends true
|
|
63
|
+
? never
|
|
64
|
+
: def extends type.cast<infer t> | ThunkCast<infer t>
|
|
65
|
+
? t
|
|
66
|
+
: def extends string
|
|
67
|
+
? inferString<def, $, args>
|
|
68
|
+
: def extends readonly unknown[]
|
|
69
|
+
? inferTuple<def, $, args>
|
|
70
|
+
: def extends RegExp
|
|
71
|
+
? string
|
|
72
|
+
: def extends object
|
|
73
|
+
? inferObjectLiteral<def, $, args>
|
|
74
|
+
: never
|
|
75
|
+
|
|
76
|
+
export type validateDefinition<def, $, args> = null extends undefined
|
|
77
|
+
? ErrorMessage<`'strict' or 'strictNullChecks' must be set to true in your tsconfig's 'compilerOptions'`>
|
|
78
|
+
: [def] extends [Terminal]
|
|
79
|
+
? unknown extends def
|
|
80
|
+
? // if def is any, never is the only way we can make validation fail
|
|
81
|
+
// since any would be assignable to a standard error message
|
|
82
|
+
never
|
|
83
|
+
: def
|
|
84
|
+
: def extends string
|
|
85
|
+
? validateString<def, $, args>
|
|
86
|
+
: def extends readonly unknown[]
|
|
87
|
+
? validateTuple<def, $, args>
|
|
88
|
+
: def extends BadDefinitionType
|
|
89
|
+
? writeBadDefinitionTypeMessage<objectKindOrDomainOf<def>>
|
|
90
|
+
: isUnknown<def> extends true
|
|
91
|
+
? // this allows the initial list of autocompletions to be populated when a user writes "type()",
|
|
92
|
+
// before having specified a definition
|
|
93
|
+
BaseCompletions<$, args> | {}
|
|
94
|
+
: validateObjectLiteral<def, $, args>
|
|
95
|
+
|
|
96
|
+
export type validateDeclared<declared, def, $, args> =
|
|
97
|
+
def extends validateDefinition<def, $, args>
|
|
98
|
+
? validateInference<def, declared, $, args>
|
|
99
|
+
: validateDefinition<def, $, args>
|
|
100
|
+
|
|
101
|
+
type validateInference<def, declared, $, args> = def extends
|
|
102
|
+
| RegExp
|
|
103
|
+
| type.cast<unknown>
|
|
104
|
+
| ThunkCast
|
|
105
|
+
| TupleExpression
|
|
106
|
+
? validateShallowInference<def, declared, $, args>
|
|
107
|
+
: def extends readonly unknown[]
|
|
108
|
+
? declared extends readonly unknown[]
|
|
109
|
+
? {
|
|
110
|
+
[i in keyof declared]: i extends keyof def
|
|
111
|
+
? validateInference<def[i], declared[i], $, args>
|
|
112
|
+
: unknown
|
|
113
|
+
}
|
|
114
|
+
: evaluate<declarationMismatch<def, declared, $, args>>
|
|
115
|
+
: def extends object
|
|
116
|
+
? evaluate<
|
|
117
|
+
{
|
|
118
|
+
[k in requiredKeyOf<declared>]: k extends keyof def
|
|
119
|
+
? validateInference<def[k], declared[k], $, args>
|
|
120
|
+
: unknown
|
|
121
|
+
} & {
|
|
122
|
+
[k in optionalKeyOf<declared> &
|
|
123
|
+
string as `${k}?`]: `${k}?` extends keyof def
|
|
124
|
+
? validateInference<def[`${k}?`], defined<declared[k]>, $, args>
|
|
125
|
+
: unknown
|
|
126
|
+
}
|
|
127
|
+
>
|
|
128
|
+
: validateShallowInference<def, declared, $, args>
|
|
129
|
+
|
|
130
|
+
type validateShallowInference<def, declared, $, args> = equals<
|
|
131
|
+
inferDefinition<def, $, args>,
|
|
132
|
+
declared
|
|
133
|
+
> extends true
|
|
134
|
+
? def
|
|
135
|
+
: evaluate<declarationMismatch<def, declared, $, args>>
|
|
136
|
+
|
|
137
|
+
type declarationMismatch<def, declared, $, args> = {
|
|
138
|
+
declared: declared
|
|
139
|
+
inferred: inferDefinition<def, $, args>
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// functions are ignored in validation so that cyclic thunk definitions can be
|
|
143
|
+
// inferred in scopes
|
|
144
|
+
type Terminal = RegExp | type.cast<unknown> | ((...args: never[]) => unknown)
|
|
145
|
+
|
|
146
|
+
export type ThunkCast<t = unknown> = () => type.cast<t>
|
|
147
|
+
|
|
148
|
+
type BadDefinitionType = Exclude<Primitive, string>
|
|
149
|
+
|
|
150
|
+
export const writeBadDefinitionTypeMessage = <actual extends string>(
|
|
151
|
+
actual: actual
|
|
152
|
+
): writeBadDefinitionTypeMessage<actual> =>
|
|
153
|
+
`Type definitions must be strings or objects (was ${actual})`
|
|
154
|
+
|
|
155
|
+
type writeBadDefinitionTypeMessage<actual extends string> =
|
|
156
|
+
`Type definitions must be strings or objects (was ${actual})`
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import type { Root } from "@arktype/schema"
|
|
2
|
+
import {
|
|
3
|
+
throwParseError,
|
|
4
|
+
type ErrorMessage,
|
|
5
|
+
type join,
|
|
6
|
+
type nominal
|
|
7
|
+
} from "@arktype/util"
|
|
8
|
+
import type { ParseContext } from "../scope.js"
|
|
9
|
+
import { DynamicState } from "./string/reduce/dynamic.js"
|
|
10
|
+
import { writeUnclosedGroupMessage } from "./string/reduce/shared.js"
|
|
11
|
+
import type { StaticState, state } from "./string/reduce/static.js"
|
|
12
|
+
import { writeUnexpectedCharacterMessage } from "./string/shift/operator/operator.js"
|
|
13
|
+
import { Scanner } from "./string/shift/scanner.js"
|
|
14
|
+
import { parseUntilFinalizer } from "./string/string.js"
|
|
15
|
+
|
|
16
|
+
export type GenericDeclaration<
|
|
17
|
+
name extends string = string,
|
|
18
|
+
params extends string = string
|
|
19
|
+
> = `${name}<${params}>`
|
|
20
|
+
|
|
21
|
+
// we put the error in a tuple so that parseGenericParams always returns a string[]
|
|
22
|
+
export type GenericParamsParseError<message extends string = string> = [
|
|
23
|
+
nominal<message, "InvalidGenericParameters">
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
export const parseGenericParams = (def: string) =>
|
|
27
|
+
parseGenericParamsRecurse(new Scanner(def))
|
|
28
|
+
|
|
29
|
+
export type parseGenericParams<def extends string> = parseParamsRecurse<
|
|
30
|
+
def,
|
|
31
|
+
"",
|
|
32
|
+
[]
|
|
33
|
+
> extends infer result extends string[]
|
|
34
|
+
? "" extends result[number]
|
|
35
|
+
? GenericParamsParseError<emptyGenericParameterMessage>
|
|
36
|
+
: result
|
|
37
|
+
: never
|
|
38
|
+
|
|
39
|
+
export const emptyGenericParameterMessage = `An empty string is not a valid generic parameter name`
|
|
40
|
+
|
|
41
|
+
export type emptyGenericParameterMessage = typeof emptyGenericParameterMessage
|
|
42
|
+
|
|
43
|
+
const parseGenericParamsRecurse = (scanner: Scanner): string[] => {
|
|
44
|
+
const param = scanner.shiftUntilNextTerminator()
|
|
45
|
+
if (param === "") {
|
|
46
|
+
throwParseError(emptyGenericParameterMessage)
|
|
47
|
+
}
|
|
48
|
+
scanner.shiftUntilNonWhitespace()
|
|
49
|
+
const nextNonWhitespace = scanner.shift()
|
|
50
|
+
return nextNonWhitespace === ""
|
|
51
|
+
? [param]
|
|
52
|
+
: nextNonWhitespace === ","
|
|
53
|
+
? [param, ...parseGenericParamsRecurse(scanner)]
|
|
54
|
+
: throwParseError(writeUnexpectedCharacterMessage(nextNonWhitespace, ","))
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
type parseParamsRecurse<
|
|
58
|
+
unscanned extends string,
|
|
59
|
+
param extends string,
|
|
60
|
+
result extends string[]
|
|
61
|
+
> = unscanned extends `${infer lookahead}${infer nextUnscanned}`
|
|
62
|
+
? lookahead extends ","
|
|
63
|
+
? parseParamsRecurse<nextUnscanned, "", [...result, param]>
|
|
64
|
+
: lookahead extends Scanner.WhiteSpaceToken
|
|
65
|
+
? param extends ""
|
|
66
|
+
? // if the next char is whitespace and we aren't in the middle of a param, skip to the next one
|
|
67
|
+
parseParamsRecurse<Scanner.skipWhitespace<nextUnscanned>, "", result>
|
|
68
|
+
: Scanner.skipWhitespace<nextUnscanned> extends `${infer nextNonWhitespace}${infer rest}`
|
|
69
|
+
? nextNonWhitespace extends ","
|
|
70
|
+
? parseParamsRecurse<rest, "", [...result, param]>
|
|
71
|
+
: GenericParamsParseError<
|
|
72
|
+
writeUnexpectedCharacterMessage<nextNonWhitespace, ",">
|
|
73
|
+
>
|
|
74
|
+
: // params end with a single whitespace character, add the current token
|
|
75
|
+
[...result, param]
|
|
76
|
+
: parseParamsRecurse<nextUnscanned, `${param}${lookahead}`, result>
|
|
77
|
+
: param extends ""
|
|
78
|
+
? result
|
|
79
|
+
: [...result, param]
|
|
80
|
+
|
|
81
|
+
export type ParsedArgs<
|
|
82
|
+
result extends unknown[] = unknown[],
|
|
83
|
+
unscanned extends string = string
|
|
84
|
+
> = {
|
|
85
|
+
result: result
|
|
86
|
+
unscanned: unscanned
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export const parseGenericArgs = (
|
|
90
|
+
name: string,
|
|
91
|
+
params: string[],
|
|
92
|
+
unscanned: string,
|
|
93
|
+
ctx: ParseContext
|
|
94
|
+
) => parseGenericArgsRecurse(name, params, unscanned, ctx, [], [])
|
|
95
|
+
|
|
96
|
+
export type parseGenericArgs<
|
|
97
|
+
name extends string,
|
|
98
|
+
params extends string[],
|
|
99
|
+
unscanned extends string,
|
|
100
|
+
$,
|
|
101
|
+
args
|
|
102
|
+
> = parseGenericArgsRecurse<name, params, unscanned, $, args, [], []>
|
|
103
|
+
|
|
104
|
+
const parseGenericArgsRecurse = (
|
|
105
|
+
name: string,
|
|
106
|
+
params: string[],
|
|
107
|
+
unscanned: string,
|
|
108
|
+
ctx: ParseContext,
|
|
109
|
+
argDefs: string[],
|
|
110
|
+
argNodes: Root[]
|
|
111
|
+
): ParsedArgs<Root[]> => {
|
|
112
|
+
const s = parseUntilFinalizer(new DynamicState(unscanned, ctx))
|
|
113
|
+
// remove the finalizing token from the argDef
|
|
114
|
+
argDefs.push(s.scanner.scanned.slice(0, -1))
|
|
115
|
+
argNodes.push(s.root)
|
|
116
|
+
const nextUnscanned = s.scanner.unscanned
|
|
117
|
+
if (s.finalizer === ">") {
|
|
118
|
+
if (argNodes.length === params.length) {
|
|
119
|
+
return {
|
|
120
|
+
result: argNodes,
|
|
121
|
+
unscanned: nextUnscanned
|
|
122
|
+
}
|
|
123
|
+
} else {
|
|
124
|
+
return s.error(writeInvalidGenericArgsMessage(name, params, argDefs))
|
|
125
|
+
}
|
|
126
|
+
} else if (s.finalizer === ",") {
|
|
127
|
+
return parseGenericArgsRecurse(
|
|
128
|
+
name,
|
|
129
|
+
params,
|
|
130
|
+
nextUnscanned,
|
|
131
|
+
ctx,
|
|
132
|
+
argDefs,
|
|
133
|
+
argNodes
|
|
134
|
+
)
|
|
135
|
+
}
|
|
136
|
+
return s.error(writeUnclosedGroupMessage(">"))
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
type parseGenericArgsRecurse<
|
|
140
|
+
name extends string,
|
|
141
|
+
params extends string[],
|
|
142
|
+
unscanned extends string,
|
|
143
|
+
$,
|
|
144
|
+
args,
|
|
145
|
+
argDefs extends string[],
|
|
146
|
+
argAsts extends unknown[]
|
|
147
|
+
> = parseUntilFinalizer<
|
|
148
|
+
state.initialize<unscanned>,
|
|
149
|
+
$,
|
|
150
|
+
args
|
|
151
|
+
> extends infer finalArgState extends StaticState
|
|
152
|
+
? {
|
|
153
|
+
defs: [
|
|
154
|
+
...argDefs,
|
|
155
|
+
finalArgState["scanned"] extends `${infer def}${"," | ">"}`
|
|
156
|
+
? def
|
|
157
|
+
: finalArgState["scanned"]
|
|
158
|
+
]
|
|
159
|
+
asts: [...argAsts, finalArgState["root"]]
|
|
160
|
+
unscanned: finalArgState["unscanned"]
|
|
161
|
+
} extends {
|
|
162
|
+
defs: infer nextDefs extends string[]
|
|
163
|
+
asts: infer nextAsts extends unknown[]
|
|
164
|
+
unscanned: infer nextUnscanned extends string
|
|
165
|
+
}
|
|
166
|
+
? finalArgState["finalizer"] extends ">"
|
|
167
|
+
? nextAsts["length"] extends params["length"]
|
|
168
|
+
? ParsedArgs<nextAsts, nextUnscanned>
|
|
169
|
+
: state.error<writeInvalidGenericArgsMessage<name, params, nextDefs>>
|
|
170
|
+
: finalArgState["finalizer"] extends ","
|
|
171
|
+
? parseGenericArgsRecurse<
|
|
172
|
+
name,
|
|
173
|
+
params,
|
|
174
|
+
nextUnscanned,
|
|
175
|
+
$,
|
|
176
|
+
args,
|
|
177
|
+
nextDefs,
|
|
178
|
+
nextAsts
|
|
179
|
+
>
|
|
180
|
+
: finalArgState["finalizer"] extends ErrorMessage
|
|
181
|
+
? finalArgState
|
|
182
|
+
: state.error<writeUnclosedGroupMessage<">">>
|
|
183
|
+
: never
|
|
184
|
+
: never
|
|
185
|
+
|
|
186
|
+
export const writeInvalidGenericArgsMessage = <
|
|
187
|
+
name extends string,
|
|
188
|
+
params extends string[],
|
|
189
|
+
argDefs extends string[]
|
|
190
|
+
>(
|
|
191
|
+
name: name,
|
|
192
|
+
params: params,
|
|
193
|
+
argDefs: argDefs
|
|
194
|
+
) =>
|
|
195
|
+
`${name}<${params.join(", ")}> requires exactly ${params.length} args (got ${
|
|
196
|
+
argDefs.length
|
|
197
|
+
}${argDefs.length === 0 ? "" : ": " + argDefs.join(", ")})`
|
|
198
|
+
|
|
199
|
+
export type writeInvalidGenericArgsMessage<
|
|
200
|
+
name extends string,
|
|
201
|
+
params extends string[],
|
|
202
|
+
argDefs extends string[]
|
|
203
|
+
> = `${name}<${join<
|
|
204
|
+
params,
|
|
205
|
+
", "
|
|
206
|
+
>}> requires exactly ${params["length"]} args (got ${argDefs["length"]}${argDefs["length"] extends 0
|
|
207
|
+
? ""
|
|
208
|
+
: `: ${join<argDefs, ",">}`})`
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { node, type Inner } from "@arktype/schema"
|
|
2
|
+
import {
|
|
3
|
+
stringify,
|
|
4
|
+
type Dict,
|
|
5
|
+
type ErrorMessage,
|
|
6
|
+
type evaluate
|
|
7
|
+
} from "@arktype/util"
|
|
8
|
+
import type { ParseContext } from "../scope.js"
|
|
9
|
+
import type { inferDefinition, validateDefinition } from "./definition.js"
|
|
10
|
+
import type { validateString } from "./semantic/validate.js"
|
|
11
|
+
import {
|
|
12
|
+
parseEntry,
|
|
13
|
+
type EntryParseResult,
|
|
14
|
+
type IndexedKey,
|
|
15
|
+
type OptionalValue,
|
|
16
|
+
type validateObjectValue
|
|
17
|
+
} from "./shared.js"
|
|
18
|
+
|
|
19
|
+
const stringAndSymbolicEntriesOf = (o: Record<string | symbol, unknown>) => [
|
|
20
|
+
...Object.entries(o),
|
|
21
|
+
...Object.getOwnPropertySymbols(o).map((k) => [k, o[k]] as const)
|
|
22
|
+
]
|
|
23
|
+
|
|
24
|
+
export const parseObjectLiteral = (def: Dict, ctx: ParseContext) => {
|
|
25
|
+
const required: Inner<"required">[] = []
|
|
26
|
+
const optional: Inner<"optional">[] = []
|
|
27
|
+
for (const entry of stringAndSymbolicEntriesOf(def)) {
|
|
28
|
+
const result = parseEntry(entry)
|
|
29
|
+
ctx.path.push(
|
|
30
|
+
`${
|
|
31
|
+
typeof result.innerKey === "symbol"
|
|
32
|
+
? `[${stringify(result.innerKey)}]`
|
|
33
|
+
: result.innerKey
|
|
34
|
+
}`
|
|
35
|
+
)
|
|
36
|
+
const valueNode = ctx.scope.parse(result.innerValue, ctx)
|
|
37
|
+
if (result.kind === "optional") {
|
|
38
|
+
optional.push({
|
|
39
|
+
key: result.innerKey,
|
|
40
|
+
value: valueNode
|
|
41
|
+
})
|
|
42
|
+
} else {
|
|
43
|
+
required.push({
|
|
44
|
+
key: result.innerKey,
|
|
45
|
+
value: valueNode
|
|
46
|
+
})
|
|
47
|
+
}
|
|
48
|
+
ctx.path.pop()
|
|
49
|
+
}
|
|
50
|
+
return node({
|
|
51
|
+
basis: "object",
|
|
52
|
+
required,
|
|
53
|
+
optional
|
|
54
|
+
})
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export type inferObjectLiteral<def extends object, $, args> = evaluate<
|
|
58
|
+
{
|
|
59
|
+
// since def is a const parameter, we remove the readonly modifier here
|
|
60
|
+
// support for builtin readonly tracked here:
|
|
61
|
+
// https://github.com/arktypeio/arktype/issues/808
|
|
62
|
+
-readonly [k in keyof def as nonOptionalKeyFrom<
|
|
63
|
+
k,
|
|
64
|
+
def[k],
|
|
65
|
+
$,
|
|
66
|
+
args
|
|
67
|
+
>]: inferDefinition<def[k], $, args>
|
|
68
|
+
} & {
|
|
69
|
+
-readonly [k in keyof def as optionalKeyFrom<k, def[k]>]?: inferDefinition<
|
|
70
|
+
def[k] extends OptionalValue<infer inner> ? inner : def[k],
|
|
71
|
+
$,
|
|
72
|
+
args
|
|
73
|
+
>
|
|
74
|
+
}
|
|
75
|
+
>
|
|
76
|
+
|
|
77
|
+
export type validateObjectLiteral<def, $, args> = {
|
|
78
|
+
[k in keyof def]: k extends IndexedKey<infer indexDef>
|
|
79
|
+
? validateString<indexDef, $, args> extends ErrorMessage<infer message>
|
|
80
|
+
? // add a nominal type here to avoid allowing the error message as input
|
|
81
|
+
indexParseError<message>
|
|
82
|
+
: inferDefinition<indexDef, $, args> extends PropertyKey
|
|
83
|
+
? // if the indexDef is syntactically and semantically valid,
|
|
84
|
+
// move on to the validating the value definition
|
|
85
|
+
validateDefinition<def[k], $, args>
|
|
86
|
+
: indexParseError<writeInvalidPropertyKeyMessage<indexDef>>
|
|
87
|
+
: validateObjectValue<def[k], $, args>
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
type nonOptionalKeyFrom<k extends PropertyKey, valueDef, $, args> = parseEntry<
|
|
91
|
+
k,
|
|
92
|
+
valueDef
|
|
93
|
+
> extends infer result extends EntryParseResult
|
|
94
|
+
? result["kind"] extends "required"
|
|
95
|
+
? result["innerKey"]
|
|
96
|
+
: result["kind"] extends "indexed"
|
|
97
|
+
? inferDefinition<result["innerKey"], $, args> & PropertyKey
|
|
98
|
+
: never
|
|
99
|
+
: never
|
|
100
|
+
|
|
101
|
+
type optionalKeyFrom<k extends PropertyKey, valueDef> = parseEntry<
|
|
102
|
+
k,
|
|
103
|
+
valueDef
|
|
104
|
+
> extends infer result extends EntryParseResult<"optional">
|
|
105
|
+
? result["innerKey"]
|
|
106
|
+
: never
|
|
107
|
+
|
|
108
|
+
declare const indexParseSymbol: unique symbol
|
|
109
|
+
|
|
110
|
+
export type indexParseError<message extends string = string> = {
|
|
111
|
+
[indexParseSymbol]: message
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export const writeInvalidPropertyKeyMessage = <indexDef extends string>(
|
|
115
|
+
indexDef: indexDef
|
|
116
|
+
): writeInvalidPropertyKeyMessage<indexDef> =>
|
|
117
|
+
`Indexed key definition '${indexDef}' must be a string, number or symbol`
|
|
118
|
+
|
|
119
|
+
type writeInvalidPropertyKeyMessage<indexDef extends string> =
|
|
120
|
+
`Indexed key definition '${indexDef}' must be a string, number or symbol`
|