arktype 2.0.0-rc.6 → 2.0.0-rc.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 +8 -9
- package/out/generic.js +3 -5
- package/out/index.d.ts +1 -1
- package/out/keys.d.ts +5 -3
- package/out/keywords/ark.d.ts +6 -1
- package/out/keywords/ark.js +1 -0
- package/out/keywords/ast.d.ts +27 -17
- package/out/keywords/constructors/Array.js +3 -3
- package/out/keywords/constructors/FormData.js +5 -5
- package/out/keywords/constructors/constructors.d.ts +0 -1
- package/out/keywords/number/epoch.js +2 -2
- package/out/keywords/number/integer.js +2 -2
- package/out/keywords/number/number.js +2 -2
- package/out/keywords/string/capitalize.js +2 -2
- package/out/keywords/string/creditCard.js +2 -2
- package/out/keywords/string/date.js +11 -11
- package/out/keywords/string/integer.js +2 -2
- package/out/keywords/string/ip.js +2 -2
- package/out/keywords/string/json.d.ts +1 -0
- package/out/keywords/string/json.js +21 -13
- package/out/keywords/string/lower.js +2 -2
- package/out/keywords/string/normalize.js +3 -3
- package/out/keywords/string/numeric.js +2 -2
- package/out/keywords/string/string.d.ts +1 -1
- package/out/keywords/string/trim.js +2 -2
- package/out/keywords/string/upper.js +2 -2
- package/out/keywords/string/url.js +4 -4
- package/out/methods/base.d.ts +10 -5
- package/out/methods/instantiate.d.ts +2 -1
- package/out/methods/object.d.ts +31 -6
- package/out/parser/definition.d.ts +4 -5
- package/out/parser/objectLiteral.d.ts +4 -5
- package/out/parser/objectLiteral.js +4 -4
- package/out/parser/semantic/infer.d.ts +4 -3
- package/out/parser/semantic/validate.d.ts +1 -1
- package/out/parser/string/reduce/dynamic.d.ts +3 -4
- package/out/parser/string/reduce/dynamic.js +3 -2
- package/out/parser/string/shift/operand/unenclosed.js +6 -2
- package/out/parser/string/shift/scanner.d.ts +3 -1
- package/out/parser/string/shift/scanner.js +5 -3
- package/out/parser/string/string.d.ts +1 -1
- package/out/parser/string/string.js +2 -0
- package/out/parser/tuple.d.ts +47 -36
- package/out/parser/tuple.js +24 -25
- package/out/scope.d.ts +22 -25
- package/out/scope.js +33 -29
- package/out/type.d.ts +10 -9
- package/out/type.js +9 -9
- package/package.json +3 -3
package/out/generic.d.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import { GenericRoot, type arkKind, type GenericAst, type GenericParamAst, type GenericParamDef, type genericParamNames, type LazyGenericBody } from "@ark/schema";
|
|
1
|
+
import { GenericRoot, type arkKind, type BaseParseContext, type GenericAst, type GenericParamAst, type GenericParamDef, type genericParamNames, type LazyGenericBody } from "@ark/schema";
|
|
2
2
|
import { type array, type Callable, type conform, type ErrorMessage, type ErrorType, type Hkt, type Json, type WhiteSpaceToken } from "@ark/util";
|
|
3
3
|
import type { type } from "./keywords/ark.ts";
|
|
4
4
|
import type { inferDefinition, validateDefinition } from "./parser/definition.ts";
|
|
5
5
|
import type { inferAstRoot } from "./parser/semantic/infer.ts";
|
|
6
6
|
import type { validateAst } from "./parser/semantic/validate.ts";
|
|
7
7
|
import type { state, StaticState } from "./parser/string/reduce/static.ts";
|
|
8
|
-
import { Scanner } from "./parser/string/shift/scanner.ts";
|
|
8
|
+
import type { Scanner } from "./parser/string/shift/scanner.ts";
|
|
9
9
|
import { parseUntilFinalizer } from "./parser/string/string.ts";
|
|
10
|
-
import type {
|
|
10
|
+
import type { Scope } from "./scope.ts";
|
|
11
11
|
import type { Type } from "./type.ts";
|
|
12
12
|
export type ParameterString<params extends string = string> = `<${params}>`;
|
|
13
13
|
export type extractParams<s extends ParameterString> = s extends ParameterString<infer params> ? params : never;
|
|
@@ -16,9 +16,9 @@ export type validateGenericArg<arg, param extends GenericParamAst, $> = type.inf
|
|
|
16
16
|
export type GenericInstantiator<params extends array<GenericParamAst>, def, $, args$> = params["length"] extends 1 ? {
|
|
17
17
|
<const a, r = instantiateGeneric<def, params, [a], $, args$>>(a: type.validate<a, args$> & validateGenericArg<a, params[0], args$>): r;
|
|
18
18
|
} : params["length"] extends 2 ? {
|
|
19
|
-
<const a, const b, r = instantiateGeneric<def, params, [a, b], $, args$>>(...args: [
|
|
20
|
-
validateGenericArg<a, params[0], args$>,
|
|
21
|
-
validateGenericArg<b, params[1], args$>
|
|
19
|
+
<const a, const b, r = instantiateGeneric<def, params, [a, b], $, args$>>(...args: [
|
|
20
|
+
type.validate<a, args$> & validateGenericArg<a, params[0], args$>,
|
|
21
|
+
type.validate<b, args$> & validateGenericArg<b, params[1], args$>
|
|
22
22
|
]): r;
|
|
23
23
|
} : params["length"] extends 3 ? {
|
|
24
24
|
<const a, const b, const c, r = instantiateGeneric<def, params, [a, b, c], $, args$>>(...args: [
|
|
@@ -127,16 +127,15 @@ export type GenericDeclaration<name extends string = string, params extends Para
|
|
|
127
127
|
export type parseValidGenericParams<def extends ParameterString, $> = conform<parseGenericParams<extractParams<def>, $>, array<GenericParamAst>>;
|
|
128
128
|
export declare const emptyGenericParameterMessage = "An empty string is not a valid generic parameter name";
|
|
129
129
|
export type emptyGenericParameterMessage = typeof emptyGenericParameterMessage;
|
|
130
|
-
export declare const parseGenericParams: (def: string, ctx: ParseContext) => array<GenericParamDef>;
|
|
131
130
|
export type parseGenericParams<def extends string, $> = parseNextNameChar<Scanner.skipWhitespace<def>, "", [
|
|
132
131
|
], $>;
|
|
133
132
|
type ParamsTerminator = WhiteSpaceToken | ",";
|
|
134
|
-
declare const
|
|
133
|
+
export declare const parseGenericParamName: (scanner: Scanner, result: GenericParamDef[], ctx: BaseParseContext) => GenericParamDef[];
|
|
135
134
|
type parseName<unscanned extends string, result extends array<GenericParamAst>, $> = parseNextNameChar<Scanner.skipWhitespace<unscanned>, "", result, $>;
|
|
136
135
|
type parseNextNameChar<unscanned extends string, name extends string, result extends array<GenericParamAst>, $> = unscanned extends `${infer lookahead}${infer nextUnscanned}` ? lookahead extends ParamsTerminator ? name extends "" ? ErrorMessage<emptyGenericParameterMessage> : lookahead extends "," ? parseName<nextUnscanned, [...result, [name, unknown]], $> : lookahead extends WhiteSpaceToken ? _parseOptionalConstraint<nextUnscanned, name, result, $> : never : parseNextNameChar<nextUnscanned, `${name}${lookahead}`, result, $> : name extends "" ? result : [...result, [name, unknown]];
|
|
137
136
|
declare const extendsToken = "extends ";
|
|
138
137
|
type extendsToken = typeof extendsToken;
|
|
139
|
-
declare const _parseOptionalConstraint: (scanner: Scanner, name: string, result: GenericParamDef[], ctx:
|
|
138
|
+
declare const _parseOptionalConstraint: (scanner: Scanner, name: string, result: GenericParamDef[], ctx: BaseParseContext) => GenericParamDef[];
|
|
140
139
|
type _parseOptionalConstraint<unscanned extends string, name extends string, result extends array<GenericParamAst>, $> = Scanner.skipWhitespace<unscanned> extends (`${extendsToken}${infer nextUnscanned}`) ? parseUntilFinalizer<state.initialize<nextUnscanned>, $, {}> extends (infer finalArgState extends StaticState) ? validateAst<finalArgState["root"], $, {}> extends (infer e extends ErrorMessage) ? e : parseName<finalArgState["unscanned"], [
|
|
141
140
|
...result,
|
|
142
141
|
[name, inferAstRoot<finalArgState["root"], $, {}>]
|
package/out/generic.js
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
import { GenericRoot } from "@ark/schema";
|
|
2
2
|
import { throwParseError } from "@ark/util";
|
|
3
3
|
import { DynamicState } from "./parser/string/reduce/dynamic.js";
|
|
4
|
-
import { Scanner } from "./parser/string/shift/scanner.js";
|
|
5
4
|
import { parseUntilFinalizer } from "./parser/string/string.js";
|
|
6
5
|
export const Generic = GenericRoot;
|
|
7
6
|
export const emptyGenericParameterMessage = "An empty string is not a valid generic parameter name";
|
|
8
|
-
export const
|
|
9
|
-
const parseName = (scanner, result, ctx) => {
|
|
7
|
+
export const parseGenericParamName = (scanner, result, ctx) => {
|
|
10
8
|
scanner.shiftUntilNonWhitespace();
|
|
11
9
|
const name = scanner.shiftUntilNextTerminator();
|
|
12
10
|
if (name === "") {
|
|
@@ -30,9 +28,9 @@ const _parseOptionalConstraint = (scanner, name, result, ctx) => {
|
|
|
30
28
|
if (scanner.lookahead === ",")
|
|
31
29
|
scanner.shift();
|
|
32
30
|
result.push(name);
|
|
33
|
-
return
|
|
31
|
+
return parseGenericParamName(scanner, result, ctx);
|
|
34
32
|
}
|
|
35
33
|
const s = parseUntilFinalizer(new DynamicState(scanner, ctx));
|
|
36
34
|
result.push([name, s.root]);
|
|
37
|
-
return
|
|
35
|
+
return parseGenericParamName(scanner, result, ctx);
|
|
38
36
|
};
|
package/out/index.d.ts
CHANGED
|
@@ -3,5 +3,5 @@ export { Hkt, inferred } from "@ark/util";
|
|
|
3
3
|
export { Generic } from "./generic.ts";
|
|
4
4
|
export { ambient, ark, declare, define, generic, type, type Ark } from "./keywords/ark.ts";
|
|
5
5
|
export { Module, type BoundModule, type Submodule } from "./module.ts";
|
|
6
|
-
export { module, scope, type Scope
|
|
6
|
+
export { module, scope, type Scope } from "./scope.ts";
|
|
7
7
|
export { Type } from "./type.ts";
|
package/out/keys.d.ts
CHANGED
|
@@ -5,12 +5,14 @@ export type toArkKey<o, k extends keyof o> = k extends number ? [
|
|
|
5
5
|
o,
|
|
6
6
|
number
|
|
7
7
|
] extends [array, k] ? NonNegativeIntegerLiteral : `${k}` : k;
|
|
8
|
-
export type arkKeyOf<o> =
|
|
8
|
+
export type arkKeyOf<o> = [
|
|
9
|
+
o
|
|
10
|
+
] extends [array] ? (number extends o["length"] ? NonNegativeIntegerLiteral : never) | {
|
|
9
11
|
[k in keyof o]-?: k extends `${infer index extends number}` ? index | k : never;
|
|
10
12
|
}[keyof o & `${number}`] : {
|
|
11
|
-
[k in keyof o]
|
|
13
|
+
[k in keyof o]-?: k extends number ? k | `${k}` : k;
|
|
12
14
|
}[keyof o];
|
|
13
|
-
export type
|
|
15
|
+
export type arkGet<o, k extends arkKeyOf<o>> = o[Extract<k extends NonNegativeIntegerLiteral ? number : k, keyof o>];
|
|
14
16
|
export type TypeKey = termOrType<Key>;
|
|
15
17
|
export type typeKeyToString<k extends TypeKey> = typeToString<k extends type.cast<infer t> ? t : k>;
|
|
16
18
|
export type writeInvalidKeysMessage<o extends string, keys extends array<string>> = `Key${keys["length"] extends 1 ? "" : "s"} ${join<keys, ", ">} ${keys["length"] extends 1 ? "does" : "do"} not exist on ${o}`;
|
package/out/keywords/ark.d.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import type { ArkErrors, arkKind } from "@ark/schema";
|
|
2
2
|
import type { inferred } from "@ark/util";
|
|
3
3
|
import type { GenericParser } from "../generic.ts";
|
|
4
|
+
import type { BaseType } from "../methods/base.ts";
|
|
4
5
|
import type { BoundModule, Module } from "../module.ts";
|
|
5
6
|
import type { inferDefinition, validateDefinition } from "../parser/definition.ts";
|
|
6
7
|
import { type bindThis, type Scope } from "../scope.ts";
|
|
7
|
-
import type { DeclarationParser, DefinitionParser, Type, TypeParser } from "../type.ts";
|
|
8
|
+
import type { DeclarationParser, DefinitionParser, SchemaParser, Type, TypeParser } from "../type.ts";
|
|
8
9
|
import type { distill } from "./ast.ts";
|
|
9
10
|
import { arkBuiltins } from "./builtins.ts";
|
|
10
11
|
import { arkPrototypes } from "./constructors/constructors.ts";
|
|
@@ -36,6 +37,9 @@ export declare namespace type {
|
|
|
36
37
|
[inferred]?: t;
|
|
37
38
|
};
|
|
38
39
|
type errors = ArkErrors;
|
|
40
|
+
/** @ts-ignore cast variance */
|
|
41
|
+
interface Any<out t = any> extends BaseType<t, any> {
|
|
42
|
+
}
|
|
39
43
|
type infer<def, $ = {}, args = bindThis<def>> = inferDefinition<def, $, args>;
|
|
40
44
|
namespace infer {
|
|
41
45
|
type In<def, $ = {}, args = {}> = distill.In<inferDefinition<def, $, args>>;
|
|
@@ -52,5 +56,6 @@ export declare namespace type {
|
|
|
52
56
|
}
|
|
53
57
|
export type type<t = unknown, $ = {}> = Type<t, $>;
|
|
54
58
|
export declare const generic: GenericParser<{}>;
|
|
59
|
+
export declare const schema: SchemaParser<{}>;
|
|
55
60
|
export declare const define: DefinitionParser<{}>;
|
|
56
61
|
export declare const declare: DeclarationParser<{}>;
|
package/out/keywords/ark.js
CHANGED
|
@@ -21,5 +21,6 @@ export const ambient = scope({
|
|
|
21
21
|
export const ark = ambient.export();
|
|
22
22
|
export const type = ambient.type;
|
|
23
23
|
export const generic = ambient.generic;
|
|
24
|
+
export const schema = ambient.schema;
|
|
24
25
|
export const define = ambient.define;
|
|
25
26
|
export const declare = ambient.declare;
|
package/out/keywords/ast.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ArkError, ArkErrors, Constraint, constraintKindOf, Morph, NodeSchema } from "@ark/schema";
|
|
2
|
-
import { type anyOrNever, type array, type conform, type equals, type leftIfEqual, type Primitive, type show } from "@ark/util";
|
|
2
|
+
import { type anyOrNever, type array, type conform, type equals, type isSafelyMappable, type leftIfEqual, type objectKindOf, type Primitive, type show } from "@ark/util";
|
|
3
3
|
import type { inferPipe } from "../intersect.ts";
|
|
4
4
|
import type { Type } from "../type.ts";
|
|
5
5
|
import type { type } from "./ark.ts";
|
|
@@ -71,12 +71,20 @@ export type parseConstraints<t> = t extends constrain<infer base, infer constrai
|
|
|
71
71
|
}> extends true ? [
|
|
72
72
|
string,
|
|
73
73
|
constraints
|
|
74
|
-
] : equals<t,
|
|
74
|
+
] : equals<t, bigint & {
|
|
75
75
|
[constrained]: constraints;
|
|
76
76
|
}> extends true ? [
|
|
77
|
-
|
|
77
|
+
bigint,
|
|
78
78
|
constraints
|
|
79
|
-
] :
|
|
79
|
+
] : equals<t, symbol & {
|
|
80
|
+
[constrained]: constraints;
|
|
81
|
+
}> extends true ? [
|
|
82
|
+
symbol,
|
|
83
|
+
constraints
|
|
84
|
+
] : objectKindOf<t> extends infer kind ? kind extends BuiltinTerminalObjectKind ? [
|
|
85
|
+
arkPrototypes.instanceOf<kind>,
|
|
86
|
+
constraints
|
|
87
|
+
] : kind extends "Array" ? null : kind extends undefined ? [Omit<base, constrained>, constraints] : [base, constraints] : never : null;
|
|
80
88
|
export type normalizePrimitiveConstraintRoot<schema extends NodeSchema<Constraint.PrimitiveKind>> = "rule" extends keyof schema ? conform<schema["rule"], PropertyKey> : conform<schema, PropertyKey>;
|
|
81
89
|
type minLengthSchemaToConstraint<schema, rule> = schema extends {
|
|
82
90
|
exclusive: true;
|
|
@@ -144,17 +152,18 @@ export type includesMorphs<t> = [
|
|
|
144
152
|
type _distill<t, opts extends distill.Options> = t extends undefined ? t : [t] extends [anyOrNever] ? t : parseConstraints<t> extends ([
|
|
145
153
|
infer base,
|
|
146
154
|
infer constraints extends Constraints
|
|
147
|
-
]) ? opts["branded"] extends true ? constrain<_distill<base, opts>, constraints> : _distill<base, opts> :
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
[k in keyof t as k extends optionalInputKeyOf<t> ? never : k]: _distill<t[k], opts>;
|
|
155
|
+
]) ? opts["branded"] extends true ? constrain<_distill<base, opts>, constraints> : _distill<base, opts> : unknown extends t ? unknown : t extends TerminallyInferredObject | Primitive ? t : t extends MorphAst<infer i, infer o> ? opts["branded"] extends true ? distillIo<i, o, opts> : distillUnbrandedIo<t, i, o, opts> : t extends array ? distillArray<t, opts> : t extends DefaultedAst<infer t> ? _distill<t, opts> : t extends Function ? t : isSafelyMappable<t> extends true ? distillMappable<t, opts> : t;
|
|
156
|
+
type distillMappable<o, opts extends distill.Options> = opts["endpoint"] extends "in" ? show<{
|
|
157
|
+
[k in keyof o as k extends optionalInputKeyOf<o> ? never : k]: _distill<o[k], opts>;
|
|
151
158
|
} & {
|
|
152
|
-
[k in optionalInputKeyOf<
|
|
159
|
+
[k in optionalInputKeyOf<o>]?: _distill<o[k], opts>;
|
|
153
160
|
}> : show<{
|
|
154
|
-
[k in keyof
|
|
161
|
+
[k in keyof o as k extends metaOptionalKeyOf<o> ? never : k]: _distill<o[k], opts>;
|
|
155
162
|
} & {
|
|
156
|
-
[k in keyof
|
|
157
|
-
}
|
|
163
|
+
[k in keyof o as k extends metaOptionalKeyOf<o> ? k : never]?: _distill<o[k], opts>;
|
|
164
|
+
}>;
|
|
165
|
+
type distillUnbrandedIo<t extends MorphAst, i, o extends Out, opts extends distill.Options> = t extends (MorphAst<constrain<infer constrainedIn, any>, Out<constrain<infer constrainedOut, any>>>) ? distillIo<constrainedIn, o extends To ? To<constrainedOut> : Out<constrainedOut>, opts> : t extends MorphAst<constrain<infer constrainedIn, any>> ? distillIo<constrainedIn, o, opts> : t extends MorphAst<any, Out<constrain<infer constrainedOut, any>>> ? distillIo<i, o extends To ? To<constrainedOut> : Out<constrainedOut>, opts> : distillIo<i, o, opts>;
|
|
166
|
+
type distillIo<i, o extends Out, opts extends distill.Options> = opts["endpoint"] extends "in" ? _distill<i, opts> : opts["endpoint"] extends "out.introspectable" ? o extends To<infer validatedOut> ? _distill<validatedOut, opts> : unknown : opts["endpoint"] extends "out" ? _distill<o[1], opts> : _distill<o[1], opts> extends infer r ? o extends To ? (In: i) => To<r> : (In: i) => Out<r> : never;
|
|
158
167
|
type optionalInputKeyOf<o> = metaDefaultedKeyOf<o> | metaOptionalKeyOf<o>;
|
|
159
168
|
type metaDefaultedKeyOf<o> = {
|
|
160
169
|
[k in keyof o]: [o[k]] extends [anyOrNever] ? never : o[k] extends DefaultedAst ? k : never;
|
|
@@ -162,16 +171,17 @@ type metaDefaultedKeyOf<o> = {
|
|
|
162
171
|
type metaOptionalKeyOf<o> = {
|
|
163
172
|
[k in keyof o]: [o[k]] extends [anyOrNever] ? never : o[k] extends OptionalAst ? k : never;
|
|
164
173
|
}[keyof o];
|
|
165
|
-
type distillArray<t extends array, opts extends distill.Options
|
|
166
|
-
_distill<{
|
|
167
|
-
[k in keyof
|
|
168
|
-
}, opts
|
|
174
|
+
type distillArray<t extends array, opts extends distill.Options> = _distillArray<[...t], opts, []> extends infer result ? distillNonArraykeys<t, t extends unknown[] ? result : Readonly<t>, opts> : never;
|
|
175
|
+
type distillNonArraykeys<originalArray extends array, distilledArray, opts extends distill.Options> = keyof originalArray extends keyof distilledArray | constrained ? distilledArray : distilledArray & _distill<{
|
|
176
|
+
[k in keyof originalArray as k extends (keyof distilledArray | (opts["branded"] extends true ? never : constrained)) ? never : k]: originalArray[k];
|
|
177
|
+
}, opts>;
|
|
169
178
|
type _distillArray<t extends array, opts extends distill.Options, prefix extends array> = t extends readonly [infer head, ...infer tail] ? _distillArray<tail, opts, [...prefix, _distill<head, opts>]> : [...prefix, ...distillPostfix<t, opts>];
|
|
170
179
|
type distillPostfix<t extends array, opts extends distill.Options, postfix extends array = []> = t extends readonly [...infer init, infer last] ? distillPostfix<init, opts, [_distill<last, opts>, ...postfix]> : [...{
|
|
171
180
|
[i in keyof t]: _distill<t[i], opts>;
|
|
172
181
|
}, ...postfix];
|
|
182
|
+
type BuiltinTerminalObjectKind = Exclude<keyof arkPrototypes.instances, "Array" | "Function">;
|
|
173
183
|
/** Objects we don't want to expand during inference like Date or Promise */
|
|
174
|
-
type
|
|
184
|
+
type TerminallyInferredObject = arkPrototypes.instanceOf<BuiltinTerminalObjectKind> | ArkEnv.prototypes;
|
|
175
185
|
export type inferPredicate<t, predicate> = predicate extends (data: any, ...args: any[]) => data is infer narrowed ? t extends constrain<unknown, infer constraints> ? applyConstraintSchema<constrain<narrowed, constraints>, "predicate", any> : applyConstraintSchema<narrowed, "predicate", any> : applyConstraintSchema<t, "predicate", any>;
|
|
176
186
|
export type constrainWithPredicate<t> = t extends constrain<unknown, infer constraints> ? applyConstraintSchema<constrain<t, constraints>, "predicate", any> : applyConstraintSchema<t, "predicate", any>;
|
|
177
187
|
export type inferPipes<t, pipes extends Morph[]> = pipes extends [infer head extends Morph, ...infer tail extends Morph[]] ? inferPipes<pipes[0] extends type.cast<infer tPipe> ? inferPipe<t, tPipe> : inferMorphOut<head> extends infer out ? (In: distill.brandable.In<t>) => Out<out> : never, tail> : t;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { genericNode, intrinsic,
|
|
1
|
+
import { genericNode, intrinsic, rootSchema } from "@ark/schema";
|
|
2
2
|
import { Hkt, liftArray } from "@ark/util";
|
|
3
3
|
import { arkModule } from "../utils.js";
|
|
4
4
|
class liftFromHkt extends Hkt {
|
|
@@ -7,9 +7,9 @@ const liftFrom = genericNode("element")(args => {
|
|
|
7
7
|
const nonArrayElement = args.element.exclude(intrinsic.Array);
|
|
8
8
|
const lifted = nonArrayElement.array();
|
|
9
9
|
return nonArrayElement
|
|
10
|
-
.
|
|
10
|
+
.rawOr(lifted)
|
|
11
11
|
.pipe(liftArray)
|
|
12
|
-
.distribute(branch => branch.assertHasKind("morph").declareOut(lifted),
|
|
12
|
+
.distribute(branch => branch.assertHasKind("morph").declareOut(lifted), rootSchema);
|
|
13
13
|
}, liftFromHkt);
|
|
14
14
|
export const arkArray = arkModule({
|
|
15
15
|
root: intrinsic.Array,
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { rootSchema } from "@ark/schema";
|
|
2
2
|
import { registry } from "@ark/util";
|
|
3
3
|
import { arkModule } from "../utils.js";
|
|
4
|
-
const value =
|
|
5
|
-
const parsedFormDataValue = value.
|
|
6
|
-
const parsed =
|
|
4
|
+
const value = rootSchema(["string", registry.FileConstructor]);
|
|
5
|
+
const parsedFormDataValue = value.rawOr(value.array());
|
|
6
|
+
const parsed = rootSchema({
|
|
7
7
|
meta: "an object representing parsed form data",
|
|
8
8
|
domain: "object",
|
|
9
9
|
index: {
|
|
@@ -15,7 +15,7 @@ export const arkFormData = arkModule({
|
|
|
15
15
|
root: ["instanceof", FormData],
|
|
16
16
|
value,
|
|
17
17
|
parsed,
|
|
18
|
-
parse:
|
|
18
|
+
parse: rootSchema({
|
|
19
19
|
in: FormData,
|
|
20
20
|
morphs: (data) => {
|
|
21
21
|
const result = {};
|
|
@@ -26,6 +26,5 @@ export declare namespace arkPrototypes {
|
|
|
26
26
|
interface instances extends ecmascript, platform {
|
|
27
27
|
}
|
|
28
28
|
type instanceOf<name extends keyof instances = keyof instances> = instances[name];
|
|
29
|
-
type instanceOfExcluding<name extends keyof instances = keyof instances> = instances[Exclude<keyof instances, name>];
|
|
30
29
|
}
|
|
31
30
|
export {};
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { rootSchema } from "@ark/schema";
|
|
2
2
|
/**
|
|
3
3
|
* As per the ECMA-262 specification:
|
|
4
4
|
* A time value supports a slightly smaller range of -8,640,000,000,000,000 to 8,640,000,000,000,000 milliseconds.
|
|
5
5
|
*
|
|
6
6
|
* @see https://262.ecma-international.org/15.0/index.html#sec-time-values-and-time-range
|
|
7
7
|
*/
|
|
8
|
-
export const epoch =
|
|
8
|
+
export const epoch = rootSchema({
|
|
9
9
|
domain: {
|
|
10
10
|
domain: "number",
|
|
11
11
|
meta: "a number representing a Unix timestamp"
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { intrinsic,
|
|
1
|
+
import { intrinsic, rootSchema } from "@ark/schema";
|
|
2
2
|
import { arkModule } from "../utils.js";
|
|
3
3
|
import { epoch } from "./epoch.js";
|
|
4
4
|
import { integer } from "./integer.js";
|
|
@@ -6,7 +6,7 @@ export const number = arkModule({
|
|
|
6
6
|
root: intrinsic.number,
|
|
7
7
|
integer,
|
|
8
8
|
epoch,
|
|
9
|
-
safe:
|
|
9
|
+
safe: rootSchema({
|
|
10
10
|
domain: "number",
|
|
11
11
|
min: Number.MIN_SAFE_INTEGER,
|
|
12
12
|
max: Number.MAX_SAFE_INTEGER,
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { rootSchema } from "@ark/schema";
|
|
2
2
|
import { arkModule } from "../utils.js";
|
|
3
3
|
import { regexStringNode } from "./utils.js";
|
|
4
4
|
const preformatted = regexStringNode(/^[A-Z].*$/, "capitalized");
|
|
5
5
|
export const capitalize = arkModule({
|
|
6
|
-
root:
|
|
6
|
+
root: rootSchema({
|
|
7
7
|
in: "string",
|
|
8
8
|
morphs: (s) => s.charAt(0).toUpperCase() + s.slice(1),
|
|
9
9
|
declaredOut: preformatted
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { rootSchema } from "@ark/schema";
|
|
2
2
|
// https://github.com/validatorjs/validator.js/blob/master/src/lib/isLuhnNumber.js
|
|
3
3
|
export const isLuhnValid = (creditCardInput) => {
|
|
4
4
|
const sanitized = creditCardInput.replace(/[- ]+/g, "");
|
|
@@ -24,7 +24,7 @@ export const isLuhnValid = (creditCardInput) => {
|
|
|
24
24
|
};
|
|
25
25
|
// https://github.com/validatorjs/validator.js/blob/master/src/lib/isCreditCard.js
|
|
26
26
|
const creditCardMatcher = /^(?:4[0-9]{12}(?:[0-9]{3,6})?|5[1-5][0-9]{14}|(222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}|6(?:011|5[0-9][0-9])[0-9]{12,15}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11}|6[27][0-9]{14}|^(81[0-9]{14,17}))$/;
|
|
27
|
-
export const creditCard =
|
|
27
|
+
export const creditCard = rootSchema({
|
|
28
28
|
domain: "string",
|
|
29
29
|
pattern: {
|
|
30
30
|
meta: "a credit card number",
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ArkErrors, intrinsic,
|
|
1
|
+
import { ArkErrors, intrinsic, rootSchema } from "@ark/schema";
|
|
2
2
|
import { number } from "../number/number.js";
|
|
3
3
|
import { arkModule } from "../utils.js";
|
|
4
4
|
import { integer } from "./integer.js";
|
|
@@ -40,14 +40,14 @@ export const tryParseDatePattern = (data, opts) => {
|
|
|
40
40
|
return writeFormattedExpected(opts.format);
|
|
41
41
|
};
|
|
42
42
|
const isParsableDate = (s) => !Number.isNaN(new Date(s).valueOf());
|
|
43
|
-
const parsableDate =
|
|
43
|
+
const parsableDate = rootSchema({
|
|
44
44
|
domain: "string",
|
|
45
45
|
predicate: {
|
|
46
46
|
meta: "a parsable date",
|
|
47
47
|
predicate: isParsableDate
|
|
48
48
|
}
|
|
49
49
|
}).assertHasKind("intersection");
|
|
50
|
-
const
|
|
50
|
+
const epochRoot = integer.root.internal
|
|
51
51
|
.narrow((s, ctx) => {
|
|
52
52
|
// we know this is safe since it has already
|
|
53
53
|
// been validated as an integer string
|
|
@@ -64,25 +64,25 @@ const epochroot = integer.root.internal
|
|
|
64
64
|
})
|
|
65
65
|
.assertHasKind("intersection");
|
|
66
66
|
const epoch = arkModule({
|
|
67
|
-
root:
|
|
68
|
-
parse:
|
|
69
|
-
in:
|
|
67
|
+
root: epochRoot,
|
|
68
|
+
parse: rootSchema({
|
|
69
|
+
in: epochRoot,
|
|
70
70
|
morphs: (s) => new Date(s),
|
|
71
71
|
declaredOut: intrinsic.Date
|
|
72
72
|
})
|
|
73
73
|
});
|
|
74
|
-
const
|
|
74
|
+
const isoRoot = regexStringNode(iso8601Matcher, "an ISO 8601 (YYYY-MM-DDTHH:mm:ss.sssZ) date").internal.assertHasKind("intersection");
|
|
75
75
|
const iso = arkModule({
|
|
76
|
-
root:
|
|
77
|
-
parse:
|
|
78
|
-
in:
|
|
76
|
+
root: isoRoot,
|
|
77
|
+
parse: rootSchema({
|
|
78
|
+
in: isoRoot,
|
|
79
79
|
morphs: (s) => new Date(s),
|
|
80
80
|
declaredOut: intrinsic.Date
|
|
81
81
|
})
|
|
82
82
|
});
|
|
83
83
|
export const stringDate = arkModule({
|
|
84
84
|
root: parsableDate,
|
|
85
|
-
parse:
|
|
85
|
+
parse: rootSchema({
|
|
86
86
|
declaredIn: parsableDate,
|
|
87
87
|
in: "string",
|
|
88
88
|
morphs: (s, ctx) => {
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { intrinsic,
|
|
1
|
+
import { intrinsic, rootSchema } from "@ark/schema";
|
|
2
2
|
import { wellFormedIntegerMatcher } from "@ark/util";
|
|
3
3
|
import { arkModule } from "../utils.js";
|
|
4
4
|
import { regexStringNode } from "./utils.js";
|
|
5
5
|
const root = regexStringNode(wellFormedIntegerMatcher, "a well-formed integer string");
|
|
6
6
|
export const integer = arkModule({
|
|
7
7
|
root,
|
|
8
|
-
parse:
|
|
8
|
+
parse: rootSchema({
|
|
9
9
|
in: root,
|
|
10
10
|
morphs: (s, ctx) => {
|
|
11
11
|
const parsed = Number.parseInt(s);
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { arkModule } from "../utils.js";
|
|
2
2
|
import { regexStringNode } from "./utils.js";
|
|
3
3
|
// Based on https://github.com/validatorjs/validator.js/blob/master/src/lib/isIP.js
|
|
4
|
+
// Adjusted to incorporate unmerged fix in https://github.com/validatorjs/validator.js/pull/2083
|
|
4
5
|
const ipv4Segment = "(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])";
|
|
5
6
|
const ipv4Address = `(${ipv4Segment}[.]){3}${ipv4Segment}`;
|
|
6
7
|
const ipv4Matcher = new RegExp(`^${ipv4Address}$`);
|
|
@@ -14,8 +15,7 @@ const ipv6Matcher = new RegExp("^(" +
|
|
|
14
15
|
`(?:${ipv6Segment}:){2}(?:(:${ipv6Segment}){0,3}:${ipv4Address}|(:${ipv6Segment}){1,5}|:)|` +
|
|
15
16
|
`(?:${ipv6Segment}:){1}(?:(:${ipv6Segment}){0,4}:${ipv4Address}|(:${ipv6Segment}){1,6}|:)|` +
|
|
16
17
|
`(?::((?::${ipv6Segment}){0,5}:${ipv4Address}|(?::${ipv6Segment}){1,7}|:))` +
|
|
17
|
-
")(%[0-9a-zA-Z
|
|
18
|
-
// Based on https://github.com/validatorjs/validator.js/blob/master/src/lib/isUUID.js
|
|
18
|
+
")(%[0-9a-zA-Z.]{1,})?$");
|
|
19
19
|
export const ip = arkModule({
|
|
20
20
|
root: ["v4 | v6", "@", "an IP address"],
|
|
21
21
|
v4: regexStringNode(ipv4Matcher, "an IPv4 address"),
|
|
@@ -3,6 +3,7 @@ import type { Branded, To, constrain } from "../ast.ts";
|
|
|
3
3
|
declare namespace string {
|
|
4
4
|
type json = constrain<string, Branded<"json">>;
|
|
5
5
|
}
|
|
6
|
+
export declare const writeJsonSyntaxErrorProblem: (error: unknown) => string;
|
|
6
7
|
export declare const json: stringJson.module;
|
|
7
8
|
export declare namespace stringJson {
|
|
8
9
|
type module = Module<submodule>;
|
|
@@ -1,25 +1,33 @@
|
|
|
1
|
-
import { intrinsic,
|
|
1
|
+
import { intrinsic, rootSchema } from "@ark/schema";
|
|
2
2
|
import { arkModule } from "../utils.js";
|
|
3
3
|
const jsonStringDescription = "a JSON string";
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
}
|
|
9
|
-
catch {
|
|
10
|
-
return false;
|
|
11
|
-
}
|
|
4
|
+
export const writeJsonSyntaxErrorProblem = (error) => {
|
|
5
|
+
if (!(error instanceof SyntaxError))
|
|
6
|
+
throw error;
|
|
7
|
+
return `must be ${jsonStringDescription} (${error})`;
|
|
12
8
|
};
|
|
13
|
-
const root =
|
|
9
|
+
const root = rootSchema({
|
|
14
10
|
domain: "string",
|
|
15
11
|
predicate: {
|
|
16
12
|
meta: jsonStringDescription,
|
|
17
|
-
predicate:
|
|
13
|
+
predicate: (s, ctx) => {
|
|
14
|
+
try {
|
|
15
|
+
JSON.parse(s);
|
|
16
|
+
return true;
|
|
17
|
+
}
|
|
18
|
+
catch (e) {
|
|
19
|
+
return ctx.reject({
|
|
20
|
+
code: "predicate",
|
|
21
|
+
expected: jsonStringDescription,
|
|
22
|
+
problem: writeJsonSyntaxErrorProblem(e)
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
}
|
|
18
26
|
}
|
|
19
27
|
});
|
|
20
28
|
export const json = arkModule({
|
|
21
29
|
root,
|
|
22
|
-
parse:
|
|
30
|
+
parse: rootSchema({
|
|
23
31
|
in: "string",
|
|
24
32
|
morphs: (s, ctx) => {
|
|
25
33
|
if (s.length === 0) {
|
|
@@ -36,7 +44,7 @@ export const json = arkModule({
|
|
|
36
44
|
return ctx.error({
|
|
37
45
|
code: "predicate",
|
|
38
46
|
expected: jsonStringDescription,
|
|
39
|
-
problem:
|
|
47
|
+
problem: writeJsonSyntaxErrorProblem(e)
|
|
40
48
|
});
|
|
41
49
|
}
|
|
42
50
|
},
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { rootSchema } from "@ark/schema";
|
|
2
2
|
import { arkModule } from "../utils.js";
|
|
3
3
|
import { regexStringNode } from "./utils.js";
|
|
4
4
|
const preformatted = regexStringNode(/^[a-z]*$/, "only lowercase letters");
|
|
5
5
|
export const lower = arkModule({
|
|
6
|
-
root:
|
|
6
|
+
root: rootSchema({
|
|
7
7
|
in: "string",
|
|
8
8
|
morphs: (s) => s.toLowerCase(),
|
|
9
9
|
declaredOut: preformatted
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { rootSchema } from "@ark/schema";
|
|
2
2
|
import { flatMorph } from "@ark/util";
|
|
3
3
|
import { arkModule } from "../utils.js";
|
|
4
4
|
export const normalizedForms = ["NFC", "NFD", "NFKC", "NFKD"];
|
|
5
5
|
const preformattedNodes = flatMorph(normalizedForms, (i, form) => [
|
|
6
6
|
form,
|
|
7
|
-
|
|
7
|
+
rootSchema({
|
|
8
8
|
domain: "string",
|
|
9
9
|
predicate: (s) => s.normalize(form) === s,
|
|
10
10
|
meta: `${form}-normalized unicode`
|
|
@@ -12,7 +12,7 @@ const preformattedNodes = flatMorph(normalizedForms, (i, form) => [
|
|
|
12
12
|
]);
|
|
13
13
|
const normalizeNodes = flatMorph(normalizedForms, (i, form) => [
|
|
14
14
|
form,
|
|
15
|
-
|
|
15
|
+
rootSchema({
|
|
16
16
|
in: "string",
|
|
17
17
|
morphs: (s) => s.normalize(form),
|
|
18
18
|
declaredOut: preformattedNodes[form]
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { intrinsic,
|
|
1
|
+
import { intrinsic, rootSchema } from "@ark/schema";
|
|
2
2
|
import { wellFormedNumberMatcher } from "@ark/util";
|
|
3
3
|
import { arkModule } from "../utils.js";
|
|
4
4
|
import { regexStringNode } from "./utils.js";
|
|
5
5
|
const root = regexStringNode(wellFormedNumberMatcher, "a well-formed numeric string");
|
|
6
6
|
export const numeric = arkModule({
|
|
7
7
|
root,
|
|
8
|
-
parse:
|
|
8
|
+
parse: rootSchema({
|
|
9
9
|
in: root,
|
|
10
10
|
morphs: (s) => Number.parseFloat(s),
|
|
11
11
|
declaredOut: intrinsic.number
|
|
@@ -43,8 +43,8 @@ export declare const string: Module<{
|
|
|
43
43
|
date: Submodule<stringDate.submodule>;
|
|
44
44
|
lower: Submodule<lower.submodule>;
|
|
45
45
|
upper: Submodule<upper.submodule>;
|
|
46
|
-
json: Submodule<stringJson.submodule>;
|
|
47
46
|
integer: Submodule<stringInteger.submodule>;
|
|
47
|
+
json: Submodule<stringJson.submodule>;
|
|
48
48
|
alpha: unknown;
|
|
49
49
|
alphanumeric: unknown;
|
|
50
50
|
creditCard: unknown;
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { rootSchema } from "@ark/schema";
|
|
2
2
|
import { arkModule } from "../utils.js";
|
|
3
3
|
import { regexStringNode } from "./utils.js";
|
|
4
4
|
const preformatted = regexStringNode(
|
|
5
5
|
// no leading or trailing whitespace
|
|
6
6
|
/^\S.*\S$|^\S?$/, "trimmed");
|
|
7
7
|
export const trim = arkModule({
|
|
8
|
-
root:
|
|
8
|
+
root: rootSchema({
|
|
9
9
|
in: "string",
|
|
10
10
|
morphs: (s) => s.trim(),
|
|
11
11
|
declaredOut: preformatted
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { rootSchema } from "@ark/schema";
|
|
2
2
|
import { arkModule } from "../utils.js";
|
|
3
3
|
import { regexStringNode } from "./utils.js";
|
|
4
4
|
const preformatted = regexStringNode(/^[A-Z]*$/, "only uppercase letters");
|
|
5
5
|
export const upper = arkModule({
|
|
6
|
-
root:
|
|
6
|
+
root: rootSchema({
|
|
7
7
|
in: "string",
|
|
8
8
|
morphs: (s) => s.toUpperCase(),
|
|
9
9
|
declaredOut: preformatted
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { rootSchema } from "@ark/schema";
|
|
2
2
|
import { arkModule } from "../utils.js";
|
|
3
3
|
const isParsableUrl = (s) => {
|
|
4
4
|
if (URL.canParse)
|
|
@@ -12,7 +12,7 @@ const isParsableUrl = (s) => {
|
|
|
12
12
|
return false;
|
|
13
13
|
}
|
|
14
14
|
};
|
|
15
|
-
const root =
|
|
15
|
+
const root = rootSchema({
|
|
16
16
|
domain: "string",
|
|
17
17
|
predicate: {
|
|
18
18
|
meta: "a URL string",
|
|
@@ -21,7 +21,7 @@ const root = rootNode({
|
|
|
21
21
|
});
|
|
22
22
|
export const url = arkModule({
|
|
23
23
|
root,
|
|
24
|
-
parse:
|
|
24
|
+
parse: rootSchema({
|
|
25
25
|
declaredIn: root,
|
|
26
26
|
in: "string",
|
|
27
27
|
morphs: (s, ctx) => {
|
|
@@ -32,6 +32,6 @@ export const url = arkModule({
|
|
|
32
32
|
return ctx.error("a URL string");
|
|
33
33
|
}
|
|
34
34
|
},
|
|
35
|
-
declaredOut:
|
|
35
|
+
declaredOut: rootSchema(URL)
|
|
36
36
|
})
|
|
37
37
|
});
|