@quentinadam/zod 0.1.3 → 0.1.5
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/zod.d.ts +36 -18
- package/dist/zod.js +278 -177
- package/package.json +1 -1
package/dist/zod.d.ts
CHANGED
|
@@ -1,35 +1,47 @@
|
|
|
1
|
+
type Path = (string | number)[];
|
|
2
|
+
type Context = {
|
|
3
|
+
path: Path;
|
|
4
|
+
errors: {
|
|
5
|
+
path: Path;
|
|
6
|
+
message: string;
|
|
7
|
+
}[];
|
|
8
|
+
};
|
|
9
|
+
type Result<T> = {
|
|
10
|
+
success: true;
|
|
11
|
+
data: T;
|
|
12
|
+
} | {
|
|
13
|
+
success: false;
|
|
14
|
+
};
|
|
15
|
+
export declare function getType(value: unknown): string;
|
|
1
16
|
export declare class Schema<T> {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
constructor({ inputType, parseFn }: {
|
|
5
|
-
inputType: string;
|
|
6
|
-
parseFn: (value: unknown) => T;
|
|
7
|
-
});
|
|
17
|
+
protected readonly safeParseFn: (value: unknown, context?: Context) => Result<T>;
|
|
18
|
+
constructor(safeParseFn: (value: unknown, context?: Context) => Result<T>);
|
|
8
19
|
parse(value: unknown): T;
|
|
9
20
|
safeParse(value: unknown): {
|
|
10
21
|
success: true;
|
|
11
22
|
data: T;
|
|
12
23
|
} | {
|
|
13
24
|
success: false;
|
|
14
|
-
|
|
25
|
+
message: string;
|
|
26
|
+
errors: {
|
|
27
|
+
path: Path;
|
|
28
|
+
message: string;
|
|
29
|
+
}[];
|
|
15
30
|
};
|
|
31
|
+
internalSafeParse(value: unknown, context?: Context): Result<T>;
|
|
16
32
|
transform<U>(transform: (value: T) => U): Schema<U>;
|
|
17
33
|
optional(): Schema<T | undefined>;
|
|
18
34
|
nullable(): Schema<T | null>;
|
|
35
|
+
nullish(): Schema<T | null | undefined>;
|
|
19
36
|
}
|
|
20
37
|
export declare class ObjectSchema<T extends Record<string, unknown>> extends Schema<T> {
|
|
21
38
|
#private;
|
|
39
|
+
get shape(): {
|
|
40
|
+
[K in keyof T]: Schema<T[K]>;
|
|
41
|
+
};
|
|
22
42
|
constructor(schema: {
|
|
23
43
|
[K in keyof T]: Schema<T[K]>;
|
|
24
44
|
}, strict?: boolean);
|
|
25
|
-
extend<U extends Record<string, unknown>>(newSchema: {
|
|
26
|
-
[K in keyof U]: Schema<U[K]>;
|
|
27
|
-
}): ObjectSchema<T & U>;
|
|
28
|
-
strict(): ObjectSchema<T>;
|
|
29
|
-
strip(): ObjectSchema<T>;
|
|
30
|
-
partial(): ObjectSchema<{
|
|
31
|
-
[K in keyof T]: T[K] | undefined;
|
|
32
|
-
}>;
|
|
33
45
|
}
|
|
34
46
|
declare function createArraySchema<T>(schema: Schema<T>): Schema<T[]>;
|
|
35
47
|
declare function createBigIntSchema(): Schema<bigint>;
|
|
@@ -39,13 +51,19 @@ declare function createInstanceofSchema<T>(schema: {
|
|
|
39
51
|
new (...args: any[]): T;
|
|
40
52
|
}): Schema<T>;
|
|
41
53
|
declare function createLazySchema<T>(fn: () => Schema<T>): Schema<T>;
|
|
42
|
-
declare function createLiteralSchema<T extends string | number | boolean | null>(
|
|
54
|
+
declare function createLiteralSchema<T extends string | number | boolean | null | undefined>(literal: T | T[]): Schema<T>;
|
|
43
55
|
declare function createObjectSchema<T extends Record<string, unknown>>(schema: {
|
|
44
56
|
[K in keyof T]: Schema<T[K]>;
|
|
45
|
-
}):
|
|
57
|
+
}): Schema<T>;
|
|
58
|
+
declare function createOptionalSchema<T>(schema: Schema<T>): Schema<T | undefined>;
|
|
59
|
+
declare function createNullableSchema<T>(schema: Schema<T>): Schema<T | null>;
|
|
46
60
|
declare function createNullSchema(): Schema<null>;
|
|
61
|
+
declare function createNullishSchema<T>(schema: Schema<T>): Schema<T | null | undefined>;
|
|
47
62
|
declare function createNumberSchema(): Schema<number>;
|
|
48
63
|
declare function createRecordSchema<T>(schema: Schema<T>): Schema<Record<string, T>>;
|
|
64
|
+
declare function createStrictObjectSchema<T extends Record<string, unknown>>(schema: {
|
|
65
|
+
[K in keyof T]: Schema<T[K]>;
|
|
66
|
+
}): Schema<T>;
|
|
49
67
|
declare function createStringSchema(): Schema<string>;
|
|
50
68
|
declare function createTupleSchema<T extends unknown[]>(schema: {
|
|
51
69
|
[K in keyof T]: Schema<T[K]>;
|
|
@@ -55,4 +73,4 @@ declare function createUnionSchema<T extends unknown[]>(schemas: {
|
|
|
55
73
|
[K in keyof T]: Schema<T[K]>;
|
|
56
74
|
}): Schema<T[number]>;
|
|
57
75
|
declare function createUnknownSchema(): Schema<unknown>;
|
|
58
|
-
export { createArraySchema as array, createBigIntSchema as bigint, createBooleanSchema as boolean, createDateSchema as date, createInstanceofSchema as instanceof, createLazySchema as lazy, createLiteralSchema as literal, createNullSchema as null, createNumberSchema as number, createObjectSchema as object, createRecordSchema as record, createStringSchema as string, createTupleSchema as tuple, createUndefinedSchema as undefined, createUnionSchema as union, createUnknownSchema as unknown, };
|
|
76
|
+
export { createArraySchema as array, createBigIntSchema as bigint, createBooleanSchema as boolean, createDateSchema as date, createInstanceofSchema as instanceof, createLazySchema as lazy, createLiteralSchema as literal, createNullableSchema as nullable, createNullishSchema as nullish, createNullSchema as null, createNumberSchema as number, createObjectSchema as object, createOptionalSchema as optional, createRecordSchema as record, createStrictObjectSchema as strictObject, createStringSchema as string, createTupleSchema as tuple, createUndefinedSchema as undefined, createUnionSchema as union, createUnknownSchema as unknown, };
|
package/dist/zod.js
CHANGED
|
@@ -1,259 +1,360 @@
|
|
|
1
|
+
export function getType(value) {
|
|
2
|
+
if (value === undefined)
|
|
3
|
+
return 'undefined';
|
|
4
|
+
if (value === null)
|
|
5
|
+
return 'null';
|
|
6
|
+
if (Array.isArray(value))
|
|
7
|
+
return 'array';
|
|
8
|
+
return typeof value;
|
|
9
|
+
}
|
|
1
10
|
export class Schema {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
this.#parseFn = parseFn;
|
|
6
|
-
this.inputType = inputType;
|
|
11
|
+
safeParseFn;
|
|
12
|
+
constructor(safeParseFn) {
|
|
13
|
+
this.safeParseFn = safeParseFn;
|
|
7
14
|
}
|
|
8
15
|
parse(value) {
|
|
9
|
-
|
|
16
|
+
const result = this.safeParse(value);
|
|
17
|
+
if (result.success) {
|
|
18
|
+
return result.data;
|
|
19
|
+
}
|
|
20
|
+
throw new Error(result.message);
|
|
10
21
|
}
|
|
11
22
|
safeParse(value) {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
return { success: false, error };
|
|
23
|
+
const context = { path: [], errors: [] };
|
|
24
|
+
const result = this.safeParseFn(value, context);
|
|
25
|
+
if (result.success) {
|
|
26
|
+
return result;
|
|
17
27
|
}
|
|
28
|
+
return {
|
|
29
|
+
success: false,
|
|
30
|
+
message: `Validation failed: ${context.errors.map((e) => `${e.message} (at path /${e.path.join('/')})`).join(', ')}`,
|
|
31
|
+
errors: context.errors,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
internalSafeParse(value, context) {
|
|
35
|
+
return this.safeParseFn(value, context);
|
|
18
36
|
}
|
|
19
37
|
transform(transform) {
|
|
20
|
-
return new Schema(
|
|
38
|
+
return new Schema((value, context) => {
|
|
39
|
+
try {
|
|
40
|
+
const result = this.internalSafeParse(value, context);
|
|
41
|
+
if (result.success) {
|
|
42
|
+
return { success: true, data: transform(result.data) };
|
|
43
|
+
}
|
|
44
|
+
return result;
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
if (context !== undefined) {
|
|
48
|
+
context.errors.push({ path: context.path, message: error instanceof Error ? error.message : String(error) });
|
|
49
|
+
}
|
|
50
|
+
return { success: false };
|
|
51
|
+
}
|
|
52
|
+
});
|
|
21
53
|
}
|
|
22
54
|
optional() {
|
|
23
|
-
return
|
|
24
|
-
inputType: `${this.inputType} | undefined`,
|
|
25
|
-
parseFn: (value) => value === undefined ? undefined : this.parse(value),
|
|
26
|
-
});
|
|
55
|
+
return createOptionalSchema(this);
|
|
27
56
|
}
|
|
28
57
|
nullable() {
|
|
29
|
-
return
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
58
|
+
return createNullableSchema(this);
|
|
59
|
+
}
|
|
60
|
+
nullish() {
|
|
61
|
+
return createNullishSchema(this);
|
|
33
62
|
}
|
|
34
63
|
}
|
|
35
64
|
export class ObjectSchema extends Schema {
|
|
36
65
|
#schema;
|
|
37
|
-
|
|
66
|
+
get shape() {
|
|
67
|
+
return this.#schema;
|
|
68
|
+
}
|
|
38
69
|
constructor(schema, strict = false) {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
if (typeof value !== 'object' || value === null) {
|
|
44
|
-
throw new Error(`Expected ${inputType}, got ${JSON.stringify(value)}`);
|
|
70
|
+
super((value, context) => {
|
|
71
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
72
|
+
if (context !== undefined) {
|
|
73
|
+
context.errors.push({ path: context.path, message: `Expected object, got ${getType(value)}` });
|
|
45
74
|
}
|
|
46
|
-
return
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
75
|
+
return { success: false };
|
|
76
|
+
}
|
|
77
|
+
const object = value;
|
|
78
|
+
const result = (() => {
|
|
79
|
+
const parsedObject = {};
|
|
80
|
+
for (const [key, valueSchema] of Object.entries(schema)) {
|
|
81
|
+
const result = valueSchema.internalSafeParse(object[key]);
|
|
82
|
+
if (!result.success) {
|
|
83
|
+
return result;
|
|
84
|
+
}
|
|
85
|
+
parsedObject[key] = result.data;
|
|
86
|
+
}
|
|
87
|
+
if (strict) {
|
|
88
|
+
for (const key of Object.keys(object)) {
|
|
89
|
+
if (!(key in schema)) {
|
|
90
|
+
return { success: false };
|
|
52
91
|
}
|
|
53
92
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
93
|
+
}
|
|
94
|
+
return { success: true, data: parsedObject };
|
|
95
|
+
})();
|
|
96
|
+
if (result.success) {
|
|
97
|
+
return result;
|
|
98
|
+
}
|
|
99
|
+
if (context !== undefined) {
|
|
100
|
+
for (const [key, valueSchema] of Object.entries(schema)) {
|
|
101
|
+
valueSchema.internalSafeParse(object[key], { path: [...context.path, key], errors: context.errors });
|
|
102
|
+
}
|
|
103
|
+
if (strict) {
|
|
104
|
+
const unrecognizedKeys = new Array();
|
|
105
|
+
for (const key of Object.keys(object)) {
|
|
106
|
+
if (!(key in schema)) {
|
|
107
|
+
unrecognizedKeys.push(key);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
if (unrecognizedKeys.length > 0) {
|
|
111
|
+
context.errors.push({ path: context.path, message: `Unrecognized keys: ${unrecognizedKeys.join(', ')}` });
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return { success: false };
|
|
61
116
|
});
|
|
62
117
|
this.#schema = schema;
|
|
63
|
-
this.#strict = strict;
|
|
64
|
-
}
|
|
65
|
-
extend(newSchema) {
|
|
66
|
-
return new ObjectSchema({ ...this.#schema, ...newSchema }, this.#strict);
|
|
67
|
-
}
|
|
68
|
-
strict() {
|
|
69
|
-
return new ObjectSchema(this.#schema, true);
|
|
70
|
-
}
|
|
71
|
-
strip() {
|
|
72
|
-
return new ObjectSchema(this.#schema, false);
|
|
73
|
-
}
|
|
74
|
-
partial() {
|
|
75
|
-
const schema = Object.fromEntries(Object.entries(this.#schema).map(([key, schema]) => [key, schema.optional()]));
|
|
76
|
-
return new ObjectSchema(schema, this.#strict);
|
|
77
118
|
}
|
|
78
119
|
}
|
|
79
120
|
function createArraySchema(schema) {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
if (!Array.isArray(value)) {
|
|
85
|
-
throw new Error(`Expected ${inputType}, got ${JSON.stringify(value)}`);
|
|
121
|
+
return new Schema((value, context) => {
|
|
122
|
+
if (!Array.isArray(value)) {
|
|
123
|
+
if (context !== undefined) {
|
|
124
|
+
context.errors.push({ path: context.path, message: `Expected array, got ${getType(value)}` });
|
|
86
125
|
}
|
|
87
|
-
return
|
|
88
|
-
}
|
|
126
|
+
return { success: false };
|
|
127
|
+
}
|
|
128
|
+
const result = (() => {
|
|
129
|
+
const parsedItems = new Array();
|
|
130
|
+
for (const item of value) {
|
|
131
|
+
const result = schema.internalSafeParse(item);
|
|
132
|
+
if (!result.success) {
|
|
133
|
+
return result;
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
parsedItems.push(result.data);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return { success: true, data: parsedItems };
|
|
140
|
+
})();
|
|
141
|
+
if (result.success) {
|
|
142
|
+
return result;
|
|
143
|
+
}
|
|
144
|
+
if (context !== undefined) {
|
|
145
|
+
for (let index = 0; index < value.length; index++) {
|
|
146
|
+
const item = value[index];
|
|
147
|
+
schema.internalSafeParse(item, { path: [...context.path, index], errors: context.errors });
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return { success: false };
|
|
89
151
|
});
|
|
90
152
|
}
|
|
91
153
|
function createBigIntSchema() {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
if (typeof value !== 'bigint') {
|
|
97
|
-
throw new Error(`Expected ${inputType}, got ${JSON.stringify(value)}`);
|
|
154
|
+
return new Schema((value, context) => {
|
|
155
|
+
if (typeof value !== 'bigint') {
|
|
156
|
+
if (context !== undefined) {
|
|
157
|
+
context.errors.push({ path: context.path, message: `Expected bigint, got ${getType(value)}` });
|
|
98
158
|
}
|
|
99
|
-
return
|
|
100
|
-
}
|
|
159
|
+
return { success: false };
|
|
160
|
+
}
|
|
161
|
+
return { success: true, data: value };
|
|
101
162
|
});
|
|
102
163
|
}
|
|
103
164
|
function createBooleanSchema() {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
if (typeof value !== 'boolean') {
|
|
109
|
-
throw new Error(`Expected ${inputType}, got ${JSON.stringify(value)}`);
|
|
165
|
+
return new Schema((value, context) => {
|
|
166
|
+
if (typeof value !== 'boolean') {
|
|
167
|
+
if (context !== undefined) {
|
|
168
|
+
context.errors.push({ path: context.path, message: `Expected boolean, got ${getType(value)}` });
|
|
110
169
|
}
|
|
111
|
-
return
|
|
112
|
-
}
|
|
170
|
+
return { success: false };
|
|
171
|
+
}
|
|
172
|
+
return { success: true, data: value };
|
|
113
173
|
});
|
|
114
174
|
}
|
|
115
175
|
function createDateSchema() {
|
|
116
|
-
|
|
117
|
-
return new Schema({
|
|
118
|
-
inputType,
|
|
119
|
-
parseFn: (value) => {
|
|
120
|
-
if (!(value instanceof Date)) {
|
|
121
|
-
throw new Error(`Expected ${inputType}, got ${JSON.stringify(value)}`);
|
|
122
|
-
}
|
|
123
|
-
return value;
|
|
124
|
-
},
|
|
125
|
-
});
|
|
176
|
+
return createInstanceofSchema(Date);
|
|
126
177
|
}
|
|
127
178
|
// deno-lint-ignore no-explicit-any
|
|
128
179
|
function createInstanceofSchema(schema) {
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
180
|
+
return new Schema((value, context) => {
|
|
181
|
+
if (!(value instanceof schema)) {
|
|
182
|
+
if (context !== undefined) {
|
|
183
|
+
context.errors.push({
|
|
184
|
+
path: context.path,
|
|
185
|
+
message: `Expected instance of ${schema.name}, got ${getType(value)}`,
|
|
186
|
+
});
|
|
135
187
|
}
|
|
136
|
-
return
|
|
137
|
-
}
|
|
188
|
+
return { success: false };
|
|
189
|
+
}
|
|
190
|
+
return { success: true, data: value };
|
|
138
191
|
});
|
|
139
192
|
}
|
|
140
193
|
function createLazySchema(fn) {
|
|
141
|
-
return new Schema(
|
|
142
|
-
inputType: '(lazy)',
|
|
143
|
-
parseFn: (value) => fn().parse(value),
|
|
144
|
-
});
|
|
194
|
+
return new Schema((value, context) => fn().internalSafeParse(value, context));
|
|
145
195
|
}
|
|
146
|
-
function createLiteralSchema(
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
196
|
+
function createLiteralSchema(literal) {
|
|
197
|
+
if (Array.isArray(literal)) {
|
|
198
|
+
return createUnionSchema(literal.map((item) => createLiteralSchema(item)));
|
|
199
|
+
}
|
|
200
|
+
return new Schema((value, context) => {
|
|
201
|
+
if (value !== literal) {
|
|
202
|
+
if (context !== undefined) {
|
|
203
|
+
context.errors.push({ path: context.path, message: `Expected literal ${literal}, got ${getType(value)}` });
|
|
153
204
|
}
|
|
154
|
-
return
|
|
155
|
-
}
|
|
205
|
+
return { success: false };
|
|
206
|
+
}
|
|
207
|
+
return { success: true, data: literal };
|
|
156
208
|
});
|
|
157
209
|
}
|
|
158
210
|
function createObjectSchema(schema) {
|
|
159
|
-
return new ObjectSchema(schema);
|
|
211
|
+
return new ObjectSchema(schema, false);
|
|
212
|
+
}
|
|
213
|
+
function createOptionalSchema(schema) {
|
|
214
|
+
return createUnionSchema([createUndefinedSchema(), schema]);
|
|
215
|
+
}
|
|
216
|
+
function createNullableSchema(schema) {
|
|
217
|
+
return createUnionSchema([createNullSchema(), schema]);
|
|
160
218
|
}
|
|
161
219
|
function createNullSchema() {
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
if (value !== null) {
|
|
167
|
-
throw new Error(`Expected ${inputType}, got ${JSON.stringify(value)}`);
|
|
220
|
+
return new Schema((value, context) => {
|
|
221
|
+
if (value !== null) {
|
|
222
|
+
if (context !== undefined) {
|
|
223
|
+
context.errors.push({ path: context.path, message: `Expected null, got ${getType(value)}` });
|
|
168
224
|
}
|
|
169
|
-
return
|
|
170
|
-
}
|
|
225
|
+
return { success: false };
|
|
226
|
+
}
|
|
227
|
+
return { success: true, data: value };
|
|
171
228
|
});
|
|
172
229
|
}
|
|
230
|
+
function createNullishSchema(schema) {
|
|
231
|
+
return createUnionSchema([createNullSchema(), createUndefinedSchema(), schema]);
|
|
232
|
+
}
|
|
173
233
|
function createNumberSchema() {
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
if (typeof value !== 'number') {
|
|
179
|
-
throw new Error(`Expected ${inputType}, got ${JSON.stringify(value)}`);
|
|
234
|
+
return new Schema((value, context) => {
|
|
235
|
+
if (typeof value !== 'number') {
|
|
236
|
+
if (context !== undefined) {
|
|
237
|
+
context.errors.push({ path: context.path, message: `Expected number, got ${getType(value)}` });
|
|
180
238
|
}
|
|
181
|
-
return
|
|
182
|
-
}
|
|
239
|
+
return { success: false };
|
|
240
|
+
}
|
|
241
|
+
return { success: true, data: value };
|
|
183
242
|
});
|
|
184
243
|
}
|
|
185
244
|
function createRecordSchema(schema) {
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
245
|
+
return new Schema((record, context) => {
|
|
246
|
+
if (typeof record !== 'object' || record === null || Array.isArray(record)) {
|
|
247
|
+
throw new Error(`Expected object, got ${getType(record)}`);
|
|
248
|
+
}
|
|
249
|
+
const result = (() => {
|
|
250
|
+
const parsedObject = {};
|
|
251
|
+
for (const [key, value] of Object.entries(record)) {
|
|
252
|
+
const result = schema.internalSafeParse(value);
|
|
253
|
+
if (!result.success) {
|
|
254
|
+
return result;
|
|
255
|
+
}
|
|
256
|
+
parsedObject[key] = result.data;
|
|
257
|
+
}
|
|
258
|
+
return { success: true, data: parsedObject };
|
|
259
|
+
})();
|
|
260
|
+
if (result.success) {
|
|
261
|
+
return result;
|
|
262
|
+
}
|
|
263
|
+
if (context !== undefined) {
|
|
264
|
+
for (const [key, value] of Object.entries(record)) {
|
|
265
|
+
schema.internalSafeParse(value, { path: [...context.path, key], errors: context.errors });
|
|
192
266
|
}
|
|
193
|
-
|
|
194
|
-
}
|
|
267
|
+
}
|
|
268
|
+
return { success: false };
|
|
195
269
|
});
|
|
196
270
|
}
|
|
271
|
+
function createStrictObjectSchema(schema) {
|
|
272
|
+
return new ObjectSchema(schema, true);
|
|
273
|
+
}
|
|
197
274
|
function createStringSchema() {
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
if (typeof value !== 'string') {
|
|
203
|
-
throw new Error(`Expected ${inputType}, got ${JSON.stringify(value)}`);
|
|
275
|
+
return new Schema((value, context) => {
|
|
276
|
+
if (typeof value !== 'string') {
|
|
277
|
+
if (context !== undefined) {
|
|
278
|
+
context.errors.push({ path: context.path, message: `Expected string, got ${getType(value)}` });
|
|
204
279
|
}
|
|
205
|
-
return
|
|
206
|
-
}
|
|
280
|
+
return { success: false };
|
|
281
|
+
}
|
|
282
|
+
return { success: true, data: value };
|
|
207
283
|
});
|
|
208
284
|
}
|
|
209
285
|
function createTupleSchema(schema) {
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
286
|
+
return new Schema((value, context) => {
|
|
287
|
+
if (!Array.isArray(value)) {
|
|
288
|
+
if (context !== undefined) {
|
|
289
|
+
context.errors.push({ path: context.path, message: `Expected array, got ${getType(value)}` });
|
|
290
|
+
}
|
|
291
|
+
return { success: false };
|
|
292
|
+
}
|
|
293
|
+
if (value.length !== schema.length) {
|
|
294
|
+
if (context !== undefined) {
|
|
295
|
+
context.errors.push({
|
|
296
|
+
path: context.path,
|
|
297
|
+
message: `Expected array of length ${schema.length}, got array of length ${value.length}`,
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
return { success: false };
|
|
301
|
+
}
|
|
302
|
+
const result = (() => {
|
|
303
|
+
const parsedItems = [];
|
|
304
|
+
let index = 0;
|
|
305
|
+
for (const itemSchema of schema) {
|
|
306
|
+
const result = itemSchema.internalSafeParse(value[index]);
|
|
307
|
+
if (!result.success) {
|
|
308
|
+
return result;
|
|
309
|
+
}
|
|
310
|
+
parsedItems.push(result.data);
|
|
311
|
+
index++;
|
|
216
312
|
}
|
|
217
|
-
return
|
|
218
|
-
}
|
|
313
|
+
return { success: true, data: parsedItems };
|
|
314
|
+
})();
|
|
315
|
+
if (result.success) {
|
|
316
|
+
return result;
|
|
317
|
+
}
|
|
318
|
+
if (context !== undefined) {
|
|
319
|
+
let index = 0;
|
|
320
|
+
for (const itemSchema of schema) {
|
|
321
|
+
itemSchema.internalSafeParse(value[index], { path: [...context.path, index], errors: context.errors });
|
|
322
|
+
index++;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
return { success: false };
|
|
219
326
|
});
|
|
220
327
|
}
|
|
221
328
|
function createUndefinedSchema() {
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
if (value !== undefined) {
|
|
227
|
-
throw new Error(`Expected ${inputType}, got ${JSON.stringify(value)}`);
|
|
329
|
+
return new Schema((value, context) => {
|
|
330
|
+
if (value !== undefined) {
|
|
331
|
+
if (context !== undefined) {
|
|
332
|
+
context.errors.push({ path: context.path, message: `Expected undefined, got ${getType(value)}` });
|
|
228
333
|
}
|
|
229
|
-
return
|
|
230
|
-
}
|
|
334
|
+
return { success: false };
|
|
335
|
+
}
|
|
336
|
+
return { success: true, data: value };
|
|
231
337
|
});
|
|
232
338
|
}
|
|
233
339
|
function createUnionSchema(schemas) {
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
340
|
+
return new Schema((value, context) => {
|
|
341
|
+
for (const schema of schemas) {
|
|
342
|
+
const result = schema.internalSafeParse(value);
|
|
343
|
+
if (result.success) {
|
|
344
|
+
return result;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
if (context !== undefined) {
|
|
238
348
|
for (const schema of schemas) {
|
|
239
|
-
|
|
240
|
-
return schema.parse(value);
|
|
241
|
-
}
|
|
242
|
-
catch (_) {
|
|
243
|
-
continue;
|
|
244
|
-
}
|
|
349
|
+
schema.internalSafeParse(value, context);
|
|
245
350
|
}
|
|
246
|
-
|
|
247
|
-
}
|
|
351
|
+
}
|
|
352
|
+
return { success: false };
|
|
248
353
|
});
|
|
249
354
|
}
|
|
250
355
|
function createUnknownSchema() {
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
inputType,
|
|
254
|
-
parseFn: (value) => {
|
|
255
|
-
return value;
|
|
256
|
-
},
|
|
356
|
+
return new Schema((value) => {
|
|
357
|
+
return { success: true, data: value };
|
|
257
358
|
});
|
|
258
359
|
}
|
|
259
|
-
export { createArraySchema as array, createBigIntSchema as bigint, createBooleanSchema as boolean, createDateSchema as date, createInstanceofSchema as instanceof, createLazySchema as lazy, createLiteralSchema as literal, createNullSchema as null, createNumberSchema as number, createObjectSchema as object, createRecordSchema as record, createStringSchema as string, createTupleSchema as tuple, createUndefinedSchema as undefined, createUnionSchema as union, createUnknownSchema as unknown, };
|
|
360
|
+
export { createArraySchema as array, createBigIntSchema as bigint, createBooleanSchema as boolean, createDateSchema as date, createInstanceofSchema as instanceof, createLazySchema as lazy, createLiteralSchema as literal, createNullableSchema as nullable, createNullishSchema as nullish, createNullSchema as null, createNumberSchema as number, createObjectSchema as object, createOptionalSchema as optional, createRecordSchema as record, createStrictObjectSchema as strictObject, createStringSchema as string, createTupleSchema as tuple, createUndefinedSchema as undefined, createUnionSchema as union, createUnknownSchema as unknown, };
|