arktype 2.0.0-beta.2 → 2.0.0-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/out/ark.d.ts +18 -4
- package/out/ark.js +22 -3
- package/out/ast.d.ts +217 -0
- package/out/ast.js +3 -0
- package/out/config.d.ts +12 -0
- package/out/generic.d.ts +106 -20
- package/out/index.d.ts +6 -5
- package/out/index.js +3 -2
- package/out/intersect.d.ts +28 -0
- package/out/intersect.js +1 -0
- package/out/keys.d.ts +16 -0
- package/out/keys.js +1 -0
- package/out/keywords/arkGenerics.d.ts +19 -0
- package/out/keywords/arkGenerics.js +19 -0
- package/out/keywords/format.d.ts +11 -0
- package/out/keywords/format.js +31 -0
- package/out/keywords/internal.d.ts +11 -0
- package/out/keywords/internal.js +25 -0
- package/out/keywords/jsObjects.d.ts +15 -0
- package/out/keywords/jsObjects.js +13 -0
- package/out/keywords/parsing.d.ts +16 -0
- package/out/keywords/parsing.js +77 -0
- package/out/keywords/platformObjects.d.ts +13 -0
- package/out/keywords/platformObjects.js +12 -0
- package/out/keywords/tsGenerics.d.ts +37 -0
- package/out/keywords/tsGenerics.js +45 -0
- package/out/keywords/tsKeywords.d.ts +18 -0
- package/out/keywords/tsKeywords.js +17 -0
- package/out/keywords/typedArray.d.ts +16 -0
- package/out/keywords/typedArray.js +14 -0
- package/out/keywords/utils/creditCard.d.ts +2 -0
- package/out/keywords/utils/creditCard.js +25 -0
- package/out/keywords/utils/date.d.ts +20 -0
- package/out/keywords/utils/date.js +36 -0
- package/out/keywords/utils/ip.d.ts +3 -0
- package/out/keywords/utils/ip.js +20 -0
- package/out/keywords/utils/regex.d.ts +2 -0
- package/out/keywords/utils/regex.js +9 -0
- package/out/keywords/validation.d.ts +19 -0
- package/out/keywords/validation.js +84 -0
- package/out/module.d.ts +19 -11
- package/out/module.js +1 -0
- package/out/parser/definition.d.ts +5 -4
- package/out/parser/objectLiteral.d.ts +7 -6
- package/out/parser/objectLiteral.js +2 -2
- package/out/parser/semantic/bounds.d.ts +2 -1
- package/out/parser/semantic/divisor.d.ts +2 -2
- package/out/parser/semantic/infer.d.ts +14 -9
- package/out/parser/semantic/keyof.d.ts +7 -0
- package/out/parser/semantic/keyof.js +1 -0
- package/out/parser/semantic/validate.d.ts +4 -5
- package/out/parser/semantic/validate.js +0 -1
- package/out/parser/string/reduce/dynamic.d.ts +2 -1
- package/out/parser/string/reduce/shared.d.ts +1 -1
- package/out/parser/string/reduce/static.d.ts +1 -1
- package/out/parser/string/shift/operand/date.d.ts +3 -1
- package/out/parser/string/shift/operand/enclosed.js +1 -1
- package/out/parser/string/shift/operand/genericArgs.d.ts +4 -4
- package/out/parser/string/shift/operand/unenclosed.d.ts +13 -8
- package/out/parser/string/shift/operator/bounds.d.ts +2 -1
- package/out/parser/string/shift/operator/bounds.js +4 -4
- package/out/parser/string/shift/operator/default.d.ts +2 -1
- package/out/parser/string/string.d.ts +2 -1
- package/out/parser/tuple.d.ts +5 -3
- package/out/parser/tuple.js +5 -5
- package/out/scope.d.ts +47 -26
- package/out/scope.js +2 -7
- package/out/subtypes/array.d.ts +13 -0
- package/out/subtypes/array.js +1 -0
- package/out/subtypes/base.d.ts +83 -0
- package/out/subtypes/base.js +1 -0
- package/out/subtypes/date.d.ts +11 -0
- package/out/subtypes/date.js +1 -0
- package/out/subtypes/instantiate.d.ts +16 -0
- package/out/subtypes/instantiate.js +1 -0
- package/out/subtypes/morph.d.ts +13 -0
- package/out/subtypes/morph.js +1 -0
- package/out/subtypes/number.d.ts +12 -0
- package/out/subtypes/number.js +1 -0
- package/out/subtypes/object.d.ts +27 -0
- package/out/subtypes/object.js +1 -0
- package/out/subtypes/string.d.ts +13 -0
- package/out/subtypes/string.js +1 -0
- package/out/subtypes/validator.d.ts +11 -0
- package/out/subtypes/validator.js +1 -0
- package/out/type.d.ts +17 -71
- package/package.json +3 -3
- package/out/match.d.ts +0 -71
- package/out/match.js +0 -50
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { scope } from "../scope.js";
|
|
2
|
+
// these are needed to create some internal types
|
|
3
|
+
import { intrinsic, node } from "@ark/schema";
|
|
4
|
+
import "./tsKeywords.js";
|
|
5
|
+
export const internal = scope({
|
|
6
|
+
lengthBoundable: intrinsic.lengthBoundable,
|
|
7
|
+
key: intrinsic.key,
|
|
8
|
+
nonNegativeIntegerString: intrinsic.nonNegativeIntegerString
|
|
9
|
+
}, {
|
|
10
|
+
prereducedAliases: true
|
|
11
|
+
}).export();
|
|
12
|
+
// reduce union of all possible values reduces to unknown
|
|
13
|
+
node("union", {
|
|
14
|
+
branches: [
|
|
15
|
+
"string",
|
|
16
|
+
"number",
|
|
17
|
+
"object",
|
|
18
|
+
"bigint",
|
|
19
|
+
"symbol",
|
|
20
|
+
{ unit: true },
|
|
21
|
+
{ unit: false },
|
|
22
|
+
{ unit: null },
|
|
23
|
+
{ unit: undefined }
|
|
24
|
+
]
|
|
25
|
+
}, { reduceTo: node("intersection", {}, { prereduced: true }) });
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Module } from "../module.js";
|
|
2
|
+
export interface jsObjectExports {
|
|
3
|
+
Array: Array<unknown>;
|
|
4
|
+
Date: Date;
|
|
5
|
+
Error: Error;
|
|
6
|
+
Function: Function;
|
|
7
|
+
Map: Map<unknown, unknown>;
|
|
8
|
+
RegExp: RegExp;
|
|
9
|
+
Set: Set<unknown>;
|
|
10
|
+
WeakMap: WeakMap<object, unknown>;
|
|
11
|
+
WeakSet: WeakSet<object>;
|
|
12
|
+
Promise: Promise<unknown>;
|
|
13
|
+
}
|
|
14
|
+
export type jsObjects = Module<jsObjectExports>;
|
|
15
|
+
export declare const jsObjects: jsObjects;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { scope } from "../scope.js";
|
|
2
|
+
export const jsObjects = scope({
|
|
3
|
+
Array: ["instanceof", Array],
|
|
4
|
+
Date: ["instanceof", Date],
|
|
5
|
+
Error: ["instanceof", Error],
|
|
6
|
+
Function: ["instanceof", Function],
|
|
7
|
+
Map: ["instanceof", Map],
|
|
8
|
+
RegExp: ["instanceof", RegExp],
|
|
9
|
+
Set: ["instanceof", Set],
|
|
10
|
+
WeakMap: ["instanceof", WeakMap],
|
|
11
|
+
WeakSet: ["instanceof", WeakSet],
|
|
12
|
+
Promise: ["instanceof", Promise]
|
|
13
|
+
}, { prereducedAliases: true }).export();
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Out, number } from "../ast.js";
|
|
2
|
+
import type { Module } from "../module.js";
|
|
3
|
+
declare const number: import("@ark/schema").BaseRoot<import("@ark/schema").InternalRootDeclaration>;
|
|
4
|
+
export type FormDataValue = string | File;
|
|
5
|
+
export type ParsedFormData = Record<string, FormDataValue | FormDataValue[]>;
|
|
6
|
+
export type parsingExports = {
|
|
7
|
+
url: (In: string) => Out<URL>;
|
|
8
|
+
number: (In: string) => Out<number>;
|
|
9
|
+
integer: (In: string) => Out<number.divisibleBy<1>>;
|
|
10
|
+
date: (In: string) => Out<Date>;
|
|
11
|
+
json: (In: string) => Out<object>;
|
|
12
|
+
formData: (In: FormData) => Out<ParsedFormData>;
|
|
13
|
+
};
|
|
14
|
+
export type parsing = Module<parsingExports>;
|
|
15
|
+
export declare const parsing: parsing;
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { rootNode } from "@ark/schema";
|
|
2
|
+
import { isWellFormedInteger, wellFormedIntegerMatcher, wellFormedNumberMatcher } from "@ark/util";
|
|
3
|
+
import { scope } from "../scope.js";
|
|
4
|
+
import { tryParseDatePattern } from "./utils/date.js";
|
|
5
|
+
import { regexStringNode } from "./utils/regex.js";
|
|
6
|
+
const number = rootNode({
|
|
7
|
+
in: regexStringNode(wellFormedNumberMatcher, "a well-formed numeric string"),
|
|
8
|
+
morphs: (s) => Number.parseFloat(s)
|
|
9
|
+
});
|
|
10
|
+
const integer = rootNode({
|
|
11
|
+
in: regexStringNode(wellFormedIntegerMatcher, "a well-formed integer string"),
|
|
12
|
+
morphs: (s, ctx) => {
|
|
13
|
+
if (!isWellFormedInteger(s))
|
|
14
|
+
return ctx.error("a well-formed integer string");
|
|
15
|
+
const parsed = Number.parseInt(s);
|
|
16
|
+
return Number.isSafeInteger(parsed) ? parsed : (ctx.error("an integer in the range Number.MIN_SAFE_INTEGER to Number.MAX_SAFE_INTEGER"));
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
const url = rootNode({
|
|
20
|
+
in: "string",
|
|
21
|
+
morphs: (s, ctx) => {
|
|
22
|
+
try {
|
|
23
|
+
return new URL(s);
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
return ctx.error("a valid URL");
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
const json = rootNode({
|
|
31
|
+
in: "string",
|
|
32
|
+
morphs: (s, ctx) => {
|
|
33
|
+
try {
|
|
34
|
+
return JSON.parse(s);
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
return ctx.error("a valid JSON string");
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
const date = rootNode({
|
|
42
|
+
in: "string",
|
|
43
|
+
morphs: (s, ctx) => {
|
|
44
|
+
const result = tryParseDatePattern(s);
|
|
45
|
+
return typeof result === "string" ? ctx.error(result) : result;
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
// support Node18
|
|
49
|
+
const File = globalThis.File ?? Blob;
|
|
50
|
+
const formData = rootNode({
|
|
51
|
+
in: FormData,
|
|
52
|
+
morphs: (data) => {
|
|
53
|
+
const result = {};
|
|
54
|
+
for (const [k, v] of data) {
|
|
55
|
+
if (k in result) {
|
|
56
|
+
const existing = result[k];
|
|
57
|
+
if (typeof existing === "string" || existing instanceof File)
|
|
58
|
+
result[k] = [existing, v];
|
|
59
|
+
else
|
|
60
|
+
existing.push(v);
|
|
61
|
+
}
|
|
62
|
+
else
|
|
63
|
+
result[k] = v;
|
|
64
|
+
}
|
|
65
|
+
return result;
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
export const parsing = scope({
|
|
69
|
+
url,
|
|
70
|
+
number,
|
|
71
|
+
integer,
|
|
72
|
+
date,
|
|
73
|
+
json,
|
|
74
|
+
formData
|
|
75
|
+
}, {
|
|
76
|
+
prereducedAliases: true
|
|
77
|
+
}).export();
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Module } from "../module.js";
|
|
2
|
+
export interface platformObjectExports {
|
|
3
|
+
ArrayBuffer: ArrayBuffer;
|
|
4
|
+
Blob: Blob;
|
|
5
|
+
File: File;
|
|
6
|
+
FormData: FormData;
|
|
7
|
+
Headers: Headers;
|
|
8
|
+
Request: Request;
|
|
9
|
+
Response: Response;
|
|
10
|
+
URL: URL;
|
|
11
|
+
}
|
|
12
|
+
export type platformObjects = Module<platformObjectExports>;
|
|
13
|
+
export declare const platformObjects: platformObjects;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { scope } from "../scope.js";
|
|
2
|
+
export const platformObjects = scope({
|
|
3
|
+
ArrayBuffer: ["instanceof", ArrayBuffer],
|
|
4
|
+
Blob: ["instanceof", Blob],
|
|
5
|
+
// support Node18
|
|
6
|
+
File: ["instanceof", globalThis.File ?? Blob],
|
|
7
|
+
FormData: ["instanceof", FormData],
|
|
8
|
+
Headers: ["instanceof", Headers],
|
|
9
|
+
Request: ["instanceof", Request],
|
|
10
|
+
Response: ["instanceof", Response],
|
|
11
|
+
URL: ["instanceof", URL]
|
|
12
|
+
}, { prereducedAliases: true }).export();
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { Hkt, type Key, type omit, type pick, type show } from "@ark/util";
|
|
2
|
+
import type { Module } from "../module.js";
|
|
3
|
+
import { type inferScope } from "../scope.js";
|
|
4
|
+
declare class RecordHkt extends Hkt<[Key, unknown]> {
|
|
5
|
+
body: Record<this[0], this[1]>;
|
|
6
|
+
}
|
|
7
|
+
declare class PickHkt extends Hkt<[object, Key]> {
|
|
8
|
+
body: pick<this[0], this[1] & keyof this[0]>;
|
|
9
|
+
}
|
|
10
|
+
declare class OmitHkt extends Hkt<[object, Key]> {
|
|
11
|
+
body: omit<this[0], this[1] & keyof this[0]>;
|
|
12
|
+
}
|
|
13
|
+
declare class PartialHkt extends Hkt<[object]> {
|
|
14
|
+
body: show<Partial<this[0]>>;
|
|
15
|
+
}
|
|
16
|
+
declare class RequiredHkt extends Hkt<[object]> {
|
|
17
|
+
body: show<Required<this[0]>>;
|
|
18
|
+
}
|
|
19
|
+
declare class ExcludeHkt extends Hkt<[unknown, unknown]> {
|
|
20
|
+
body: Exclude<this[0], this[1]>;
|
|
21
|
+
}
|
|
22
|
+
declare class ExtractHkt extends Hkt<[unknown, unknown]> {
|
|
23
|
+
body: Extract<this[0], this[1]>;
|
|
24
|
+
}
|
|
25
|
+
declare const tsGenericsExports: {
|
|
26
|
+
Record: import("@ark/schema").GenericRoot<readonly [["K", Key], ["V", unknown]], RecordHkt>;
|
|
27
|
+
Pick: import("@ark/schema").GenericRoot<readonly [["T", object], ["K", Key]], PickHkt>;
|
|
28
|
+
Omit: import("@ark/schema").GenericRoot<readonly [["T", object], ["K", Key]], OmitHkt>;
|
|
29
|
+
Exclude: import("@ark/schema").GenericRoot<readonly [["T", unknown], ["U", unknown]], ExcludeHkt>;
|
|
30
|
+
Extract: import("@ark/schema").GenericRoot<readonly [["T", unknown], ["U", unknown]], ExtractHkt>;
|
|
31
|
+
Partial: import("@ark/schema").GenericRoot<readonly [["T", object]], PartialHkt>;
|
|
32
|
+
Required: import("@ark/schema").GenericRoot<readonly [["T", object]], RequiredHkt>;
|
|
33
|
+
};
|
|
34
|
+
export type tsGenericsExports = inferScope<typeof tsGenericsExports>;
|
|
35
|
+
export type tsGenerics = Module<tsGenericsExports>;
|
|
36
|
+
export declare const tsGenerics: tsGenerics;
|
|
37
|
+
export {};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { genericNode } from "@ark/schema";
|
|
2
|
+
import { Hkt } from "@ark/util";
|
|
3
|
+
import { scope } from "../scope.js";
|
|
4
|
+
import { internal } from "./internal.js";
|
|
5
|
+
import { tsKeywords } from "./tsKeywords.js";
|
|
6
|
+
class RecordHkt extends Hkt {
|
|
7
|
+
}
|
|
8
|
+
const Record = genericNode(["K", internal.key], "V")(args => ({
|
|
9
|
+
domain: "object",
|
|
10
|
+
index: {
|
|
11
|
+
signature: args.K,
|
|
12
|
+
value: args.V
|
|
13
|
+
}
|
|
14
|
+
}), RecordHkt);
|
|
15
|
+
class PickHkt extends Hkt {
|
|
16
|
+
}
|
|
17
|
+
const Pick = genericNode(["T", tsKeywords.object], ["K", internal.key])(args => args.T.pick(args.K), PickHkt);
|
|
18
|
+
class OmitHkt extends Hkt {
|
|
19
|
+
}
|
|
20
|
+
const Omit = genericNode(["T", tsKeywords.object], ["K", internal.key])(args => args.T.omit(args.K), OmitHkt);
|
|
21
|
+
class PartialHkt extends Hkt {
|
|
22
|
+
}
|
|
23
|
+
const Partial = genericNode(["T", tsKeywords.object])(args => args.T.partial(), PartialHkt);
|
|
24
|
+
class RequiredHkt extends Hkt {
|
|
25
|
+
}
|
|
26
|
+
const Required = genericNode(["T", tsKeywords.object])(args => args.T.required(), RequiredHkt);
|
|
27
|
+
class ExcludeHkt extends Hkt {
|
|
28
|
+
}
|
|
29
|
+
const Exclude = genericNode("T", "U")(args => args.T.exclude(args.U), ExcludeHkt);
|
|
30
|
+
class ExtractHkt extends Hkt {
|
|
31
|
+
}
|
|
32
|
+
const Extract = genericNode("T", "U")(args => args.T.extract(args.U), ExtractHkt);
|
|
33
|
+
const tsGenericsExports = {
|
|
34
|
+
Record,
|
|
35
|
+
Pick,
|
|
36
|
+
Omit,
|
|
37
|
+
Exclude,
|
|
38
|
+
Extract,
|
|
39
|
+
Partial,
|
|
40
|
+
Required
|
|
41
|
+
};
|
|
42
|
+
const $ = scope(tsGenericsExports, {
|
|
43
|
+
prereducedAliases: true
|
|
44
|
+
});
|
|
45
|
+
export const tsGenerics = $.export();
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Module } from "../module.js";
|
|
2
|
+
export interface tsKeywordExports {
|
|
3
|
+
any: any;
|
|
4
|
+
bigint: bigint;
|
|
5
|
+
boolean: boolean;
|
|
6
|
+
false: false;
|
|
7
|
+
never: never;
|
|
8
|
+
null: null;
|
|
9
|
+
number: number;
|
|
10
|
+
object: object;
|
|
11
|
+
string: string;
|
|
12
|
+
symbol: symbol;
|
|
13
|
+
true: true;
|
|
14
|
+
unknown: unknown;
|
|
15
|
+
undefined: undefined;
|
|
16
|
+
}
|
|
17
|
+
export type tsKeywords = Module<tsKeywordExports>;
|
|
18
|
+
export declare const tsKeywords: tsKeywords;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { intrinsic } from "@ark/schema";
|
|
2
|
+
import { scope } from "../scope.js";
|
|
3
|
+
export const tsKeywords = scope({
|
|
4
|
+
any: intrinsic.unknown,
|
|
5
|
+
bigint: intrinsic.bigint,
|
|
6
|
+
boolean: intrinsic.boolean,
|
|
7
|
+
false: intrinsic.false,
|
|
8
|
+
never: intrinsic.never,
|
|
9
|
+
null: intrinsic.null,
|
|
10
|
+
number: intrinsic.number,
|
|
11
|
+
object: intrinsic.object,
|
|
12
|
+
string: intrinsic.string,
|
|
13
|
+
symbol: intrinsic.symbol,
|
|
14
|
+
true: intrinsic.true,
|
|
15
|
+
unknown: intrinsic.unknown,
|
|
16
|
+
undefined: intrinsic.undefined
|
|
17
|
+
}, { prereducedAliases: true }).export();
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Module } from "../module.js";
|
|
2
|
+
export interface typedArrayExports {
|
|
3
|
+
Int8: Int8Array;
|
|
4
|
+
Uint8: Uint8Array;
|
|
5
|
+
Uint8Clamped: Uint8ClampedArray;
|
|
6
|
+
Int16: Int16Array;
|
|
7
|
+
Uint16: Uint16Array;
|
|
8
|
+
Int32: Int32Array;
|
|
9
|
+
Uint32: Uint32Array;
|
|
10
|
+
Float32: Float32Array;
|
|
11
|
+
Float64: Float64Array;
|
|
12
|
+
BigInt64: BigInt64Array;
|
|
13
|
+
BigUint64: BigUint64Array;
|
|
14
|
+
}
|
|
15
|
+
export type typedArray = Module<typedArrayExports>;
|
|
16
|
+
export declare const typedArray: typedArray;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { scope } from "../scope.js";
|
|
2
|
+
export const typedArray = scope({
|
|
3
|
+
Int8: ["instanceof", Int8Array],
|
|
4
|
+
Uint8: ["instanceof", Uint8Array],
|
|
5
|
+
Uint8Clamped: ["instanceof", Uint8ClampedArray],
|
|
6
|
+
Int16: ["instanceof", Int16Array],
|
|
7
|
+
Uint16: ["instanceof", Uint16Array],
|
|
8
|
+
Int32: ["instanceof", Int32Array],
|
|
9
|
+
Uint32: ["instanceof", Uint32Array],
|
|
10
|
+
Float32: ["instanceof", Float32Array],
|
|
11
|
+
Float64: ["instanceof", Float64Array],
|
|
12
|
+
BigInt64: ["instanceof", BigInt64Array],
|
|
13
|
+
BigUint64: ["instanceof", BigUint64Array]
|
|
14
|
+
}, { prereducedAliases: true }).export();
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// https://github.com/validatorjs/validator.js/blob/master/src/lib/isLuhnNumber.js
|
|
2
|
+
export const isLuhnValid = (creditCardInput) => {
|
|
3
|
+
const sanitized = creditCardInput.replace(/[- ]+/g, "");
|
|
4
|
+
let sum = 0;
|
|
5
|
+
let digit;
|
|
6
|
+
let tmpNum;
|
|
7
|
+
let shouldDouble = false;
|
|
8
|
+
for (let i = sanitized.length - 1; i >= 0; i--) {
|
|
9
|
+
digit = sanitized.substring(i, i + 1);
|
|
10
|
+
tmpNum = Number.parseInt(digit, 10);
|
|
11
|
+
if (shouldDouble) {
|
|
12
|
+
tmpNum *= 2;
|
|
13
|
+
if (tmpNum >= 10)
|
|
14
|
+
sum += (tmpNum % 10) + 1;
|
|
15
|
+
else
|
|
16
|
+
sum += tmpNum;
|
|
17
|
+
}
|
|
18
|
+
else
|
|
19
|
+
sum += tmpNum;
|
|
20
|
+
shouldDouble = !shouldDouble;
|
|
21
|
+
}
|
|
22
|
+
return !!(sum % 10 === 0 ? sanitized : false);
|
|
23
|
+
};
|
|
24
|
+
// https://github.com/validatorjs/validator.js/blob/master/src/lib/isCreditCard.js
|
|
25
|
+
export 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}))$/;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
type DayDelimiter = "." | "/" | "-";
|
|
2
|
+
type DayPart = DayPatterns[PartKey];
|
|
3
|
+
type PartKey = keyof DayPatterns;
|
|
4
|
+
type DayPatterns = {
|
|
5
|
+
y: "yy" | "yyyy";
|
|
6
|
+
m: "mm" | "m";
|
|
7
|
+
d: "dd" | "d";
|
|
8
|
+
};
|
|
9
|
+
type fragment<part extends DayPart, delimiter extends DayDelimiter> = `${delimiter}${part}` | "";
|
|
10
|
+
export type DayPattern<delimiter extends DayDelimiter = DayDelimiter> = delimiter extends unknown ? {
|
|
11
|
+
[k1 in keyof DayPatterns]: {
|
|
12
|
+
[k2 in Exclude<keyof DayPatterns, k1>]: `${DayPatterns[k1]}${fragment<DayPatterns[k2], delimiter>}${fragment<DayPatterns[Exclude<keyof DayPatterns, k1 | k2>], delimiter>}`;
|
|
13
|
+
}[Exclude<keyof DayPatterns, k1>];
|
|
14
|
+
}[keyof DayPatterns] : never;
|
|
15
|
+
export type DateFormat = "iso8601" | DayPattern;
|
|
16
|
+
export type DateOptions = {
|
|
17
|
+
format?: DateFormat;
|
|
18
|
+
};
|
|
19
|
+
export declare const tryParseDatePattern: (data: string, opts?: DateOptions) => Date | string;
|
|
20
|
+
export {};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
const dayDelimiterMatcher = /^[./-]$/;
|
|
2
|
+
// ISO 8601 date/time modernized from https://github.com/validatorjs/validator.js/blob/master/src/lib/isISO8601.js
|
|
3
|
+
// Based on https://tc39.es/ecma262/#sec-date-time-string-format, the T
|
|
4
|
+
// delimiter for date/time is mandatory. Regex from validator.js strict matcher:
|
|
5
|
+
const iso8601Matcher = /^([+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-3])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T]((([01]\d|2[0-3])((:?)[0-5]\d)?|24:?00)([.,]\d+(?!:))?)?(\17[0-5]\d([.,]\d+)?)?([zZ]|([+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/;
|
|
6
|
+
const isValidDateInstance = (date) => !Number.isNaN(+date);
|
|
7
|
+
const writeFormattedExpected = (format) => `a ${format}-formatted date`;
|
|
8
|
+
export const tryParseDatePattern = (data, opts) => {
|
|
9
|
+
if (!opts?.format) {
|
|
10
|
+
const result = new Date(data);
|
|
11
|
+
return isValidDateInstance(result) ? result : "a valid date";
|
|
12
|
+
}
|
|
13
|
+
if (opts.format === "iso8601") {
|
|
14
|
+
return iso8601Matcher.test(data) ?
|
|
15
|
+
new Date(data)
|
|
16
|
+
: writeFormattedExpected("iso8601");
|
|
17
|
+
}
|
|
18
|
+
const dataParts = data.split(dayDelimiterMatcher);
|
|
19
|
+
// will be the first delimiter matched, if there is one
|
|
20
|
+
const delimiter = data[dataParts[0].length];
|
|
21
|
+
const formatParts = delimiter ? opts.format.split(delimiter) : [opts.format];
|
|
22
|
+
if (dataParts.length !== formatParts.length)
|
|
23
|
+
return writeFormattedExpected(opts.format);
|
|
24
|
+
const parsedParts = {};
|
|
25
|
+
for (let i = 0; i < formatParts.length; i++) {
|
|
26
|
+
if (dataParts[i].length !== formatParts[i].length &&
|
|
27
|
+
// if format is "m" or "d", data is allowed to be 1 or 2 characters
|
|
28
|
+
!(formatParts[i].length === 1 && dataParts[i].length === 2))
|
|
29
|
+
return writeFormattedExpected(opts.format);
|
|
30
|
+
parsedParts[formatParts[i][0]] = dataParts[i];
|
|
31
|
+
}
|
|
32
|
+
const date = new Date(`${parsedParts.m}/${parsedParts.d}/${parsedParts.y}`);
|
|
33
|
+
if (`${date.getDate()}` === parsedParts.d)
|
|
34
|
+
return date;
|
|
35
|
+
return writeFormattedExpected(opts.format);
|
|
36
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { rootNode } from "@ark/schema";
|
|
2
|
+
import { regexStringNode } from "./regex.js";
|
|
3
|
+
// Based on https://github.com/validatorjs/validator.js/blob/master/src/lib/isIP.js
|
|
4
|
+
const ipv4Segment = "(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])";
|
|
5
|
+
const ipv4Address = `(${ipv4Segment}[.]){3}${ipv4Segment}`;
|
|
6
|
+
const ipv4Matcher = new RegExp(`^${ipv4Address}$`);
|
|
7
|
+
export const ipv4 = regexStringNode(ipv4Matcher, "a valid IPv4 address");
|
|
8
|
+
const ipv6Segment = "(?:[0-9a-fA-F]{1,4})";
|
|
9
|
+
const ipv6Matcher = new RegExp("^(" +
|
|
10
|
+
`(?:${ipv6Segment}:){7}(?:${ipv6Segment}|:)|` +
|
|
11
|
+
`(?:${ipv6Segment}:){6}(?:${ipv4Address}|:${ipv6Segment}|:)|` +
|
|
12
|
+
`(?:${ipv6Segment}:){5}(?::${ipv4Address}|(:${ipv6Segment}){1,2}|:)|` +
|
|
13
|
+
`(?:${ipv6Segment}:){4}(?:(:${ipv6Segment}){0,1}:${ipv4Address}|(:${ipv6Segment}){1,3}|:)|` +
|
|
14
|
+
`(?:${ipv6Segment}:){3}(?:(:${ipv6Segment}){0,2}:${ipv4Address}|(:${ipv6Segment}){1,4}|:)|` +
|
|
15
|
+
`(?:${ipv6Segment}:){2}(?:(:${ipv6Segment}){0,3}:${ipv4Address}|(:${ipv6Segment}){1,5}|:)|` +
|
|
16
|
+
`(?:${ipv6Segment}:){1}(?:(:${ipv6Segment}){0,4}:${ipv4Address}|(:${ipv6Segment}){1,6}|:)|` +
|
|
17
|
+
`(?::((?::${ipv6Segment}){0,5}:${ipv4Address}|(?::${ipv6Segment}){1,7}|:))` +
|
|
18
|
+
")(%[0-9a-zA-Z-.:]{1,})?$");
|
|
19
|
+
export const ipv6 = regexStringNode(ipv6Matcher, "a valid IPv6 address");
|
|
20
|
+
export const ip = rootNode([ipv4, ipv6]);
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { anonymous, AtLeast, AtMost, DivisibleBy, number, string } from "../ast.js";
|
|
2
|
+
import type { Module } from "../module.js";
|
|
3
|
+
export interface validationExports {
|
|
4
|
+
alpha: string.matching<anonymous>;
|
|
5
|
+
alphanumeric: string.matching<anonymous>;
|
|
6
|
+
digits: string.matching<anonymous>;
|
|
7
|
+
lowercase: string.matching<anonymous>;
|
|
8
|
+
uppercase: string.matching<anonymous>;
|
|
9
|
+
creditCard: string.matching<anonymous>;
|
|
10
|
+
email: string.matching<anonymous>;
|
|
11
|
+
uuid: string.matching<anonymous>;
|
|
12
|
+
url: string.matching<anonymous>;
|
|
13
|
+
semver: string.matching<anonymous>;
|
|
14
|
+
ip: string.matching<anonymous>;
|
|
15
|
+
integer: number.divisibleBy<1>;
|
|
16
|
+
unixTimestamp: number.is<DivisibleBy<1> & AtMost<8640000000000000> & AtLeast<-8640000000000000>>;
|
|
17
|
+
}
|
|
18
|
+
export type validation = Module<validationExports>;
|
|
19
|
+
export declare const validation: validation;
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { rootNode } from "@ark/schema";
|
|
2
|
+
import { scope } from "../scope.js";
|
|
3
|
+
import { creditCardMatcher, isLuhnValid } from "./utils/creditCard.js";
|
|
4
|
+
import { ip } from "./utils/ip.js";
|
|
5
|
+
import { regexStringNode } from "./utils/regex.js";
|
|
6
|
+
// Non-trivial expressions should have an explanation or attribution
|
|
7
|
+
const url = rootNode({
|
|
8
|
+
domain: "string",
|
|
9
|
+
predicate: {
|
|
10
|
+
meta: "a valid URL",
|
|
11
|
+
predicate: (s) => {
|
|
12
|
+
try {
|
|
13
|
+
new URL(s);
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
// https://www.regular-expressions.info/email.html
|
|
23
|
+
const emailMatcher = /^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/;
|
|
24
|
+
const email = regexStringNode(emailMatcher, "a valid email");
|
|
25
|
+
const uuidMatcher = /^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$/;
|
|
26
|
+
// https://github.com/validatorjs/validator.js/blob/master/src/lib/isUUID.js
|
|
27
|
+
const uuid = regexStringNode(uuidMatcher, "a valid UUID");
|
|
28
|
+
const semverMatcher = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/;
|
|
29
|
+
// https://semver.org/
|
|
30
|
+
const semver = regexStringNode(semverMatcher, "a valid semantic version (see https://semver.org/)");
|
|
31
|
+
const creditCard = rootNode({
|
|
32
|
+
domain: "string",
|
|
33
|
+
pattern: {
|
|
34
|
+
meta: "a valid credit card number",
|
|
35
|
+
rule: creditCardMatcher.source
|
|
36
|
+
},
|
|
37
|
+
predicate: {
|
|
38
|
+
meta: "a valid credit card number",
|
|
39
|
+
predicate: isLuhnValid
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
/**
|
|
43
|
+
* As per the ECMA-262 specification:
|
|
44
|
+
* A time value supports a slightly smaller range of -8,640,000,000,000,000 to 8,640,000,000,000,000 milliseconds.
|
|
45
|
+
*
|
|
46
|
+
* @see https://262.ecma-international.org/15.0/index.html#sec-time-values-and-time-range
|
|
47
|
+
*/
|
|
48
|
+
const unixTimestamp = rootNode({
|
|
49
|
+
domain: {
|
|
50
|
+
domain: "number",
|
|
51
|
+
meta: "a number representing a Unix timestamp"
|
|
52
|
+
},
|
|
53
|
+
divisor: {
|
|
54
|
+
rule: 1,
|
|
55
|
+
meta: `an integer representing a Unix timestamp`
|
|
56
|
+
},
|
|
57
|
+
min: {
|
|
58
|
+
rule: -8640000000000000,
|
|
59
|
+
meta: `a Unix timestamp after -8640000000000000`
|
|
60
|
+
},
|
|
61
|
+
max: {
|
|
62
|
+
rule: 8640000000000000,
|
|
63
|
+
meta: "a Unix timestamp before 8640000000000000"
|
|
64
|
+
},
|
|
65
|
+
meta: "an integer representing a safe Unix timestamp"
|
|
66
|
+
});
|
|
67
|
+
export const validation = scope({
|
|
68
|
+
alpha: regexStringNode(/^[A-Za-z]*$/, "only letters"),
|
|
69
|
+
alphanumeric: regexStringNode(/^[A-Za-z\d]*$/, "only letters and digits 0-9"),
|
|
70
|
+
digits: regexStringNode(/^\d*$/, "only digits 0-9"),
|
|
71
|
+
lowercase: regexStringNode(/^[a-z]*$/, "only lowercase letters"),
|
|
72
|
+
uppercase: regexStringNode(/^[A-Z]*$/, "only uppercase letters"),
|
|
73
|
+
creditCard,
|
|
74
|
+
email,
|
|
75
|
+
uuid,
|
|
76
|
+
url,
|
|
77
|
+
semver,
|
|
78
|
+
ip,
|
|
79
|
+
integer: rootNode({
|
|
80
|
+
domain: "number",
|
|
81
|
+
divisor: 1
|
|
82
|
+
}),
|
|
83
|
+
unixTimestamp
|
|
84
|
+
}, { prereducedAliases: true }).export();
|
package/out/module.d.ts
CHANGED
|
@@ -1,14 +1,22 @@
|
|
|
1
|
-
import { RootModule, type
|
|
1
|
+
import { RootModule, type GenericAst, type PreparsedNodeResolution } from "@ark/schema";
|
|
2
2
|
import type { anyOrNever } from "@ark/util";
|
|
3
3
|
import type { Generic } from "./generic.js";
|
|
4
4
|
import type { Type } from "./type.js";
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}
|
|
12
|
-
export
|
|
13
|
-
|
|
14
|
-
export {
|
|
5
|
+
export declare const Module: new <$ extends {}>(exports: exportScope<$>) => Module<$>;
|
|
6
|
+
export interface Module<$ extends {} = {}> extends RootModule<exportScope<$>> {
|
|
7
|
+
}
|
|
8
|
+
export type exportScope<$> = {
|
|
9
|
+
[k in keyof $]: instantiateExport<$[k], $>;
|
|
10
|
+
} & unknown;
|
|
11
|
+
export declare const BoundModule: new <exports extends {}, $ extends {}>(exports: bindExportsToScope<exports, $>, $: $) => BoundModule<exports, $>;
|
|
12
|
+
export interface BoundModule<exports extends {}, $> extends RootModule<bindExportsToScope<exports, $>> {
|
|
13
|
+
}
|
|
14
|
+
export type bindExportsToScope<exports, $> = {
|
|
15
|
+
[k in keyof exports]: instantiateExport<exports[k], $>;
|
|
16
|
+
} & unknown;
|
|
17
|
+
export type Submodule<exports extends {}> = RootModule<exports>;
|
|
18
|
+
export type instantiateExport<t, $> = [
|
|
19
|
+
t
|
|
20
|
+
] extends [PreparsedNodeResolution] ? [
|
|
21
|
+
t
|
|
22
|
+
] extends [anyOrNever] ? Type<t, $> : t extends GenericAst<infer params, infer body, infer body$> ? Generic<params, body, body$, $> : t extends Submodule<infer exports> ? BoundModule<exports, $> : never : Type<t, $>;
|
package/out/module.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { type BaseRoot
|
|
2
|
-
import { type ErrorMessage, type Primitive, type anyOrNever, type array, type defined, type equals, type
|
|
1
|
+
import { type BaseRoot } from "@ark/schema";
|
|
2
|
+
import { type ErrorMessage, type Primitive, type anyOrNever, type array, type defined, type equals, type objectKindOrDomainOf, type optionalKeyOf, type requiredKeyOf, type show } from "@ark/util";
|
|
3
3
|
import type { type } from "../ark.js";
|
|
4
|
+
import type { string } from "../ast.js";
|
|
4
5
|
import type { ParseContext } from "../scope.js";
|
|
5
6
|
import { type inferObjectLiteral, type validateObjectLiteral } from "./objectLiteral.js";
|
|
6
7
|
import type { validateString } from "./semantic/validate.js";
|
|
@@ -9,8 +10,8 @@ import { type TupleExpression, type inferTuple, type validateTuple } from "./tup
|
|
|
9
10
|
export declare const parseObject: (def: object, ctx: ParseContext) => BaseRoot;
|
|
10
11
|
export type inferDefinition<def, $, args> = [
|
|
11
12
|
def
|
|
12
|
-
] extends [anyOrNever] ? def
|
|
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>>> :
|
|
13
|
+
] extends [anyOrNever] ? def : def extends type.cast<infer t> | 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;
|
|
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>;
|
|
14
15
|
export type validateDeclared<declared, def, $, args> = def extends validateDefinition<def, $, args> ? validateInference<def, declared, $, args> : validateDefinition<def, $, args>;
|
|
15
16
|
type validateInference<def, declared, $, args> = def extends RegExp | type.cast<unknown> | ThunkCast | TupleExpression ? validateShallowInference<def, declared, $, args> : def extends array ? declared extends array ? {
|
|
16
17
|
[i in keyof declared]: i extends keyof def ? validateInference<def[i], declared[i], $, args> : declared[i];
|