arktype 1.0.28-alpha → 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 -0
- package/dist/main.d.ts.map +1 -0
- package/dist/main.js +8 -0
- 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/{mjs/scopes/validation → scopes/utils}/creditCard.js +14 -12
- package/dist/scopes/utils/creditCard.js.map +1 -0
- package/dist/{types/scopes/validation → scopes/utils}/date.d.ts +3 -2
- package/dist/scopes/utils/date.d.ts.map +1 -0
- package/dist/{mjs/scopes/validation → scopes/utils}/date.js +24 -19
- 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 +41 -108
- 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/LICENSE +0 -7
- package/README.md +0 -351
- package/dist/cjs/main.js +0 -38
- package/dist/cjs/nodes/branch.js +0 -140
- package/dist/cjs/nodes/compose.js +0 -163
- package/dist/cjs/nodes/discriminate.js +0 -265
- package/dist/cjs/nodes/node.js +0 -139
- package/dist/cjs/nodes/predicate.js +0 -86
- package/dist/cjs/nodes/rules/class.js +0 -25
- package/dist/cjs/nodes/rules/collapsibleSet.js +0 -48
- package/dist/cjs/nodes/rules/divisor.js +0 -29
- package/dist/cjs/nodes/rules/props.js +0 -167
- package/dist/cjs/nodes/rules/range.js +0 -105
- package/dist/cjs/nodes/rules/regex.js +0 -26
- package/dist/cjs/nodes/rules/rules.js +0 -123
- package/dist/cjs/package.json +0 -3
- package/dist/cjs/parse/ast/ast.js +0 -4
- package/dist/cjs/parse/ast/bound.js +0 -16
- package/dist/cjs/parse/ast/config.js +0 -13
- package/dist/cjs/parse/ast/distributableFunction.js +0 -48
- package/dist/cjs/parse/ast/divisor.js +0 -9
- package/dist/cjs/parse/ast/intersection.js +0 -30
- package/dist/cjs/parse/ast/keyof.js +0 -111
- package/dist/cjs/parse/ast/morph.js +0 -58
- package/dist/cjs/parse/ast/narrow.js +0 -22
- package/dist/cjs/parse/ast/node.js +0 -4
- package/dist/cjs/parse/ast/tuple.js +0 -94
- package/dist/cjs/parse/ast/union.js +0 -9
- package/dist/cjs/parse/ast/utils.js +0 -4
- package/dist/cjs/parse/definition.js +0 -61
- package/dist/cjs/parse/record.js +0 -37
- package/dist/cjs/parse/string/reduce/dynamic.js +0 -167
- package/dist/cjs/parse/string/reduce/shared.js +0 -23
- package/dist/cjs/parse/string/reduce/static.js +0 -4
- package/dist/cjs/parse/string/shift/operand/enclosed.js +0 -54
- package/dist/cjs/parse/string/shift/operand/operand.js +0 -11
- package/dist/cjs/parse/string/shift/operand/unenclosed.js +0 -53
- package/dist/cjs/parse/string/shift/operator/bounds.js +0 -87
- package/dist/cjs/parse/string/shift/operator/divisor.js +0 -35
- package/dist/cjs/parse/string/shift/operator/operator.js +0 -26
- package/dist/cjs/parse/string/shift/scanner.js +0 -114
- package/dist/cjs/parse/string/string.js +0 -43
- package/dist/cjs/scopes/ark.js +0 -45
- package/dist/cjs/scopes/cache.js +0 -52
- package/dist/cjs/scopes/expressions.js +0 -29
- package/dist/cjs/scopes/jsObjects.js +0 -93
- package/dist/cjs/scopes/scope.js +0 -323
- package/dist/cjs/scopes/tsKeywords.js +0 -113
- package/dist/cjs/scopes/type.js +0 -50
- package/dist/cjs/scopes/validation/creditCard.js +0 -47
- package/dist/cjs/scopes/validation/date.js +0 -62
- package/dist/cjs/scopes/validation/validation.js +0 -108
- package/dist/cjs/traverse/problems.js +0 -281
- package/dist/cjs/traverse/traverse.js +0 -390
- package/dist/cjs/utils/chainableNoOpProxy.js +0 -23
- package/dist/cjs/utils/data.js +0 -53
- package/dist/cjs/utils/domains.js +0 -30
- package/dist/cjs/utils/errors.js +0 -26
- package/dist/cjs/utils/freeze.js +0 -15
- package/dist/cjs/utils/generics.js +0 -63
- package/dist/cjs/utils/numericLiterals.js +0 -75
- package/dist/cjs/utils/objectKinds.js +0 -69
- package/dist/cjs/utils/paths.js +0 -32
- package/dist/cjs/utils/serialize.js +0 -74
- package/dist/cjs/utils/unionToTuple.js +0 -4
- package/dist/mjs/main.js +0 -8
- package/dist/mjs/nodes/branch.js +0 -123
- package/dist/mjs/nodes/compose.js +0 -141
- package/dist/mjs/nodes/discriminate.js +0 -250
- package/dist/mjs/nodes/node.js +0 -117
- package/dist/mjs/nodes/predicate.js +0 -69
- package/dist/mjs/nodes/rules/class.js +0 -11
- package/dist/mjs/nodes/rules/collapsibleSet.js +0 -34
- package/dist/mjs/nodes/rules/divisor.js +0 -15
- package/dist/mjs/nodes/rules/props.js +0 -149
- package/dist/mjs/nodes/rules/range.js +0 -86
- package/dist/mjs/nodes/rules/regex.js +0 -11
- package/dist/mjs/nodes/rules/rules.js +0 -106
- package/dist/mjs/package.json +0 -3
- package/dist/mjs/parse/ast/ast.js +0 -1
- package/dist/mjs/parse/ast/bound.js +0 -2
- package/dist/mjs/parse/ast/config.js +0 -9
- package/dist/mjs/parse/ast/distributableFunction.js +0 -34
- package/dist/mjs/parse/ast/divisor.js +0 -1
- package/dist/mjs/parse/ast/intersection.js +0 -16
- package/dist/mjs/parse/ast/keyof.js +0 -103
- package/dist/mjs/parse/ast/morph.js +0 -44
- package/dist/mjs/parse/ast/narrow.js +0 -14
- package/dist/mjs/parse/ast/node.js +0 -1
- package/dist/mjs/parse/ast/tuple.js +0 -80
- package/dist/mjs/parse/ast/union.js +0 -1
- package/dist/mjs/parse/ast/utils.js +0 -1
- package/dist/mjs/parse/definition.js +0 -46
- package/dist/mjs/parse/record.js +0 -29
- package/dist/mjs/parse/string/reduce/dynamic.js +0 -159
- package/dist/mjs/parse/string/reduce/shared.js +0 -6
- package/dist/mjs/parse/string/reduce/static.js +0 -1
- package/dist/mjs/parse/string/shift/operand/enclosed.js +0 -39
- package/dist/mjs/parse/string/shift/operand/operand.js +0 -3
- package/dist/mjs/parse/string/shift/operand/unenclosed.js +0 -36
- package/dist/mjs/parse/string/shift/operator/bounds.js +0 -70
- package/dist/mjs/parse/string/shift/operator/divisor.js +0 -21
- package/dist/mjs/parse/string/shift/operator/operator.js +0 -13
- package/dist/mjs/parse/string/shift/scanner.js +0 -106
- package/dist/mjs/parse/string/string.js +0 -28
- package/dist/mjs/scopes/ark.js +0 -28
- package/dist/mjs/scopes/cache.js +0 -38
- package/dist/mjs/scopes/expressions.js +0 -89
- package/dist/mjs/scopes/jsObjects.js +0 -82
- package/dist/mjs/scopes/scope.js +0 -303
- package/dist/mjs/scopes/tsKeywords.js +0 -116
- package/dist/mjs/scopes/type.js +0 -35
- package/dist/mjs/scopes/validation/validation.js +0 -112
- package/dist/mjs/traverse/problems.js +0 -260
- package/dist/mjs/traverse/traverse.js +0 -373
- package/dist/mjs/utils/chainableNoOpProxy.js +0 -15
- package/dist/mjs/utils/data.js +0 -38
- package/dist/mjs/utils/domains.js +0 -15
- package/dist/mjs/utils/errors.js +0 -10
- package/dist/mjs/utils/freeze.js +0 -7
- package/dist/mjs/utils/generics.js +0 -38
- package/dist/mjs/utils/numericLiterals.js +0 -72
- package/dist/mjs/utils/objectKinds.js +0 -55
- package/dist/mjs/utils/paths.js +0 -18
- package/dist/mjs/utils/serialize.js +0 -58
- package/dist/mjs/utils/unionToTuple.js +0 -1
- package/dist/tsconfig.temp.tsbuildinfo +0 -1
- package/dist/types/main.d.ts +0 -15
- package/dist/types/nodes/branch.d.ts +0 -29
- package/dist/types/nodes/compose.d.ts +0 -98
- package/dist/types/nodes/discriminate.d.ts +0 -26
- package/dist/types/nodes/node.d.ts +0 -69
- package/dist/types/nodes/predicate.d.ts +0 -18
- package/dist/types/nodes/rules/class.d.ts +0 -4
- package/dist/types/nodes/rules/collapsibleSet.d.ts +0 -4
- package/dist/types/nodes/rules/divisor.d.ts +0 -3
- package/dist/types/nodes/rules/props.d.ts +0 -43
- package/dist/types/nodes/rules/range.d.ts +0 -34
- package/dist/types/nodes/rules/regex.d.ts +0 -5
- package/dist/types/nodes/rules/rules.d.ts +0 -33
- package/dist/types/parse/ast/ast.d.ts +0 -24
- package/dist/types/parse/ast/bound.d.ts +0 -27
- package/dist/types/parse/ast/config.d.ts +0 -15
- package/dist/types/parse/ast/distributableFunction.d.ts +0 -18
- package/dist/types/parse/ast/divisor.d.ts +0 -13
- package/dist/types/parse/ast/intersection.d.ts +0 -31
- package/dist/types/parse/ast/keyof.d.ts +0 -10
- package/dist/types/parse/ast/morph.d.ts +0 -18
- package/dist/types/parse/ast/narrow.d.ts +0 -21
- package/dist/types/parse/ast/node.d.ts +0 -35
- package/dist/types/parse/ast/tuple.d.ts +0 -41
- package/dist/types/parse/ast/union.d.ts +0 -17
- package/dist/types/parse/ast/utils.d.ts +0 -7
- package/dist/types/parse/definition.d.ts +0 -30
- package/dist/types/parse/record.d.ts +0 -26
- package/dist/types/parse/string/reduce/dynamic.d.ts +0 -34
- package/dist/types/parse/string/reduce/shared.d.ts +0 -25
- package/dist/types/parse/string/reduce/static.d.ts +0 -130
- package/dist/types/parse/string/shift/operand/enclosed.d.ts +0 -28
- package/dist/types/parse/string/shift/operand/operand.d.ts +0 -9
- package/dist/types/parse/string/shift/operand/unenclosed.d.ts +0 -18
- package/dist/types/parse/string/shift/operator/bounds.d.ts +0 -21
- package/dist/types/parse/string/shift/operator/divisor.d.ts +0 -13
- package/dist/types/parse/string/shift/operator/operator.d.ts +0 -13
- package/dist/types/parse/string/shift/scanner.d.ts +0 -89
- package/dist/types/parse/string/string.d.ts +0 -25
- package/dist/types/scopes/ark.d.ts +0 -209
- package/dist/types/scopes/cache.d.ts +0 -14
- package/dist/types/scopes/expressions.d.ts +0 -136
- package/dist/types/scopes/jsObjects.d.ts +0 -26
- package/dist/types/scopes/scope.d.ts +0 -99
- package/dist/types/scopes/tsKeywords.d.ts +0 -50
- package/dist/types/scopes/type.d.ts +0 -67
- package/dist/types/scopes/validation/creditCard.d.ts +0 -2
- package/dist/types/scopes/validation/validation.d.ts +0 -49
- package/dist/types/traverse/problems.d.ts +0 -88
- package/dist/types/traverse/traverse.d.ts +0 -55
- package/dist/types/utils/chainableNoOpProxy.d.ts +0 -14
- package/dist/types/utils/data.d.ts +0 -13
- package/dist/types/utils/domains.d.ts +0 -32
- package/dist/types/utils/errors.d.ts +0 -6
- package/dist/types/utils/freeze.d.ts +0 -2
- package/dist/types/utils/generics.d.ts +0 -125
- package/dist/types/utils/numericLiterals.d.ts +0 -46
- package/dist/types/utils/objectKinds.d.ts +0 -84
- package/dist/types/utils/paths.d.ts +0 -9
- package/dist/types/utils/serialize.d.ts +0 -37
- package/dist/types/utils/unionToTuple.d.ts +0 -11
|
@@ -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`
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
NumericallyBoundable,
|
|
3
|
+
writeUnboundableMessage
|
|
4
|
+
} from "@arktype/schema"
|
|
5
|
+
import type { ErrorMessage } from "@arktype/util"
|
|
6
|
+
import type { DateLiteral } from "../string/shift/operand/date.js"
|
|
7
|
+
import type {
|
|
8
|
+
BoundKind,
|
|
9
|
+
Comparator,
|
|
10
|
+
InvertedComparators,
|
|
11
|
+
LimitLiteral,
|
|
12
|
+
writeInvalidLimitMessage
|
|
13
|
+
} from "../string/shift/operator/bounds.js"
|
|
14
|
+
import type { inferAst } from "./semantic.js"
|
|
15
|
+
import type { astToString } from "./utils.js"
|
|
16
|
+
import type { validateAst } from "./validate.js"
|
|
17
|
+
|
|
18
|
+
export type validateRange<
|
|
19
|
+
l,
|
|
20
|
+
comparator extends Comparator,
|
|
21
|
+
r,
|
|
22
|
+
$,
|
|
23
|
+
args
|
|
24
|
+
> = l extends LimitLiteral
|
|
25
|
+
? validateBound<r, InvertedComparators[comparator], l, "left", $, args>
|
|
26
|
+
: l extends [infer leftAst, Comparator, unknown]
|
|
27
|
+
? ErrorMessage<writeDoubleRightBoundMessage<astToString<leftAst>>>
|
|
28
|
+
: validateBound<l, comparator, r & LimitLiteral, "right", $, args>
|
|
29
|
+
|
|
30
|
+
export type validateBound<
|
|
31
|
+
boundedAst,
|
|
32
|
+
comparator extends Comparator,
|
|
33
|
+
limit extends LimitLiteral,
|
|
34
|
+
boundKind extends BoundKind,
|
|
35
|
+
$,
|
|
36
|
+
args
|
|
37
|
+
> = inferAst<boundedAst, $, args> extends infer bounded
|
|
38
|
+
? [bounded] extends [NumericallyBoundable]
|
|
39
|
+
? limit extends number
|
|
40
|
+
? validateAst<boundedAst, $, args>
|
|
41
|
+
: ErrorMessage<writeInvalidLimitMessage<comparator, limit, boundKind>>
|
|
42
|
+
: bounded extends Date
|
|
43
|
+
? limit extends DateLiteral
|
|
44
|
+
? validateAst<boundedAst, $, args>
|
|
45
|
+
: ErrorMessage<writeInvalidLimitMessage<comparator, limit, boundKind>>
|
|
46
|
+
: ErrorMessage<
|
|
47
|
+
writeUnboundableMessage<
|
|
48
|
+
astToString<
|
|
49
|
+
boundKind extends "left"
|
|
50
|
+
? boundedAst[0 & keyof boundedAst]
|
|
51
|
+
: boundedAst
|
|
52
|
+
>
|
|
53
|
+
>
|
|
54
|
+
>
|
|
55
|
+
: never
|
|
56
|
+
|
|
57
|
+
export const writeDoubleRightBoundMessage = <root extends string>(
|
|
58
|
+
root: root
|
|
59
|
+
): writeDoubleRightBoundMessage<root> =>
|
|
60
|
+
`Expression ${root} must have at most one right bound`
|
|
61
|
+
|
|
62
|
+
type writeDoubleRightBoundMessage<root extends string> =
|
|
63
|
+
`Expression ${root} must have at most one right bound`
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { writeIndivisibleMessage } from "@arktype/schema"
|
|
2
|
+
import type { ErrorMessage } from "@arktype/util"
|
|
3
|
+
import type { inferAst } from "./semantic.js"
|
|
4
|
+
import type { astToString } from "./utils.js"
|
|
5
|
+
import type { validateAst } from "./validate.js"
|
|
6
|
+
|
|
7
|
+
export type validateDivisor<l, $, args> = isDivisible<
|
|
8
|
+
inferAst<l, $, args>
|
|
9
|
+
> extends true
|
|
10
|
+
? validateAst<l, $, args>
|
|
11
|
+
: ErrorMessage<writeIndivisibleMessage<astToString<l>>>
|
|
12
|
+
|
|
13
|
+
type isDivisible<data> = [data] extends [number] ? true : false
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { Out } from "@arktype/schema"
|
|
2
|
+
import {
|
|
3
|
+
Hkt,
|
|
4
|
+
type List,
|
|
5
|
+
type conform,
|
|
6
|
+
type evaluate,
|
|
7
|
+
type intersectArrays,
|
|
8
|
+
type isAny
|
|
9
|
+
} from "@arktype/util"
|
|
10
|
+
import type { MorphAst } from "../tuple.js"
|
|
11
|
+
|
|
12
|
+
export type inferIntersection<l, r> = [l] extends [never]
|
|
13
|
+
? never
|
|
14
|
+
: [r] extends [never]
|
|
15
|
+
? never
|
|
16
|
+
: [l & r] extends [never]
|
|
17
|
+
? never
|
|
18
|
+
: isAny<l | r> extends true
|
|
19
|
+
? any
|
|
20
|
+
: l extends MorphAst<infer lIn, infer lOut>
|
|
21
|
+
? r extends MorphAst
|
|
22
|
+
? never
|
|
23
|
+
: (In: evaluate<lIn & r>) => Out<lOut>
|
|
24
|
+
: r extends MorphAst<infer rIn, infer rOut>
|
|
25
|
+
? (In: evaluate<rIn & l>) => Out<rOut>
|
|
26
|
+
: [l, r] extends [object, object]
|
|
27
|
+
? intersectObjects<l, r> extends infer result
|
|
28
|
+
? result
|
|
29
|
+
: never
|
|
30
|
+
: l & r
|
|
31
|
+
|
|
32
|
+
declare class MorphableIntersection extends Hkt.Kind {
|
|
33
|
+
f: (
|
|
34
|
+
In: conform<this[Hkt.key], [l: unknown, r: unknown]>
|
|
35
|
+
) => inferIntersection<(typeof In)[0], (typeof In)[1]>
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
type intersectObjects<l, r> = [l, r] extends [
|
|
39
|
+
infer lList extends List,
|
|
40
|
+
infer rList extends List
|
|
41
|
+
]
|
|
42
|
+
? intersectArrays<lList, rList, MorphableIntersection>
|
|
43
|
+
: evaluate<
|
|
44
|
+
{
|
|
45
|
+
[k in keyof l]: k extends keyof r ? inferIntersection<l[k], r[k]> : l[k]
|
|
46
|
+
} & Omit<r, keyof l>
|
|
47
|
+
>
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import type { BigintLiteral, List, NumberLiteral } from "@arktype/util"
|
|
2
|
+
import type {
|
|
3
|
+
UnparsedScope,
|
|
4
|
+
resolve,
|
|
5
|
+
tryInferSubmoduleReference
|
|
6
|
+
} from "../../scope.js"
|
|
7
|
+
import type { GenericProps } from "../../type.js"
|
|
8
|
+
import type { inferDefinition } from "../definition.js"
|
|
9
|
+
import type { DateLiteral } from "../string/shift/operand/date.js"
|
|
10
|
+
import type { StringLiteral } from "../string/shift/operand/enclosed.js"
|
|
11
|
+
import type {
|
|
12
|
+
Comparator,
|
|
13
|
+
LimitLiteral
|
|
14
|
+
} from "../string/shift/operator/bounds.js"
|
|
15
|
+
import type { inferIntersection } from "./intersections.js"
|
|
16
|
+
|
|
17
|
+
export type inferAst<ast, $, args> = ast extends List
|
|
18
|
+
? inferExpression<ast, $, args>
|
|
19
|
+
: inferTerminal<ast, $, args>
|
|
20
|
+
|
|
21
|
+
export type GenericInstantiationAst<
|
|
22
|
+
g extends GenericProps = GenericProps,
|
|
23
|
+
argAsts extends unknown[] = unknown[]
|
|
24
|
+
> = [g, "<>", argAsts]
|
|
25
|
+
|
|
26
|
+
export type inferExpression<
|
|
27
|
+
ast extends List,
|
|
28
|
+
$,
|
|
29
|
+
args
|
|
30
|
+
> = ast extends GenericInstantiationAst
|
|
31
|
+
? inferDefinition<
|
|
32
|
+
ast[0]["definition"],
|
|
33
|
+
ast[0]["$"] extends UnparsedScope
|
|
34
|
+
? // If the generic was defined in the current scope, its definition can be
|
|
35
|
+
// resolved using the same scope as that of the input args.
|
|
36
|
+
$
|
|
37
|
+
: // Otherwise, use the scope that was explicitly associated with it.
|
|
38
|
+
ast[0]["$"],
|
|
39
|
+
{
|
|
40
|
+
// Using keyof g["parameters"] & number here results in the element types
|
|
41
|
+
// being mixed- another reason TS should not have separate `${number}` and number keys!
|
|
42
|
+
[i in keyof ast[0]["parameters"] &
|
|
43
|
+
`${number}` as ast[0]["parameters"][i]]: inferAst<
|
|
44
|
+
ast[2][i & keyof ast[2]],
|
|
45
|
+
$,
|
|
46
|
+
args
|
|
47
|
+
>
|
|
48
|
+
}
|
|
49
|
+
>
|
|
50
|
+
: ast[1] extends "[]"
|
|
51
|
+
? inferAst<ast[0], $, args>[]
|
|
52
|
+
: ast[1] extends "|"
|
|
53
|
+
? inferAst<ast[0], $, args> | inferAst<ast[2], $, args>
|
|
54
|
+
: ast[1] extends "&"
|
|
55
|
+
? inferIntersection<
|
|
56
|
+
inferAst<ast[0], $, args>,
|
|
57
|
+
inferAst<ast[2], $, args>
|
|
58
|
+
>
|
|
59
|
+
: ast[1] extends Comparator
|
|
60
|
+
? ast[0] extends LimitLiteral
|
|
61
|
+
? inferAst<ast[2], $, args>
|
|
62
|
+
: inferAst<ast[0], $, args>
|
|
63
|
+
: ast[1] extends "%"
|
|
64
|
+
? inferAst<ast[0], $, args>
|
|
65
|
+
: ast[0] extends "keyof"
|
|
66
|
+
? keyof inferAst<ast[1], $, args>
|
|
67
|
+
: never
|
|
68
|
+
|
|
69
|
+
export type PrefixOperator = "keyof" | "instanceof" | "===" | "node"
|
|
70
|
+
|
|
71
|
+
export type PrefixExpression<
|
|
72
|
+
operator extends PrefixOperator = PrefixOperator,
|
|
73
|
+
operand = unknown
|
|
74
|
+
> = [operator, operand]
|
|
75
|
+
|
|
76
|
+
export type PostfixOperator = "[]"
|
|
77
|
+
|
|
78
|
+
export type PostfixExpression<
|
|
79
|
+
operator extends PostfixOperator = PostfixOperator,
|
|
80
|
+
operand = unknown
|
|
81
|
+
> = readonly [operand, operator]
|
|
82
|
+
|
|
83
|
+
export type InfixOperator = "|" | "&" | Comparator | "%" | ":" | "=>" | "@"
|
|
84
|
+
|
|
85
|
+
export type InfixExpression<
|
|
86
|
+
operator extends InfixOperator = InfixOperator,
|
|
87
|
+
l = unknown,
|
|
88
|
+
r = unknown
|
|
89
|
+
> = [l, operator, r]
|
|
90
|
+
|
|
91
|
+
export type RegexLiteral<source extends string = string> = `/${source}/`
|
|
92
|
+
|
|
93
|
+
export type inferTerminal<token, $, args> = token extends keyof args | keyof $
|
|
94
|
+
? resolve<token, $, args>
|
|
95
|
+
: token extends StringLiteral<infer Text>
|
|
96
|
+
? Text
|
|
97
|
+
: token extends RegexLiteral
|
|
98
|
+
? string
|
|
99
|
+
: token extends DateLiteral
|
|
100
|
+
? Date
|
|
101
|
+
: token extends NumberLiteral<infer value>
|
|
102
|
+
? value
|
|
103
|
+
: token extends BigintLiteral<infer value>
|
|
104
|
+
? value
|
|
105
|
+
: // doing this last allows us to infer never if it isn't valid rather than check
|
|
106
|
+
// if it's a valid submodule reference ahead of time
|
|
107
|
+
tryInferSubmoduleReference<$, token>
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { List, Stringifiable } from "@arktype/util"
|
|
2
|
+
import type { Comparator } from "../string/shift/operator/bounds.js"
|
|
3
|
+
import type { InfixExpression, PostfixExpression } from "./semantic.js"
|
|
4
|
+
|
|
5
|
+
export type astToString<ast> = `'${astToStringRecurse<ast>}'`
|
|
6
|
+
|
|
7
|
+
type astToStringRecurse<ast> = ast extends PostfixExpression<
|
|
8
|
+
infer operator,
|
|
9
|
+
infer operand
|
|
10
|
+
>
|
|
11
|
+
? operator extends "[]"
|
|
12
|
+
? `${groupAst<operand>}[]`
|
|
13
|
+
: never
|
|
14
|
+
: ast extends InfixExpression<infer operator, infer l, infer r>
|
|
15
|
+
? operator extends "&" | "|" | "%" | Comparator
|
|
16
|
+
? `${groupAst<l>}${operator}${groupAst<r>}`
|
|
17
|
+
: never
|
|
18
|
+
: ast extends Stringifiable
|
|
19
|
+
? `${ast extends bigint ? `${ast}n` : ast}`
|
|
20
|
+
: "..."
|
|
21
|
+
|
|
22
|
+
type groupAst<ast> = ast extends List
|
|
23
|
+
? ast[1] extends "[]"
|
|
24
|
+
? astToStringRecurse<ast>
|
|
25
|
+
: `(${astToStringRecurse<ast>})`
|
|
26
|
+
: astToStringRecurse<ast>
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
BigintLiteral,
|
|
3
|
+
Completion,
|
|
4
|
+
ErrorMessage,
|
|
5
|
+
NumberLiteral,
|
|
6
|
+
writeMalformedNumericLiteralMessage
|
|
7
|
+
} from "@arktype/util"
|
|
8
|
+
import type { Module } from "../../scope.js"
|
|
9
|
+
import type { GenericProps } from "../../type.js"
|
|
10
|
+
import type { writeInvalidGenericArgsMessage } from "../generic.js"
|
|
11
|
+
import type { writeMissingSubmoduleAccessMessage } from "../string/shift/operand/unenclosed.js"
|
|
12
|
+
import type { Comparator } from "../string/shift/operator/bounds.js"
|
|
13
|
+
import type { parseString } from "../string/string.js"
|
|
14
|
+
import type { validateRange } from "./bounds.js"
|
|
15
|
+
import type { validateDivisor } from "./divisor.js"
|
|
16
|
+
import type {
|
|
17
|
+
GenericInstantiationAst,
|
|
18
|
+
InfixExpression,
|
|
19
|
+
PostfixExpression
|
|
20
|
+
} from "./semantic.js"
|
|
21
|
+
import type { astToString } from "./utils.js"
|
|
22
|
+
|
|
23
|
+
export type validateAst<ast, $, args> = ast extends string
|
|
24
|
+
? validateStringAst<ast, $>
|
|
25
|
+
: ast extends PostfixExpression<infer operator, infer operand>
|
|
26
|
+
? operator extends "[]"
|
|
27
|
+
? validateAst<operand, $, args>
|
|
28
|
+
: never
|
|
29
|
+
: ast extends InfixExpression<infer operator, infer l, infer r>
|
|
30
|
+
? operator extends "&" | "|"
|
|
31
|
+
? validateInfix<ast, $, args>
|
|
32
|
+
: operator extends Comparator
|
|
33
|
+
? validateRange<l, operator, r, $, args>
|
|
34
|
+
: operator extends "%"
|
|
35
|
+
? validateDivisor<l, $, args>
|
|
36
|
+
: undefined
|
|
37
|
+
: ast extends readonly ["keyof", infer operand]
|
|
38
|
+
? validateAst<operand, $, args>
|
|
39
|
+
: ast extends GenericInstantiationAst
|
|
40
|
+
? validateGenericArgs<ast["2"], $, args>
|
|
41
|
+
: ErrorMessage<writeUnexpectedExpressionMessage<astToString<ast>>>
|
|
42
|
+
|
|
43
|
+
type writeUnexpectedExpressionMessage<expression extends string> =
|
|
44
|
+
`Unexpectedly failed to parse the expression resulting from ${expression}`
|
|
45
|
+
|
|
46
|
+
type validateGenericArgs<argAsts extends unknown[], $, args> = argAsts extends [
|
|
47
|
+
infer head,
|
|
48
|
+
...infer tail
|
|
49
|
+
]
|
|
50
|
+
? validateAst<head, $, args> extends ErrorMessage<infer message>
|
|
51
|
+
? ErrorMessage<message>
|
|
52
|
+
: validateGenericArgs<tail, $, args>
|
|
53
|
+
: undefined
|
|
54
|
+
|
|
55
|
+
export const writeUnsatisfiableExpressionError = <expression extends string>(
|
|
56
|
+
expression: expression
|
|
57
|
+
): writeUnsatisfiableExpressionError<expression> =>
|
|
58
|
+
`${expression} results in an unsatisfiable type`
|
|
59
|
+
|
|
60
|
+
export type writeUnsatisfiableExpressionError<expression extends string> =
|
|
61
|
+
`${expression} results in an unsatisfiable type`
|
|
62
|
+
|
|
63
|
+
type validateStringAst<def extends string, $> = def extends NumberLiteral<
|
|
64
|
+
infer value
|
|
65
|
+
>
|
|
66
|
+
? number extends value
|
|
67
|
+
? ErrorMessage<writeMalformedNumericLiteralMessage<def, "number">>
|
|
68
|
+
: undefined
|
|
69
|
+
: def extends BigintLiteral<infer value>
|
|
70
|
+
? bigint extends value
|
|
71
|
+
? ErrorMessage<writeMalformedNumericLiteralMessage<def, "bigint">>
|
|
72
|
+
: undefined
|
|
73
|
+
: def extends keyof $
|
|
74
|
+
? // these problems would've been caught during a fullStringParse, but it's most
|
|
75
|
+
// efficient to check for them here in case the string was naively parsed
|
|
76
|
+
$[def] extends GenericProps
|
|
77
|
+
? ErrorMessage<
|
|
78
|
+
writeInvalidGenericArgsMessage<def, $[def]["parameters"], []>
|
|
79
|
+
>
|
|
80
|
+
: $[def] extends Module
|
|
81
|
+
? ErrorMessage<writeMissingSubmoduleAccessMessage<def>>
|
|
82
|
+
: undefined
|
|
83
|
+
: def extends ErrorMessage
|
|
84
|
+
? def
|
|
85
|
+
: undefined
|
|
86
|
+
|
|
87
|
+
export type validateString<def extends string, $, args> = validateAst<
|
|
88
|
+
parseString<def, $, args>,
|
|
89
|
+
$,
|
|
90
|
+
args
|
|
91
|
+
> extends infer result extends ErrorMessage
|
|
92
|
+
? result extends Completion<infer text>
|
|
93
|
+
? text
|
|
94
|
+
: result
|
|
95
|
+
: def
|
|
96
|
+
|
|
97
|
+
type validateInfix<ast extends InfixExpression, $, args> = validateAst<
|
|
98
|
+
ast[0],
|
|
99
|
+
$,
|
|
100
|
+
args
|
|
101
|
+
> extends ErrorMessage<infer message>
|
|
102
|
+
? ErrorMessage<message>
|
|
103
|
+
: validateAst<ast[2], $, args> extends ErrorMessage<infer message>
|
|
104
|
+
? ErrorMessage<message>
|
|
105
|
+
: undefined
|
package/parser/shared.ts
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import type { extend } from "@arktype/util"
|
|
2
|
+
import type { validateDefinition } from "./definition.js"
|
|
3
|
+
import { Scanner } from "./string/shift/scanner.js"
|
|
4
|
+
|
|
5
|
+
type parsedKey<result extends KeyParseResult> = result
|
|
6
|
+
|
|
7
|
+
type OptionalTuple<value> = readonly [value, "?"]
|
|
8
|
+
|
|
9
|
+
type KeyParseResult<kind extends ParsedKeyKind = ParsedKeyKind> = {
|
|
10
|
+
kind: kind
|
|
11
|
+
innerKey: string | symbol
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
type ValueParseResult<kind extends ParsedValueKind = ParsedValueKind> = {
|
|
15
|
+
kind: kind
|
|
16
|
+
innerValue: unknown
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export type EntryParseResult<kind extends ParsedKeyKind = ParsedKeyKind> =
|
|
20
|
+
extend<
|
|
21
|
+
KeyParseResult<kind>,
|
|
22
|
+
{
|
|
23
|
+
innerValue: unknown
|
|
24
|
+
}
|
|
25
|
+
>
|
|
26
|
+
|
|
27
|
+
type ParsedValueKind = "required" | "optional"
|
|
28
|
+
|
|
29
|
+
type ParsedKeyKind = "required" | "optional" | "indexed"
|
|
30
|
+
|
|
31
|
+
type parsedEntry<result extends EntryParseResult> = result
|
|
32
|
+
|
|
33
|
+
export type OptionalValue<value> =
|
|
34
|
+
| OptionalStringDefinition<value & string>
|
|
35
|
+
| OptionalTuple<value>
|
|
36
|
+
|
|
37
|
+
export type OptionalStringDefinition<name extends string = string> = `${name}?`
|
|
38
|
+
|
|
39
|
+
export type IndexedKey<def extends string = string> = `[${def}]`
|
|
40
|
+
|
|
41
|
+
export type validateObjectValue<def, $, args> = def extends OptionalTuple<
|
|
42
|
+
infer value
|
|
43
|
+
>
|
|
44
|
+
? readonly [validateObjectValueString<value, $, args>, "?"]
|
|
45
|
+
: validateObjectValueString<def, $, args>
|
|
46
|
+
|
|
47
|
+
type validateObjectValueString<def, $, args> =
|
|
48
|
+
def extends OptionalStringDefinition<infer innerValue>
|
|
49
|
+
? `${validateDefinition<innerValue, $, args>}?`
|
|
50
|
+
: validateDefinition<def, $, args>
|
|
51
|
+
|
|
52
|
+
type DefinitionEntry = readonly [string | symbol, unknown]
|
|
53
|
+
|
|
54
|
+
const getInnerValue = (value: unknown): ValueParseResult => {
|
|
55
|
+
if (typeof value === "string") {
|
|
56
|
+
if (value[value.length - 1] === "?") {
|
|
57
|
+
return {
|
|
58
|
+
kind: "optional",
|
|
59
|
+
innerValue: value.slice(0, -1)
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
} else if (Array.isArray(value)) {
|
|
63
|
+
if (value.length === 2 && value[1] === "?") {
|
|
64
|
+
return {
|
|
65
|
+
kind: "optional",
|
|
66
|
+
innerValue: getInnerValue(value[0]).innerValue
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return {
|
|
71
|
+
kind: "required",
|
|
72
|
+
innerValue: value
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export const parseEntry = ([key, value]: DefinitionEntry): EntryParseResult => {
|
|
77
|
+
const keyParseResult: KeyParseResult =
|
|
78
|
+
typeof key === "string" && key.at(-1) === "?"
|
|
79
|
+
? key.at(-2) === Scanner.escapeToken
|
|
80
|
+
? { innerKey: `${key.slice(0, -2)}?`, kind: "required" }
|
|
81
|
+
: { innerKey: key.slice(0, -1), kind: "optional" }
|
|
82
|
+
: { innerKey: key, kind: "required" }
|
|
83
|
+
const valueParseResult = getInnerValue(value)
|
|
84
|
+
return {
|
|
85
|
+
innerKey: keyParseResult.innerKey,
|
|
86
|
+
innerValue: valueParseResult.innerValue,
|
|
87
|
+
kind:
|
|
88
|
+
keyParseResult.kind === "indexed"
|
|
89
|
+
? "indexed"
|
|
90
|
+
: valueParseResult.kind === "optional"
|
|
91
|
+
? "optional"
|
|
92
|
+
: keyParseResult.kind
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export type parseEntry<
|
|
97
|
+
keyDef extends PropertyKey,
|
|
98
|
+
valueDef
|
|
99
|
+
> = parseKey<keyDef> extends infer keyParseResult extends KeyParseResult
|
|
100
|
+
? valueDef extends OptionalValue<infer innerValue>
|
|
101
|
+
? parsedEntry<{
|
|
102
|
+
kind: "optional"
|
|
103
|
+
innerKey: keyParseResult["innerKey"]
|
|
104
|
+
innerValue: innerValue extends OptionalStringDefinition<
|
|
105
|
+
infer innerStringValue
|
|
106
|
+
>
|
|
107
|
+
? innerStringValue
|
|
108
|
+
: innerValue
|
|
109
|
+
}>
|
|
110
|
+
: parsedEntry<{
|
|
111
|
+
kind: keyParseResult["kind"]
|
|
112
|
+
innerKey: keyParseResult["innerKey"]
|
|
113
|
+
innerValue: valueDef
|
|
114
|
+
}>
|
|
115
|
+
: never
|
|
116
|
+
|
|
117
|
+
type parseKey<k> = k extends OptionalStringDefinition<infer inner>
|
|
118
|
+
? inner extends `${infer baseName}${Scanner.EscapeToken}`
|
|
119
|
+
? parsedKey<{
|
|
120
|
+
kind: "required"
|
|
121
|
+
innerKey: OptionalStringDefinition<baseName>
|
|
122
|
+
}>
|
|
123
|
+
: parsedKey<{
|
|
124
|
+
kind: "optional"
|
|
125
|
+
innerKey: inner
|
|
126
|
+
}>
|
|
127
|
+
: k extends IndexedKey<infer def>
|
|
128
|
+
? parsedKey<{
|
|
129
|
+
kind: "indexed"
|
|
130
|
+
innerKey: def
|
|
131
|
+
}>
|
|
132
|
+
: k extends `${Scanner.EscapeToken}${infer escapedIndexKey extends
|
|
133
|
+
IndexedKey}`
|
|
134
|
+
? parsedKey<{
|
|
135
|
+
kind: "required"
|
|
136
|
+
innerKey: escapedIndexKey
|
|
137
|
+
}>
|
|
138
|
+
: parsedKey<{
|
|
139
|
+
kind: "required"
|
|
140
|
+
innerKey: k & (string | symbol)
|
|
141
|
+
}>
|