arktype 2.0.0-rc.1 → 2.0.0-rc.10
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/config.d.ts +1 -1
- package/out/generic.d.ts +47 -48
- package/out/generic.js +6 -8
- package/out/index.d.ts +4 -4
- package/out/index.js +2 -2
- package/out/keywords/builtins.js +2 -2
- package/out/keywords/constructors/Array.d.ts +2 -2
- package/out/keywords/constructors/Array.js +7 -7
- package/out/keywords/constructors/Date.d.ts +32 -0
- package/out/keywords/constructors/FormData.d.ts +2 -2
- package/out/keywords/constructors/FormData.js +8 -8
- package/out/keywords/constructors/TypedArray.js +2 -2
- package/out/keywords/constructors/constructors.d.ts +0 -1
- package/out/keywords/constructors/constructors.js +2 -2
- package/out/keywords/inference.d.ts +227 -0
- package/out/keywords/keywords.d.ts +61 -0
- package/out/keywords/{ark.js → keywords.js} +10 -8
- package/out/keywords/number/epoch.d.ts +1 -2
- package/out/keywords/number/epoch.js +2 -2
- package/out/keywords/number/integer.js +2 -2
- package/out/keywords/number/number.d.ts +32 -6
- package/out/keywords/number/number.js +17 -5
- package/out/keywords/string/alpha.d.ts +1 -1
- package/out/keywords/string/alphanumeric.d.ts +1 -1
- package/out/keywords/string/capitalize.d.ts +2 -2
- package/out/keywords/string/capitalize.js +4 -4
- package/out/keywords/string/creditCard.d.ts +1 -1
- package/out/keywords/string/creditCard.js +2 -2
- package/out/keywords/string/date.d.ts +4 -8
- package/out/keywords/string/date.js +16 -16
- package/out/keywords/string/digits.d.ts +1 -1
- package/out/keywords/string/email.d.ts +1 -1
- package/out/keywords/string/integer.d.ts +2 -2
- package/out/keywords/string/integer.js +7 -7
- package/out/keywords/string/ip.d.ts +2 -2
- package/out/keywords/string/ip.js +5 -5
- package/out/keywords/string/json.d.ts +3 -2
- package/out/keywords/string/json.js +24 -16
- package/out/keywords/string/lower.d.ts +2 -2
- package/out/keywords/string/lower.js +4 -4
- package/out/keywords/string/normalize.d.ts +23 -27
- package/out/keywords/string/normalize.js +14 -14
- package/out/keywords/string/numeric.d.ts +2 -2
- package/out/keywords/string/numeric.js +7 -7
- package/out/keywords/string/semver.d.ts +1 -1
- package/out/keywords/string/string.d.ts +39 -42
- package/out/keywords/string/string.js +3 -3
- package/out/keywords/string/trim.d.ts +2 -2
- package/out/keywords/string/trim.js +4 -4
- package/out/keywords/string/upper.d.ts +2 -2
- package/out/keywords/string/upper.js +4 -4
- package/out/keywords/string/url.d.ts +2 -2
- package/out/keywords/string/url.js +8 -8
- package/out/keywords/string/uuid.d.ts +11 -11
- package/out/keywords/string/uuid.js +3 -3
- package/out/keywords/ts.d.ts +11 -0
- package/out/keywords/ts.js +7 -3
- package/out/keywords/utils.d.ts +1 -1
- package/out/keywords/utils.js +1 -1
- package/out/{subtypes → methods}/array.d.ts +6 -6
- package/out/methods/base.d.ts +96 -0
- package/out/{subtypes → methods}/date.d.ts +5 -5
- package/out/{subtypes → methods}/instantiate.d.ts +5 -4
- package/out/methods/morph.d.ts +12 -0
- package/out/{subtypes → methods}/number.d.ts +6 -6
- package/out/methods/object.d.ts +81 -0
- package/out/{subtypes → methods}/string.d.ts +7 -7
- package/out/{subtypes → methods}/validator.d.ts +2 -2
- package/out/module.d.ts +2 -2
- package/out/parser/{semantic → ast}/bounds.d.ts +3 -3
- package/out/parser/ast/default.d.ts +8 -0
- package/out/parser/{semantic → ast}/infer.d.ts +11 -11
- package/out/parser/{semantic → ast}/utils.d.ts +1 -1
- package/out/parser/{semantic → ast}/validate.d.ts +6 -6
- package/out/parser/definition.d.ts +8 -9
- package/out/parser/objectLiteral.d.ts +11 -37
- package/out/parser/objectLiteral.js +6 -24
- package/out/parser/{string/reduce → reduce}/dynamic.d.ts +5 -7
- package/out/parser/{string/reduce → reduce}/dynamic.js +5 -6
- package/out/parser/{string/reduce → reduce}/shared.d.ts +1 -1
- package/out/parser/{string/reduce → reduce}/static.d.ts +1 -1
- package/out/parser/{string/shift → shift}/operand/date.d.ts +3 -1
- package/out/parser/{string/shift → shift}/operand/enclosed.d.ts +3 -2
- package/out/parser/{string/shift → shift}/operand/enclosed.js +7 -3
- package/out/parser/{string/shift → shift}/operand/unenclosed.d.ts +3 -3
- package/out/parser/{string/shift → shift}/operand/unenclosed.js +7 -3
- package/out/parser/{string/shift → shift}/operator/bounds.d.ts +3 -3
- package/out/parser/{string/shift → shift}/operator/default.d.ts +3 -6
- package/out/parser/{string/shift → shift}/operator/default.js +1 -6
- package/out/parser/{string/shift → shift}/scanner.d.ts +3 -1
- package/out/parser/{string/shift → shift}/scanner.js +5 -3
- package/out/parser/{string/string.d.ts → string.d.ts} +6 -7
- package/out/parser/{string/string.js → string.js} +2 -0
- package/out/parser/tuple.d.ts +54 -43
- package/out/parser/tuple.js +36 -30
- package/out/scope.d.ts +32 -39
- package/out/scope.js +43 -35
- package/out/type.d.ts +24 -21
- package/out/type.js +14 -10
- package/package.json +3 -3
- package/out/intersect.d.ts +0 -28
- package/out/keys.d.ts +0 -16
- package/out/keywords/ark.d.ts +0 -39
- package/out/keywords/ast.d.ts +0 -193
- package/out/parser/semantic/default.d.ts +0 -9
- package/out/parser/semantic/default.js +0 -1
- package/out/subtypes/base.d.ts +0 -85
- package/out/subtypes/morph.d.ts +0 -13
- package/out/subtypes/object.d.ts +0 -33
- /package/out/{intersect.js → keywords/constructors/Date.js} +0 -0
- /package/out/keywords/{ast.js → inference.js} +0 -0
- /package/out/{subtypes → methods}/array.js +0 -0
- /package/out/{subtypes → methods}/base.js +0 -0
- /package/out/{subtypes → methods}/date.js +0 -0
- /package/out/{subtypes → methods}/instantiate.js +0 -0
- /package/out/{subtypes → methods}/morph.js +0 -0
- /package/out/{subtypes → methods}/number.js +0 -0
- /package/out/{subtypes → methods}/object.js +0 -0
- /package/out/{subtypes → methods}/string.js +0 -0
- /package/out/{subtypes → methods}/validator.js +0 -0
- /package/out/parser/{semantic → ast}/bounds.js +0 -0
- /package/out/{keys.js → parser/ast/default.js} +0 -0
- /package/out/parser/{semantic → ast}/divisor.d.ts +0 -0
- /package/out/parser/{semantic → ast}/divisor.js +0 -0
- /package/out/parser/{semantic → ast}/infer.js +0 -0
- /package/out/parser/{semantic → ast}/keyof.d.ts +0 -0
- /package/out/parser/{semantic → ast}/keyof.js +0 -0
- /package/out/parser/{semantic → ast}/utils.js +0 -0
- /package/out/parser/{semantic → ast}/validate.js +0 -0
- /package/out/parser/{string/reduce → reduce}/shared.js +0 -0
- /package/out/parser/{string/reduce → reduce}/static.js +0 -0
- /package/out/parser/{string/shift → shift}/operand/date.js +0 -0
- /package/out/parser/{string/shift → shift}/operand/genericArgs.d.ts +0 -0
- /package/out/parser/{string/shift → shift}/operand/genericArgs.js +0 -0
- /package/out/parser/{string/shift → shift}/operand/operand.d.ts +0 -0
- /package/out/parser/{string/shift → shift}/operand/operand.js +0 -0
- /package/out/parser/{string/shift → shift}/operator/bounds.js +0 -0
- /package/out/parser/{string/shift → shift}/operator/divisor.d.ts +0 -0
- /package/out/parser/{string/shift → shift}/operator/divisor.js +0 -0
- /package/out/parser/{string/shift → shift}/operator/operator.d.ts +0 -0
- /package/out/parser/{string/shift → shift}/operator/operator.js +0 -0
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import type { ArkErrors, BaseRoot, Disjoint, JsonSchema, MetaSchema, Morph, UndeclaredKeyBehavior } from "@ark/schema";
|
|
2
|
+
import type { anyOrNever, array, Callable, ErrorMessage, inferred, Json, unset } from "@ark/util";
|
|
3
|
+
import type { ArkAmbient } from "../config.ts";
|
|
4
|
+
import type { applyConstraint, Default, distill, inferIntersection, inferMorphOut, inferPipes, InferredMorph, Optional, To } from "../keywords/inference.ts";
|
|
5
|
+
import type { type } from "../keywords/keywords.ts";
|
|
6
|
+
import type { Scope } from "../scope.ts";
|
|
7
|
+
import type { ArrayType } from "./array.ts";
|
|
8
|
+
import type { instantiateType } from "./instantiate.ts";
|
|
9
|
+
/** @ts-ignore cast variance */
|
|
10
|
+
interface Type<out t = unknown, $ = {}> extends Callable<(data: unknown) => distill.Out<t> | ArkErrors> {
|
|
11
|
+
t: t;
|
|
12
|
+
infer: this["inferOut"];
|
|
13
|
+
inferBrandableIn: distill.brandable.In<t>;
|
|
14
|
+
inferBrandableOut: distill.brandable.Out<t>;
|
|
15
|
+
inferIntrospectableOut: distill.introspectable.Out<t>;
|
|
16
|
+
inferOut: distill.Out<t>;
|
|
17
|
+
inferIn: distill.In<t>;
|
|
18
|
+
inferredOutIsIntrospectable: t extends InferredMorph<any, infer o> ? [
|
|
19
|
+
o
|
|
20
|
+
] extends [anyOrNever] ? true : o extends To ? true : false : true;
|
|
21
|
+
[inferred]: t;
|
|
22
|
+
json: Json;
|
|
23
|
+
toJSON(): Json;
|
|
24
|
+
meta: ArkAmbient.meta;
|
|
25
|
+
precompilation: string | undefined;
|
|
26
|
+
toJsonSchema(): JsonSchema;
|
|
27
|
+
description: string;
|
|
28
|
+
expression: string;
|
|
29
|
+
internal: BaseRoot;
|
|
30
|
+
$: Scope<$>;
|
|
31
|
+
assert(data: unknown): this["infer"];
|
|
32
|
+
allows(data: unknown): data is this["inferIn"];
|
|
33
|
+
traverse(data: unknown): this["infer"] | ArkErrors;
|
|
34
|
+
configure(meta: MetaSchema): this;
|
|
35
|
+
describe(description: string): this;
|
|
36
|
+
onUndeclaredKey(behavior: UndeclaredKeyBehavior): this;
|
|
37
|
+
onDeepUndeclaredKey(behavior: UndeclaredKeyBehavior): this;
|
|
38
|
+
from(literal: this["inferIn"]): this["infer"];
|
|
39
|
+
as<t = unset>(...args: validateChainedAsArgs<t>): instantiateType<t, $>;
|
|
40
|
+
get in(): instantiateType<this["inferBrandableIn"], $>;
|
|
41
|
+
get out(): instantiateType<this["inferIntrospectableOut"], $>;
|
|
42
|
+
intersect<const def>(def: type.validate<def, $>): type.infer<def, $> extends infer r ? instantiateType<inferIntersection<t, r>, $> | Disjoint : never;
|
|
43
|
+
and<const def>(def: type.validate<def, $>): type.infer<def, $> extends infer r ? instantiateType<inferIntersection<t, r>, $> : never;
|
|
44
|
+
or<const def>(def: type.validate<def, $>): type.infer<def, $> extends infer r ? instantiateType<t | r, $> : never;
|
|
45
|
+
array(): ArrayType<t[], $>;
|
|
46
|
+
pipe: ChainedPipes<t, $>;
|
|
47
|
+
equals<const def>(def: type.validate<def, $>): boolean;
|
|
48
|
+
ifEquals<const def>(def: type.validate<def, $>): type.infer<def, $> extends infer r ? instantiateType<r, $> | undefined : never;
|
|
49
|
+
extends<const def>(other: type.validate<def, $>): boolean;
|
|
50
|
+
ifExtends<const def>(other: type.validate<def, $>): type.infer<def, $> extends infer r ? instantiateType<r, $> | undefined : never;
|
|
51
|
+
overlaps<const def>(r: type.validate<def, $>): boolean;
|
|
52
|
+
extract<const def>(r: type.validate<def, $>): type.infer<def, $> extends infer r ? instantiateType<Extract<t, r>, $> : never;
|
|
53
|
+
exclude<const def>(r: type.validate<def, $>): type.infer<def, $> extends infer r ? instantiateType<Exclude<t, r>, $> : never;
|
|
54
|
+
distribute<mapOut, reduceOut = mapOut[]>(mapBranch: (branch: Type, i: number, branches: array<Type>) => mapOut, reduceMapped?: (mappedBranches: mapOut[]) => reduceOut): NoInfer<reduceOut>;
|
|
55
|
+
optional<r = applyConstraint<t, Optional>>(): instantiateType<r, $>;
|
|
56
|
+
default<const value extends this["inferIn"], r = applyConstraint<t, Default<value>>>(value: value): instantiateType<r, $>;
|
|
57
|
+
/** @deprecated */
|
|
58
|
+
apply: Function["apply"];
|
|
59
|
+
/** @deprecated */
|
|
60
|
+
bind: Function["bind"];
|
|
61
|
+
/** @deprecated */
|
|
62
|
+
call: Function["call"];
|
|
63
|
+
/** @deprecated */
|
|
64
|
+
caller: Function;
|
|
65
|
+
/** @deprecated */
|
|
66
|
+
length: number;
|
|
67
|
+
/** @deprecated */
|
|
68
|
+
name: string;
|
|
69
|
+
/** @deprecated */
|
|
70
|
+
prototype: Function["prototype"];
|
|
71
|
+
/** @deprecated */
|
|
72
|
+
arguments: Function["arguments"];
|
|
73
|
+
/** @deprecated */
|
|
74
|
+
Symbol: never;
|
|
75
|
+
}
|
|
76
|
+
interface ChainedPipeSignature<t, $> {
|
|
77
|
+
<a extends Morph<distill.Out<t>>, r = instantiateType<inferPipes<t, [a]>, $>>(a: a): NoInfer<r>;
|
|
78
|
+
<a extends Morph<distill.Out<t>>, b extends Morph<inferMorphOut<a>>, r = instantiateType<inferPipes<t, [a, b]>, $>>(a: a, b: b): NoInfer<r>;
|
|
79
|
+
<a extends Morph<distill.Out<t>>, b extends Morph<inferMorphOut<a>>, c extends Morph<inferMorphOut<b>>, r = instantiateType<inferPipes<t, [a, b, c]>, $>>(a: a, b: b, c: c): NoInfer<r>;
|
|
80
|
+
<a extends Morph<distill.Out<t>>, b extends Morph<inferMorphOut<a>>, c extends Morph<inferMorphOut<b>>, d extends Morph<inferMorphOut<c>>, r = instantiateType<inferPipes<t, [a, b, c, d]>, $>>(a: a, b: b, c: c, d: d): NoInfer<r>;
|
|
81
|
+
<a extends Morph<distill.Out<t>>, b extends Morph<inferMorphOut<a>>, c extends Morph<inferMorphOut<b>>, d extends Morph<inferMorphOut<c>>, e extends Morph<inferMorphOut<d>>, r = instantiateType<inferPipes<t, [a, b, c, d, e]>, $>>(a: a, b: b, c: c, d: d, e: e): NoInfer<r>;
|
|
82
|
+
<a extends Morph<distill.Out<t>>, b extends Morph<inferMorphOut<a>>, c extends Morph<inferMorphOut<b>>, d extends Morph<inferMorphOut<c>>, e extends Morph<inferMorphOut<d>>, f extends Morph<inferMorphOut<e>>, r = instantiateType<inferPipes<t, [a, b, c, d, e, f]>, $>>(a: a, b: b, c: c, d: d, e: e, f: f): NoInfer<r>;
|
|
83
|
+
<a extends Morph<distill.Out<t>>, b extends Morph<inferMorphOut<a>>, c extends Morph<inferMorphOut<b>>, d extends Morph<inferMorphOut<c>>, e extends Morph<inferMorphOut<d>>, f extends Morph<inferMorphOut<e>>, g extends Morph<inferMorphOut<f>>, r = instantiateType<inferPipes<t, [a, b, c, d, e, f, g]>, $>>(a: a, b: b, c: c, d: d, e: e, f: f, g: g): NoInfer<r>;
|
|
84
|
+
}
|
|
85
|
+
export interface ChainedPipes<t, $> extends ChainedPipeSignature<t, $> {
|
|
86
|
+
try: ChainedPipeSignature<t, $>;
|
|
87
|
+
}
|
|
88
|
+
type validateChainedAsArgs<t> = [
|
|
89
|
+
t
|
|
90
|
+
] extends [unset] ? [
|
|
91
|
+
t
|
|
92
|
+
] extends [anyOrNever] ? [
|
|
93
|
+
] : [
|
|
94
|
+
ErrorMessage<"as requires an explicit type parameter like myType.as<t>()">
|
|
95
|
+
] : [];
|
|
96
|
+
export type { Type as BaseType };
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import type { ExclusiveDateRangeSchema, exclusivizeRangeSchema, InclusiveDateRangeSchema } from "@ark/schema";
|
|
2
|
-
import type {
|
|
2
|
+
import type { applyConstraintSchema } from "../keywords/inference.ts";
|
|
3
3
|
import type { ObjectType } from "./object.ts";
|
|
4
4
|
/** @ts-ignore cast variance */
|
|
5
5
|
interface Type<out t extends globalThis.Date = globalThis.Date, $ = {}> extends ObjectType<t, $> {
|
|
6
|
-
atOrAfter<const schema extends InclusiveDateRangeSchema>(schema: schema): Type<
|
|
7
|
-
atOrBefore<const schema extends InclusiveDateRangeSchema>(schema: schema): Type<
|
|
8
|
-
laterThan<const schema extends ExclusiveDateRangeSchema>(schema: schema): Type<
|
|
9
|
-
earlierThan<const schema extends ExclusiveDateRangeSchema>(schema: schema): Type<
|
|
6
|
+
atOrAfter<const schema extends InclusiveDateRangeSchema>(schema: schema): Type<applyConstraintSchema<t, "after", schema>, $>;
|
|
7
|
+
atOrBefore<const schema extends InclusiveDateRangeSchema>(schema: schema): Type<applyConstraintSchema<t, "before", schema>, $>;
|
|
8
|
+
laterThan<const schema extends ExclusiveDateRangeSchema>(schema: schema): Type<applyConstraintSchema<t, "after", exclusivizeRangeSchema<schema>>, $>;
|
|
9
|
+
earlierThan<const schema extends ExclusiveDateRangeSchema>(schema: schema): Type<applyConstraintSchema<t, "before", exclusivizeRangeSchema<schema>>, $>;
|
|
10
10
|
}
|
|
11
11
|
export type { Type as DateType };
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { anyOrNever, array } from "@ark/util";
|
|
2
|
-
import type {
|
|
2
|
+
import type { InferredMorph } from "../keywords/inference.ts";
|
|
3
3
|
import type { ArrayType } from "./array.ts";
|
|
4
|
+
import type { BaseType } from "./base.ts";
|
|
4
5
|
import type { DateType } from "./date.ts";
|
|
5
6
|
import type { MorphType } from "./morph.ts";
|
|
6
7
|
import type { NumberType } from "./number.ts";
|
|
@@ -8,9 +9,9 @@ import type { ObjectType } from "./object.ts";
|
|
|
8
9
|
import type { StringType } from "./string.ts";
|
|
9
10
|
import type { ValidatorType } from "./validator.ts";
|
|
10
11
|
export type instantiateType<t, $> = [
|
|
11
|
-
Extract<t,
|
|
12
|
+
Extract<t, InferredMorph>
|
|
12
13
|
] extends [anyOrNever] ? [
|
|
13
14
|
t
|
|
14
|
-
] extends [string] ? StringType<t, $> : [t] extends [number] ? NumberType<t, $> : [t] extends [object] ? [
|
|
15
|
+
] extends [anyOrNever] ? BaseType<t, $> : [t] extends [string] ? StringType<t, $> : [t] extends [number] ? NumberType<t, $> : [t] extends [object] ? [
|
|
15
16
|
t
|
|
16
|
-
] extends [array] ? ArrayType<t, $> : [t] extends [Date] ? DateType<t, $> : ObjectType<t, $> : ValidatorType<t, $> : MorphType<t, $>;
|
|
17
|
+
] extends [array] ? ArrayType<t, $> : [t] extends [Date] ? DateType<t, $> : ObjectType<t, $> : unknown extends t ? BaseType<unknown, $> : ValidatorType<t, $> : MorphType<t, $>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Predicate, PredicateCast } from "@ark/schema";
|
|
2
|
+
import type { applyConstraintSchema, inferPipe, Out, To } from "../keywords/inference.ts";
|
|
3
|
+
import type { type } from "../keywords/keywords.ts";
|
|
4
|
+
import type { BaseType } from "./base.ts";
|
|
5
|
+
/** @ts-ignore cast variance */
|
|
6
|
+
interface Type<out t = unknown, $ = {}> extends BaseType<t, $> {
|
|
7
|
+
to<const def, r = type.infer<def, $>>(def: type.validate<def, $>): Type<inferPipe<t, NoInfer<r>>, $>;
|
|
8
|
+
narrow<narrowed extends this["infer"] = never>(predicate: Predicate<this["infer"]> | PredicateCast<this["infer"], narrowed>): Type<([
|
|
9
|
+
narrowed
|
|
10
|
+
] extends [never] ? applyConstraintSchema<this["inferBrandableOut"], "predicate", Predicate> : narrowed) extends infer o ? this["inferredOutIsIntrospectable"] extends true ? (In: this["inferBrandableIn"]) => To<o> : (In: this["inferBrandableIn"]) => Out<o> : never, $>;
|
|
11
|
+
}
|
|
12
|
+
export type { Type as MorphType };
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import type { Divisor, ExclusiveNumericRangeSchema, exclusivizeRangeSchema, InclusiveNumericRangeSchema } from "@ark/schema";
|
|
2
|
-
import type {
|
|
2
|
+
import type { applyConstraintSchema } from "../keywords/inference.ts";
|
|
3
3
|
import type { ValidatorType } from "./validator.ts";
|
|
4
4
|
/** @ts-ignore cast variance */
|
|
5
5
|
interface Type<out t extends number = number, $ = {}> extends ValidatorType<t, $> {
|
|
6
|
-
divisibleBy<const schema extends Divisor.Schema>(schema: schema): Type<
|
|
7
|
-
atLeast<const schema extends InclusiveNumericRangeSchema>(schema: schema): Type<
|
|
8
|
-
atMost<const schema extends InclusiveNumericRangeSchema>(schema: schema): Type<
|
|
9
|
-
moreThan<const schema extends ExclusiveNumericRangeSchema>(schema: schema): Type<
|
|
10
|
-
lessThan<const schema extends ExclusiveNumericRangeSchema>(schema: schema): Type<
|
|
6
|
+
divisibleBy<const schema extends Divisor.Schema>(schema: schema): Type<applyConstraintSchema<t, "divisor", schema>, $>;
|
|
7
|
+
atLeast<const schema extends InclusiveNumericRangeSchema>(schema: schema): Type<applyConstraintSchema<t, "min", schema>, $>;
|
|
8
|
+
atMost<const schema extends InclusiveNumericRangeSchema>(schema: schema): Type<applyConstraintSchema<t, "max", schema>, $>;
|
|
9
|
+
moreThan<const schema extends ExclusiveNumericRangeSchema>(schema: schema): Type<applyConstraintSchema<t, "min", exclusivizeRangeSchema<schema>>, $>;
|
|
10
|
+
lessThan<const schema extends ExclusiveNumericRangeSchema>(schema: schema): Type<applyConstraintSchema<t, "max", exclusivizeRangeSchema<schema>>, $>;
|
|
11
11
|
}
|
|
12
12
|
export type { Type as NumberType };
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import type { BaseMappedPropInner, OptionalMappedPropInner, Prop } from "@ark/schema";
|
|
2
|
+
import type { arkGet, arkIndexableOf, arkKeyOf, array, ErrorType, inferred, intersectUnion, Json, Key, listable, merge, optionalKeyOf, show, toArkKey } from "@ark/util";
|
|
3
|
+
import type { applyConstraint, constrain, Default, Optional, parseConstraints } from "../keywords/inference.ts";
|
|
4
|
+
import type { type } from "../keywords/keywords.ts";
|
|
5
|
+
import type { ArrayType } from "./array.ts";
|
|
6
|
+
import type { instantiateType } from "./instantiate.ts";
|
|
7
|
+
import type { ValidatorType } from "./validator.ts";
|
|
8
|
+
/** @ts-ignore cast variance */
|
|
9
|
+
interface Type<out t extends object = object, $ = {}> extends ValidatorType<t, $> {
|
|
10
|
+
readonly(): t extends array ? ArrayType<{
|
|
11
|
+
readonly [i in keyof t]: t[i];
|
|
12
|
+
}, $> : Type<{
|
|
13
|
+
readonly [k in keyof t]: t[k];
|
|
14
|
+
}, $>;
|
|
15
|
+
keyof(): instantiateType<arkKeyOf<t>, $>;
|
|
16
|
+
get<const k1 extends arkIndexableOf<t>>(k1: k1 | type.cast<k1>): instantiateType<arkGet<t, k1>, $> extends infer r ? r : never;
|
|
17
|
+
get<const k1 extends arkIndexableOf<t>, const k2 extends arkIndexableOf<arkGet<t, k1>>>(k1: k1 | type.cast<k1>, k2: k2 | type.cast<k2>): instantiateType<arkGet<arkGet<t, k1>, k2>, $> extends infer r ? r : never;
|
|
18
|
+
get<const k1 extends arkIndexableOf<t>, const k2 extends arkIndexableOf<arkGet<t, k1>>, const k3 extends arkIndexableOf<arkGet<arkGet<t, k1>, k2>>>(k1: k1 | type.cast<k1>, k2: k2 | type.cast<k2>, k3: k3 | type.cast<k3>): instantiateType<arkGet<arkGet<arkGet<t, k1>, k2>, k3>, $> extends infer r ? r : never;
|
|
19
|
+
pick<const key extends arkKeyOf<t> = never>(...keys: (key | type.cast<key>)[]): Type<{
|
|
20
|
+
[k in keyof t as Extract<toArkKey<t, k>, key>]: t[k];
|
|
21
|
+
}, $>;
|
|
22
|
+
omit<const key extends arkKeyOf<t> = never>(...keys: (key | type.cast<key>)[]): Type<{
|
|
23
|
+
[k in keyof t as Exclude<toArkKey<t, k>, key>]: t[k];
|
|
24
|
+
}, $>;
|
|
25
|
+
merge<const def, r = type.infer<def, $>>(def: type.validate<def, $> & (r extends object ? unknown : ErrorType<"Merged type must be an object", [actual: r]>)): Type<merge<t, NoInfer<r & object>>, $>;
|
|
26
|
+
required(): Type<{
|
|
27
|
+
[k in keyof t]-?: t[k];
|
|
28
|
+
}, $>;
|
|
29
|
+
partial(): Type<{
|
|
30
|
+
[k in keyof t]?: t[k];
|
|
31
|
+
}, $>;
|
|
32
|
+
map<transformed extends listable<MappedTypeProp>>(flatMapEntry: (entry: typePropOf<t, $>) => transformed): Type<constructMapped<t, transformed>, $>;
|
|
33
|
+
props: array<typePropOf<t, $>>;
|
|
34
|
+
}
|
|
35
|
+
type typePropOf<o, $> = keyof o extends infer k ? k extends keyof o ? typeProp<o, k, $> : never : never;
|
|
36
|
+
type typeProp<o, k extends keyof o, $, t = o[k] & ({} | null)> = parseConstraints<t> extends ([
|
|
37
|
+
infer base,
|
|
38
|
+
infer constraints extends Default | Optional
|
|
39
|
+
]) ? constraints extends Default<infer defaultValue> ? DefaultedTypeProp<k & Key, keyof constraints extends keyof Default ? base : constrain<base, Omit<constraints, keyof Default>>, defaultValue, $> : constraints extends Optional ? BaseTypeProp<"optional", k & Key, keyof constraints extends keyof Optional ? base : constrain<base, Omit<constraints, keyof Optional>>, $> : never : BaseTypeProp<k extends optionalKeyOf<o> ? "optional" : "required", k & Key, t, $>;
|
|
40
|
+
export interface BaseTypeProp<kind extends Prop.Kind = Prop.Kind, k extends Key = Key,
|
|
41
|
+
/** @ts-ignore cast variance */
|
|
42
|
+
out v = unknown, $ = {}> {
|
|
43
|
+
kind: kind;
|
|
44
|
+
key: k;
|
|
45
|
+
value: instantiateType<v, $>;
|
|
46
|
+
meta: ArkEnv.meta;
|
|
47
|
+
toJSON: () => Json;
|
|
48
|
+
}
|
|
49
|
+
export interface DefaultedTypeProp<k extends Key = Key, v = unknown, defaultValue = v, $ = {}> extends BaseTypeProp<"optional", k, v, $> {
|
|
50
|
+
default: defaultValue;
|
|
51
|
+
}
|
|
52
|
+
type MappedTypeProp<k extends Key = Key, v = unknown> = BaseMappedTypeProp<k, v> | OptionalMappedTypeProp<k, v>;
|
|
53
|
+
type BaseMappedTypeProp<k extends Key, v> = merge<BaseMappedPropInner, {
|
|
54
|
+
key: k;
|
|
55
|
+
value: type.cast<v>;
|
|
56
|
+
}>;
|
|
57
|
+
type OptionalMappedTypeProp<k extends Key, v> = merge<OptionalMappedPropInner, {
|
|
58
|
+
key: k;
|
|
59
|
+
value: type.cast<v>;
|
|
60
|
+
default?: v;
|
|
61
|
+
}>;
|
|
62
|
+
type constructMapped<t, transformed extends listable<MappedTypeProp>> = show<intersectUnion<fromTypeProps<t, transformed extends array ? transformed : [transformed]>>>;
|
|
63
|
+
type fromTypeProps<t, props extends array<MappedTypeProp>> = show<{
|
|
64
|
+
[prop in props[number] as Extract<applyHomomorphicOptionality<t, prop>, {
|
|
65
|
+
kind: "required";
|
|
66
|
+
}>["key"]]: prop["value"][inferred];
|
|
67
|
+
} & {
|
|
68
|
+
[prop in props[number] as Extract<applyHomomorphicOptionality<t, prop>, {
|
|
69
|
+
kind: "optional";
|
|
70
|
+
default?: never;
|
|
71
|
+
}>["key"]]?: prop["value"][inferred];
|
|
72
|
+
} & {
|
|
73
|
+
[prop in props[number] as Extract<applyHomomorphicOptionality<t, prop>, {
|
|
74
|
+
kind: "optional";
|
|
75
|
+
default: unknown;
|
|
76
|
+
}>["key"]]: applyConstraint<prop["value"][inferred], Default<prop["default" & keyof prop]>>;
|
|
77
|
+
}>;
|
|
78
|
+
type applyHomomorphicOptionality<t, prop extends MappedTypeProp> = prop["kind"] extends string ? prop : prop & {
|
|
79
|
+
kind: prop["key"] extends optionalKeyOf<t> ? "optional" : "required";
|
|
80
|
+
};
|
|
81
|
+
export type { Type as ObjectType };
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import type { ExactLength, ExclusiveNumericRangeSchema, exclusivizeRangeSchema, InclusiveNumericRangeSchema, Pattern } from "@ark/schema";
|
|
2
|
-
import type {
|
|
2
|
+
import type { applyConstraintSchema } from "../keywords/inference.ts";
|
|
3
3
|
import type { ValidatorType } from "./validator.ts";
|
|
4
4
|
/** @ts-ignore cast variance */
|
|
5
5
|
interface Type<out t extends string = string, $ = {}> extends ValidatorType<t, $> {
|
|
6
|
-
matching<const schema extends Pattern.Schema>(schema: schema): Type<
|
|
7
|
-
atLeastLength<const schema extends InclusiveNumericRangeSchema>(schema: schema): Type<
|
|
8
|
-
atMostLength<const schema extends InclusiveNumericRangeSchema>(schema: schema): Type<
|
|
9
|
-
moreThanLength<const schema extends ExclusiveNumericRangeSchema>(schema: schema): Type<
|
|
10
|
-
lessThanLength<const schema extends ExclusiveNumericRangeSchema>(schema: schema): Type<
|
|
11
|
-
exactlyLength<const schema extends ExactLength.Schema>(schema: schema): Type<
|
|
6
|
+
matching<const schema extends Pattern.Schema>(schema: schema): Type<applyConstraintSchema<t, "pattern", schema>, $>;
|
|
7
|
+
atLeastLength<const schema extends InclusiveNumericRangeSchema>(schema: schema): Type<applyConstraintSchema<t, "minLength", schema>, $>;
|
|
8
|
+
atMostLength<const schema extends InclusiveNumericRangeSchema>(schema: schema): Type<applyConstraintSchema<t, "maxLength", schema>, $>;
|
|
9
|
+
moreThanLength<const schema extends ExclusiveNumericRangeSchema>(schema: schema): Type<applyConstraintSchema<t, "minLength", exclusivizeRangeSchema<schema>>, $>;
|
|
10
|
+
lessThanLength<const schema extends ExclusiveNumericRangeSchema>(schema: schema): Type<applyConstraintSchema<t, "maxLength", exclusivizeRangeSchema<schema>>, $>;
|
|
11
|
+
exactlyLength<const schema extends ExactLength.Schema>(schema: schema): Type<applyConstraintSchema<t, "exactLength", schema>, $>;
|
|
12
12
|
}
|
|
13
13
|
export type { Type as StringType };
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import type { Predicate, PredicateCast } from "@ark/schema";
|
|
2
|
-
import type {
|
|
2
|
+
import type { applyConstraintSchema } from "../keywords/inference.ts";
|
|
3
3
|
import type { BaseType } from "./base.ts";
|
|
4
4
|
import type { instantiateType } from "./instantiate.ts";
|
|
5
5
|
/** @ts-ignore cast variance */
|
|
6
6
|
interface Type<out t = unknown, $ = {}> extends BaseType<t, $> {
|
|
7
7
|
narrow<narrowed extends this["infer"] = never>(predicate: Predicate<this["infer"]> | PredicateCast<this["infer"], narrowed>): instantiateType<[
|
|
8
8
|
narrowed
|
|
9
|
-
] extends [never] ?
|
|
9
|
+
] extends [never] ? applyConstraintSchema<t, "predicate", Predicate> : narrowed, $>;
|
|
10
10
|
}
|
|
11
11
|
export type { Type as ValidatorType };
|
package/out/module.d.ts
CHANGED
|
@@ -12,8 +12,8 @@ export interface BoundModule<exports extends {}, $> extends RootModule<bindExpor
|
|
|
12
12
|
export type bindExportsToScope<exports, $> = {
|
|
13
13
|
[k in keyof exports]: instantiateExport<exports[k], $>;
|
|
14
14
|
} & unknown;
|
|
15
|
-
export type Submodule<exports extends {}> = RootModule<exports & ("
|
|
16
|
-
[inferred]: exports["
|
|
15
|
+
export type Submodule<exports extends {}> = RootModule<exports & ("root" extends keyof exports ? {
|
|
16
|
+
[inferred]: exports["root"];
|
|
17
17
|
} : {})>;
|
|
18
18
|
export type instantiateExport<t, $> = [
|
|
19
19
|
t
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { writeUnboundableMessage } from "@ark/schema";
|
|
2
2
|
import type { ErrorMessage, array, typeToString } from "@ark/util";
|
|
3
|
-
import type { LimitLiteral } from "../../keywords/
|
|
4
|
-
import type { Comparator } from "../
|
|
5
|
-
import type { BoundExpressionKind, writeInvalidLimitMessage } from "../
|
|
3
|
+
import type { LimitLiteral } from "../../keywords/inference.ts";
|
|
4
|
+
import type { Comparator } from "../reduce/shared.ts";
|
|
5
|
+
import type { BoundExpressionKind, writeInvalidLimitMessage } from "../shift/operator/bounds.ts";
|
|
6
6
|
import type { inferAstIn } from "./infer.ts";
|
|
7
7
|
import type { astToString } from "./utils.ts";
|
|
8
8
|
import type { validateAst } from "./validate.ts";
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { writeUnassignableDefaultValueMessage } from "@ark/schema";
|
|
2
|
+
import type { ErrorMessage } from "@ark/util";
|
|
3
|
+
import type { type } from "../../keywords/keywords.ts";
|
|
4
|
+
import type { UnitLiteral } from "../shift/operator/default.ts";
|
|
5
|
+
import type { inferAstOut } from "./infer.ts";
|
|
6
|
+
import type { astToString } from "./utils.ts";
|
|
7
|
+
import type { validateAst } from "./validate.ts";
|
|
8
|
+
export type validateDefault<baseAst, unitLiteral extends UnitLiteral, $, args> = validateAst<baseAst, $, args> extends infer e extends ErrorMessage ? e : type.infer<unitLiteral> extends inferAstOut<baseAst, $, args> ? undefined : ErrorMessage<writeUnassignableDefaultValueMessage<astToString<baseAst>, unitLiteral>>;
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import type { GenericAst } from "@ark/schema";
|
|
2
|
-
import type { Hkt, array } from "@ark/util";
|
|
3
|
-
import type {
|
|
4
|
-
import type {
|
|
2
|
+
import type { Hkt, arkKeyOf, array } from "@ark/util";
|
|
3
|
+
import type { Date } from "../../keywords/constructors/Date.ts";
|
|
4
|
+
import type { Default, LimitLiteral, Optional, applyConstraint, applyConstraintSchema, distill, inferIntersection, normalizeLimit } from "../../keywords/inference.ts";
|
|
5
|
+
import type { type } from "../../keywords/keywords.ts";
|
|
5
6
|
import type { UnparsedScope } from "../../scope.ts";
|
|
6
|
-
import type { inferAmbient } from "../../type.ts";
|
|
7
7
|
import type { inferDefinition } from "../definition.ts";
|
|
8
|
-
import type { Comparator, MinComparator } from "../
|
|
8
|
+
import type { Comparator, MinComparator } from "../reduce/shared.ts";
|
|
9
9
|
export type inferAstRoot<ast, $, args> = ast extends array ? inferExpression<ast, $, args> : never;
|
|
10
|
-
export type inferAstIn<ast, $, args> =
|
|
11
|
-
export type inferAstOut<ast, $, args> =
|
|
10
|
+
export type inferAstIn<ast, $, args> = distill.In<inferAstRoot<ast, $, args>>;
|
|
11
|
+
export type inferAstOut<ast, $, args> = distill.Out<inferAstRoot<ast, $, args>>;
|
|
12
12
|
export type DefAst<def = unknown, alias extends string = string> = [
|
|
13
13
|
def,
|
|
14
14
|
"def",
|
|
@@ -25,15 +25,15 @@ export type inferExpression<ast, $, args> = ast extends array ? ast extends Infe
|
|
|
25
25
|
[i in keyof argAsts]: inferExpression<argAsts[i], $, args>;
|
|
26
26
|
}> : inferDefinition<g["bodyDef"], resolveScope<g["$"], $>, {
|
|
27
27
|
[i in keyof g["names"] & `${number}` as g["names"][i]]: inferExpression<argAsts[i & keyof argAsts], resolveScope<g["arg$"], $>, args>;
|
|
28
|
-
}> : ast[1] extends "[]" ? inferExpression<ast[0], $, args>[] : ast[1] extends "|" ? inferExpression<ast[0], $, args> | inferExpression<ast[2], $, args> : ast[1] extends "&" ? inferIntersection<inferExpression<ast[0], $, args>, inferExpression<ast[2], $, args>> : ast[1] extends "=" ?
|
|
29
|
-
export type
|
|
28
|
+
}> : ast[1] extends "[]" ? inferExpression<ast[0], $, args>[] : ast[1] extends "|" ? inferExpression<ast[0], $, args> | inferExpression<ast[2], $, args> : ast[1] extends "&" ? inferIntersection<inferExpression<ast[0], $, args>, inferExpression<ast[2], $, args>> : ast[1] extends "=" ? type.infer<ast[2]> extends infer defaultValue ? applyConstraint<inferExpression<ast[0], $, args>, Default<defaultValue>> : never : ast[1] extends Comparator ? ast[0] extends LimitLiteral ? brandBound<inferExpression<ast[2], $, args>, ast[1], ast[0]> : brandBound<inferExpression<ast[0], $, args>, ast[1], ast[2] & LimitLiteral> : ast[1] extends "%" ? applyConstraintSchema<inferExpression<ast[0], $, args>, "divisor", ast[2] & number> : ast[1] extends "?" ? applyConstraint<inferExpression<ast[0], $, args>, Optional> : ast[0] extends "keyof" ? arkKeyOf<inferExpression<ast[1], $, args>> : never : never;
|
|
29
|
+
export type brandBound<brandableIn, comparator extends Comparator, limit extends LimitLiteral> = distill.In<brandableIn> extends infer In ? comparator extends "==" ? In extends number ? limit : In extends Date ? Date.literal<normalizeLimit<limit>> : applyConstraintSchema<brandableIn, "exactLength", limit & number> : applyConstraintSchema<brandableIn, In extends number ? comparator extends MinComparator ? "min" : "max" : In extends string | array ? comparator extends MinComparator ? "minLength" : "maxLength" : comparator extends MinComparator ? "after" : "before", {
|
|
30
30
|
rule: normalizeLimit<limit>;
|
|
31
31
|
exclusive: comparator extends ">" | "<" ? true : false;
|
|
32
32
|
}> : never;
|
|
33
33
|
export type PrefixOperator = "keyof" | "instanceof" | "===" | "node";
|
|
34
34
|
export type PrefixExpression<operator extends PrefixOperator = PrefixOperator, operand = unknown> = [operator, operand];
|
|
35
|
-
export type PostfixOperator = "[]";
|
|
35
|
+
export type PostfixOperator = "[]" | "?";
|
|
36
36
|
export type PostfixExpression<operator extends PostfixOperator = PostfixOperator, operand = unknown> = readonly [operand, operator];
|
|
37
|
-
export type InfixOperator = "|" | "&" | Comparator | "%" | ":" | "=>" | "@";
|
|
37
|
+
export type InfixOperator = "|" | "&" | Comparator | "%" | ":" | "=>" | "@" | "=";
|
|
38
38
|
export type InfixExpression<operator extends InfixOperator = InfixOperator, l = unknown, r = unknown> = [l, operator, r];
|
|
39
39
|
export {};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import type { Stringifiable } from "@ark/util";
|
|
2
|
-
import type { Comparator } from "../
|
|
2
|
+
import type { Comparator } from "../reduce/shared.ts";
|
|
3
3
|
import type { DefAst, InferredAst, InfixExpression, PostfixExpression } from "./infer.ts";
|
|
4
4
|
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}` : "...";
|
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
import type { arkKind, GenericParamAst, PrivateDeclaration, writeMissingSubmoduleAccessMessage, writeUnsatisfiedParameterConstraintMessage } from "@ark/schema";
|
|
2
2
|
import type { anyOrNever, array, BigintLiteral, charsAfterFirst, Completion, ErrorMessage, NumberLiteral, typeToString, writeMalformedNumericLiteralMessage } from "@ark/util";
|
|
3
3
|
import type { Generic } from "../../generic.ts";
|
|
4
|
-
import type { Comparator } from "../
|
|
5
|
-
import type { writeInvalidGenericArgCountMessage } from "../
|
|
6
|
-
import type { UnitLiteral } from "../
|
|
7
|
-
import type { parseString } from "../string
|
|
4
|
+
import type { Comparator } from "../reduce/shared.ts";
|
|
5
|
+
import type { writeInvalidGenericArgCountMessage } from "../shift/operand/genericArgs.ts";
|
|
6
|
+
import type { UnitLiteral } from "../shift/operator/default.ts";
|
|
7
|
+
import type { parseString } from "../string.ts";
|
|
8
8
|
import type { validateRange } from "./bounds.ts";
|
|
9
9
|
import type { validateDefault } from "./default.ts";
|
|
10
10
|
import type { validateDivisor } from "./divisor.ts";
|
|
11
11
|
import type { DefAst, GenericInstantiationAst, inferAstRoot, InferredAst, InfixExpression, PostfixExpression } from "./infer.ts";
|
|
12
12
|
import type { validateKeyof } from "./keyof.ts";
|
|
13
13
|
import type { astToString } from "./utils.ts";
|
|
14
|
-
export type validateAst<ast, $, args> = ast extends ErrorMessage ? ast : ast extends InferredAst ? validateInferredAst<ast[0], ast[2]> : ast extends DefAst ? ast[2] extends PrivateDeclaration ? ErrorMessage<writePrefixedPrivateReferenceMessage<ast[2]>> : undefined : ast extends PostfixExpression<infer operator, infer operand> ? operator extends "[]" ? validateAst<operand, $, args> : never : ast extends InfixExpression<infer operator, infer l, infer r> ? operator extends "&" | "|" ? validateInfix<ast, $, args> : operator extends Comparator ? validateRange<l, operator, r, $, args> : operator extends "%" ? validateDivisor<l, $, args> :
|
|
14
|
+
export type validateAst<ast, $, args> = ast extends ErrorMessage ? ast : ast extends InferredAst ? validateInferredAst<ast[0], ast[2]> : ast extends DefAst ? ast[2] extends PrivateDeclaration ? ErrorMessage<writePrefixedPrivateReferenceMessage<ast[2]>> : undefined : ast extends PostfixExpression<infer operator, infer operand> ? operator extends "[]" ? validateAst<operand, $, args> : operator extends "?" ? validateAst<operand, $, args> : never : ast extends InfixExpression<infer operator, infer l, infer r> ? operator extends "&" | "|" ? validateInfix<ast, $, args> : operator extends Comparator ? validateRange<l, operator, r, $, args> : operator extends "%" ? validateDivisor<l, $, args> : operator extends "=" ? validateDefault<l, r & UnitLiteral, $, args> : never : ast extends ["keyof", infer operand] ? validateKeyof<operand, $, args> : ast extends GenericInstantiationAst<infer g, infer argAsts> ? validateGenericArgs<g["paramsAst"], argAsts, $, args, []> : ErrorMessage<writeUnexpectedExpressionMessage<astToString<ast>>> & {
|
|
15
15
|
ast: ast;
|
|
16
16
|
};
|
|
17
17
|
type writeUnexpectedExpressionMessage<expression extends string> = `Unexpectedly failed to parse the expression resulting from ${expression}`;
|
|
@@ -20,7 +20,7 @@ export declare const writePrefixedPrivateReferenceMessage: <def extends PrivateD
|
|
|
20
20
|
export type writePrefixedPrivateReferenceMessage<def extends PrivateDeclaration> = `Private type references should not include '#'. Use '${charsAfterFirst<def>}' instead.`;
|
|
21
21
|
type validateInferredAst<inferred, def extends string> = def extends NumberLiteral ? number extends inferred ? ErrorMessage<writeMalformedNumericLiteralMessage<def, "number">> : undefined : def extends BigintLiteral ? bigint extends inferred ? ErrorMessage<writeMalformedNumericLiteralMessage<def, "bigint">> : undefined : [inferred] extends [anyOrNever] ? undefined : def extends PrivateDeclaration ? ErrorMessage<writePrefixedPrivateReferenceMessage<def>> : inferred extends Generic ? ErrorMessage<writeInvalidGenericArgCountMessage<def, inferred["names"], []>> : inferred extends {
|
|
22
22
|
[arkKind]: "module";
|
|
23
|
-
} ? "
|
|
23
|
+
} ? "root" extends keyof inferred ? undefined : ErrorMessage<writeMissingSubmoduleAccessMessage<def>> : def extends ErrorMessage ? def : undefined;
|
|
24
24
|
export type validateString<def extends string, $, args> = validateAst<parseString<def, $, args>, $, args> extends (infer result extends ErrorMessage) ? result extends Completion<infer text> ? text : result : def;
|
|
25
25
|
type validateInfix<ast extends InfixExpression, $, args> = validateAst<ast[0], $, args> extends infer e extends ErrorMessage ? e : validateAst<ast[2], $, args> extends infer e extends ErrorMessage ? e : undefined;
|
|
26
26
|
export {};
|
|
@@ -1,17 +1,16 @@
|
|
|
1
|
-
import { type BaseRoot } from "@ark/schema";
|
|
1
|
+
import { type BaseParseContext, type BaseRoot } from "@ark/schema";
|
|
2
2
|
import { type ErrorMessage, type Fn, type Primitive, type anyOrNever, type array, type defined, type equals, type ifEmptyObjectLiteral, type objectKindOrDomainOf, type optionalKeyOf, type requiredKeyOf, type show } from "@ark/util";
|
|
3
|
-
import type { type } from "../keywords/
|
|
3
|
+
import type { type } from "../keywords/keywords.ts";
|
|
4
4
|
import type { string } from "../keywords/string/string.ts";
|
|
5
|
-
import type {
|
|
5
|
+
import type { validateString } from "./ast/validate.ts";
|
|
6
6
|
import { type inferObjectLiteral, type validateObjectLiteral } from "./objectLiteral.ts";
|
|
7
|
-
import type {
|
|
8
|
-
import type { BaseCompletions, inferString } from "./string/string.ts";
|
|
7
|
+
import type { BaseCompletions, inferString } from "./string.ts";
|
|
9
8
|
import { type TupleExpression, type inferTuple, type validateTuple } from "./tuple.ts";
|
|
10
|
-
export declare const parseObject: (def: object, ctx:
|
|
9
|
+
export declare const parseObject: (def: object, ctx: BaseParseContext) => BaseRoot;
|
|
11
10
|
export type inferDefinition<def, $, args> = [
|
|
12
11
|
def
|
|
13
|
-
] extends [anyOrNever] ? def : def extends type.cast<infer t> ? ifEmptyObjectLiteral<
|
|
14
|
-
export type validateDefinition<def, $, args> = null extends undefined ? ErrorMessage<`'strict' or 'strictNullChecks' must be set to true in your tsconfig's 'compilerOptions'`> : [def] extends [Terminal] ? def : def extends string ? validateString<def, $, args> : def extends array ? validateTuple<def, $, args> : def extends BadDefinitionType ? ErrorMessage<writeBadDefinitionTypeMessage<objectKindOrDomainOf<def>>> : unknown extends def ? BaseCompletions<$, args> | {} : validateObjectLiteral<def, $, args>;
|
|
12
|
+
] extends [anyOrNever] ? def : def extends type.cast<infer t> ? ifEmptyObjectLiteral<def, object, t> : def extends ThunkCast<infer t> ? t : def extends string ? inferString<def, $, args> : def extends array ? inferTuple<def, $, args> : def extends RegExp ? string.matching<string> : def extends object ? inferObjectLiteral<def, $, args> : never;
|
|
13
|
+
export type validateDefinition<def, $, args> = null extends undefined ? ErrorMessage<`'strict' or 'strictNullChecks' must be set to true in your tsconfig's 'compilerOptions'`> : [def] extends [Terminal] ? def : def extends string ? validateString<def, $, args> : def extends array ? validateTuple<def, $, args> : def extends BadDefinitionType ? ErrorMessage<writeBadDefinitionTypeMessage<objectKindOrDomainOf<def>>> : unknown extends def ? BaseCompletions<$, args> | {} : RegExp extends def ? def : validateObjectLiteral<def, $, args>;
|
|
15
14
|
export type validateDeclared<declared, def, $, args> = def extends validateDefinition<def, $, args> ? validateInference<def, declared, $, args> : validateDefinition<def, $, args>;
|
|
16
15
|
type validateInference<def, declared, $, args> = def extends RegExp | type.cast<unknown> | ThunkCast | TupleExpression ? validateShallowInference<def, declared, $, args> : def extends array ? declared extends array ? {
|
|
17
16
|
[i in keyof declared]: i extends keyof def ? validateInference<def[i], declared[i], $, args> : declared[i];
|
|
@@ -25,7 +24,7 @@ type declarationMismatch<def, declared, $, args> = {
|
|
|
25
24
|
declared: declared;
|
|
26
25
|
inferred: inferDefinition<def, $, args>;
|
|
27
26
|
};
|
|
28
|
-
type Terminal =
|
|
27
|
+
type Terminal = type.cast<unknown> | Fn;
|
|
29
28
|
export type ThunkCast<t = unknown> = () => type.cast<t>;
|
|
30
29
|
type BadDefinitionType = Exclude<Primitive, string>;
|
|
31
30
|
export declare const writeBadDefinitionTypeMessage: <actual extends string>(actual: actual) => writeBadDefinitionTypeMessage<actual>;
|
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import { type BaseRoot, type Index, type Optional, type Required, type UndeclaredKeyBehavior, type writeInvalidPropertyKeyMessage } from "@ark/schema";
|
|
2
|
-
import { type anyOrNever, type
|
|
3
|
-
import type { constrain
|
|
4
|
-
import type {
|
|
1
|
+
import { type BaseParseContext, type BaseRoot, type Index, type Optional, type Required, type UndeclaredKeyBehavior, type writeInvalidPropertyKeyMessage } from "@ark/schema";
|
|
2
|
+
import { type anyOrNever, type Dict, type ErrorMessage, type ErrorType, type EscapeToken, type Key, type listable, type merge, type show } from "@ark/util";
|
|
3
|
+
import type { constrain } from "../keywords/inference.ts";
|
|
4
|
+
import type { astToString } from "./ast/utils.ts";
|
|
5
|
+
import type { validateString } from "./ast/validate.ts";
|
|
5
6
|
import type { inferDefinition, validateDefinition } from "./definition.ts";
|
|
6
|
-
|
|
7
|
-
import type { validateString } from "./semantic/validate.ts";
|
|
8
|
-
export declare const parseObjectLiteral: (def: Dict, ctx: ParseContext) => BaseRoot;
|
|
7
|
+
export declare const parseObjectLiteral: (def: Dict, ctx: BaseParseContext) => BaseRoot;
|
|
9
8
|
export declare const writeInvalidUndeclaredBehaviorMessage: (actual: unknown) => string;
|
|
10
9
|
export declare const nonLeadingSpreadError = "Spread operator may only be used as the first key in an object";
|
|
11
10
|
export type inferObjectLiteral<def extends object, $, args> = show<"..." extends keyof def ? merge<inferDefinition<def["..."], $, args>, _inferObjectLiteral<def, $, args>> : _inferObjectLiteral<def, $, args>>;
|
|
@@ -13,41 +12,18 @@ export type inferObjectLiteral<def extends object, $, args> = show<"..." extends
|
|
|
13
12
|
* Infers the contents of an object literal, ignoring a spread definition
|
|
14
13
|
*/
|
|
15
14
|
type _inferObjectLiteral<def extends object, $, args> = {
|
|
16
|
-
-readonly [k in keyof def as nonOptionalKeyFrom<k, $, args>]:
|
|
15
|
+
-readonly [k in keyof def as nonOptionalKeyFrom<k, $, args>]: [
|
|
17
16
|
def[k]
|
|
18
|
-
] extends [anyOrNever] ? def[k] :
|
|
17
|
+
] extends [anyOrNever] ? def[k] : inferDefinition<def[k], $, args>;
|
|
19
18
|
} & {
|
|
20
19
|
-readonly [k in keyof def as optionalKeyFrom<k>]?: inferDefinition<def[k], $, args>;
|
|
21
20
|
};
|
|
22
21
|
export type validateObjectLiteral<def, $, args> = {
|
|
23
|
-
[k in keyof def]: k extends IndexKey<infer indexDef> ? validateString<indexDef, $, args> extends ErrorMessage<infer message> ? ErrorType<message> : inferDefinition<indexDef, $, args> extends Key | constrain<Key, {}> ? validateDefinition<def[k], $, args> : ErrorType<writeInvalidPropertyKeyMessage<indexDef>> : k extends "..." ? inferDefinition<def[k], $, args> extends object ? validateDefinition<def[k], $, args> : ErrorType<writeInvalidSpreadTypeMessage<astToString<def[k]>>> : k extends "+" ? UndeclaredKeyBehavior :
|
|
22
|
+
[k in keyof def]: k extends IndexKey<infer indexDef> ? validateString<indexDef, $, args> extends ErrorMessage<infer message> ? ErrorType<message> : inferDefinition<indexDef, $, args> extends Key | constrain<Key, {}> ? validateDefinition<def[k], $, args> : ErrorType<writeInvalidPropertyKeyMessage<indexDef>> : k extends "..." ? inferDefinition<def[k], $, args> extends object ? validateDefinition<def[k], $, args> : ErrorType<writeInvalidSpreadTypeMessage<astToString<def[k]>>> : k extends "+" ? UndeclaredKeyBehavior : validateDefinition<def[k], $, args>;
|
|
24
23
|
};
|
|
25
|
-
type validateDefaultableValue<def, k extends keyof def, $, args> = def[k] extends DefaultValueTuple ? [
|
|
26
|
-
def[k]
|
|
27
|
-
] extends [anyOrNever] ?
|
|
28
|
-
/** this extra [anyOrNever] check is required to ensure that nested `type` invocations
|
|
29
|
-
* like the following are not prematurely validated by the outer call:
|
|
30
|
-
*
|
|
31
|
-
* ```ts
|
|
32
|
-
* type({
|
|
33
|
-
* "test?": type("string").pipe(x => x === "true")
|
|
34
|
-
* })
|
|
35
|
-
* ```
|
|
36
|
-
*/
|
|
37
|
-
def[k] : validateDefaultValueTuple<def[k], k, $, args> : validateDefinition<def[k], $, args>;
|
|
38
|
-
type DefaultValueTuple<baseDef = unknown, defaultValue = unknown> = readonly [
|
|
39
|
-
baseDef,
|
|
40
|
-
"=",
|
|
41
|
-
defaultValue
|
|
42
|
-
];
|
|
43
|
-
type validateDefaultValueTuple<def extends DefaultValueTuple, k extends PropertyKey, $, args> = parseKey<k>["kind"] extends "required" ? conform<def, readonly [
|
|
44
|
-
validateDefinition<def[0], $, args>,
|
|
45
|
-
"=",
|
|
46
|
-
inferDefinition<def[0], $, args>
|
|
47
|
-
]> : ErrorMessage<invalidDefaultKeyKindMessage>;
|
|
48
24
|
type nonOptionalKeyFrom<k, $, args> = parseKey<k> extends PreparsedKey<"required", infer inner> ? inner : parseKey<k> extends PreparsedKey<"index", infer inner> ? inferDefinition<inner, $, args> extends infer t extends Key ? t : never : never;
|
|
49
25
|
type optionalKeyFrom<k> = parseKey<k> extends PreparsedKey<"optional", infer inner> ? inner : never;
|
|
50
|
-
type PreparsedKey<kind extends ParsedKeyKind = ParsedKeyKind, key extends Key = Key> = {
|
|
26
|
+
export type PreparsedKey<kind extends ParsedKeyKind = ParsedKeyKind, key extends Key = Key> = {
|
|
51
27
|
kind: kind;
|
|
52
28
|
key: key;
|
|
53
29
|
};
|
|
@@ -66,9 +42,7 @@ export type ParsedSpreadEntry = {
|
|
|
66
42
|
kind: "spread";
|
|
67
43
|
node: BaseRoot;
|
|
68
44
|
};
|
|
69
|
-
export declare const parseEntry: (key: Key, value: unknown, ctx:
|
|
70
|
-
export declare const invalidDefaultKeyKindMessage = "Only required keys may specify default values, e.g. { value: 'number = 0' }";
|
|
71
|
-
export type invalidDefaultKeyKindMessage = typeof invalidDefaultKeyKindMessage;
|
|
45
|
+
export declare const parseEntry: (key: Key, value: unknown, ctx: BaseParseContext) => listable<ParsedEntry>;
|
|
72
46
|
declare const parseKey: (key: Key) => PreparsedKey;
|
|
73
47
|
type parseKey<k> = k extends `${infer inner}?` ? inner extends `${infer baseName}${EscapeToken}` ? PreparsedKey.from<{
|
|
74
48
|
kind: "required";
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { append, escapeToken,
|
|
3
|
-
import { writeUnassignableDefaultValueMessage } from "./semantic/default.js";
|
|
1
|
+
import { normalizeIndex } from "@ark/schema";
|
|
2
|
+
import { append, escapeToken, printable, stringAndSymbolicEntriesOf, throwParseError } from "@ark/util";
|
|
4
3
|
export const parseObjectLiteral = (def, ctx) => {
|
|
5
4
|
let spread;
|
|
6
5
|
const structure = {};
|
|
@@ -29,7 +28,7 @@ export const parseObjectLiteral = (def, ctx) => {
|
|
|
29
28
|
structure[entry.kind] = append(structure[entry.kind], entry);
|
|
30
29
|
}
|
|
31
30
|
const structureNode = ctx.$.node("structure", structure);
|
|
32
|
-
return ctx.$.
|
|
31
|
+
return ctx.$.parseSchema({
|
|
33
32
|
domain: "object",
|
|
34
33
|
structure: spread?.merge(structureNode) ?? structureNode
|
|
35
34
|
});
|
|
@@ -44,25 +43,10 @@ export const parseEntry = (key, value, ctx) => {
|
|
|
44
43
|
return { kind: "undeclared", behavior: value };
|
|
45
44
|
}
|
|
46
45
|
if (parsedKey.kind === "...")
|
|
47
|
-
return { kind: "spread", node: ctx.$.
|
|
48
|
-
const parsedValue =
|
|
49
|
-
[ctx.$.parse(value[0], ctx), "=", value[2]]
|
|
50
|
-
: ctx.$.parse(value, ctx, true);
|
|
51
|
-
if (isArray(parsedValue)) {
|
|
52
|
-
if (parsedKey.kind !== "required")
|
|
53
|
-
throwParseError(invalidDefaultKeyKindMessage);
|
|
54
|
-
const out = parsedValue[0].traverse(parsedValue[2]);
|
|
55
|
-
if (out instanceof ArkErrors) {
|
|
56
|
-
throwParseError(writeUnassignableDefaultValueMessage(printable(parsedKey.key), out.message));
|
|
57
|
-
}
|
|
58
|
-
return ctx.$.node("optional", {
|
|
59
|
-
key: parsedKey.key,
|
|
60
|
-
value: parsedValue[0],
|
|
61
|
-
default: parsedValue[2]
|
|
62
|
-
});
|
|
63
|
-
}
|
|
46
|
+
return { kind: "spread", node: ctx.$.parseOwnDefinitionFormat(value, ctx) };
|
|
47
|
+
const parsedValue = ctx.$.parseOwnDefinitionFormat(value, ctx);
|
|
64
48
|
if (parsedKey.kind === "index") {
|
|
65
|
-
const signature = ctx.$.
|
|
49
|
+
const signature = ctx.$.parseOwnDefinitionFormat(parsedKey.key, ctx);
|
|
66
50
|
const normalized = normalizeIndex(signature, parsedValue, ctx.$);
|
|
67
51
|
return (normalized.index ?
|
|
68
52
|
normalized.required ?
|
|
@@ -75,8 +59,6 @@ export const parseEntry = (key, value, ctx) => {
|
|
|
75
59
|
value: parsedValue
|
|
76
60
|
});
|
|
77
61
|
};
|
|
78
|
-
// single quote use here is better for TypeScript's inlined error to avoid escapes
|
|
79
|
-
export const invalidDefaultKeyKindMessage = `Only required keys may specify default values, e.g. { value: 'number = 0' }`;
|
|
80
62
|
const parseKey = (key) => typeof key === "symbol" ? { kind: "required", key }
|
|
81
63
|
: key.at(-1) === "?" ?
|
|
82
64
|
key.at(-2) === escapeToken ?
|