zod 3.25.0-canary.20250211T214501 → 4.0.0-beta.0
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/dist/commonjs/checks.d.ts +1 -0
- package/dist/commonjs/checks.js +32 -0
- package/dist/commonjs/coerce.d.ts +17 -0
- package/dist/commonjs/coerce.js +57 -0
- package/dist/commonjs/compat.d.ts +47 -0
- package/dist/commonjs/compat.js +73 -0
- package/dist/commonjs/errors.d.ts +23 -0
- package/dist/commonjs/errors.js +57 -0
- package/dist/commonjs/external.d.ts +8 -0
- package/dist/commonjs/external.js +67 -0
- package/dist/commonjs/index.d.ts +4 -0
- package/{lib → dist/commonjs}/index.js +19 -9
- package/dist/commonjs/iso.d.ts +22 -0
- package/dist/commonjs/iso.js +70 -0
- package/dist/commonjs/package.json +3 -0
- package/dist/commonjs/parse.d.ts +17 -0
- package/dist/commonjs/parse.js +44 -0
- package/dist/commonjs/schemas.d.ts +738 -0
- package/dist/commonjs/schemas.js +1171 -0
- package/dist/esm/checks.d.ts +1 -0
- package/dist/esm/checks.js +1 -0
- package/dist/esm/coerce.d.ts +17 -0
- package/dist/esm/coerce.js +17 -0
- package/dist/esm/compat.d.ts +47 -0
- package/dist/esm/compat.js +33 -0
- package/dist/esm/errors.d.ts +23 -0
- package/dist/esm/errors.js +20 -0
- package/dist/esm/external.d.ts +8 -0
- package/dist/esm/external.js +11 -0
- package/dist/esm/index.d.ts +4 -0
- package/dist/esm/index.js +4 -0
- package/dist/esm/iso.d.ts +22 -0
- package/dist/esm/iso.js +30 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/parse.d.ts +17 -0
- package/dist/esm/parse.js +8 -0
- package/dist/esm/schemas.d.ts +738 -0
- package/dist/esm/schemas.js +1057 -0
- package/package.json +59 -99
- package/src/checks.ts +30 -0
- package/src/coerce.ts +27 -0
- package/src/compat.ts +74 -0
- package/src/errors.ts +47 -0
- package/src/external.ts +29 -0
- package/src/index.ts +5 -0
- package/src/iso.ts +90 -0
- package/src/parse.ts +32 -0
- package/src/schemas.ts +2324 -0
- package/README.md +0 -3074
- package/index.d.ts +0 -2
- package/lib/ZodError.d.ts +0 -164
- package/lib/ZodError.js +0 -137
- package/lib/__tests__/Mocker.d.ts +0 -17
- package/lib/__tests__/Mocker.js +0 -57
- package/lib/benchmarks/datetime.d.ts +0 -5
- package/lib/benchmarks/datetime.js +0 -54
- package/lib/benchmarks/discriminatedUnion.d.ts +0 -5
- package/lib/benchmarks/discriminatedUnion.js +0 -79
- package/lib/benchmarks/index.d.ts +0 -1
- package/lib/benchmarks/index.js +0 -59
- package/lib/benchmarks/ipv4.d.ts +0 -5
- package/lib/benchmarks/ipv4.js +0 -54
- package/lib/benchmarks/object.d.ts +0 -5
- package/lib/benchmarks/object.js +0 -70
- package/lib/benchmarks/primitives.d.ts +0 -5
- package/lib/benchmarks/primitives.js +0 -170
- package/lib/benchmarks/realworld.d.ts +0 -5
- package/lib/benchmarks/realworld.js +0 -56
- package/lib/benchmarks/string.d.ts +0 -5
- package/lib/benchmarks/string.js +0 -55
- package/lib/benchmarks/union.d.ts +0 -5
- package/lib/benchmarks/union.js +0 -79
- package/lib/errors.d.ts +0 -5
- package/lib/errors.js +0 -17
- package/lib/external.d.ts +0 -6
- package/lib/external.js +0 -22
- package/lib/helpers/enumUtil.d.ts +0 -8
- package/lib/helpers/enumUtil.js +0 -2
- package/lib/helpers/errorUtil.d.ts +0 -9
- package/lib/helpers/errorUtil.js +0 -8
- package/lib/helpers/parseUtil.d.ts +0 -78
- package/lib/helpers/parseUtil.js +0 -125
- package/lib/helpers/partialUtil.d.ts +0 -8
- package/lib/helpers/partialUtil.js +0 -2
- package/lib/helpers/typeAliases.d.ts +0 -2
- package/lib/helpers/typeAliases.js +0 -2
- package/lib/helpers/util.d.ts +0 -82
- package/lib/helpers/util.js +0 -142
- package/lib/index.d.ts +0 -4
- package/lib/index.mjs +0 -4405
- package/lib/index.umd.js +0 -4520
- package/lib/locales/en.d.ts +0 -3
- package/lib/locales/en.js +0 -129
- package/lib/standard-schema.d.ts +0 -102
- package/lib/standard-schema.js +0 -2
- package/lib/types.d.ts +0 -1062
- package/lib/types.js +0 -3846
package/index.d.ts
DELETED
package/lib/ZodError.d.ts
DELETED
|
@@ -1,164 +0,0 @@
|
|
|
1
|
-
import type { TypeOf, ZodType } from ".";
|
|
2
|
-
import { Primitive } from "./helpers/typeAliases";
|
|
3
|
-
import { util, ZodParsedType } from "./helpers/util";
|
|
4
|
-
type allKeys<T> = T extends any ? keyof T : never;
|
|
5
|
-
export type inferFlattenedErrors<T extends ZodType<any, any, any>, U = string> = typeToFlattenedError<TypeOf<T>, U>;
|
|
6
|
-
export type typeToFlattenedError<T, U = string> = {
|
|
7
|
-
formErrors: U[];
|
|
8
|
-
fieldErrors: {
|
|
9
|
-
[P in allKeys<T>]?: U[];
|
|
10
|
-
};
|
|
11
|
-
};
|
|
12
|
-
export declare const ZodIssueCode: {
|
|
13
|
-
invalid_type: "invalid_type";
|
|
14
|
-
invalid_literal: "invalid_literal";
|
|
15
|
-
custom: "custom";
|
|
16
|
-
invalid_union: "invalid_union";
|
|
17
|
-
invalid_union_discriminator: "invalid_union_discriminator";
|
|
18
|
-
invalid_enum_value: "invalid_enum_value";
|
|
19
|
-
unrecognized_keys: "unrecognized_keys";
|
|
20
|
-
invalid_arguments: "invalid_arguments";
|
|
21
|
-
invalid_return_type: "invalid_return_type";
|
|
22
|
-
invalid_date: "invalid_date";
|
|
23
|
-
invalid_string: "invalid_string";
|
|
24
|
-
too_small: "too_small";
|
|
25
|
-
too_big: "too_big";
|
|
26
|
-
invalid_intersection_types: "invalid_intersection_types";
|
|
27
|
-
not_multiple_of: "not_multiple_of";
|
|
28
|
-
not_finite: "not_finite";
|
|
29
|
-
};
|
|
30
|
-
export type ZodIssueCode = keyof typeof ZodIssueCode;
|
|
31
|
-
export type ZodIssueBase = {
|
|
32
|
-
path: (string | number)[];
|
|
33
|
-
message?: string;
|
|
34
|
-
};
|
|
35
|
-
export interface ZodInvalidTypeIssue extends ZodIssueBase {
|
|
36
|
-
code: typeof ZodIssueCode.invalid_type;
|
|
37
|
-
expected: ZodParsedType;
|
|
38
|
-
received: ZodParsedType;
|
|
39
|
-
}
|
|
40
|
-
export interface ZodInvalidLiteralIssue extends ZodIssueBase {
|
|
41
|
-
code: typeof ZodIssueCode.invalid_literal;
|
|
42
|
-
expected: unknown;
|
|
43
|
-
received: unknown;
|
|
44
|
-
}
|
|
45
|
-
export interface ZodUnrecognizedKeysIssue extends ZodIssueBase {
|
|
46
|
-
code: typeof ZodIssueCode.unrecognized_keys;
|
|
47
|
-
keys: string[];
|
|
48
|
-
}
|
|
49
|
-
export interface ZodInvalidUnionIssue extends ZodIssueBase {
|
|
50
|
-
code: typeof ZodIssueCode.invalid_union;
|
|
51
|
-
unionErrors: ZodError[];
|
|
52
|
-
}
|
|
53
|
-
export interface ZodInvalidUnionDiscriminatorIssue extends ZodIssueBase {
|
|
54
|
-
code: typeof ZodIssueCode.invalid_union_discriminator;
|
|
55
|
-
options: Primitive[];
|
|
56
|
-
}
|
|
57
|
-
export interface ZodInvalidEnumValueIssue extends ZodIssueBase {
|
|
58
|
-
received: string | number;
|
|
59
|
-
code: typeof ZodIssueCode.invalid_enum_value;
|
|
60
|
-
options: (string | number)[];
|
|
61
|
-
}
|
|
62
|
-
export interface ZodInvalidArgumentsIssue extends ZodIssueBase {
|
|
63
|
-
code: typeof ZodIssueCode.invalid_arguments;
|
|
64
|
-
argumentsError: ZodError;
|
|
65
|
-
}
|
|
66
|
-
export interface ZodInvalidReturnTypeIssue extends ZodIssueBase {
|
|
67
|
-
code: typeof ZodIssueCode.invalid_return_type;
|
|
68
|
-
returnTypeError: ZodError;
|
|
69
|
-
}
|
|
70
|
-
export interface ZodInvalidDateIssue extends ZodIssueBase {
|
|
71
|
-
code: typeof ZodIssueCode.invalid_date;
|
|
72
|
-
}
|
|
73
|
-
export type StringValidation = "email" | "url" | "emoji" | "uuid" | "nanoid" | "regex" | "cuid" | "cuid2" | "ulid" | "datetime" | "date" | "time" | "duration" | "ip" | "cidr" | "base64" | "jwt" | "base64url" | {
|
|
74
|
-
includes: string;
|
|
75
|
-
position?: number;
|
|
76
|
-
} | {
|
|
77
|
-
startsWith: string;
|
|
78
|
-
} | {
|
|
79
|
-
endsWith: string;
|
|
80
|
-
};
|
|
81
|
-
export interface ZodInvalidStringIssue extends ZodIssueBase {
|
|
82
|
-
code: typeof ZodIssueCode.invalid_string;
|
|
83
|
-
validation: StringValidation;
|
|
84
|
-
}
|
|
85
|
-
export interface ZodTooSmallIssue extends ZodIssueBase {
|
|
86
|
-
code: typeof ZodIssueCode.too_small;
|
|
87
|
-
minimum: number | bigint;
|
|
88
|
-
inclusive: boolean;
|
|
89
|
-
exact?: boolean;
|
|
90
|
-
type: "array" | "string" | "number" | "set" | "date" | "bigint";
|
|
91
|
-
}
|
|
92
|
-
export interface ZodTooBigIssue extends ZodIssueBase {
|
|
93
|
-
code: typeof ZodIssueCode.too_big;
|
|
94
|
-
maximum: number | bigint;
|
|
95
|
-
inclusive: boolean;
|
|
96
|
-
exact?: boolean;
|
|
97
|
-
type: "array" | "string" | "number" | "set" | "date" | "bigint";
|
|
98
|
-
}
|
|
99
|
-
export interface ZodInvalidIntersectionTypesIssue extends ZodIssueBase {
|
|
100
|
-
code: typeof ZodIssueCode.invalid_intersection_types;
|
|
101
|
-
}
|
|
102
|
-
export interface ZodNotMultipleOfIssue extends ZodIssueBase {
|
|
103
|
-
code: typeof ZodIssueCode.not_multiple_of;
|
|
104
|
-
multipleOf: number | bigint;
|
|
105
|
-
}
|
|
106
|
-
export interface ZodNotFiniteIssue extends ZodIssueBase {
|
|
107
|
-
code: typeof ZodIssueCode.not_finite;
|
|
108
|
-
}
|
|
109
|
-
export interface ZodCustomIssue extends ZodIssueBase {
|
|
110
|
-
code: typeof ZodIssueCode.custom;
|
|
111
|
-
params?: {
|
|
112
|
-
[k: string]: any;
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
export type DenormalizedError = {
|
|
116
|
-
[k: string]: DenormalizedError | string[];
|
|
117
|
-
};
|
|
118
|
-
export type ZodIssueOptionalMessage = ZodInvalidTypeIssue | ZodInvalidLiteralIssue | ZodUnrecognizedKeysIssue | ZodInvalidUnionIssue | ZodInvalidUnionDiscriminatorIssue | ZodInvalidEnumValueIssue | ZodInvalidArgumentsIssue | ZodInvalidReturnTypeIssue | ZodInvalidDateIssue | ZodInvalidStringIssue | ZodTooSmallIssue | ZodTooBigIssue | ZodInvalidIntersectionTypesIssue | ZodNotMultipleOfIssue | ZodNotFiniteIssue | ZodCustomIssue;
|
|
119
|
-
export type ZodIssue = ZodIssueOptionalMessage & {
|
|
120
|
-
fatal?: boolean;
|
|
121
|
-
message: string;
|
|
122
|
-
};
|
|
123
|
-
export declare const quotelessJson: (obj: any) => string;
|
|
124
|
-
type recursiveZodFormattedError<T> = T extends [any, ...any[]] ? {
|
|
125
|
-
[K in keyof T]?: ZodFormattedError<T[K]>;
|
|
126
|
-
} : T extends any[] ? {
|
|
127
|
-
[k: number]: ZodFormattedError<T[number]>;
|
|
128
|
-
} : T extends object ? {
|
|
129
|
-
[K in keyof T]?: ZodFormattedError<T[K]>;
|
|
130
|
-
} : unknown;
|
|
131
|
-
export type ZodFormattedError<T, U = string> = {
|
|
132
|
-
_errors: U[];
|
|
133
|
-
} & recursiveZodFormattedError<NonNullable<T>>;
|
|
134
|
-
export type inferFormattedError<T extends ZodType<any, any, any>, U = string> = ZodFormattedError<TypeOf<T>, U>;
|
|
135
|
-
export declare class ZodError<T = any> extends Error {
|
|
136
|
-
issues: ZodIssue[];
|
|
137
|
-
get errors(): ZodIssue[];
|
|
138
|
-
constructor(issues: ZodIssue[]);
|
|
139
|
-
format(): ZodFormattedError<T>;
|
|
140
|
-
format<U>(mapper: (issue: ZodIssue) => U): ZodFormattedError<T, U>;
|
|
141
|
-
static create: (issues: ZodIssue[]) => ZodError<any>;
|
|
142
|
-
static assert(value: unknown): asserts value is ZodError;
|
|
143
|
-
toString(): string;
|
|
144
|
-
get message(): string;
|
|
145
|
-
get isEmpty(): boolean;
|
|
146
|
-
addIssue: (sub: ZodIssue) => void;
|
|
147
|
-
addIssues: (subs?: ZodIssue[]) => void;
|
|
148
|
-
flatten(): typeToFlattenedError<T>;
|
|
149
|
-
flatten<U>(mapper?: (issue: ZodIssue) => U): typeToFlattenedError<T, U>;
|
|
150
|
-
get formErrors(): typeToFlattenedError<T, string>;
|
|
151
|
-
}
|
|
152
|
-
type stripPath<T extends object> = T extends any ? util.OmitKeys<T, "path"> : never;
|
|
153
|
-
export type IssueData = stripPath<ZodIssueOptionalMessage> & {
|
|
154
|
-
path?: (string | number)[];
|
|
155
|
-
fatal?: boolean;
|
|
156
|
-
};
|
|
157
|
-
export type ErrorMapCtx = {
|
|
158
|
-
defaultError: string;
|
|
159
|
-
data: any;
|
|
160
|
-
};
|
|
161
|
-
export type ZodErrorMap = (issue: ZodIssueOptionalMessage, _ctx: ErrorMapCtx) => {
|
|
162
|
-
message: string;
|
|
163
|
-
};
|
|
164
|
-
export {};
|
package/lib/ZodError.js
DELETED
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ZodError = exports.quotelessJson = exports.ZodIssueCode = void 0;
|
|
4
|
-
const util_1 = require("./helpers/util");
|
|
5
|
-
exports.ZodIssueCode = util_1.util.arrayToEnum([
|
|
6
|
-
"invalid_type",
|
|
7
|
-
"invalid_literal",
|
|
8
|
-
"custom",
|
|
9
|
-
"invalid_union",
|
|
10
|
-
"invalid_union_discriminator",
|
|
11
|
-
"invalid_enum_value",
|
|
12
|
-
"unrecognized_keys",
|
|
13
|
-
"invalid_arguments",
|
|
14
|
-
"invalid_return_type",
|
|
15
|
-
"invalid_date",
|
|
16
|
-
"invalid_string",
|
|
17
|
-
"too_small",
|
|
18
|
-
"too_big",
|
|
19
|
-
"invalid_intersection_types",
|
|
20
|
-
"not_multiple_of",
|
|
21
|
-
"not_finite",
|
|
22
|
-
]);
|
|
23
|
-
const quotelessJson = (obj) => {
|
|
24
|
-
const json = JSON.stringify(obj, null, 2);
|
|
25
|
-
return json.replace(/"([^"]+)":/g, "$1:");
|
|
26
|
-
};
|
|
27
|
-
exports.quotelessJson = quotelessJson;
|
|
28
|
-
class ZodError extends Error {
|
|
29
|
-
get errors() {
|
|
30
|
-
return this.issues;
|
|
31
|
-
}
|
|
32
|
-
constructor(issues) {
|
|
33
|
-
super();
|
|
34
|
-
this.issues = [];
|
|
35
|
-
this.addIssue = (sub) => {
|
|
36
|
-
this.issues = [...this.issues, sub];
|
|
37
|
-
};
|
|
38
|
-
this.addIssues = (subs = []) => {
|
|
39
|
-
this.issues = [...this.issues, ...subs];
|
|
40
|
-
};
|
|
41
|
-
const actualProto = new.target.prototype;
|
|
42
|
-
if (Object.setPrototypeOf) {
|
|
43
|
-
// eslint-disable-next-line ban/ban
|
|
44
|
-
Object.setPrototypeOf(this, actualProto);
|
|
45
|
-
}
|
|
46
|
-
else {
|
|
47
|
-
this.__proto__ = actualProto;
|
|
48
|
-
}
|
|
49
|
-
this.name = "ZodError";
|
|
50
|
-
this.issues = issues;
|
|
51
|
-
}
|
|
52
|
-
format(_mapper) {
|
|
53
|
-
const mapper = _mapper ||
|
|
54
|
-
function (issue) {
|
|
55
|
-
return issue.message;
|
|
56
|
-
};
|
|
57
|
-
const fieldErrors = { _errors: [] };
|
|
58
|
-
const processError = (error) => {
|
|
59
|
-
for (const issue of error.issues) {
|
|
60
|
-
if (issue.code === "invalid_union") {
|
|
61
|
-
issue.unionErrors.map(processError);
|
|
62
|
-
}
|
|
63
|
-
else if (issue.code === "invalid_return_type") {
|
|
64
|
-
processError(issue.returnTypeError);
|
|
65
|
-
}
|
|
66
|
-
else if (issue.code === "invalid_arguments") {
|
|
67
|
-
processError(issue.argumentsError);
|
|
68
|
-
}
|
|
69
|
-
else if (issue.path.length === 0) {
|
|
70
|
-
fieldErrors._errors.push(mapper(issue));
|
|
71
|
-
}
|
|
72
|
-
else {
|
|
73
|
-
let curr = fieldErrors;
|
|
74
|
-
let i = 0;
|
|
75
|
-
while (i < issue.path.length) {
|
|
76
|
-
const el = issue.path[i];
|
|
77
|
-
const terminal = i === issue.path.length - 1;
|
|
78
|
-
if (!terminal) {
|
|
79
|
-
curr[el] = curr[el] || { _errors: [] };
|
|
80
|
-
// if (typeof el === "string") {
|
|
81
|
-
// curr[el] = curr[el] || { _errors: [] };
|
|
82
|
-
// } else if (typeof el === "number") {
|
|
83
|
-
// const errorArray: any = [];
|
|
84
|
-
// errorArray._errors = [];
|
|
85
|
-
// curr[el] = curr[el] || errorArray;
|
|
86
|
-
// }
|
|
87
|
-
}
|
|
88
|
-
else {
|
|
89
|
-
curr[el] = curr[el] || { _errors: [] };
|
|
90
|
-
curr[el]._errors.push(mapper(issue));
|
|
91
|
-
}
|
|
92
|
-
curr = curr[el];
|
|
93
|
-
i++;
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
};
|
|
98
|
-
processError(this);
|
|
99
|
-
return fieldErrors;
|
|
100
|
-
}
|
|
101
|
-
static assert(value) {
|
|
102
|
-
if (!(value instanceof ZodError)) {
|
|
103
|
-
throw new Error(`Not a ZodError: ${value}`);
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
toString() {
|
|
107
|
-
return this.message;
|
|
108
|
-
}
|
|
109
|
-
get message() {
|
|
110
|
-
return JSON.stringify(this.issues, util_1.util.jsonStringifyReplacer, 2);
|
|
111
|
-
}
|
|
112
|
-
get isEmpty() {
|
|
113
|
-
return this.issues.length === 0;
|
|
114
|
-
}
|
|
115
|
-
flatten(mapper = (issue) => issue.message) {
|
|
116
|
-
const fieldErrors = {};
|
|
117
|
-
const formErrors = [];
|
|
118
|
-
for (const sub of this.issues) {
|
|
119
|
-
if (sub.path.length > 0) {
|
|
120
|
-
fieldErrors[sub.path[0]] = fieldErrors[sub.path[0]] || [];
|
|
121
|
-
fieldErrors[sub.path[0]].push(mapper(sub));
|
|
122
|
-
}
|
|
123
|
-
else {
|
|
124
|
-
formErrors.push(mapper(sub));
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
return { formErrors, fieldErrors };
|
|
128
|
-
}
|
|
129
|
-
get formErrors() {
|
|
130
|
-
return this.flatten();
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
exports.ZodError = ZodError;
|
|
134
|
-
ZodError.create = (issues) => {
|
|
135
|
-
const error = new ZodError(issues);
|
|
136
|
-
return error;
|
|
137
|
-
};
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
export declare class Mocker {
|
|
2
|
-
pick: (...args: any[]) => any;
|
|
3
|
-
get string(): string;
|
|
4
|
-
get number(): number;
|
|
5
|
-
get bigint(): bigint;
|
|
6
|
-
get boolean(): boolean;
|
|
7
|
-
get date(): Date;
|
|
8
|
-
get symbol(): symbol;
|
|
9
|
-
get null(): null;
|
|
10
|
-
get undefined(): undefined;
|
|
11
|
-
get stringOptional(): any;
|
|
12
|
-
get stringNullable(): any;
|
|
13
|
-
get numberOptional(): any;
|
|
14
|
-
get numberNullable(): any;
|
|
15
|
-
get booleanOptional(): any;
|
|
16
|
-
get booleanNullable(): any;
|
|
17
|
-
}
|
package/lib/__tests__/Mocker.js
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Mocker = void 0;
|
|
4
|
-
function getRandomInt(max) {
|
|
5
|
-
return Math.floor(Math.random() * Math.floor(max));
|
|
6
|
-
}
|
|
7
|
-
const testSymbol = Symbol("test");
|
|
8
|
-
class Mocker {
|
|
9
|
-
constructor() {
|
|
10
|
-
this.pick = (...args) => {
|
|
11
|
-
return args[getRandomInt(args.length)];
|
|
12
|
-
};
|
|
13
|
-
}
|
|
14
|
-
get string() {
|
|
15
|
-
return Math.random().toString(36).substring(7);
|
|
16
|
-
}
|
|
17
|
-
get number() {
|
|
18
|
-
return Math.random() * 100;
|
|
19
|
-
}
|
|
20
|
-
get bigint() {
|
|
21
|
-
return BigInt(Math.floor(Math.random() * 10000));
|
|
22
|
-
}
|
|
23
|
-
get boolean() {
|
|
24
|
-
return Math.random() < 0.5;
|
|
25
|
-
}
|
|
26
|
-
get date() {
|
|
27
|
-
return new Date(Math.floor(Date.now() * Math.random()));
|
|
28
|
-
}
|
|
29
|
-
get symbol() {
|
|
30
|
-
return testSymbol;
|
|
31
|
-
}
|
|
32
|
-
get null() {
|
|
33
|
-
return null;
|
|
34
|
-
}
|
|
35
|
-
get undefined() {
|
|
36
|
-
return undefined;
|
|
37
|
-
}
|
|
38
|
-
get stringOptional() {
|
|
39
|
-
return this.pick(this.string, this.undefined);
|
|
40
|
-
}
|
|
41
|
-
get stringNullable() {
|
|
42
|
-
return this.pick(this.string, this.null);
|
|
43
|
-
}
|
|
44
|
-
get numberOptional() {
|
|
45
|
-
return this.pick(this.number, this.undefined);
|
|
46
|
-
}
|
|
47
|
-
get numberNullable() {
|
|
48
|
-
return this.pick(this.number, this.null);
|
|
49
|
-
}
|
|
50
|
-
get booleanOptional() {
|
|
51
|
-
return this.pick(this.boolean, this.undefined);
|
|
52
|
-
}
|
|
53
|
-
get booleanNullable() {
|
|
54
|
-
return this.pick(this.boolean, this.null);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
exports.Mocker = Mocker;
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const benchmark_1 = __importDefault(require("benchmark"));
|
|
7
|
-
const datetimeValidationSuite = new benchmark_1.default.Suite("datetime");
|
|
8
|
-
const DATA = "2021-01-01";
|
|
9
|
-
const MONTHS_31 = new Set([1, 3, 5, 7, 8, 10, 12]);
|
|
10
|
-
const MONTHS_30 = new Set([4, 6, 9, 11]);
|
|
11
|
-
const simpleDatetimeRegex = /^(\d{4})-(\d{2})-(\d{2})$/;
|
|
12
|
-
const datetimeRegexNoLeapYearValidation = /^\d{4}-((0[13578]|10|12)-31|(0[13-9]|1[0-2])-30|(0[1-9]|1[0-2])-(0[1-9]|1\d|2\d))$/;
|
|
13
|
-
const datetimeRegexWithLeapYearValidation = /^((\d\d[2468][048]|\d\d[13579][26]|\d\d0[48]|[02468][048]00|[13579][26]00)-02-29|\d{4}-((0[13578]|1[02])-(0[1-9]|[12]\d|3[01])|(0[469]|11)-(0[1-9]|[12]\d|30)|(02)-(0[1-9]|1\d|2[0-8])))$/;
|
|
14
|
-
datetimeValidationSuite
|
|
15
|
-
.add("new Date()", () => {
|
|
16
|
-
return !isNaN(new Date(DATA).getTime());
|
|
17
|
-
})
|
|
18
|
-
.add("regex (no validation)", () => {
|
|
19
|
-
return simpleDatetimeRegex.test(DATA);
|
|
20
|
-
})
|
|
21
|
-
.add("regex (no leap year)", () => {
|
|
22
|
-
return datetimeRegexNoLeapYearValidation.test(DATA);
|
|
23
|
-
})
|
|
24
|
-
.add("regex (w/ leap year)", () => {
|
|
25
|
-
return datetimeRegexWithLeapYearValidation.test(DATA);
|
|
26
|
-
})
|
|
27
|
-
.add("capture groups + code", () => {
|
|
28
|
-
const match = DATA.match(simpleDatetimeRegex);
|
|
29
|
-
if (!match)
|
|
30
|
-
return false;
|
|
31
|
-
// Extract year, month, and day from the capture groups
|
|
32
|
-
const year = Number.parseInt(match[1], 10);
|
|
33
|
-
const month = Number.parseInt(match[2], 10); // month is 0-indexed in JavaScript Date, so subtract 1
|
|
34
|
-
const day = Number.parseInt(match[3], 10);
|
|
35
|
-
if (month === 2) {
|
|
36
|
-
if ((year % 4 === 0 && year % 100 !== 0) || year % 400 === 0) {
|
|
37
|
-
return day <= 29;
|
|
38
|
-
}
|
|
39
|
-
return day <= 28;
|
|
40
|
-
}
|
|
41
|
-
if (MONTHS_30.has(month)) {
|
|
42
|
-
return day <= 30;
|
|
43
|
-
}
|
|
44
|
-
if (MONTHS_31.has(month)) {
|
|
45
|
-
return day <= 31;
|
|
46
|
-
}
|
|
47
|
-
return false;
|
|
48
|
-
})
|
|
49
|
-
.on("cycle", (e) => {
|
|
50
|
-
console.log(`${datetimeValidationSuite.name}: ${e.target}`);
|
|
51
|
-
});
|
|
52
|
-
exports.default = {
|
|
53
|
-
suites: [datetimeValidationSuite],
|
|
54
|
-
};
|
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const benchmark_1 = __importDefault(require("benchmark"));
|
|
7
|
-
const index_1 = require("../index");
|
|
8
|
-
const doubleSuite = new benchmark_1.default.Suite("z.discriminatedUnion: double");
|
|
9
|
-
const manySuite = new benchmark_1.default.Suite("z.discriminatedUnion: many");
|
|
10
|
-
const aSchema = index_1.z.object({
|
|
11
|
-
type: index_1.z.literal("a"),
|
|
12
|
-
});
|
|
13
|
-
const objA = {
|
|
14
|
-
type: "a",
|
|
15
|
-
};
|
|
16
|
-
const bSchema = index_1.z.object({
|
|
17
|
-
type: index_1.z.literal("b"),
|
|
18
|
-
});
|
|
19
|
-
const objB = {
|
|
20
|
-
type: "b",
|
|
21
|
-
};
|
|
22
|
-
const cSchema = index_1.z.object({
|
|
23
|
-
type: index_1.z.literal("c"),
|
|
24
|
-
});
|
|
25
|
-
const objC = {
|
|
26
|
-
type: "c",
|
|
27
|
-
};
|
|
28
|
-
const dSchema = index_1.z.object({
|
|
29
|
-
type: index_1.z.literal("d"),
|
|
30
|
-
});
|
|
31
|
-
const double = index_1.z.discriminatedUnion("type", [aSchema, bSchema]);
|
|
32
|
-
const many = index_1.z.discriminatedUnion("type", [aSchema, bSchema, cSchema, dSchema]);
|
|
33
|
-
doubleSuite
|
|
34
|
-
.add("valid: a", () => {
|
|
35
|
-
double.parse(objA);
|
|
36
|
-
})
|
|
37
|
-
.add("valid: b", () => {
|
|
38
|
-
double.parse(objB);
|
|
39
|
-
})
|
|
40
|
-
.add("invalid: null", () => {
|
|
41
|
-
try {
|
|
42
|
-
double.parse(null);
|
|
43
|
-
}
|
|
44
|
-
catch (err) { }
|
|
45
|
-
})
|
|
46
|
-
.add("invalid: wrong shape", () => {
|
|
47
|
-
try {
|
|
48
|
-
double.parse(objC);
|
|
49
|
-
}
|
|
50
|
-
catch (err) { }
|
|
51
|
-
})
|
|
52
|
-
.on("cycle", (e) => {
|
|
53
|
-
console.log(`${doubleSuite.name}: ${e.target}`);
|
|
54
|
-
});
|
|
55
|
-
manySuite
|
|
56
|
-
.add("valid: a", () => {
|
|
57
|
-
many.parse(objA);
|
|
58
|
-
})
|
|
59
|
-
.add("valid: c", () => {
|
|
60
|
-
many.parse(objC);
|
|
61
|
-
})
|
|
62
|
-
.add("invalid: null", () => {
|
|
63
|
-
try {
|
|
64
|
-
many.parse(null);
|
|
65
|
-
}
|
|
66
|
-
catch (err) { }
|
|
67
|
-
})
|
|
68
|
-
.add("invalid: wrong shape", () => {
|
|
69
|
-
try {
|
|
70
|
-
many.parse({ type: "unknown" });
|
|
71
|
-
}
|
|
72
|
-
catch (err) { }
|
|
73
|
-
})
|
|
74
|
-
.on("cycle", (e) => {
|
|
75
|
-
console.log(`${manySuite.name}: ${e.target}`);
|
|
76
|
-
});
|
|
77
|
-
exports.default = {
|
|
78
|
-
suites: [doubleSuite, manySuite],
|
|
79
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/lib/benchmarks/index.js
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const datetime_1 = __importDefault(require("./datetime"));
|
|
7
|
-
const discriminatedUnion_1 = __importDefault(require("./discriminatedUnion"));
|
|
8
|
-
const ipv4_1 = __importDefault(require("./ipv4"));
|
|
9
|
-
const object_1 = __importDefault(require("./object"));
|
|
10
|
-
const primitives_1 = __importDefault(require("./primitives"));
|
|
11
|
-
const realworld_1 = __importDefault(require("./realworld"));
|
|
12
|
-
const string_1 = __importDefault(require("./string"));
|
|
13
|
-
const union_1 = __importDefault(require("./union"));
|
|
14
|
-
const argv = process.argv.slice(2);
|
|
15
|
-
let suites = [];
|
|
16
|
-
if (!argv.length) {
|
|
17
|
-
suites = [
|
|
18
|
-
...realworld_1.default.suites,
|
|
19
|
-
...primitives_1.default.suites,
|
|
20
|
-
...string_1.default.suites,
|
|
21
|
-
...object_1.default.suites,
|
|
22
|
-
...union_1.default.suites,
|
|
23
|
-
...discriminatedUnion_1.default.suites,
|
|
24
|
-
];
|
|
25
|
-
}
|
|
26
|
-
else {
|
|
27
|
-
if (argv.includes("--realworld")) {
|
|
28
|
-
suites.push(...realworld_1.default.suites);
|
|
29
|
-
}
|
|
30
|
-
if (argv.includes("--primitives")) {
|
|
31
|
-
suites.push(...primitives_1.default.suites);
|
|
32
|
-
}
|
|
33
|
-
if (argv.includes("--string")) {
|
|
34
|
-
suites.push(...string_1.default.suites);
|
|
35
|
-
}
|
|
36
|
-
if (argv.includes("--object")) {
|
|
37
|
-
suites.push(...object_1.default.suites);
|
|
38
|
-
}
|
|
39
|
-
if (argv.includes("--union")) {
|
|
40
|
-
suites.push(...union_1.default.suites);
|
|
41
|
-
}
|
|
42
|
-
if (argv.includes("--discriminatedUnion")) {
|
|
43
|
-
suites.push(...datetime_1.default.suites);
|
|
44
|
-
}
|
|
45
|
-
if (argv.includes("--datetime")) {
|
|
46
|
-
suites.push(...datetime_1.default.suites);
|
|
47
|
-
}
|
|
48
|
-
if (argv.includes("--ipv4")) {
|
|
49
|
-
suites.push(...ipv4_1.default.suites);
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
for (const suite of suites) {
|
|
53
|
-
suite.run({});
|
|
54
|
-
}
|
|
55
|
-
// exit on Ctrl-C
|
|
56
|
-
process.on("SIGINT", function () {
|
|
57
|
-
console.log("Exiting...");
|
|
58
|
-
process.exit();
|
|
59
|
-
});
|
package/lib/benchmarks/ipv4.d.ts
DELETED
package/lib/benchmarks/ipv4.js
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const benchmark_1 = __importDefault(require("benchmark"));
|
|
7
|
-
const suite = new benchmark_1.default.Suite("ipv4");
|
|
8
|
-
const DATA = "127.0.0.1";
|
|
9
|
-
const ipv4RegexA = /^(((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2}))\.){3}((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2}))$/;
|
|
10
|
-
const ipv4RegexB = /^(?:(?:(?=(25[0-5]))\1|(?=(2[0-4][0-9]))\2|(?=(1[0-9]{2}))\3|(?=([0-9]{1,2}))\4)\.){3}(?:(?=(25[0-5]))\5|(?=(2[0-4][0-9]))\6|(?=(1[0-9]{2}))\7|(?=([0-9]{1,2}))\8)$/;
|
|
11
|
-
const ipv4RegexC = /^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$/;
|
|
12
|
-
const ipv4RegexD = /^(\b25[0-5]|\b2[0-4][0-9]|\b[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/;
|
|
13
|
-
const ipv4RegexE = /^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.){3}(25[0-5]|(2[0-4]|1\d|[1-9]|)\d)$/;
|
|
14
|
-
const ipv4RegexF = /^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$/;
|
|
15
|
-
const ipv4RegexG = /^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)(\.(?!$)|$)){4}$/;
|
|
16
|
-
const ipv4RegexH = /^((25[0-5]|(2[0-4]|1[0-9]|[1-9]|)[0-9])(\.(?!$)|$)){4}$/;
|
|
17
|
-
const ipv4RegexI = /^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/;
|
|
18
|
-
suite
|
|
19
|
-
.add("A", () => {
|
|
20
|
-
return ipv4RegexA.test(DATA);
|
|
21
|
-
})
|
|
22
|
-
.add("B", () => {
|
|
23
|
-
return ipv4RegexB.test(DATA);
|
|
24
|
-
})
|
|
25
|
-
.add("C", () => {
|
|
26
|
-
return ipv4RegexC.test(DATA);
|
|
27
|
-
})
|
|
28
|
-
.add("D", () => {
|
|
29
|
-
return ipv4RegexD.test(DATA);
|
|
30
|
-
})
|
|
31
|
-
.add("E", () => {
|
|
32
|
-
return ipv4RegexE.test(DATA);
|
|
33
|
-
})
|
|
34
|
-
.add("F", () => {
|
|
35
|
-
return ipv4RegexF.test(DATA);
|
|
36
|
-
})
|
|
37
|
-
.add("G", () => {
|
|
38
|
-
return ipv4RegexG.test(DATA);
|
|
39
|
-
})
|
|
40
|
-
.add("H", () => {
|
|
41
|
-
return ipv4RegexH.test(DATA);
|
|
42
|
-
})
|
|
43
|
-
.add("I", () => {
|
|
44
|
-
return ipv4RegexI.test(DATA);
|
|
45
|
-
})
|
|
46
|
-
.on("cycle", (e) => {
|
|
47
|
-
console.log(`${suite.name}: ${e.target}`);
|
|
48
|
-
});
|
|
49
|
-
exports.default = {
|
|
50
|
-
suites: [suite],
|
|
51
|
-
};
|
|
52
|
-
if (require.main === module) {
|
|
53
|
-
suite.run();
|
|
54
|
-
}
|