arktype 2.0.0-rc.26 → 2.0.0-rc.28
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/attributes.d.ts +61 -241
- package/out/attributes.js +2 -1
- package/out/generic.d.ts +4 -4
- package/out/keywords/{constructors/Array.d.ts → Array.d.ts} +2 -2
- package/out/keywords/{constructors/Array.js → Array.js} +1 -1
- package/out/keywords/{constructors/FormData.d.ts → FormData.d.ts} +2 -2
- package/out/keywords/{constructors/FormData.js → FormData.js} +1 -1
- package/out/keywords/{constructors/TypedArray.d.ts → TypedArray.d.ts} +1 -1
- package/out/keywords/{constructors/TypedArray.js → TypedArray.js} +1 -1
- package/out/keywords/{constructors/constructors.d.ts → constructors.d.ts} +1 -1
- package/out/keywords/{constructors/constructors.js → constructors.js} +1 -1
- package/out/keywords/keywords.d.ts +6 -9
- package/out/keywords/keywords.js +3 -3
- package/out/keywords/number.d.ts +23 -0
- package/out/keywords/{number/epoch.js → number.js} +23 -1
- package/out/keywords/string.d.ts +327 -0
- package/out/keywords/string.js +398 -0
- package/out/methods/array.d.ts +6 -7
- package/out/methods/base.d.ts +10 -14
- package/out/methods/date.d.ts +5 -6
- package/out/methods/number.d.ts +6 -7
- package/out/methods/object.d.ts +5 -5
- package/out/methods/string.d.ts +7 -8
- package/out/parser/ast/generic.d.ts +17 -0
- package/out/parser/ast/infer.d.ts +4 -18
- package/out/parser/ast/validate.d.ts +10 -6
- package/out/parser/ast/validate.js +2 -0
- package/out/parser/definition.d.ts +17 -8
- package/out/parser/definition.js +20 -2
- package/out/parser/objectLiteral.d.ts +39 -39
- package/out/parser/objectLiteral.js +75 -49
- package/out/parser/property.d.ts +37 -0
- package/out/parser/property.js +16 -0
- package/out/parser/reduce/dynamic.d.ts +1 -1
- package/out/parser/shift/operand/enclosed.d.ts +1 -3
- package/out/parser/shift/operand/unenclosed.d.ts +2 -1
- package/out/parser/shift/operator/default.d.ts +2 -1
- package/out/parser/shift/operator/default.js +1 -1
- package/out/parser/string.d.ts +5 -4
- package/out/parser/string.js +18 -1
- package/out/parser/tupleExpressions.d.ts +47 -0
- package/out/parser/tupleExpressions.js +67 -0
- package/out/parser/tupleLiteral.d.ts +80 -0
- package/out/parser/tupleLiteral.js +119 -0
- package/out/scope.d.ts +8 -7
- package/out/scope.js +12 -32
- package/out/type.d.ts +5 -5
- package/package.json +3 -3
- package/out/keywords/constructors/Date.d.ts +0 -37
- package/out/keywords/number/epoch.d.ts +0 -10
- package/out/keywords/number/integer.d.ts +0 -3
- package/out/keywords/number/integer.js +0 -5
- package/out/keywords/number/number.d.ts +0 -58
- package/out/keywords/number/number.js +0 -21
- package/out/keywords/string/alpha.d.ts +0 -7
- package/out/keywords/string/alpha.js +0 -2
- package/out/keywords/string/alphanumeric.d.ts +0 -7
- package/out/keywords/string/alphanumeric.js +0 -2
- package/out/keywords/string/base64.d.ts +0 -21
- package/out/keywords/string/base64.js +0 -6
- package/out/keywords/string/capitalize.d.ts +0 -15
- package/out/keywords/string/capitalize.js +0 -12
- package/out/keywords/string/creditCard.d.ts +0 -8
- package/out/keywords/string/creditCard.js +0 -37
- package/out/keywords/string/date.d.ts +0 -55
- package/out/keywords/string/date.js +0 -98
- package/out/keywords/string/digits.d.ts +0 -7
- package/out/keywords/string/digits.js +0 -2
- package/out/keywords/string/email.d.ts +0 -7
- package/out/keywords/string/email.js +0 -4
- package/out/keywords/string/integer.d.ts +0 -16
- package/out/keywords/string/integer.js +0 -16
- package/out/keywords/string/ip.d.ts +0 -20
- package/out/keywords/string/ip.js +0 -23
- package/out/keywords/string/json.d.ts +0 -16
- package/out/keywords/string/json.js +0 -54
- package/out/keywords/string/lower.d.ts +0 -15
- package/out/keywords/string/lower.js +0 -12
- package/out/keywords/string/normalize.d.ts +0 -88
- package/out/keywords/string/normalize.js +0 -43
- package/out/keywords/string/numeric.d.ts +0 -15
- package/out/keywords/string/numeric.js +0 -13
- package/out/keywords/string/semver.d.ts +0 -7
- package/out/keywords/string/semver.js +0 -4
- package/out/keywords/string/string.d.ts +0 -135
- package/out/keywords/string/string.js +0 -43
- package/out/keywords/string/trim.d.ts +0 -15
- package/out/keywords/string/trim.js +0 -14
- package/out/keywords/string/upper.d.ts +0 -15
- package/out/keywords/string/upper.js +0 -12
- package/out/keywords/string/url.d.ts +0 -15
- package/out/keywords/string/url.js +0 -37
- package/out/keywords/string/utils.d.ts +0 -2
- package/out/keywords/string/utils.js +0 -10
- package/out/keywords/string/uuid.d.ts +0 -42
- package/out/keywords/string/uuid.js +0 -19
- package/out/parser/tuple.d.ts +0 -148
- package/out/parser/tuple.js +0 -164
- /package/out/{keywords/constructors/Date.js → parser/ast/generic.js} +0 -0
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import type { satisfy } from "@ark/util";
|
|
2
|
-
import type { Anonymous, AtLeast, AtMost, AttributeKind, Attributes, brand, Default, DivisibleBy, LessThan, MoreThan, Nominal, of, Optional } from "../../attributes.ts";
|
|
3
|
-
import type { Module, Submodule } from "../../module.ts";
|
|
4
|
-
import { epoch } from "./epoch.ts";
|
|
5
|
-
import { integer } from "./integer.ts";
|
|
6
|
-
export declare const number: number.module;
|
|
7
|
-
export declare namespace number {
|
|
8
|
-
type atLeast<rule> = of<number, AtLeast<rule>>;
|
|
9
|
-
type moreThan<rule> = of<number, MoreThan<rule>>;
|
|
10
|
-
type atMost<rule> = of<number, AtMost<rule>>;
|
|
11
|
-
type lessThan<rule> = of<number, LessThan<rule>>;
|
|
12
|
-
type divisibleBy<rule> = of<number, DivisibleBy<rule>>;
|
|
13
|
-
type anonymous = of<number, Anonymous>;
|
|
14
|
-
type optional = of<number, Optional>;
|
|
15
|
-
type defaultsTo<rule> = of<number, Default<rule>>;
|
|
16
|
-
type nominal<rule> = of<number, Nominal<rule>>;
|
|
17
|
-
type NaN = nominal<"NaN">;
|
|
18
|
-
type Infinity = nominal<"Infinity">;
|
|
19
|
-
type NegativeInfinity = nominal<"NegativeInfinity">;
|
|
20
|
-
type safe = nominal<"safe">;
|
|
21
|
-
type is<attributes extends Attributes> = of<number, attributes>;
|
|
22
|
-
type AttributableKind = satisfy<AttributeKind, "divisibleBy" | "moreThan" | "atLeast" | "atMost" | "lessThan">;
|
|
23
|
-
type minSchemaToConstraint<schema, rule> = schema extends {
|
|
24
|
-
exclusive: true;
|
|
25
|
-
} ? MoreThan<rule> : AtLeast<rule>;
|
|
26
|
-
type maxSchemaToConstraint<schema, rule> = schema extends {
|
|
27
|
-
exclusive: true;
|
|
28
|
-
} ? LessThan<rule> : AtMost<rule>;
|
|
29
|
-
type withSingleAttribute<kind extends AttributableKind, value extends Attributes[kind]> = raw.withSingleAttribute<kind, value>;
|
|
30
|
-
namespace raw {
|
|
31
|
-
type withSingleAttribute<kind, value> = kind extends "nominal" ? nominal<value> : kind extends "divisibleBy" ? divisibleBy<value> : kind extends "moreThan" ? moreThan<value> : kind extends "atLeast" ? atLeast<value> : kind extends "atMost" ? atMost<value> : kind extends "lessThan" ? lessThan<value> : kind extends "optional" ? optional : kind extends "defaultsTo" ? defaultsTo<value> : never;
|
|
32
|
-
}
|
|
33
|
-
type module = Module<submodule>;
|
|
34
|
-
type submodule = Submodule<$>;
|
|
35
|
-
type $ = {
|
|
36
|
-
root: number;
|
|
37
|
-
epoch: epoch;
|
|
38
|
-
integer: integer;
|
|
39
|
-
safe: safe;
|
|
40
|
-
NaN: NaN;
|
|
41
|
-
Infinity: Infinity;
|
|
42
|
-
NegativeInfinity: NegativeInfinity;
|
|
43
|
-
};
|
|
44
|
-
type branded<rule> = brand<number, Nominal<rule>>;
|
|
45
|
-
namespace branded {
|
|
46
|
-
type atLeast<rule> = brand<number, AtLeast<rule>>;
|
|
47
|
-
type moreThan<rule> = brand<number, MoreThan<rule>>;
|
|
48
|
-
type atMost<rule> = brand<number, AtMost<rule>>;
|
|
49
|
-
type lessThan<rule> = brand<number, LessThan<rule>>;
|
|
50
|
-
type divisibleBy<rule> = brand<number, DivisibleBy<rule>>;
|
|
51
|
-
type is<attributes extends Attributes> = brand<number, attributes>;
|
|
52
|
-
type anonymous = brand<number, Anonymous>;
|
|
53
|
-
type withSingleAttribute<kind extends AttributableKind, value extends Attributes[kind]> = raw.withSingleAttribute<kind, value>;
|
|
54
|
-
namespace raw {
|
|
55
|
-
type withSingleAttribute<kind, value> = kind extends "nominal" ? branded<value> : kind extends "divisibleBy" ? divisibleBy<value> : kind extends "moreThan" ? moreThan<value> : kind extends "atLeast" ? atLeast<value> : kind extends "atMost" ? atMost<value> : kind extends "lessThan" ? lessThan<value> : never;
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
}
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { intrinsic, rootSchema } from "@ark/schema";
|
|
2
|
-
import { arkModule } from "../utils.js";
|
|
3
|
-
import { epoch } from "./epoch.js";
|
|
4
|
-
import { integer } from "./integer.js";
|
|
5
|
-
export const number = arkModule({
|
|
6
|
-
root: intrinsic.number,
|
|
7
|
-
integer,
|
|
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]
|
|
21
|
-
});
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import type { Nominal, of } from "../../attributes.ts";
|
|
2
|
-
declare namespace string {
|
|
3
|
-
type alphanumeric = of<string, Nominal<"alphanumeric">>;
|
|
4
|
-
}
|
|
5
|
-
export declare const alphanumeric: import("@ark/schema").IntersectionNode;
|
|
6
|
-
export type alphanumeric = string.alphanumeric;
|
|
7
|
-
export {};
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import type { Nominal, of } from "../../attributes.ts";
|
|
2
|
-
import type { Module, Submodule } from "../../module.ts";
|
|
3
|
-
export declare const base64: Module<{
|
|
4
|
-
root: unknown;
|
|
5
|
-
url: unknown;
|
|
6
|
-
}>;
|
|
7
|
-
declare namespace string {
|
|
8
|
-
type base64 = of<string, Nominal<"base64">>;
|
|
9
|
-
namespace base64 {
|
|
10
|
-
type url = of<string, Nominal<"base64.url">>;
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
export declare namespace base64 {
|
|
14
|
-
type module = Module<submodule>;
|
|
15
|
-
type submodule = Submodule<$>;
|
|
16
|
-
type $ = {
|
|
17
|
-
root: string.base64;
|
|
18
|
-
url: string.base64.url;
|
|
19
|
-
};
|
|
20
|
-
}
|
|
21
|
-
export {};
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { arkModule } from "../utils.js";
|
|
2
|
-
import { regexStringNode } from "./utils.js";
|
|
3
|
-
export const base64 = arkModule({
|
|
4
|
-
root: regexStringNode(/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/, "base64-encoded"),
|
|
5
|
-
url: regexStringNode(/^(?:[A-Za-z0-9_-]{4})*(?:[A-Za-z0-9_-]{2}(?:==|%3D%3D)?|[A-Za-z0-9_-]{3}(?:=|%3D)?)?$/, "base64url-encoded")
|
|
6
|
-
});
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import type { Nominal, of, To } from "../../attributes.ts";
|
|
2
|
-
import type { Module, Submodule } from "../../module.ts";
|
|
3
|
-
declare namespace string {
|
|
4
|
-
type capitalized = of<string, Nominal<"capitalized">>;
|
|
5
|
-
}
|
|
6
|
-
export declare const capitalize: capitalize.module;
|
|
7
|
-
export declare namespace capitalize {
|
|
8
|
-
type module = Module<submodule>;
|
|
9
|
-
type submodule = Submodule<$>;
|
|
10
|
-
type $ = {
|
|
11
|
-
root: (In: string) => To<string.capitalized>;
|
|
12
|
-
preformatted: string.capitalized;
|
|
13
|
-
};
|
|
14
|
-
}
|
|
15
|
-
export {};
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { rootSchema } from "@ark/schema";
|
|
2
|
-
import { arkModule } from "../utils.js";
|
|
3
|
-
import { regexStringNode } from "./utils.js";
|
|
4
|
-
const preformatted = regexStringNode(/^[A-Z].*$/, "capitalized");
|
|
5
|
-
export const capitalize = arkModule({
|
|
6
|
-
root: rootSchema({
|
|
7
|
-
in: "string",
|
|
8
|
-
morphs: (s) => s.charAt(0).toUpperCase() + s.slice(1),
|
|
9
|
-
declaredOut: preformatted
|
|
10
|
-
}),
|
|
11
|
-
preformatted
|
|
12
|
-
});
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import type { Nominal, of } from "../../attributes.ts";
|
|
2
|
-
export declare const isLuhnValid: (creditCardInput: string) => boolean;
|
|
3
|
-
declare namespace string {
|
|
4
|
-
type creditCard = of<string, Nominal<"creditCard">>;
|
|
5
|
-
}
|
|
6
|
-
export declare const creditCard: import("@ark/schema").BaseRoot<import("@ark/schema").InternalRootDeclaration>;
|
|
7
|
-
export type creditCard = string.creditCard;
|
|
8
|
-
export {};
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import { rootSchema } from "@ark/schema";
|
|
2
|
-
// https://github.com/validatorjs/validator.js/blob/master/src/lib/isLuhnNumber.js
|
|
3
|
-
export const isLuhnValid = (creditCardInput) => {
|
|
4
|
-
const sanitized = creditCardInput.replace(/[- ]+/g, "");
|
|
5
|
-
let sum = 0;
|
|
6
|
-
let digit;
|
|
7
|
-
let tmpNum;
|
|
8
|
-
let shouldDouble = false;
|
|
9
|
-
for (let i = sanitized.length - 1; i >= 0; i--) {
|
|
10
|
-
digit = sanitized.substring(i, i + 1);
|
|
11
|
-
tmpNum = Number.parseInt(digit, 10);
|
|
12
|
-
if (shouldDouble) {
|
|
13
|
-
tmpNum *= 2;
|
|
14
|
-
if (tmpNum >= 10)
|
|
15
|
-
sum += (tmpNum % 10) + 1;
|
|
16
|
-
else
|
|
17
|
-
sum += tmpNum;
|
|
18
|
-
}
|
|
19
|
-
else
|
|
20
|
-
sum += tmpNum;
|
|
21
|
-
shouldDouble = !shouldDouble;
|
|
22
|
-
}
|
|
23
|
-
return !!(sum % 10 === 0 ? sanitized : false);
|
|
24
|
-
};
|
|
25
|
-
// https://github.com/validatorjs/validator.js/blob/master/src/lib/isCreditCard.js
|
|
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 = rootSchema({
|
|
28
|
-
domain: "string",
|
|
29
|
-
pattern: {
|
|
30
|
-
meta: "a credit card number",
|
|
31
|
-
rule: creditCardMatcher.source
|
|
32
|
-
},
|
|
33
|
-
predicate: {
|
|
34
|
-
meta: "a credit card number",
|
|
35
|
-
predicate: isLuhnValid
|
|
36
|
-
}
|
|
37
|
-
});
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import type { Nominal, To, of } from "../../attributes.ts";
|
|
2
|
-
import type { Module, Submodule } from "../../module.ts";
|
|
3
|
-
type DayDelimiter = "." | "/" | "-";
|
|
4
|
-
type DayPart = DayPatterns[PartKey];
|
|
5
|
-
type PartKey = keyof DayPatterns;
|
|
6
|
-
type DayPatterns = {
|
|
7
|
-
y: "yy" | "yyyy";
|
|
8
|
-
m: "mm" | "m";
|
|
9
|
-
d: "dd" | "d";
|
|
10
|
-
};
|
|
11
|
-
type fragment<part extends DayPart, delimiter extends DayDelimiter> = `${delimiter}${part}` | "";
|
|
12
|
-
export type DayPattern<delimiter extends DayDelimiter = DayDelimiter> = delimiter extends unknown ? {
|
|
13
|
-
[k1 in keyof DayPatterns]: {
|
|
14
|
-
[k2 in Exclude<keyof DayPatterns, k1>]: `${DayPatterns[k1]}${fragment<DayPatterns[k2], delimiter>}${fragment<DayPatterns[Exclude<keyof DayPatterns, k1 | k2>], delimiter>}`;
|
|
15
|
-
}[Exclude<keyof DayPatterns, k1>];
|
|
16
|
-
}[keyof DayPatterns] : never;
|
|
17
|
-
export type DateFormat = "iso" | DayPattern;
|
|
18
|
-
export type DateOptions = {
|
|
19
|
-
format?: DateFormat;
|
|
20
|
-
};
|
|
21
|
-
export declare const iso8601Matcher: RegExp;
|
|
22
|
-
export declare const tryParseDatePattern: (data: string, opts?: DateOptions) => Date | string;
|
|
23
|
-
declare namespace string {
|
|
24
|
-
type date = of<string, Nominal<"date">>;
|
|
25
|
-
namespace date {
|
|
26
|
-
type epoch = of<string, Nominal<"date.epoch">>;
|
|
27
|
-
type iso = of<string, Nominal<"date.iso">>;
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
export declare const stringDate: stringDate.module;
|
|
31
|
-
export declare namespace stringDate {
|
|
32
|
-
type module = Module<stringDate.submodule>;
|
|
33
|
-
type submodule = Submodule<$>;
|
|
34
|
-
type $ = {
|
|
35
|
-
root: string.date;
|
|
36
|
-
parse: (In: string.date) => To<Date>;
|
|
37
|
-
iso: iso.submodule;
|
|
38
|
-
epoch: epoch.submodule;
|
|
39
|
-
};
|
|
40
|
-
namespace iso {
|
|
41
|
-
type submodule = Submodule<$>;
|
|
42
|
-
type $ = {
|
|
43
|
-
root: string.date.iso;
|
|
44
|
-
parse: (In: string.date.iso) => To<Date>;
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
namespace epoch {
|
|
48
|
-
type submodule = Submodule<$>;
|
|
49
|
-
type $ = {
|
|
50
|
-
root: string.date.epoch;
|
|
51
|
-
parse: (In: string.date.epoch) => To<Date>;
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
export {};
|
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
import { ArkErrors, intrinsic, rootSchema } from "@ark/schema";
|
|
2
|
-
import { number } from "../number/number.js";
|
|
3
|
-
import { arkModule } from "../utils.js";
|
|
4
|
-
import { integer } from "./integer.js";
|
|
5
|
-
import { regexStringNode } from "./utils.js";
|
|
6
|
-
const dayDelimiterMatcher = /^[./-]$/;
|
|
7
|
-
// ISO 8601 date/time modernized from https://github.com/validatorjs/validator.js/blob/master/src/lib/isISO8601.js
|
|
8
|
-
// Based on https://tc39.es/ecma262/#sec-date-time-string-format, the T
|
|
9
|
-
// delimiter for date/time is mandatory. Regex from validator.js strict matcher:
|
|
10
|
-
export 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)?)?)?)?$/;
|
|
11
|
-
const isValidDateInstance = (date) => !Number.isNaN(+date);
|
|
12
|
-
const writeFormattedExpected = (format) => `a ${format}-formatted date`;
|
|
13
|
-
export const tryParseDatePattern = (data, opts) => {
|
|
14
|
-
if (!opts?.format) {
|
|
15
|
-
const result = new Date(data);
|
|
16
|
-
return isValidDateInstance(result) ? result : "a valid date";
|
|
17
|
-
}
|
|
18
|
-
if (opts.format === "iso") {
|
|
19
|
-
return iso8601Matcher.test(data) ?
|
|
20
|
-
new Date(data)
|
|
21
|
-
: writeFormattedExpected("iso");
|
|
22
|
-
}
|
|
23
|
-
const dataParts = data.split(dayDelimiterMatcher);
|
|
24
|
-
// will be the first delimiter matched, if there is one
|
|
25
|
-
const delimiter = data[dataParts[0].length];
|
|
26
|
-
const formatParts = delimiter ? opts.format.split(delimiter) : [opts.format];
|
|
27
|
-
if (dataParts.length !== formatParts.length)
|
|
28
|
-
return writeFormattedExpected(opts.format);
|
|
29
|
-
const parsedParts = {};
|
|
30
|
-
for (let i = 0; i < formatParts.length; i++) {
|
|
31
|
-
if (dataParts[i].length !== formatParts[i].length &&
|
|
32
|
-
// if format is "m" or "d", data is allowed to be 1 or 2 characters
|
|
33
|
-
!(formatParts[i].length === 1 && dataParts[i].length === 2))
|
|
34
|
-
return writeFormattedExpected(opts.format);
|
|
35
|
-
parsedParts[formatParts[i][0]] = dataParts[i];
|
|
36
|
-
}
|
|
37
|
-
const date = new Date(`${parsedParts.m}/${parsedParts.d}/${parsedParts.y}`);
|
|
38
|
-
if (`${date.getDate()}` === parsedParts.d)
|
|
39
|
-
return date;
|
|
40
|
-
return writeFormattedExpected(opts.format);
|
|
41
|
-
};
|
|
42
|
-
const isParsableDate = (s) => !Number.isNaN(new Date(s).valueOf());
|
|
43
|
-
const parsableDate = rootSchema({
|
|
44
|
-
domain: "string",
|
|
45
|
-
predicate: {
|
|
46
|
-
meta: "a parsable date",
|
|
47
|
-
predicate: isParsableDate
|
|
48
|
-
}
|
|
49
|
-
}).assertHasKind("intersection");
|
|
50
|
-
const epochRoot = integer.root.internal
|
|
51
|
-
.narrow((s, ctx) => {
|
|
52
|
-
// we know this is safe since it has already
|
|
53
|
-
// been validated as an integer string
|
|
54
|
-
const n = Number.parseInt(s);
|
|
55
|
-
const out = number.epoch(n);
|
|
56
|
-
if (out instanceof ArkErrors) {
|
|
57
|
-
ctx.errors.merge(out);
|
|
58
|
-
return false;
|
|
59
|
-
}
|
|
60
|
-
return true;
|
|
61
|
-
})
|
|
62
|
-
.withMeta({
|
|
63
|
-
description: "an integer string representing a safe Unix timestamp"
|
|
64
|
-
})
|
|
65
|
-
.assertHasKind("intersection");
|
|
66
|
-
const epoch = arkModule({
|
|
67
|
-
root: epochRoot,
|
|
68
|
-
parse: rootSchema({
|
|
69
|
-
in: epochRoot,
|
|
70
|
-
morphs: (s) => new Date(s),
|
|
71
|
-
declaredOut: intrinsic.Date
|
|
72
|
-
})
|
|
73
|
-
});
|
|
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
|
-
morphs: (s) => new Date(s),
|
|
80
|
-
declaredOut: intrinsic.Date
|
|
81
|
-
})
|
|
82
|
-
});
|
|
83
|
-
export const stringDate = arkModule({
|
|
84
|
-
root: parsableDate,
|
|
85
|
-
parse: rootSchema({
|
|
86
|
-
declaredIn: parsableDate,
|
|
87
|
-
in: "string",
|
|
88
|
-
morphs: (s, ctx) => {
|
|
89
|
-
const date = new Date(s);
|
|
90
|
-
if (Number.isNaN(date.valueOf()))
|
|
91
|
-
return ctx.error("a parsable date");
|
|
92
|
-
return date;
|
|
93
|
-
},
|
|
94
|
-
declaredOut: intrinsic.Date
|
|
95
|
-
}),
|
|
96
|
-
iso,
|
|
97
|
-
epoch
|
|
98
|
-
});
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import type { Nominal, of, To } from "../../attributes.ts";
|
|
2
|
-
import type { Module, Submodule } from "../../module.ts";
|
|
3
|
-
import type { number } from "../number/number.ts";
|
|
4
|
-
declare namespace string {
|
|
5
|
-
type integer = of<string, Nominal<"integer">>;
|
|
6
|
-
}
|
|
7
|
-
export declare const integer: stringInteger.module;
|
|
8
|
-
export declare namespace stringInteger {
|
|
9
|
-
type module = Module<submodule>;
|
|
10
|
-
type submodule = Submodule<$>;
|
|
11
|
-
type $ = {
|
|
12
|
-
root: string.integer;
|
|
13
|
-
parse: (In: string.integer) => To<number.divisibleBy<1>>;
|
|
14
|
-
};
|
|
15
|
-
}
|
|
16
|
-
export {};
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { intrinsic, rootSchema } from "@ark/schema";
|
|
2
|
-
import { wellFormedIntegerMatcher } from "@ark/util";
|
|
3
|
-
import { arkModule } from "../utils.js";
|
|
4
|
-
import { regexStringNode } from "./utils.js";
|
|
5
|
-
const root = regexStringNode(wellFormedIntegerMatcher, "a well-formed integer string");
|
|
6
|
-
export const integer = arkModule({
|
|
7
|
-
root,
|
|
8
|
-
parse: rootSchema({
|
|
9
|
-
in: root,
|
|
10
|
-
morphs: (s, ctx) => {
|
|
11
|
-
const parsed = Number.parseInt(s);
|
|
12
|
-
return Number.isSafeInteger(parsed) ? parsed : (ctx.error("an integer in the range Number.MIN_SAFE_INTEGER to Number.MAX_SAFE_INTEGER"));
|
|
13
|
-
},
|
|
14
|
-
declaredOut: intrinsic.integer
|
|
15
|
-
})
|
|
16
|
-
});
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import type { Nominal, of } from "../../attributes.ts";
|
|
2
|
-
import type { Module, Submodule } from "../../module.ts";
|
|
3
|
-
declare namespace string {
|
|
4
|
-
type ip = of<string, Nominal<"ip">>;
|
|
5
|
-
namespace ip {
|
|
6
|
-
type v4 = of<string, Nominal<"ip.v4">>;
|
|
7
|
-
type v6 = of<string, Nominal<"ip.v6">>;
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
export declare const ip: ip.module;
|
|
11
|
-
export declare namespace ip {
|
|
12
|
-
type module = Module<submodule>;
|
|
13
|
-
type submodule = Submodule<$>;
|
|
14
|
-
type $ = {
|
|
15
|
-
root: string.ip;
|
|
16
|
-
v4: string.ip.v4;
|
|
17
|
-
v6: string.ip.v6;
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
export {};
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { arkModule } from "../utils.js";
|
|
2
|
-
import { regexStringNode } from "./utils.js";
|
|
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
|
|
5
|
-
const ipv4Segment = "(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])";
|
|
6
|
-
const ipv4Address = `(${ipv4Segment}[.]){3}${ipv4Segment}`;
|
|
7
|
-
const ipv4Matcher = new RegExp(`^${ipv4Address}$`);
|
|
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 ip = arkModule({
|
|
20
|
-
root: ["v4 | v6", "@", "an IP address"],
|
|
21
|
-
v4: regexStringNode(ipv4Matcher, "an IPv4 address"),
|
|
22
|
-
v6: regexStringNode(ipv6Matcher, "an IPv6 address")
|
|
23
|
-
});
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import type { Nominal, To, of } from "../../attributes.ts";
|
|
2
|
-
import type { Module, Submodule } from "../../module.ts";
|
|
3
|
-
declare namespace string {
|
|
4
|
-
type json = of<string, Nominal<"json">>;
|
|
5
|
-
}
|
|
6
|
-
export declare const writeJsonSyntaxErrorProblem: (error: unknown) => string;
|
|
7
|
-
export declare const json: stringJson.module;
|
|
8
|
-
export declare namespace stringJson {
|
|
9
|
-
type module = Module<submodule>;
|
|
10
|
-
type submodule = Submodule<$>;
|
|
11
|
-
type $ = {
|
|
12
|
-
root: string.json;
|
|
13
|
-
parse: (In: string.json) => To<object>;
|
|
14
|
-
};
|
|
15
|
-
}
|
|
16
|
-
export {};
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import { intrinsic, rootSchema } from "@ark/schema";
|
|
2
|
-
import { arkModule } from "../utils.js";
|
|
3
|
-
const jsonStringDescription = "a JSON string";
|
|
4
|
-
export const writeJsonSyntaxErrorProblem = (error) => {
|
|
5
|
-
if (!(error instanceof SyntaxError))
|
|
6
|
-
throw error;
|
|
7
|
-
return `must be ${jsonStringDescription} (${error})`;
|
|
8
|
-
};
|
|
9
|
-
const root = rootSchema({
|
|
10
|
-
domain: "string",
|
|
11
|
-
predicate: {
|
|
12
|
-
meta: jsonStringDescription,
|
|
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
|
-
}
|
|
26
|
-
}
|
|
27
|
-
});
|
|
28
|
-
const parseJson = (s, ctx) => {
|
|
29
|
-
if (s.length === 0) {
|
|
30
|
-
return ctx.error({
|
|
31
|
-
code: "predicate",
|
|
32
|
-
expected: jsonStringDescription,
|
|
33
|
-
actual: "empty"
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
try {
|
|
37
|
-
return JSON.parse(s);
|
|
38
|
-
}
|
|
39
|
-
catch (e) {
|
|
40
|
-
return ctx.error({
|
|
41
|
-
code: "predicate",
|
|
42
|
-
expected: jsonStringDescription,
|
|
43
|
-
problem: writeJsonSyntaxErrorProblem(e)
|
|
44
|
-
});
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
|
-
export const json = arkModule({
|
|
48
|
-
root,
|
|
49
|
-
parse: rootSchema({
|
|
50
|
-
in: "string",
|
|
51
|
-
morphs: parseJson,
|
|
52
|
-
declaredOut: intrinsic.json
|
|
53
|
-
})
|
|
54
|
-
});
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import type { Nominal, of, To } from "../../attributes.ts";
|
|
2
|
-
import type { Module, Submodule } from "../../module.ts";
|
|
3
|
-
declare namespace string {
|
|
4
|
-
type lowercase = of<string, Nominal<"lowercase">>;
|
|
5
|
-
}
|
|
6
|
-
export declare const lower: lower.module;
|
|
7
|
-
export declare namespace lower {
|
|
8
|
-
type module = Module<submodule>;
|
|
9
|
-
type submodule = Submodule<$>;
|
|
10
|
-
type $ = {
|
|
11
|
-
root: (In: string) => To<string.lowercase>;
|
|
12
|
-
preformatted: string.lowercase;
|
|
13
|
-
};
|
|
14
|
-
}
|
|
15
|
-
export {};
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { rootSchema } from "@ark/schema";
|
|
2
|
-
import { arkModule } from "../utils.js";
|
|
3
|
-
import { regexStringNode } from "./utils.js";
|
|
4
|
-
const preformatted = regexStringNode(/^[a-z]*$/, "only lowercase letters");
|
|
5
|
-
export const lower = arkModule({
|
|
6
|
-
root: rootSchema({
|
|
7
|
-
in: "string",
|
|
8
|
-
morphs: (s) => s.toLowerCase(),
|
|
9
|
-
declaredOut: preformatted
|
|
10
|
-
}),
|
|
11
|
-
preformatted
|
|
12
|
-
});
|