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,61 @@
|
|
|
1
|
+
import type { ArkErrors, arkKind } from "@ark/schema";
|
|
2
|
+
import type { inferred } from "@ark/util";
|
|
3
|
+
import type { GenericParser } from "../generic.ts";
|
|
4
|
+
import type { BaseType } from "../methods/base.ts";
|
|
5
|
+
import type { BoundModule, Module } from "../module.ts";
|
|
6
|
+
import type { inferDefinition, validateDefinition } from "../parser/definition.ts";
|
|
7
|
+
import { type bindThis, type Scope } from "../scope.ts";
|
|
8
|
+
import type { DeclarationParser, DefinitionParser, SchemaParser, Type, TypeParser } from "../type.ts";
|
|
9
|
+
import { arkBuiltins } from "./builtins.ts";
|
|
10
|
+
import { arkPrototypes } from "./constructors/constructors.ts";
|
|
11
|
+
import type { distill } from "./inference.ts";
|
|
12
|
+
import { number } from "./number/number.ts";
|
|
13
|
+
import { string } from "./string/string.ts";
|
|
14
|
+
import { arkTsGenerics, arkTsKeywords, unknown } from "./ts.ts";
|
|
15
|
+
export interface Ark extends Omit<Ark.keywords, keyof Ark.wrapped>, Ark.wrapped {
|
|
16
|
+
}
|
|
17
|
+
export declare namespace Ark {
|
|
18
|
+
interface keywords extends arkTsKeywords.$, arkTsGenerics.$, arkPrototypes.keywords, arkBuiltins.$ {
|
|
19
|
+
}
|
|
20
|
+
interface wrapped extends arkPrototypes.wrapped {
|
|
21
|
+
string: string.submodule;
|
|
22
|
+
number: number.submodule;
|
|
23
|
+
unknown: unknown.submodule;
|
|
24
|
+
}
|
|
25
|
+
interface typeAttachments extends arkTsKeywords.$ {
|
|
26
|
+
Key: arkBuiltins.$["Key"];
|
|
27
|
+
Record: arkTsGenerics.$["Record"];
|
|
28
|
+
}
|
|
29
|
+
interface boundTypeAttachments<$> extends Omit<BoundModule<typeAttachments, $>, arkKind> {
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
export declare const ark: Scope<Ark>;
|
|
33
|
+
export declare const keywords: Module<Ark>;
|
|
34
|
+
export declare const type: TypeParser<{}>;
|
|
35
|
+
export declare namespace type {
|
|
36
|
+
type cast<t> = {
|
|
37
|
+
[inferred]?: t;
|
|
38
|
+
};
|
|
39
|
+
type errors = ArkErrors;
|
|
40
|
+
/** @ts-ignore cast variance */
|
|
41
|
+
interface Any<out t = any> extends BaseType<t, any> {
|
|
42
|
+
}
|
|
43
|
+
type infer<def, $ = {}, args = bindThis<def>> = inferDefinition<def, $, args>;
|
|
44
|
+
namespace infer {
|
|
45
|
+
type In<def, $ = {}, args = {}> = distill.In<inferDefinition<def, $, args>>;
|
|
46
|
+
type Out<def, $ = {}, args = {}> = distill.Out<inferDefinition<def, $, args>>;
|
|
47
|
+
namespace brandable {
|
|
48
|
+
type In<def, $ = {}, args = {}> = distill.brandable.In<inferDefinition<def, $, args>>;
|
|
49
|
+
type Out<def, $ = {}, args = {}> = distill.brandable.Out<inferDefinition<def, $, args>>;
|
|
50
|
+
}
|
|
51
|
+
namespace introspectable {
|
|
52
|
+
type Out<def, $ = {}, args = {}> = distill.introspectable.Out<inferDefinition<def, $, args>>;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
type validate<def, $ = {}, args = bindThis<def>> = validateDefinition<def, $, args>;
|
|
56
|
+
}
|
|
57
|
+
export type type<t = unknown, $ = {}> = Type<t, $>;
|
|
58
|
+
export declare const generic: GenericParser<{}>;
|
|
59
|
+
export declare const schema: SchemaParser<{}>;
|
|
60
|
+
export declare const define: DefinitionParser<{}>;
|
|
61
|
+
export declare const declare: DeclarationParser<{}>;
|
|
@@ -3,22 +3,24 @@ import { arkBuiltins } from "./builtins.js";
|
|
|
3
3
|
import { arkPrototypes } from "./constructors/constructors.js";
|
|
4
4
|
import { number } from "./number/number.js";
|
|
5
5
|
import { string } from "./string/string.js";
|
|
6
|
-
import { arkTsGenerics, arkTsKeywords } from "./ts.js";
|
|
6
|
+
import { arkTsGenerics, arkTsKeywords, unknown } from "./ts.js";
|
|
7
7
|
$arkTypeRegistry.typeAttachments = {
|
|
8
8
|
...arkTsKeywords,
|
|
9
9
|
Key: arkBuiltins.Key,
|
|
10
10
|
Record: arkTsGenerics.Record
|
|
11
11
|
};
|
|
12
|
-
export const
|
|
12
|
+
export const ark = scope({
|
|
13
13
|
...arkTsKeywords,
|
|
14
14
|
...arkTsGenerics,
|
|
15
15
|
...arkPrototypes,
|
|
16
16
|
...arkBuiltins,
|
|
17
17
|
string,
|
|
18
|
-
number
|
|
18
|
+
number,
|
|
19
|
+
unknown
|
|
19
20
|
}, { prereducedAliases: true, ambient: true });
|
|
20
|
-
export const
|
|
21
|
-
export const type =
|
|
22
|
-
export const generic =
|
|
23
|
-
export const
|
|
24
|
-
export const
|
|
21
|
+
export const keywords = ark.export();
|
|
22
|
+
export const type = ark.type;
|
|
23
|
+
export const generic = ark.generic;
|
|
24
|
+
export const schema = ark.schema;
|
|
25
|
+
export const define = ark.define;
|
|
26
|
+
export const declare = ark.declare;
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import type { AtLeast, AtMost, DivisibleBy } from "
|
|
2
|
-
import type { number } from "./number.ts";
|
|
1
|
+
import type { AtLeast, AtMost, DivisibleBy, number } from "./number.ts";
|
|
3
2
|
/**
|
|
4
3
|
* As per the ECMA-262 specification:
|
|
5
4
|
* A time value supports a slightly smaller range of -8,640,000,000,000,000 to 8,640,000,000,000,000 milliseconds.
|
|
@@ -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,9 +1,23 @@
|
|
|
1
|
-
import { type Constraint, type NodeSchema } from "@ark/schema";
|
|
2
1
|
import type { Module, Submodule } from "../../module.ts";
|
|
3
|
-
import type {
|
|
2
|
+
import type { Branded, constrain, constraint, Constraints, Default, Narrowed, Optional } from "../inference.ts";
|
|
4
3
|
import { epoch } from "./epoch.ts";
|
|
5
4
|
import { integer } from "./integer.ts";
|
|
6
5
|
export declare const number: number.module;
|
|
6
|
+
export type AtLeast<rule> = {
|
|
7
|
+
atLeast: constraint<rule>;
|
|
8
|
+
};
|
|
9
|
+
export type AtMost<rule> = {
|
|
10
|
+
atMost: constraint<rule>;
|
|
11
|
+
};
|
|
12
|
+
export type MoreThan<rule> = {
|
|
13
|
+
moreThan: constraint<rule>;
|
|
14
|
+
};
|
|
15
|
+
export type LessThan<rule> = {
|
|
16
|
+
lessThan: constraint<rule>;
|
|
17
|
+
};
|
|
18
|
+
export type DivisibleBy<rule> = {
|
|
19
|
+
divisibleBy: constraint<rule>;
|
|
20
|
+
};
|
|
7
21
|
export declare namespace number {
|
|
8
22
|
type atLeast<rule> = constrain<number, AtLeast<rule>>;
|
|
9
23
|
type moreThan<rule> = constrain<number, MoreThan<rule>>;
|
|
@@ -11,18 +25,30 @@ export declare namespace number {
|
|
|
11
25
|
type lessThan<rule> = constrain<number, LessThan<rule>>;
|
|
12
26
|
type divisibleBy<rule> = constrain<number, DivisibleBy<rule>>;
|
|
13
27
|
type narrowed = constrain<number, Narrowed>;
|
|
28
|
+
type optional = constrain<number, Optional>;
|
|
29
|
+
type defaultsTo<rule> = constrain<number, Default<rule>>;
|
|
14
30
|
type branded<rule> = constrain<number, Branded<rule>>;
|
|
31
|
+
type NaN = branded<"NaN">;
|
|
32
|
+
type Infinity = branded<"Infinity">;
|
|
33
|
+
type NegativeInfinity = branded<"NegativeInfinity">;
|
|
34
|
+
type safe = branded<"safe">;
|
|
15
35
|
type is<constraints extends Constraints> = constrain<number, constraints>;
|
|
16
|
-
type
|
|
36
|
+
type minSchemaToConstraint<schema, rule> = schema extends {
|
|
17
37
|
exclusive: true;
|
|
18
|
-
} ?
|
|
38
|
+
} ? MoreThan<rule> : AtLeast<rule>;
|
|
39
|
+
type maxSchemaToConstraint<schema, rule> = schema extends {
|
|
19
40
|
exclusive: true;
|
|
20
|
-
} ?
|
|
41
|
+
} ? LessThan<rule> : AtMost<rule>;
|
|
42
|
+
type withConstraint<constraint> = constraint extends MoreThan<infer rule> ? moreThan<rule> : constraint extends AtLeast<infer rule> ? atLeast<rule> : constraint extends AtMost<infer rule> ? atMost<rule> : constraint extends LessThan<infer rule> ? lessThan<rule> : constraint extends DivisibleBy<infer rule> ? divisibleBy<rule> : constraint extends Optional ? optional : constraint extends Default<infer rule> ? defaultsTo<rule> : constraint extends Narrowed ? narrowed : never;
|
|
21
43
|
type module = Module<submodule>;
|
|
22
44
|
type submodule = Submodule<$>;
|
|
23
45
|
type $ = {
|
|
24
|
-
|
|
46
|
+
root: number;
|
|
25
47
|
epoch: epoch;
|
|
26
48
|
integer: integer;
|
|
49
|
+
safe: safe;
|
|
50
|
+
NaN: NaN;
|
|
51
|
+
Infinity: Infinity;
|
|
52
|
+
NegativeInfinity: NegativeInfinity;
|
|
27
53
|
};
|
|
28
54
|
}
|
|
@@ -1,9 +1,21 @@
|
|
|
1
|
-
import { intrinsic } from "@ark/schema";
|
|
2
|
-
import {
|
|
1
|
+
import { intrinsic, rootSchema } from "@ark/schema";
|
|
2
|
+
import { arkModule } from "../utils.js";
|
|
3
3
|
import { epoch } from "./epoch.js";
|
|
4
4
|
import { integer } from "./integer.js";
|
|
5
|
-
export const number =
|
|
6
|
-
|
|
5
|
+
export const number = arkModule({
|
|
6
|
+
root: intrinsic.number,
|
|
7
7
|
integer,
|
|
8
|
-
epoch
|
|
8
|
+
epoch,
|
|
9
|
+
safe: rootSchema({
|
|
10
|
+
domain: "number",
|
|
11
|
+
min: Number.MIN_SAFE_INTEGER,
|
|
12
|
+
max: Number.MAX_SAFE_INTEGER,
|
|
13
|
+
predicate: {
|
|
14
|
+
predicate: n => !Number.isNaN(n),
|
|
15
|
+
meta: "a safe number"
|
|
16
|
+
}
|
|
17
|
+
}),
|
|
18
|
+
NaN: ["===", Number.NaN],
|
|
19
|
+
Infinity: ["===", Number.POSITIVE_INFINITY],
|
|
20
|
+
NegativeInfinity: ["===", Number.NEGATIVE_INFINITY]
|
|
9
21
|
});
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Module, Submodule } from "../../module.ts";
|
|
2
|
-
import type { Branded, constrain, To } from "../
|
|
2
|
+
import type { Branded, constrain, To } from "../inference.ts";
|
|
3
3
|
declare namespace string {
|
|
4
4
|
type capitalized = constrain<string, Branded<"capitalized">>;
|
|
5
5
|
}
|
|
@@ -8,7 +8,7 @@ export declare namespace capitalize {
|
|
|
8
8
|
type module = Module<submodule>;
|
|
9
9
|
type submodule = Submodule<$>;
|
|
10
10
|
type $ = {
|
|
11
|
-
|
|
11
|
+
root: (In: string) => To<string.capitalized>;
|
|
12
12
|
preformatted: string.capitalized;
|
|
13
13
|
};
|
|
14
14
|
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { rootSchema } from "@ark/schema";
|
|
2
|
+
import { arkModule } from "../utils.js";
|
|
3
3
|
import { regexStringNode } from "./utils.js";
|
|
4
4
|
const preformatted = regexStringNode(/^[A-Z].*$/, "capitalized");
|
|
5
|
-
export const capitalize =
|
|
6
|
-
|
|
5
|
+
export const capitalize = arkModule({
|
|
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 type { Branded, constrain } from "../
|
|
1
|
+
import type { Branded, constrain } from "../inference.ts";
|
|
2
2
|
export declare const isLuhnValid: (creditCardInput: string) => boolean;
|
|
3
3
|
declare namespace string {
|
|
4
4
|
type creditCard = constrain<string, Branded<"creditCard">>;
|
|
@@ -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,6 +1,5 @@
|
|
|
1
1
|
import type { Module, Submodule } from "../../module.ts";
|
|
2
|
-
import type {
|
|
3
|
-
import type { Branded, To, constrain } from "../ast.ts";
|
|
2
|
+
import type { Branded, To, constrain } from "../inference.ts";
|
|
4
3
|
type DayDelimiter = "." | "/" | "-";
|
|
5
4
|
type DayPart = DayPatterns[PartKey];
|
|
6
5
|
type PartKey = keyof DayPatterns;
|
|
@@ -33,25 +32,22 @@ export declare namespace stringDate {
|
|
|
33
32
|
type module = Module<stringDate.submodule>;
|
|
34
33
|
type submodule = Submodule<$>;
|
|
35
34
|
type $ = {
|
|
36
|
-
|
|
35
|
+
root: string.date;
|
|
37
36
|
parse: (In: string.date) => To<Date>;
|
|
38
37
|
iso: iso.submodule;
|
|
39
38
|
epoch: epoch.submodule;
|
|
40
39
|
};
|
|
41
|
-
type shallowResolutions = {
|
|
42
|
-
[k in keyof $ as `string.date.${k}`]: $[k] extends type.cast<infer t> ? t : $[k];
|
|
43
|
-
};
|
|
44
40
|
namespace iso {
|
|
45
41
|
type submodule = Submodule<$>;
|
|
46
42
|
type $ = {
|
|
47
|
-
|
|
43
|
+
root: string.date.iso;
|
|
48
44
|
parse: (In: string.date.iso) => To<Date>;
|
|
49
45
|
};
|
|
50
46
|
}
|
|
51
47
|
namespace epoch {
|
|
52
48
|
type submodule = Submodule<$>;
|
|
53
49
|
type $ = {
|
|
54
|
-
|
|
50
|
+
root: string.date.epoch;
|
|
55
51
|
parse: (In: string.date.epoch) => To<Date>;
|
|
56
52
|
};
|
|
57
53
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { ArkErrors, intrinsic,
|
|
1
|
+
import { ArkErrors, intrinsic, rootSchema } from "@ark/schema";
|
|
2
2
|
import { number } from "../number/number.js";
|
|
3
|
-
import {
|
|
3
|
+
import { arkModule } from "../utils.js";
|
|
4
4
|
import { integer } from "./integer.js";
|
|
5
5
|
import { regexStringNode } from "./utils.js";
|
|
6
6
|
const dayDelimiterMatcher = /^[./-]$/;
|
|
@@ -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
|
|
@@ -63,26 +63,26 @@ const epoch$root = integer.$root.internal
|
|
|
63
63
|
description: "an integer string representing a safe Unix timestamp"
|
|
64
64
|
})
|
|
65
65
|
.assertHasKind("intersection");
|
|
66
|
-
const epoch =
|
|
67
|
-
|
|
68
|
-
parse:
|
|
69
|
-
in:
|
|
66
|
+
const epoch = arkModule({
|
|
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
|
|
75
|
-
const iso =
|
|
76
|
-
|
|
77
|
-
parse:
|
|
78
|
-
in:
|
|
74
|
+
const isoRoot = regexStringNode(iso8601Matcher, "an ISO 8601 (YYYY-MM-DDTHH:mm:ss.sssZ) date").internal.assertHasKind("intersection");
|
|
75
|
+
const iso = arkModule({
|
|
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
|
-
export const stringDate =
|
|
84
|
-
|
|
85
|
-
parse:
|
|
83
|
+
export const stringDate = arkModule({
|
|
84
|
+
root: parsableDate,
|
|
85
|
+
parse: rootSchema({
|
|
86
86
|
declaredIn: parsableDate,
|
|
87
87
|
in: "string",
|
|
88
88
|
morphs: (s, ctx) => {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Module, Submodule } from "../../module.ts";
|
|
2
|
-
import type { Branded, constrain, To } from "../
|
|
2
|
+
import type { Branded, constrain, To } from "../inference.ts";
|
|
3
3
|
import type { number } from "../number/number.ts";
|
|
4
4
|
declare namespace string {
|
|
5
5
|
type integer = constrain<string, Branded<"integer">>;
|
|
@@ -9,7 +9,7 @@ export declare namespace stringInteger {
|
|
|
9
9
|
type module = Module<submodule>;
|
|
10
10
|
type submodule = Submodule<$>;
|
|
11
11
|
type $ = {
|
|
12
|
-
|
|
12
|
+
root: string.integer;
|
|
13
13
|
parse: (In: string.integer) => To<number.divisibleBy<1>>;
|
|
14
14
|
};
|
|
15
15
|
}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { intrinsic,
|
|
1
|
+
import { intrinsic, rootSchema } from "@ark/schema";
|
|
2
2
|
import { wellFormedIntegerMatcher } from "@ark/util";
|
|
3
|
-
import {
|
|
3
|
+
import { arkModule } from "../utils.js";
|
|
4
4
|
import { regexStringNode } from "./utils.js";
|
|
5
|
-
const
|
|
6
|
-
export const integer =
|
|
7
|
-
|
|
8
|
-
parse:
|
|
9
|
-
in:
|
|
5
|
+
const root = regexStringNode(wellFormedIntegerMatcher, "a well-formed integer string");
|
|
6
|
+
export const integer = arkModule({
|
|
7
|
+
root,
|
|
8
|
+
parse: rootSchema({
|
|
9
|
+
in: root,
|
|
10
10
|
morphs: (s, ctx) => {
|
|
11
11
|
const parsed = Number.parseInt(s);
|
|
12
12
|
return Number.isSafeInteger(parsed) ? parsed : (ctx.error("an integer in the range Number.MIN_SAFE_INTEGER to Number.MAX_SAFE_INTEGER"));
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Module, Submodule } from "../../module.ts";
|
|
2
|
-
import type { Branded, constrain } from "../
|
|
2
|
+
import type { Branded, constrain } from "../inference.ts";
|
|
3
3
|
declare namespace string {
|
|
4
4
|
type ip = constrain<string, Branded<"ip">>;
|
|
5
5
|
namespace ip {
|
|
@@ -12,7 +12,7 @@ export declare namespace ip {
|
|
|
12
12
|
type module = Module<submodule>;
|
|
13
13
|
type submodule = Submodule<$>;
|
|
14
14
|
type $ = {
|
|
15
|
-
|
|
15
|
+
root: string.ip;
|
|
16
16
|
v4: string.ip.v4;
|
|
17
17
|
v6: string.ip.v6;
|
|
18
18
|
};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import {
|
|
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,10 +15,9 @@ 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
|
-
|
|
19
|
-
|
|
20
|
-
$root: ["v4 | v6", "@", "an IP address"],
|
|
18
|
+
")(%[0-9a-zA-Z.]{1,})?$");
|
|
19
|
+
export const ip = arkModule({
|
|
20
|
+
root: ["v4 | v6", "@", "an IP address"],
|
|
21
21
|
v4: regexStringNode(ipv4Matcher, "an IPv4 address"),
|
|
22
22
|
v6: regexStringNode(ipv6Matcher, "an IPv6 address")
|
|
23
23
|
});
|
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import type { Module, Submodule } from "../../module.ts";
|
|
2
|
-
import type { Branded, To, constrain } from "../
|
|
2
|
+
import type { Branded, To, constrain } from "../inference.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>;
|
|
9
10
|
type submodule = Submodule<$>;
|
|
10
11
|
type $ = {
|
|
11
|
-
|
|
12
|
+
root: string.json;
|
|
12
13
|
parse: (In: string.json) => To<object>;
|
|
13
14
|
};
|
|
14
15
|
}
|
|
@@ -1,25 +1,33 @@
|
|
|
1
|
-
import { intrinsic,
|
|
2
|
-
import {
|
|
1
|
+
import { intrinsic, rootSchema } from "@ark/schema";
|
|
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
|
|
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
|
-
export const json =
|
|
21
|
-
|
|
22
|
-
parse:
|
|
28
|
+
export const json = arkModule({
|
|
29
|
+
root,
|
|
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 = submodule({
|
|
|
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,5 +1,5 @@
|
|
|
1
1
|
import type { Module, Submodule } from "../../module.ts";
|
|
2
|
-
import type { Branded, constrain, To } from "../
|
|
2
|
+
import type { Branded, constrain, To } from "../inference.ts";
|
|
3
3
|
declare namespace string {
|
|
4
4
|
type lowercase = constrain<string, Branded<"lowercase">>;
|
|
5
5
|
}
|
|
@@ -8,7 +8,7 @@ export declare namespace lower {
|
|
|
8
8
|
type module = Module<submodule>;
|
|
9
9
|
type submodule = Submodule<$>;
|
|
10
10
|
type $ = {
|
|
11
|
-
|
|
11
|
+
root: (In: string) => To<string.lowercase>;
|
|
12
12
|
preformatted: string.lowercase;
|
|
13
13
|
};
|
|
14
14
|
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { rootSchema } from "@ark/schema";
|
|
2
|
+
import { arkModule } from "../utils.js";
|
|
3
3
|
import { regexStringNode } from "./utils.js";
|
|
4
4
|
const preformatted = regexStringNode(/^[a-z]*$/, "only lowercase letters");
|
|
5
|
-
export const lower =
|
|
6
|
-
|
|
5
|
+
export const lower = arkModule({
|
|
6
|
+
root: rootSchema({
|
|
7
7
|
in: "string",
|
|
8
8
|
morphs: (s) => s.toLowerCase(),
|
|
9
9
|
declaredOut: preformatted
|