arktype 2.0.3 → 2.0.4
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/README.md +2 -1
- package/out/attributes.d.ts +1 -3
- package/out/config.d.ts +1 -1
- package/out/config.js +2 -1
- package/out/keywords/keywords.js +1 -0
- package/out/keywords/string.d.ts +1 -1
- package/out/match.d.ts +72 -0
- package/out/match.js +57 -0
- package/out/parser/ast/bounds.d.ts +4 -4
- package/out/parser/ast/divisor.d.ts +5 -3
- package/out/parser/ast/utils.d.ts +5 -1
- package/out/scope.d.ts +2 -0
- package/out/scope.js +2 -0
- package/package.json +5 -4
package/README.md
CHANGED
|
@@ -11,7 +11,7 @@ controls
|
|
|
11
11
|
playsInline
|
|
12
12
|
muted
|
|
13
13
|
disablePictureInPicture
|
|
14
|
-
src="https://github.com/user-attachments/assets/
|
|
14
|
+
src="https://github.com/user-attachments/assets/69fdded6-50a9-402d-a28c-afa58db63c90"
|
|
15
15
|
/>
|
|
16
16
|
|
|
17
17
|
## Docs
|
|
@@ -120,6 +120,7 @@ Otherwise, consider sending me an email (david@arktype.io) or [message me on Dis
|
|
|
120
120
|
<th>Phalangers</th>
|
|
121
121
|
<th>WilliamConnatser</th>
|
|
122
122
|
<th>JameEnder</th>
|
|
123
|
+
<th>tylim88</th>
|
|
123
124
|
</tr>
|
|
124
125
|
<tr>
|
|
125
126
|
<td>
|
package/out/attributes.d.ts
CHANGED
|
@@ -19,9 +19,7 @@ export declare namespace distill {
|
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
21
|
type finalizeDistillation<t, distilled> = equals<t, distilled> extends true ? t : distilled;
|
|
22
|
-
type _distill<t, endpoint extends distill.Endpoint, seen> = t extends undefined ? t : [t] extends [anyOrNever | seen] ? t : unknown extends t ? unknown : t extends Brand<infer base> ? endpoint extends "in" ? base : t : t extends TerminallyInferredObject | Primitive ? t : t extends Function ? t extends InferredMorph<infer i, infer o> ?
|
|
23
|
-
ReturnType<t>
|
|
24
|
-
] extends [anyOrNever] ? t : distillIo<i, o, endpoint, seen> : t : t extends Default<infer constraint> ? _distill<constraint, endpoint, seen> : t extends array ? distillArray<t, endpoint, seen | t> : isSafelyMappable<t> extends true ? distillMappable<t, endpoint, seen | t> : t;
|
|
22
|
+
type _distill<t, endpoint extends distill.Endpoint, seen> = t extends undefined ? t : [t] extends [anyOrNever | seen] ? t : unknown extends t ? unknown : t extends Brand<infer base> ? endpoint extends "in" ? base : t : t extends TerminallyInferredObject | Primitive ? t : t extends Function ? t extends (...args: never) => anyOrNever ? t : t extends InferredMorph<infer i, infer o> ? distillIo<i, o, endpoint, seen> : t : t extends Default<infer constraint> ? _distill<constraint, endpoint, seen> : t extends array ? distillArray<t, endpoint, seen | t> : isSafelyMappable<t> extends true ? distillMappable<t, endpoint, seen | t> : t;
|
|
25
23
|
type distillMappable<o, endpoint extends distill.Endpoint, seen> = endpoint extends "in" ? show<{
|
|
26
24
|
[k in keyof o as k extends inferredDefaultKeyOf<o> ? never : k]: _distill<o[k], endpoint, seen>;
|
|
27
25
|
} & {
|
package/out/config.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { ArkSchemaRegistry } from "@ark/schema";
|
|
2
2
|
import type { Ark } from "./keywords/keywords.ts";
|
|
3
3
|
import type { exportScope } from "./module.ts";
|
|
4
|
-
export { configure, type ArkConfig, type ArkSchemaRegistry } from "@ark/schema";
|
|
4
|
+
export { configure, type ArkConfig, type ArkSchemaRegistry } from "@ark/schema/config";
|
|
5
5
|
export interface ArkTypeRegistryContents extends ArkSchemaRegistry {
|
|
6
6
|
ambient: exportScope<Ark>;
|
|
7
7
|
}
|
package/out/config.js
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
// eslint-disable-next-line @typescript-eslint/no-restricted-imports
|
|
2
|
+
export { configure } from "@ark/schema/config";
|
package/out/keywords/keywords.js
CHANGED
|
@@ -24,6 +24,7 @@ export const ark = scope({
|
|
|
24
24
|
}, { prereducedAliases: true, ambient: true });
|
|
25
25
|
export const keywords = ark.export();
|
|
26
26
|
export const type = ark.type;
|
|
27
|
+
// export const match: MatchParser<{}> = ark.match as never
|
|
27
28
|
export const generic = ark.generic;
|
|
28
29
|
export const schema = ark.schema;
|
|
29
30
|
export const define = ark.define;
|
package/out/keywords/string.d.ts
CHANGED
|
@@ -246,7 +246,6 @@ export declare namespace uuid {
|
|
|
246
246
|
};
|
|
247
247
|
}
|
|
248
248
|
export declare const string: Module<{
|
|
249
|
-
root: unknown;
|
|
250
249
|
trim: Submodule<trim.submodule>;
|
|
251
250
|
normalize: Submodule<{
|
|
252
251
|
root: unknown;
|
|
@@ -267,6 +266,7 @@ export declare const string: Module<{
|
|
|
267
266
|
preformatted: unknown;
|
|
268
267
|
}>;
|
|
269
268
|
}>;
|
|
269
|
+
root: unknown;
|
|
270
270
|
integer: Submodule<stringInteger.submodule>;
|
|
271
271
|
json: Submodule<stringJson.submodule>;
|
|
272
272
|
alpha: unknown;
|
package/out/match.d.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { type BaseRoot } from "@ark/schema";
|
|
2
|
+
import { Callable, type array, type Fn, type isDisjoint, type numericStringKeyOf, type propValueOf, type unionToTuple } from "@ark/util";
|
|
3
|
+
import type { distill, inferIntersection } from "./attributes.ts";
|
|
4
|
+
import type { type } from "./keywords/keywords.ts";
|
|
5
|
+
import type { InternalScope } from "./scope.ts";
|
|
6
|
+
type Thens = array<(In: any) => unknown>;
|
|
7
|
+
type MatchParserContext = {
|
|
8
|
+
thens: Thens;
|
|
9
|
+
$: unknown;
|
|
10
|
+
input: unknown;
|
|
11
|
+
};
|
|
12
|
+
export type MatchParser<$> = CaseMatchParser<{
|
|
13
|
+
thens: [];
|
|
14
|
+
$: $;
|
|
15
|
+
input: unknown;
|
|
16
|
+
}> & {
|
|
17
|
+
(): ChainableMatchParser<{
|
|
18
|
+
thens: [];
|
|
19
|
+
$: $;
|
|
20
|
+
input: unknown;
|
|
21
|
+
}>;
|
|
22
|
+
from<typedInput>(): ChainableMatchParser<{
|
|
23
|
+
thens: [];
|
|
24
|
+
$: $;
|
|
25
|
+
input: typedInput;
|
|
26
|
+
}>;
|
|
27
|
+
};
|
|
28
|
+
type addBranches<ctx extends MatchParserContext, branches extends readonly unknown[]> = branches extends Thens ? {
|
|
29
|
+
$: ctx["$"];
|
|
30
|
+
input: ctx["input"];
|
|
31
|
+
thens: [...ctx["thens"], ...branches];
|
|
32
|
+
} : never;
|
|
33
|
+
type inferMatchBranch<def, ctx extends MatchParserContext> = distill.Out<inferIntersection<ctx["input"], type.infer<def, ctx["$"]>>>;
|
|
34
|
+
type ChainableMatchParser<ctx extends MatchParserContext> = {
|
|
35
|
+
when: <def, ret>(when: type.validate<def, ctx["$"]>, then: (In: inferMatchBranch<def, ctx>) => ret) => ChainableMatchParser<addBranches<ctx, [(In: inferMatchBranch<def, ctx>) => ret]>>;
|
|
36
|
+
cases: CaseMatchParser<ctx>;
|
|
37
|
+
orThrow: () => finalizeMatchParser<addBranches<ctx, [(In: ctx["input"]) => never]>>;
|
|
38
|
+
default: MatchParserDefaultInvocation<ctx>;
|
|
39
|
+
};
|
|
40
|
+
type MatchParserDefaultInvocation<ctx extends MatchParserContext> = {
|
|
41
|
+
<f extends (In: ctx["input"]) => unknown>(f: f): finalizeWithDefault<ctx, ReturnType<f>>;
|
|
42
|
+
<const value>(value: value): finalizeWithDefault<ctx, value>;
|
|
43
|
+
};
|
|
44
|
+
type validateCases<cases, ctx extends MatchParserContext> = {
|
|
45
|
+
[def in keyof cases | keyof ctx["$"] | "default"]?: def extends "default" ? (In: ctx["input"]) => unknown : def extends type.validate<def, ctx["$"]> ? (In: inferMatchBranch<def, ctx>) => unknown : type.validate<def, ctx["$"]>;
|
|
46
|
+
};
|
|
47
|
+
type errorCases<cases, ctx extends MatchParserContext> = {
|
|
48
|
+
[def in keyof cases]?: def extends "default" ? (In: ctx["input"]) => unknown : def extends type.validate<def, ctx["$"]> ? (In: inferMatchBranch<def, ctx>) => unknown : type.validate<def, ctx["$"]>;
|
|
49
|
+
} & {
|
|
50
|
+
[k in Exclude<keyof ctx["$"], keyof cases>]?: (In: distill.Out<inferIntersection<ctx["input"], ctx["$"][k]>>) => unknown;
|
|
51
|
+
} & {
|
|
52
|
+
default?: (In: ctx["input"]) => unknown;
|
|
53
|
+
};
|
|
54
|
+
export type CaseMatchParser<ctx extends MatchParserContext> = <cases>(def: cases extends validateCases<cases, ctx> ? cases : errorCases<cases, ctx>) => cases extends {
|
|
55
|
+
default: (...args: never[]) => infer defaultReturn;
|
|
56
|
+
} ? finalizeWithDefault<addBranches<ctx, unionToTuple<cases[Exclude<keyof cases, "default">]>>, defaultReturn> : ChainableMatchParser<addBranches<ctx, unionToTuple<propValueOf<cases>>>>;
|
|
57
|
+
type finalizeWithDefault<ctx extends MatchParserContext, defaultReturn> = finalizeMatchParser<addBranches<ctx, [(_: ctx["input"]) => defaultReturn]>>;
|
|
58
|
+
type finalizeMatchParser<ctx extends MatchParserContext> = MatchInvocation<{
|
|
59
|
+
thens: ctx["thens"];
|
|
60
|
+
input: ctx["input"];
|
|
61
|
+
}>;
|
|
62
|
+
type MatchInvocationContext = {
|
|
63
|
+
thens: readonly Fn[];
|
|
64
|
+
input: unknown;
|
|
65
|
+
};
|
|
66
|
+
export type MatchInvocation<ctx extends MatchInvocationContext> = <const data extends ctx["input"]>(data: data) => {
|
|
67
|
+
[i in numericStringKeyOf<ctx["thens"]>]: isDisjoint<data, Parameters<ctx["thens"][i]>[0]> extends true ? never : ReturnType<ctx["thens"][i]>;
|
|
68
|
+
}[numericStringKeyOf<ctx["thens"]>];
|
|
69
|
+
export declare class InternalMatchParser extends Callable<(...args: unknown[]) => BaseRoot> {
|
|
70
|
+
constructor($: InternalScope);
|
|
71
|
+
}
|
|
72
|
+
export {};
|
package/out/match.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { intrinsic } from "@ark/schema";
|
|
2
|
+
import { Callable } from "@ark/util";
|
|
3
|
+
export class InternalMatchParser extends Callable {
|
|
4
|
+
constructor($) {
|
|
5
|
+
super(() => {
|
|
6
|
+
const matchParser = () => {
|
|
7
|
+
const handledCases = [];
|
|
8
|
+
let defaultCase = null;
|
|
9
|
+
const parser = {
|
|
10
|
+
when: (when, then) => {
|
|
11
|
+
handledCases.push({
|
|
12
|
+
when: $.parse(when),
|
|
13
|
+
then
|
|
14
|
+
});
|
|
15
|
+
return parser;
|
|
16
|
+
},
|
|
17
|
+
finalize: () => {
|
|
18
|
+
const branches = handledCases.flatMap(({ when, then }) => {
|
|
19
|
+
if (when.kind === "union") {
|
|
20
|
+
return when.branches.map(branch => ({
|
|
21
|
+
in: branch,
|
|
22
|
+
morphs: [then]
|
|
23
|
+
}));
|
|
24
|
+
}
|
|
25
|
+
if (when.hasKind("morph"))
|
|
26
|
+
return [{ in: when, morphs: [...when.morphs, then] }];
|
|
27
|
+
return [{ in: when, morphs: [then] }];
|
|
28
|
+
});
|
|
29
|
+
if (defaultCase)
|
|
30
|
+
branches.push({ in: intrinsic.unknown, morphs: [defaultCase] });
|
|
31
|
+
const matchers = $.node("union", {
|
|
32
|
+
branches,
|
|
33
|
+
ordered: true
|
|
34
|
+
});
|
|
35
|
+
return matchers.assert;
|
|
36
|
+
},
|
|
37
|
+
orThrow: () =>
|
|
38
|
+
// implicitly finalize, we don't need to do anything else because we throw either way
|
|
39
|
+
parser.finalize(),
|
|
40
|
+
default: (x) => {
|
|
41
|
+
if (x instanceof Function)
|
|
42
|
+
defaultCase = x;
|
|
43
|
+
else
|
|
44
|
+
defaultCase = () => x;
|
|
45
|
+
return parser.finalize();
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
return parser;
|
|
49
|
+
};
|
|
50
|
+
return Object.assign(() => matchParser(), {
|
|
51
|
+
only: () => matchParser()
|
|
52
|
+
});
|
|
53
|
+
}, {
|
|
54
|
+
bind: $
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import type { writeUnboundableMessage } from "@ark/schema";
|
|
2
2
|
import type { ErrorMessage, array, typeToString } from "@ark/util";
|
|
3
|
-
import type { LimitLiteral } from "../../attributes.ts";
|
|
3
|
+
import type { InferredMorph, LimitLiteral } from "../../attributes.ts";
|
|
4
4
|
import type { Comparator } from "../reduce/shared.ts";
|
|
5
5
|
import type { BoundExpressionKind, writeInvalidLimitMessage } from "../shift/operator/bounds.ts";
|
|
6
|
-
import type {
|
|
7
|
-
import type { astToString } from "./utils.ts";
|
|
6
|
+
import type { inferAstRoot } from "./infer.ts";
|
|
7
|
+
import type { astToString, writeConstrainedMorphMessage } from "./utils.ts";
|
|
8
8
|
import type { validateAst } from "./validate.ts";
|
|
9
9
|
export type validateRange<l, comparator extends Comparator, r, $, args> = [
|
|
10
10
|
l
|
|
11
11
|
] extends [LimitLiteral] ? validateBound<r, comparator, l, "left", $, args> : [l] extends [[infer leftAst, Comparator, unknown]] ? ErrorMessage<writeDoubleRightBoundMessage<astToString<leftAst>>> : validateBound<l, comparator, r & LimitLiteral, "right", $, args>;
|
|
12
|
-
export type validateBound<boundedAst, comparator extends Comparator, limit extends LimitLiteral, boundKind extends BoundExpressionKind, $, args> =
|
|
12
|
+
export type validateBound<boundedAst, comparator extends Comparator, limit extends LimitLiteral, boundKind extends BoundExpressionKind, $, args> = inferAstRoot<boundedAst, $, args> extends infer bounded ? isNumericallyBoundable<bounded> extends true ? limit extends number ? validateAst<boundedAst, $, args> : ErrorMessage<writeInvalidLimitMessage<comparator, limit, boundKind>> : [bounded] extends [Date] ? validateAst<boundedAst, $, args> : [bounded] extends [InferredMorph] ? ErrorMessage<writeConstrainedMorphMessage<boundedAst>> : ErrorMessage<writeUnboundableMessage<typeToString<bounded>>> : never;
|
|
13
13
|
type isNumericallyBoundable<bounded> = [
|
|
14
14
|
bounded
|
|
15
15
|
] extends [number] ? true : [bounded] extends [string] ? true : [bounded] extends [array] ? true : false;
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import type { writeIndivisibleMessage } from "@ark/schema";
|
|
2
2
|
import type { ErrorMessage } from "@ark/util";
|
|
3
|
-
import type {
|
|
3
|
+
import type { InferredMorph } from "../../attributes.ts";
|
|
4
|
+
import type { inferAstRoot } from "./infer.ts";
|
|
5
|
+
import type { writeConstrainedMorphMessage } from "./utils.ts";
|
|
4
6
|
import type { validateAst } from "./validate.ts";
|
|
5
|
-
export type validateDivisor<l, $, args> =
|
|
7
|
+
export type validateDivisor<l, $, args> = inferAstRoot<l, $, args> extends infer data ? [
|
|
6
8
|
data
|
|
7
|
-
] extends [number] ? validateAst<l, $, args> : ErrorMessage<writeIndivisibleMessage<data>> : never;
|
|
9
|
+
] extends [number] ? validateAst<l, $, args> : [data] extends [InferredMorph] ? ErrorMessage<writeConstrainedMorphMessage<l>> : ErrorMessage<writeIndivisibleMessage<data>> : never;
|
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
import type { Stringifiable } from "@ark/util";
|
|
1
|
+
import type { satisfy, Stringifiable } from "@ark/util";
|
|
2
2
|
import type { Comparator } from "../reduce/shared.ts";
|
|
3
|
+
import type { ArkTypeScanner } from "../shift/scanner.ts";
|
|
3
4
|
import type { DefAst, InferredAst, InfixExpression, PostfixExpression } from "./infer.ts";
|
|
4
5
|
export type astToString<ast> = ast extends InferredAst | DefAst ? ast[2] : ast extends PostfixExpression<infer operator, infer operand> ? operator extends "[]" ? `${astToString<operand>}[]` : never : ast extends InfixExpression<infer operator, infer l, infer r> ? operator extends "&" | "|" | "%" | Comparator ? `${astToString<l>} ${operator} ${astToString<r>}` : never : ast extends Stringifiable ? `${ast extends bigint ? `${ast}n` : ast}` : "...";
|
|
6
|
+
export type ConstraintOperator = satisfy<ArkTypeScanner.OperatorToken, "%" | Comparator>;
|
|
7
|
+
export type writeConstrainedMorphMessage<constrainedAst> = `To constrain the output of ${astToString<constrainedAst>}, pipe like myMorph.to('number > 0').
|
|
8
|
+
To constrain the input, intersect like myMorph.and('number > 0').`;
|
package/out/scope.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { type Brand, type ErrorType, type JsonStructure, type anyOrNever, type a
|
|
|
3
3
|
import type { ArkSchemaRegistry } from "./config.ts";
|
|
4
4
|
import { type GenericDeclaration, type GenericParser, type ParameterString, type baseGenericConstraints, type parseGenericParams, type parseValidGenericParams } from "./generic.ts";
|
|
5
5
|
import type { Ark, type } from "./keywords/keywords.ts";
|
|
6
|
+
import { InternalMatchParser } from "./match.ts";
|
|
6
7
|
import type { BoundModule, Module, Submodule, instantiateExport } from "./module.ts";
|
|
7
8
|
import type { DefAst, InferredAst } from "./parser/ast/infer.ts";
|
|
8
9
|
import { type inferDefinition } from "./parser/definition.ts";
|
|
@@ -75,6 +76,7 @@ export declare class InternalScope<$ extends {} = {}> extends BaseScope<$> {
|
|
|
75
76
|
enumerated: EnumeratedTypeParser<$>;
|
|
76
77
|
instanceOf: InstanceOfTypeParser<$>;
|
|
77
78
|
type: InternalTypeParser;
|
|
79
|
+
match: InternalMatchParser;
|
|
78
80
|
declare: () => {
|
|
79
81
|
type: InternalTypeParser;
|
|
80
82
|
};
|
package/out/scope.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { $ark, BaseScope, hasArkKind, parseGeneric } from "@ark/schema";
|
|
2
2
|
import { flatMorph, isArray, isThunk, throwParseError } from "@ark/util";
|
|
3
3
|
import { parseGenericParamName } from "./generic.js";
|
|
4
|
+
import { InternalMatchParser } from "./match.js";
|
|
4
5
|
import { shallowDefaultableMessage, shallowOptionalMessage } from "./parser/ast/validate.js";
|
|
5
6
|
import { parseInnerDefinition } from "./parser/definition.js";
|
|
6
7
|
import { ArkTypeScanner } from "./parser/shift/scanner.js";
|
|
@@ -74,6 +75,7 @@ export class InternalScope extends BaseScope {
|
|
|
74
75
|
enumerated = (...values) => this.units(values);
|
|
75
76
|
instanceOf = ctor => this.node("proto", { proto: ctor }, { prereduced: true });
|
|
76
77
|
type = new InternalTypeParser(this);
|
|
78
|
+
match = new InternalMatchParser(this);
|
|
77
79
|
declare = () => ({
|
|
78
80
|
type: this.type
|
|
79
81
|
});
|
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": "2.0.
|
|
4
|
+
"version": "2.0.4",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
@@ -34,14 +34,15 @@
|
|
|
34
34
|
"out"
|
|
35
35
|
],
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@ark/
|
|
38
|
-
"@ark/
|
|
37
|
+
"@ark/util": "0.39.0",
|
|
38
|
+
"@ark/schema": "0.39.0"
|
|
39
39
|
},
|
|
40
40
|
"publishConfig": {
|
|
41
41
|
"access": "public"
|
|
42
42
|
},
|
|
43
43
|
"scripts": {
|
|
44
44
|
"build": "ts ../repo/build.ts",
|
|
45
|
-
"test": "ts ../repo/testPackage.ts"
|
|
45
|
+
"test": "ts ../repo/testPackage.ts; pnpm testIntegration",
|
|
46
|
+
"testIntegration": "ts ./__tests__/integration/importConfig.ts"
|
|
46
47
|
}
|
|
47
48
|
}
|