arktype 2.1.6 → 2.1.8
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/generic.d.ts +27 -67
- package/out/keywords/string.d.ts +2 -0
- package/out/keywords/string.js +2 -0
- package/out/match.d.ts +44 -16
- package/out/match.js +42 -24
- package/out/methods/base.d.ts +2 -2
- package/package.json +3 -3
package/out/generic.d.ts
CHANGED
|
@@ -14,83 +14,43 @@ export type extractParams<s extends ParameterString> = s extends ParameterString
|
|
|
14
14
|
export type validateParameterString<s extends ParameterString, $> = parseGenericParams<extractParams<s>, $> extends infer e extends ErrorMessage ? e : s;
|
|
15
15
|
export type validateGenericArg<arg, param extends GenericParamAst, $> = type.infer<arg, $> extends param[1] ? unknown : ErrorType<`Invalid argument for ${param[0]}`, [expected: param[1]]>;
|
|
16
16
|
export type GenericInstantiator<params extends array<GenericParamAst>, def, $, args$> = params["length"] extends 1 ? {
|
|
17
|
-
<const a, r = instantiateGeneric<def, params, [a], $, args$>>(a: type.validate<a, args$> & validateGenericArg<a, params[0], args$>): r;
|
|
17
|
+
<const a, r = instantiateGeneric<def, params, [a], $, args$>>(a: type.validate<a, args$> & validateGenericArg<a, params[0], args$>): r extends infer _ ? _ : never;
|
|
18
18
|
} : params["length"] extends 2 ? {
|
|
19
19
|
<const a, const b, r = instantiateGeneric<def, params, [a, b], $, args$>>(...args: [
|
|
20
20
|
type.validate<a, args$> & validateGenericArg<a, params[0], args$>,
|
|
21
21
|
type.validate<b, args$> & validateGenericArg<b, params[1], args$>
|
|
22
|
-
]): r;
|
|
22
|
+
]): r extends infer _ ? _ : never;
|
|
23
23
|
} : params["length"] extends 3 ? {
|
|
24
24
|
<const a, const b, const c, r = instantiateGeneric<def, params, [a, b, c], $, args$>>(...args: [
|
|
25
|
-
type.validate<a, args$>,
|
|
26
|
-
type.validate<b, args$>,
|
|
27
|
-
type.validate<c, args$>
|
|
28
|
-
]
|
|
29
|
-
validateGenericArg<a, params[0], args$>,
|
|
30
|
-
validateGenericArg<b, params[1], args$>,
|
|
31
|
-
validateGenericArg<c, params[2], args$>
|
|
32
|
-
]): r;
|
|
25
|
+
type.validate<a, args$> & validateGenericArg<a, params[0], args$>,
|
|
26
|
+
type.validate<b, args$> & validateGenericArg<b, params[1], args$>,
|
|
27
|
+
type.validate<c, args$> & validateGenericArg<c, params[2], args$>
|
|
28
|
+
]): r extends infer _ ? _ : never;
|
|
33
29
|
} : params["length"] extends 4 ? {
|
|
34
30
|
<const a, const b, const c, const d, r = instantiateGeneric<def, params, [a, b, c, d], $, args$>>(...args: [
|
|
35
|
-
type.validate<a, args$>,
|
|
36
|
-
type.validate<b, args$>,
|
|
37
|
-
type.validate<c, args$>,
|
|
38
|
-
type.validate<d, args$>
|
|
39
|
-
]
|
|
40
|
-
validateGenericArg<a, params[0], args$>,
|
|
41
|
-
validateGenericArg<b, params[1], args$>,
|
|
42
|
-
validateGenericArg<c, params[2], args$>,
|
|
43
|
-
validateGenericArg<d, params[3], args$>
|
|
44
|
-
]): r;
|
|
45
|
-
} : params["length"] extends 4 ? {
|
|
46
|
-
<const a, const b, const c, const d, const e, r = instantiateGeneric<def, params, [a, b, c, d, e], $, args$>>(...args: [
|
|
47
|
-
type.validate<a, args$>,
|
|
48
|
-
type.validate<b, args$>,
|
|
49
|
-
type.validate<c, args$>,
|
|
50
|
-
type.validate<d, args$>,
|
|
51
|
-
type.validate<e, args$>
|
|
52
|
-
] & [
|
|
53
|
-
validateGenericArg<a, params[0], args$>,
|
|
54
|
-
validateGenericArg<b, params[1], args$>,
|
|
55
|
-
validateGenericArg<c, params[2], args$>,
|
|
56
|
-
validateGenericArg<d, params[3], args$>,
|
|
57
|
-
validateGenericArg<e, params[4], args$>
|
|
58
|
-
]): r;
|
|
31
|
+
type.validate<a, args$> & validateGenericArg<a, params[0], args$>,
|
|
32
|
+
type.validate<b, args$> & validateGenericArg<b, params[1], args$>,
|
|
33
|
+
type.validate<c, args$> & validateGenericArg<c, params[2], args$>,
|
|
34
|
+
type.validate<d, args$> & validateGenericArg<d, params[3], args$>
|
|
35
|
+
]): r extends infer _ ? _ : never;
|
|
59
36
|
} : params["length"] extends 5 ? {
|
|
60
|
-
<const a, const b, const c, const d, const e,
|
|
61
|
-
type.validate<a, args$>,
|
|
62
|
-
type.validate<b, args$>,
|
|
63
|
-
type.validate<c, args$>,
|
|
64
|
-
type.validate<d, args$>,
|
|
65
|
-
type.validate<e, args
|
|
66
|
-
|
|
67
|
-
] & [
|
|
68
|
-
validateGenericArg<a, params[0], args$>,
|
|
69
|
-
validateGenericArg<b, params[1], args$>,
|
|
70
|
-
validateGenericArg<c, params[2], args$>,
|
|
71
|
-
validateGenericArg<d, params[3], args$>,
|
|
72
|
-
validateGenericArg<e, params[4], args$>,
|
|
73
|
-
validateGenericArg<f, params[5], args$>
|
|
74
|
-
]): r;
|
|
37
|
+
<const a, const b, const c, const d, const e, r = instantiateGeneric<def, params, [a, b, c, d, e], $, args$>>(...args: [
|
|
38
|
+
type.validate<a, args$> & validateGenericArg<a, params[0], args$>,
|
|
39
|
+
type.validate<b, args$> & validateGenericArg<b, params[1], args$>,
|
|
40
|
+
type.validate<c, args$> & validateGenericArg<c, params[2], args$>,
|
|
41
|
+
type.validate<d, args$> & validateGenericArg<d, params[3], args$>,
|
|
42
|
+
type.validate<e, args$> & validateGenericArg<e, params[4], args$>
|
|
43
|
+
]): r extends infer _ ? _ : never;
|
|
75
44
|
} : params["length"] extends 6 ? {
|
|
76
|
-
<const a, const b, const c, const d, const e, const f,
|
|
77
|
-
type.validate<a, args$>,
|
|
78
|
-
type.validate<b, args$>,
|
|
79
|
-
type.validate<c, args$>,
|
|
80
|
-
type.validate<d, args$>,
|
|
81
|
-
type.validate<e, args$>,
|
|
82
|
-
type.validate<f, args
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
validateGenericArg<a, params[0], args$>,
|
|
86
|
-
validateGenericArg<b, params[1], args$>,
|
|
87
|
-
validateGenericArg<c, params[2], args$>,
|
|
88
|
-
validateGenericArg<d, params[3], args$>,
|
|
89
|
-
validateGenericArg<e, params[4], args$>,
|
|
90
|
-
validateGenericArg<f, params[5], args$>,
|
|
91
|
-
validateGenericArg<g, params[6], args$>
|
|
92
|
-
]): r;
|
|
93
|
-
} : (error: ErrorMessage<`You may not define more than 7 positional generic parameters`>) => never;
|
|
45
|
+
<const a, const b, const c, const d, const e, const f, r = instantiateGeneric<def, params, [a, b, c, d, e, f], $, args$>>(...args: [
|
|
46
|
+
type.validate<a, args$> & validateGenericArg<a, params[0], args$>,
|
|
47
|
+
type.validate<b, args$> & validateGenericArg<b, params[1], args$>,
|
|
48
|
+
type.validate<c, args$> & validateGenericArg<c, params[2], args$>,
|
|
49
|
+
type.validate<d, args$> & validateGenericArg<d, params[3], args$>,
|
|
50
|
+
type.validate<e, args$> & validateGenericArg<e, params[4], args$>,
|
|
51
|
+
type.validate<f, args$> & validateGenericArg<f, params[5], args$>
|
|
52
|
+
]): r extends infer _ ? _ : never;
|
|
53
|
+
} : (error: ErrorMessage<`You may not define more than 6 positional generic parameters`>) => never;
|
|
94
54
|
type instantiateGeneric<def, params extends array<GenericParamAst>, args, $, args$> = Type<[
|
|
95
55
|
def
|
|
96
56
|
] extends [Hkt] ? Hkt.apply<def, {
|
package/out/keywords/string.d.ts
CHANGED
|
@@ -252,6 +252,7 @@ export declare const string: Module<{
|
|
|
252
252
|
root: unknown;
|
|
253
253
|
alpha: unknown;
|
|
254
254
|
alphanumeric: unknown;
|
|
255
|
+
hex: unknown;
|
|
255
256
|
base64: Submodule<{
|
|
256
257
|
root: unknown;
|
|
257
258
|
url: unknown;
|
|
@@ -308,6 +309,7 @@ export declare namespace string {
|
|
|
308
309
|
root: string;
|
|
309
310
|
alpha: string;
|
|
310
311
|
alphanumeric: string;
|
|
312
|
+
hex: string;
|
|
311
313
|
base64: base64.submodule;
|
|
312
314
|
capitalize: capitalize.submodule;
|
|
313
315
|
creditCard: string;
|
package/out/keywords/string.js
CHANGED
|
@@ -25,6 +25,7 @@ export const stringInteger = Scope.module({
|
|
|
25
25
|
}, {
|
|
26
26
|
name: "string.integer"
|
|
27
27
|
});
|
|
28
|
+
const hex = regexStringNode(/^[0-9a-fA-F]+$/, "hex characters only");
|
|
28
29
|
const base64 = Scope.module({
|
|
29
30
|
root: regexStringNode(/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/, "base64-encoded"),
|
|
30
31
|
url: regexStringNode(/^(?:[A-Za-z0-9_-]{4})*(?:[A-Za-z0-9_-]{2}(?:==|%3D%3D)?|[A-Za-z0-9_-]{3}(?:=|%3D)?)?$/, "base64url-encoded")
|
|
@@ -418,6 +419,7 @@ export const string = Scope.module({
|
|
|
418
419
|
root: intrinsic.string,
|
|
419
420
|
alpha: regexStringNode(/^[A-Za-z]*$/, "only letters"),
|
|
420
421
|
alphanumeric: regexStringNode(/^[A-Za-z\d]*$/, "only letters and digits 0-9"),
|
|
422
|
+
hex,
|
|
421
423
|
base64,
|
|
422
424
|
capitalize,
|
|
423
425
|
creditCard,
|
package/out/match.d.ts
CHANGED
|
@@ -9,29 +9,32 @@ type MatchParserContext<input = unknown> = {
|
|
|
9
9
|
cases: Morph[];
|
|
10
10
|
$: unknown;
|
|
11
11
|
input: input;
|
|
12
|
+
checked: boolean;
|
|
12
13
|
key: PropertyKey | null;
|
|
13
14
|
};
|
|
14
15
|
declare namespace ctx {
|
|
15
16
|
type from<ctx extends MatchParserContext> = ctx;
|
|
16
|
-
type init<$, input = unknown> = from<{
|
|
17
|
+
type init<$, input = unknown, checked extends boolean = false> = from<{
|
|
17
18
|
cases: [];
|
|
18
19
|
$: $;
|
|
19
20
|
input: input;
|
|
21
|
+
checked: checked;
|
|
20
22
|
key: null;
|
|
21
23
|
}>;
|
|
22
24
|
type atKey<ctx extends MatchParserContext, key extends string> = from<{
|
|
23
25
|
cases: ctx["cases"];
|
|
24
26
|
$: ctx["$"];
|
|
25
27
|
input: ctx["input"];
|
|
28
|
+
checked: ctx["checked"];
|
|
26
29
|
key: key;
|
|
27
30
|
}>;
|
|
28
31
|
}
|
|
29
32
|
export interface MatchParser<$> extends CaseMatchParser<ctx.init<$>> {
|
|
30
|
-
in<const def>(def: type.validate<def, $>): ChainableMatchParser<ctx.init<$, type.infer<def,
|
|
33
|
+
in<const def>(def: type.validate<def, $>): ChainableMatchParser<ctx.init<$, type.infer<def, $>, true>>;
|
|
31
34
|
in<const typedInput = never>(...args: [typedInput] extends [never] ? [
|
|
32
|
-
ErrorMessage<"
|
|
35
|
+
ErrorMessage<"in requires a definition or type argument (in('string') or in<string>())">
|
|
33
36
|
] : []): ChainableMatchParser<ctx.init<$, typedInput>>;
|
|
34
|
-
in<const def>(def: type.validate<def, $>): ChainableMatchParser<ctx.init<$, type.infer<def,
|
|
37
|
+
in<const def>(def: type.validate<def, $>): ChainableMatchParser<ctx.init<$, type.infer<def, $>, true>>;
|
|
35
38
|
case: CaseParser<ctx.init<$>>;
|
|
36
39
|
at: AtParser<ctx.init<$>>;
|
|
37
40
|
}
|
|
@@ -39,38 +42,58 @@ type addCasesToContext<ctx extends MatchParserContext, cases extends unknown[]>
|
|
|
39
42
|
$: ctx["$"];
|
|
40
43
|
input: ctx["input"];
|
|
41
44
|
cases: [...ctx["cases"], ...cases];
|
|
45
|
+
checked: ctx["checked"];
|
|
42
46
|
key: ctx["key"];
|
|
43
47
|
}> : never;
|
|
44
48
|
type addDefaultToContext<ctx extends MatchParserContext, defaultCase extends DefaultCase<ctx>> = ctx.from<{
|
|
45
49
|
$: ctx["$"];
|
|
46
50
|
input: defaultCase extends "never" ? Morph.In<ctx["cases"][number]> : ctx["input"];
|
|
47
|
-
cases: defaultCase extends
|
|
51
|
+
cases: defaultCase extends "never" | "assert" ? ctx["cases"] : defaultCase extends Morph ? ctx["checked"] extends true ? [
|
|
52
|
+
(In: unknown) => ArkErrors,
|
|
53
|
+
...ctx["cases"],
|
|
54
|
+
defaultCase
|
|
55
|
+
] : [...ctx["cases"], defaultCase] : [
|
|
56
|
+
...ctx["cases"],
|
|
57
|
+
(In: ctx["input"]) => ArkErrors
|
|
58
|
+
];
|
|
59
|
+
checked: ctx["checked"];
|
|
48
60
|
key: ctx["key"];
|
|
49
61
|
}>;
|
|
50
|
-
type
|
|
51
|
-
|
|
62
|
+
type CaseKeyKind = "def" | "string";
|
|
63
|
+
type casesToMorphTuple<cases, ctx extends MatchParserContext, kind extends CaseKeyKind> = unionToTuple<propValueOf<{
|
|
64
|
+
[def in Exclude<keyof cases, "default">]: cases[def] extends (Morph<never, infer o>) ? kind extends "def" ? (In: inferCaseArg<def extends number ? `${number}` : def, ctx, "in">) => o : (In: maybeLiftToKey<def, ctx>) => o : never;
|
|
52
65
|
}>>;
|
|
53
|
-
type addCasesToParser<cases, ctx extends MatchParserContext> = cases extends {
|
|
66
|
+
type addCasesToParser<cases, ctx extends MatchParserContext, kind extends CaseKeyKind> = cases extends {
|
|
54
67
|
default: infer defaultDef extends DefaultCase<ctx>;
|
|
55
|
-
} ? finalizeMatchParser<addCasesToContext<ctx, casesToMorphTuple<cases, ctx>>, defaultDef> : ChainableMatchParser<addCasesToContext<ctx, casesToMorphTuple<cases, ctx>>>;
|
|
56
|
-
type inferCaseArg<def, ctx extends MatchParserContext, endpoint extends "in" | "out"> = _finalizeCaseArg<ctx["
|
|
57
|
-
|
|
58
|
-
|
|
68
|
+
} ? finalizeMatchParser<addCasesToContext<ctx, casesToMorphTuple<cases, ctx, kind>>, defaultDef> : ChainableMatchParser<addCasesToContext<ctx, casesToMorphTuple<cases, ctx, kind>>>;
|
|
69
|
+
type inferCaseArg<def, ctx extends MatchParserContext, endpoint extends "in" | "out"> = _finalizeCaseArg<maybeLiftToKey<type.infer<def, ctx["$"]>, ctx>, ctx, endpoint>;
|
|
70
|
+
type maybeLiftToKey<t, ctx extends MatchParserContext> = ctx["key"] extends PropertyKey ? {
|
|
71
|
+
[k in ctx["key"]]: t;
|
|
72
|
+
} : t;
|
|
59
73
|
type _finalizeCaseArg<t, ctx extends MatchParserContext, endpoint extends "in" | "out"> = [
|
|
60
74
|
distill<t, "in">,
|
|
61
75
|
distill<t, endpoint>
|
|
62
76
|
] extends [infer i, infer result] ? i extends ctx["input"] ? result : Extract<ctx["input"], i> extends never ? result : Extract<ctx["input"], result> : never;
|
|
63
77
|
type CaseParser<ctx extends MatchParserContext> = <const def, ret>(def: type.validate<def, ctx["$"]>, resolve: (In: inferCaseArg<def, ctx, "out">) => ret) => ChainableMatchParser<addCasesToContext<ctx, [(In: inferCaseArg<def, ctx, "in">) => ret]>>;
|
|
64
78
|
type validateKey<key extends Key, ctx extends MatchParserContext> = ctx["key"] extends Key ? ErrorMessage<doubleAtMessage> : ctx["cases"]["length"] extends 0 ? keyof ctx["input"] extends never ? key : conform<key, keyof ctx["input"]> : ErrorMessage<chainedAtMessage>;
|
|
79
|
+
interface StringsParser<ctx extends MatchParserContext> {
|
|
80
|
+
<const cases>(def: cases extends validateStringCases<cases, ctx> ? cases : validateStringCases<cases, ctx>): addCasesToParser<cases, ctx, "string">;
|
|
81
|
+
}
|
|
82
|
+
type validateStringCases<cases, ctx extends MatchParserContext> = {
|
|
83
|
+
[k in keyof cases | stringValue<ctx> | "default"]?: k extends "default" ? DefaultCase<ctx> : k extends stringValue<ctx> ? (In: _finalizeCaseArg<maybeLiftToKey<k, ctx>, ctx, "out">) => unknown : ErrorType<`${k & string} must be a possible string value`>;
|
|
84
|
+
};
|
|
85
|
+
type stringValue<ctx extends MatchParserContext> = ctx["key"] extends keyof ctx["input"] ? ctx["input"][ctx["key"]] extends string ? ctx["input"][ctx["key"]] : never : ctx["input"] extends string ? ctx["input"] : never;
|
|
65
86
|
interface AtParser<ctx extends MatchParserContext> {
|
|
66
87
|
<const key extends string>(key: validateKey<key, ctx>): ChainableMatchParser<ctx.atKey<ctx, key>>;
|
|
67
|
-
<const key extends string, const cases, ctxAtKey extends MatchParserContext = ctx.atKey<ctx, key>>(key: validateKey<key, ctx>, cases: cases extends validateCases<cases, ctxAtKey> ? cases : errorCases<cases, ctxAtKey>): addCasesToParser<cases, ctxAtKey>;
|
|
88
|
+
<const key extends string, const cases, ctxAtKey extends MatchParserContext = ctx.atKey<ctx, key>>(key: validateKey<key, ctx>, cases: cases extends validateCases<cases, ctxAtKey> ? cases : errorCases<cases, ctxAtKey>): addCasesToParser<cases, ctxAtKey, "def">;
|
|
68
89
|
}
|
|
69
90
|
interface ChainableMatchParser<ctx extends MatchParserContext> {
|
|
70
91
|
case: CaseParser<ctx>;
|
|
71
92
|
match: CaseMatchParser<ctx>;
|
|
72
93
|
default: DefaultMethod<ctx>;
|
|
73
94
|
at: AtParser<ctx>;
|
|
95
|
+
/** @experimental */
|
|
96
|
+
strings: StringsParser<ctx>;
|
|
74
97
|
}
|
|
75
98
|
export type DefaultCaseKeyword = "never" | "assert" | "reject";
|
|
76
99
|
type DefaultCase<ctx extends MatchParserContext = MatchParserContext<any>> = DefaultCaseKeyword | Morph<ctx["input"]>;
|
|
@@ -85,7 +108,7 @@ type errorCases<cases, ctx extends MatchParserContext> = {
|
|
|
85
108
|
} & {
|
|
86
109
|
default?: DefaultCase<ctx>;
|
|
87
110
|
};
|
|
88
|
-
export type CaseMatchParser<ctx extends MatchParserContext> = <const cases>(def: cases extends validateCases<cases, ctx> ? cases : errorCases<cases, ctx>) => addCasesToParser<cases, ctx>;
|
|
111
|
+
export type CaseMatchParser<ctx extends MatchParserContext> = <const cases>(def: cases extends validateCases<cases, ctx> ? cases : errorCases<cases, ctx>) => addCasesToParser<cases, ctx, "def">;
|
|
89
112
|
type finalizeMatchParser<ctx extends MatchParserContext, defaultCase extends DefaultCase<ctx>> = addDefaultToContext<ctx, defaultCase> extends (infer ctx extends MatchParserContext) ? Match<ctx["input"], ctx["cases"]> : never;
|
|
90
113
|
export interface Match<In = any, cases extends Morph[] = Morph[]> extends Inferred<(In: Morph.In<cases[number]>) => Out<ReturnType<cases[number]>>> {
|
|
91
114
|
<const data extends In>(data: data): {
|
|
@@ -95,20 +118,25 @@ export interface Match<In = any, cases extends Morph[] = Morph[]> extends Inferr
|
|
|
95
118
|
export declare class InternalMatchParser extends Callable<InternalCaseParserFn> {
|
|
96
119
|
$: InternalScope;
|
|
97
120
|
constructor($: InternalScope);
|
|
98
|
-
in(): InternalChainedMatchParser;
|
|
121
|
+
in(def?: unknown): InternalChainedMatchParser;
|
|
99
122
|
at(key: Key, cases?: InternalCases): InternalChainedMatchParser | Match;
|
|
100
123
|
case(when: unknown, then: Morph): InternalChainedMatchParser;
|
|
101
124
|
}
|
|
102
125
|
type InternalCases = Record<string, Morph | DefaultCase>;
|
|
103
126
|
type InternalCaseParserFn = (cases: InternalCases) => InternalChainedMatchParser | Match;
|
|
127
|
+
type CaseEntry = [BaseRoot, Morph] | ["default", DefaultCase];
|
|
104
128
|
export declare class InternalChainedMatchParser extends Callable<InternalCaseParserFn> {
|
|
105
129
|
$: InternalScope;
|
|
130
|
+
in: BaseRoot | undefined;
|
|
106
131
|
protected key: Key | undefined;
|
|
107
132
|
protected branches: BaseRoot[];
|
|
108
|
-
constructor($: InternalScope);
|
|
133
|
+
constructor($: InternalScope, In?: BaseRoot);
|
|
109
134
|
at(key: Key, cases?: InternalCases): InternalChainedMatchParser | Match;
|
|
110
135
|
case(def: unknown, resolver: Morph): InternalChainedMatchParser;
|
|
136
|
+
protected caseEntry(node: BaseRoot, resolver: Morph): InternalChainedMatchParser;
|
|
111
137
|
match(cases: InternalCases): InternalChainedMatchParser | Match;
|
|
138
|
+
strings(cases: InternalCases): InternalChainedMatchParser | Match;
|
|
139
|
+
protected caseEntries(entries: CaseEntry[]): InternalChainedMatchParser | Match;
|
|
112
140
|
default(defaultCase: DefaultCase): Match;
|
|
113
141
|
}
|
|
114
142
|
export declare const throwOnDefault: ArkErrors.Handler;
|
package/out/match.js
CHANGED
|
@@ -8,8 +8,8 @@ export class InternalMatchParser extends Callable {
|
|
|
8
8
|
});
|
|
9
9
|
this.$ = $;
|
|
10
10
|
}
|
|
11
|
-
in() {
|
|
12
|
-
return new InternalChainedMatchParser(this
|
|
11
|
+
in(def) {
|
|
12
|
+
return new InternalChainedMatchParser(this.$, def === undefined ? undefined : this.$.parse(def));
|
|
13
13
|
}
|
|
14
14
|
at(key, cases) {
|
|
15
15
|
return new InternalChainedMatchParser(this.$).at(key, cases);
|
|
@@ -20,27 +20,13 @@ export class InternalMatchParser extends Callable {
|
|
|
20
20
|
}
|
|
21
21
|
export class InternalChainedMatchParser extends Callable {
|
|
22
22
|
$;
|
|
23
|
+
in;
|
|
23
24
|
key;
|
|
24
25
|
branches = [];
|
|
25
|
-
constructor(
|
|
26
|
-
super(cases =>
|
|
27
|
-
const entries = Object.entries(cases);
|
|
28
|
-
for (let i = 0; i < entries.length; i++) {
|
|
29
|
-
const [k, v] = entries[i];
|
|
30
|
-
if (k === "default") {
|
|
31
|
-
if (i !== entries.length - 1) {
|
|
32
|
-
throwParseError(`default may only be specified as the last key of a switch definition`);
|
|
33
|
-
}
|
|
34
|
-
return this.default(v);
|
|
35
|
-
}
|
|
36
|
-
if (typeof v !== "function") {
|
|
37
|
-
return throwParseError(`Value for case "${k}" must be a function (was ${domainOf(v)})`);
|
|
38
|
-
}
|
|
39
|
-
this.case(k, v);
|
|
40
|
-
}
|
|
41
|
-
return this;
|
|
42
|
-
});
|
|
26
|
+
constructor($, In) {
|
|
27
|
+
super(cases => this.caseEntries(Object.entries(cases).map(([k, v]) => k === "default" ? [k, v] : [this.$.parse(k), v])));
|
|
43
28
|
this.$ = $;
|
|
29
|
+
this.in = In;
|
|
44
30
|
}
|
|
45
31
|
at(key, cases) {
|
|
46
32
|
if (this.key)
|
|
@@ -51,14 +37,38 @@ export class InternalChainedMatchParser extends Callable {
|
|
|
51
37
|
return cases ? this.match(cases) : this;
|
|
52
38
|
}
|
|
53
39
|
case(def, resolver) {
|
|
54
|
-
|
|
55
|
-
|
|
40
|
+
return this.caseEntry(this.$.parse(def), resolver);
|
|
41
|
+
}
|
|
42
|
+
caseEntry(node, resolver) {
|
|
43
|
+
const wrappableNode = this.key ? this.$.parse({ [this.key]: node }) : node;
|
|
44
|
+
const branch = wrappableNode.pipe(resolver);
|
|
56
45
|
this.branches.push(branch);
|
|
57
46
|
return this;
|
|
58
47
|
}
|
|
59
48
|
match(cases) {
|
|
60
49
|
return this(cases);
|
|
61
50
|
}
|
|
51
|
+
strings(cases) {
|
|
52
|
+
return this.caseEntries(Object.entries(cases).map(([k, v]) => k === "default" ?
|
|
53
|
+
[k, v]
|
|
54
|
+
: [this.$.node("unit", { unit: k }), v]));
|
|
55
|
+
}
|
|
56
|
+
caseEntries(entries) {
|
|
57
|
+
for (let i = 0; i < entries.length; i++) {
|
|
58
|
+
const [k, v] = entries[i];
|
|
59
|
+
if (k === "default") {
|
|
60
|
+
if (i !== entries.length - 1) {
|
|
61
|
+
throwParseError(`default may only be specified as the last key of a switch definition`);
|
|
62
|
+
}
|
|
63
|
+
return this.default(v);
|
|
64
|
+
}
|
|
65
|
+
if (typeof v !== "function") {
|
|
66
|
+
return throwParseError(`Value for case "${k}" must be a function (was ${domainOf(v)})`);
|
|
67
|
+
}
|
|
68
|
+
this.caseEntry(k, v);
|
|
69
|
+
}
|
|
70
|
+
return this;
|
|
71
|
+
}
|
|
62
72
|
default(defaultCase) {
|
|
63
73
|
if (typeof defaultCase === "function")
|
|
64
74
|
this.case(intrinsic.unknown, defaultCase);
|
|
@@ -68,8 +78,16 @@ export class InternalChainedMatchParser extends Callable {
|
|
|
68
78
|
};
|
|
69
79
|
if (defaultCase === "never" || defaultCase === "assert")
|
|
70
80
|
schema.meta = { onFail: throwOnDefault };
|
|
71
|
-
const
|
|
72
|
-
|
|
81
|
+
const cases = this.$.node("union", schema);
|
|
82
|
+
if (!this.in)
|
|
83
|
+
return this.$.finalize(cases);
|
|
84
|
+
let inputValidatedCases = this.in.pipe(cases);
|
|
85
|
+
if (defaultCase === "never" || defaultCase === "assert") {
|
|
86
|
+
inputValidatedCases = inputValidatedCases.withMeta({
|
|
87
|
+
onFail: throwOnDefault
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
return this.$.finalize(inputValidatedCases);
|
|
73
91
|
}
|
|
74
92
|
}
|
|
75
93
|
export const throwOnDefault = errors => errors.throw();
|
package/out/methods/base.d.ts
CHANGED
|
@@ -135,11 +135,11 @@ export interface Inferred<out t = unknown, $ = {}> {
|
|
|
135
135
|
* const criticalPayload = type({
|
|
136
136
|
* superImportantValue: "string"
|
|
137
137
|
* })
|
|
138
|
-
* // throws
|
|
138
|
+
* // throws TraversalError: superImportantValue must be a string (was missing)
|
|
139
139
|
* const data = criticalPayload.assert({ irrelevantValue: "whoops" })
|
|
140
140
|
* console.log(data.superImportantValue) // valid output can be accessed directly
|
|
141
141
|
*
|
|
142
|
-
* @throws {
|
|
142
|
+
* @throws {TraversalError}
|
|
143
143
|
*/
|
|
144
144
|
assert(data: unknown): this["infer"];
|
|
145
145
|
/**
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "arktype",
|
|
3
3
|
"description": "Optimized runtime validation for TypeScript syntax",
|
|
4
|
-
"version": "2.1.
|
|
4
|
+
"version": "2.1.8",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
@@ -34,8 +34,8 @@
|
|
|
34
34
|
"out"
|
|
35
35
|
],
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@ark/util": "0.44.
|
|
38
|
-
"@ark/schema": "0.44.
|
|
37
|
+
"@ark/util": "0.44.3",
|
|
38
|
+
"@ark/schema": "0.44.3"
|
|
39
39
|
},
|
|
40
40
|
"publishConfig": {
|
|
41
41
|
"access": "public"
|