@forklaunch/validator 0.3.12 → 0.4.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/lib/index-V6qNDTlr.d.mts +476 -0
- package/lib/index-V6qNDTlr.d.ts +476 -0
- package/lib/index.d.mts +6 -0
- package/lib/index.d.ts +6 -2
- package/lib/index.js +18 -1
- package/lib/index.mjs +0 -0
- package/lib/src/typebox/index.d.mts +220 -0
- package/lib/src/typebox/index.d.ts +220 -3
- package/lib/src/typebox/index.js +448 -1
- package/lib/src/typebox/index.mjs +406 -0
- package/lib/src/zod/index.d.mts +106 -0
- package/lib/src/zod/index.d.ts +106 -3
- package/lib/src/zod/index.js +291 -1
- package/lib/src/zod/index.mjs +244 -0
- package/lib/tests/utils/mockSchemaValidator.d.mts +69 -0
- package/lib/tests/utils/mockSchemaValidator.d.ts +29 -25
- package/lib/tests/utils/mockSchemaValidator.js +143 -79
- package/lib/tests/utils/mockSchemaValidator.mjs +99 -0
- package/package.json +21 -8
- package/lib/index.d.ts.map +0 -1
- package/lib/src/shared/types/schema.types.d.ts +0 -255
- package/lib/src/shared/types/schema.types.d.ts.map +0 -1
- package/lib/src/shared/types/schema.types.js +0 -1
- package/lib/src/typebox/index.d.ts.map +0 -1
- package/lib/src/typebox/staticSchemaValidator.d.ts +0 -99
- package/lib/src/typebox/staticSchemaValidator.d.ts.map +0 -1
- package/lib/src/typebox/staticSchemaValidator.js +0 -99
- package/lib/src/typebox/typeboxSchemaValidator.d.ts +0 -122
- package/lib/src/typebox/typeboxSchemaValidator.d.ts.map +0 -1
- package/lib/src/typebox/typeboxSchemaValidator.js +0 -362
- package/lib/src/typebox/types/schema.types.d.ts +0 -59
- package/lib/src/typebox/types/schema.types.d.ts.map +0 -1
- package/lib/src/typebox/types/schema.types.js +0 -1
- package/lib/src/zod/index.d.ts.map +0 -1
- package/lib/src/zod/staticSchemaValidator.d.ts +0 -99
- package/lib/src/zod/staticSchemaValidator.d.ts.map +0 -1
- package/lib/src/zod/staticSchemaValidator.js +0 -99
- package/lib/src/zod/types/schema.types.d.ts +0 -71
- package/lib/src/zod/types/schema.types.d.ts.map +0 -1
- package/lib/src/zod/types/schema.types.js +0 -1
- package/lib/src/zod/zodSchemaValidator.d.ts +0 -110
- package/lib/src/zod/zodSchemaValidator.d.ts.map +0 -1
- package/lib/src/zod/zodSchemaValidator.js +0 -192
- package/lib/tests/typebox/advancedParse.test.d.ts +0 -2
- package/lib/tests/typebox/advancedParse.test.d.ts.map +0 -1
- package/lib/tests/typebox/advancedParse.test.js +0 -514
- package/lib/tests/typebox/largeSchema.test.d.ts +0 -2
- package/lib/tests/typebox/largeSchema.test.d.ts.map +0 -1
- package/lib/tests/typebox/largeSchema.test.js +0 -150
- package/lib/tests/typebox/schemaValidator.test.d.ts +0 -2
- package/lib/tests/typebox/schemaValidator.test.d.ts.map +0 -1
- package/lib/tests/typebox/schemaValidator.test.js +0 -253
- package/lib/tests/typebox/typeEquality.test.d.ts +0 -2
- package/lib/tests/typebox/typeEquality.test.d.ts.map +0 -1
- package/lib/tests/typebox/typeEquality.test.js +0 -100
- package/lib/tests/utils/compare.d.ts +0 -2
- package/lib/tests/utils/compare.d.ts.map +0 -1
- package/lib/tests/utils/compare.js +0 -8
- package/lib/tests/utils/mockSchemaValidator.d.ts.map +0 -1
- package/lib/tests/zod/advancedParse.test.d.ts +0 -2
- package/lib/tests/zod/advancedParse.test.d.ts.map +0 -1
- package/lib/tests/zod/advancedParse.test.js +0 -514
- package/lib/tests/zod/largeSchema.test.d.ts +0 -2
- package/lib/tests/zod/largeSchema.test.d.ts.map +0 -1
- package/lib/tests/zod/largeSchema.test.js +0 -150
- package/lib/tests/zod/schemaValidator.test.d.ts +0 -2
- package/lib/tests/zod/schemaValidator.test.d.ts.map +0 -1
- package/lib/tests/zod/schemaValidator.test.js +0 -205
- package/lib/tests/zod/typeEquality.test.d.ts +0 -2
- package/lib/tests/zod/typeEquality.test.d.ts.map +0 -1
- package/lib/tests/zod/typeEquality.test.js +0 -100
- package/lib/tsconfig.tsbuildinfo +0 -1
- package/lib/vitest.config.d.ts +0 -3
- package/lib/vitest.config.d.ts.map +0 -1
- package/lib/vitest.config.js +0 -7
package/lib/src/zod/index.js
CHANGED
|
@@ -1 +1,291 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/zod/index.ts
|
|
21
|
+
var zod_exports = {};
|
|
22
|
+
__export(zod_exports, {
|
|
23
|
+
SchemaValidator: () => SchemaValidator,
|
|
24
|
+
any: () => any,
|
|
25
|
+
array: () => array,
|
|
26
|
+
bigint: () => bigint,
|
|
27
|
+
boolean: () => boolean,
|
|
28
|
+
date: () => date,
|
|
29
|
+
email: () => email,
|
|
30
|
+
enum_: () => enum_,
|
|
31
|
+
isSchema: () => isSchema,
|
|
32
|
+
literal: () => literal,
|
|
33
|
+
never: () => never,
|
|
34
|
+
nullish: () => nullish,
|
|
35
|
+
number: () => number,
|
|
36
|
+
openapi: () => openapi,
|
|
37
|
+
optional: () => optional,
|
|
38
|
+
parse: () => parse,
|
|
39
|
+
schemify: () => schemify,
|
|
40
|
+
string: () => string,
|
|
41
|
+
symbol: () => symbol,
|
|
42
|
+
union: () => union,
|
|
43
|
+
unknown: () => unknown,
|
|
44
|
+
uri: () => uri,
|
|
45
|
+
uuid: () => uuid,
|
|
46
|
+
validate: () => validate
|
|
47
|
+
});
|
|
48
|
+
module.exports = __toCommonJS(zod_exports);
|
|
49
|
+
|
|
50
|
+
// src/zod/zodSchemaValidator.ts
|
|
51
|
+
var import_zod_openapi = require("@anatine/zod-openapi");
|
|
52
|
+
var import_zod = require("zod");
|
|
53
|
+
var ZodSchemaValidator = class {
|
|
54
|
+
_Type;
|
|
55
|
+
_SchemaCatchall;
|
|
56
|
+
_ValidSchemaObject;
|
|
57
|
+
string = import_zod.z.string();
|
|
58
|
+
uuid = import_zod.z.coerce.string().uuid();
|
|
59
|
+
email = import_zod.z.coerce.string().email();
|
|
60
|
+
uri = import_zod.z.coerce.string().url();
|
|
61
|
+
number = import_zod.z.coerce.number();
|
|
62
|
+
bigint = import_zod.z.coerce.bigint();
|
|
63
|
+
boolean = import_zod.z.preprocess((val) => {
|
|
64
|
+
if (typeof val === "string") {
|
|
65
|
+
if (val.toLowerCase() === "true") return true;
|
|
66
|
+
if (val.toLowerCase() === "false") return false;
|
|
67
|
+
}
|
|
68
|
+
return val;
|
|
69
|
+
}, import_zod.z.boolean());
|
|
70
|
+
date = import_zod.z.coerce.date();
|
|
71
|
+
symbol = import_zod.z.symbol();
|
|
72
|
+
nullish = import_zod.z.union([import_zod.z.void(), import_zod.z.null(), import_zod.z.undefined()]);
|
|
73
|
+
any = import_zod.z.any();
|
|
74
|
+
unknown = import_zod.z.unknown();
|
|
75
|
+
never = import_zod.z.never();
|
|
76
|
+
/**
|
|
77
|
+
* Pretty print Zod errors.
|
|
78
|
+
*
|
|
79
|
+
* @param {ZodError} error - The Zod error to pretty print.
|
|
80
|
+
* @returns
|
|
81
|
+
*/
|
|
82
|
+
prettyPrintZodErrors(error) {
|
|
83
|
+
if (!error) return;
|
|
84
|
+
const errorMessages = error.errors.map((err, index) => {
|
|
85
|
+
const path = err.path.length > 0 ? err.path.join(" > ") : "root";
|
|
86
|
+
return `${index + 1}. Path: ${path}
|
|
87
|
+
Message: ${err.message}`;
|
|
88
|
+
});
|
|
89
|
+
return `Validation failed with the following errors:
|
|
90
|
+
${errorMessages.join("\n\n")}`;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Compiles schema if this exists, for optimal performance.
|
|
94
|
+
*
|
|
95
|
+
* @param {ZodObject<ZodRawShape>} schema - The schema to compile.
|
|
96
|
+
* @returns {ZodResolve<T>} - The compiled schema.
|
|
97
|
+
*/
|
|
98
|
+
compile(schema) {
|
|
99
|
+
return schema;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Convert a schema to a Zod schema.
|
|
103
|
+
* @param {ZodIdiomaticSchema} schema - The schema to convert.
|
|
104
|
+
* @returns {ZodResolve<T>} The resolved schema.
|
|
105
|
+
*/
|
|
106
|
+
schemify(schema) {
|
|
107
|
+
if (typeof schema === "string" || typeof schema === "number" || typeof schema === "boolean") {
|
|
108
|
+
return import_zod.z.literal(schema);
|
|
109
|
+
}
|
|
110
|
+
if (schema instanceof import_zod.ZodType) {
|
|
111
|
+
return schema;
|
|
112
|
+
}
|
|
113
|
+
const newSchema = {};
|
|
114
|
+
Object.getOwnPropertyNames(schema).forEach((key) => {
|
|
115
|
+
if (schema[key] instanceof import_zod.ZodType) {
|
|
116
|
+
newSchema[key] = schema[key];
|
|
117
|
+
} else {
|
|
118
|
+
newSchema[key] = this.schemify(schema[key]);
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
return import_zod.z.object(newSchema);
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Make a schema optional.
|
|
125
|
+
* @param {ZodIdiomaticSchema} schema - The schema to make optional.
|
|
126
|
+
* @returns {ZodOptional<ZodResolve<T>>} The optional schema.
|
|
127
|
+
*/
|
|
128
|
+
optional(schema) {
|
|
129
|
+
if (schema instanceof import_zod.ZodType) {
|
|
130
|
+
return schema.optional();
|
|
131
|
+
} else {
|
|
132
|
+
return this.schemify(schema).optional();
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Create an array schema.
|
|
137
|
+
* @param {ZodIdiomaticSchema} schema - The schema to use for array items.
|
|
138
|
+
* @returns {ZodArray<ZodResolve<T>>} The array schema.
|
|
139
|
+
*/
|
|
140
|
+
array(schema) {
|
|
141
|
+
if (schema instanceof import_zod.ZodType) {
|
|
142
|
+
return schema.array();
|
|
143
|
+
} else {
|
|
144
|
+
return this.schemify(schema).array();
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Create a union schema.
|
|
149
|
+
* @param {ZodUnionContainer} schemas - The schemas to union.
|
|
150
|
+
* @returns {ZodUnion<UnionZodResolve<T>>} The union schema.
|
|
151
|
+
*/
|
|
152
|
+
union(schemas) {
|
|
153
|
+
const resolvedSchemas = schemas.map((schema) => {
|
|
154
|
+
if (schema instanceof import_zod.ZodType) {
|
|
155
|
+
return schema;
|
|
156
|
+
}
|
|
157
|
+
return this.schemify(schema);
|
|
158
|
+
});
|
|
159
|
+
return import_zod.z.union(
|
|
160
|
+
resolvedSchemas
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Create a literal schema.
|
|
165
|
+
* @param {LiteralSchema} value - The literal value.
|
|
166
|
+
* @returns {ZodLiteral<ZodResolve<T>>} The literal schema.
|
|
167
|
+
*/
|
|
168
|
+
literal(value) {
|
|
169
|
+
return import_zod.z.literal(value);
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Create an enum schema.
|
|
173
|
+
* @param {Record<string, LiteralSchema>} schemaEnum - The enum schema.
|
|
174
|
+
* @returns {ZodUnion<UnionZodResolve<[T, T, ...T[]]>>} The enum schema.
|
|
175
|
+
*/
|
|
176
|
+
enum_(schemaEnum) {
|
|
177
|
+
return this.union(Object.values(schemaEnum));
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Checks if a value is a Zod schema.
|
|
181
|
+
* @param {unknown} value - The value to check.
|
|
182
|
+
* @returns {boolean} True if the value is a Zod schema.
|
|
183
|
+
*/
|
|
184
|
+
isSchema(value) {
|
|
185
|
+
return value instanceof import_zod.ZodType;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Validate a value against a schema.
|
|
189
|
+
* @param {ZodCatchall} schema - The schema to validate against.
|
|
190
|
+
* @param {unknown} value - The value to validate.
|
|
191
|
+
* @returns {boolean} True if valid, otherwise false.
|
|
192
|
+
*/
|
|
193
|
+
validate(schema, value) {
|
|
194
|
+
return schema.safeParse(value).success;
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Parses a value to a schema validation.
|
|
198
|
+
*
|
|
199
|
+
* @param {ZodCatchall} schema - The schema to validate against.
|
|
200
|
+
* @param {unknown} value - The value to validate.
|
|
201
|
+
* @returns {ParseResult} - The discrimintated parsed value if successful, the error if unsuccessful.
|
|
202
|
+
*/
|
|
203
|
+
parse(schema, value) {
|
|
204
|
+
try {
|
|
205
|
+
const result = schema.safeParse(value);
|
|
206
|
+
return result.success ? { ok: true, value: result.data } : {
|
|
207
|
+
ok: false,
|
|
208
|
+
error: this.prettyPrintZodErrors(result.error)
|
|
209
|
+
};
|
|
210
|
+
} catch (error) {
|
|
211
|
+
return {
|
|
212
|
+
ok: false,
|
|
213
|
+
error: `Unexpected zod safeParse error: ${error.message}`
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Convert a schema to an OpenAPI schema object.
|
|
219
|
+
* @param {ZodIdiomaticSchema} schema - The schema to convert.
|
|
220
|
+
* @returns {SchemaObject} The OpenAPI schema object.
|
|
221
|
+
*/
|
|
222
|
+
openapi(schema) {
|
|
223
|
+
return (0, import_zod_openapi.generateSchema)(this.schemify(schema));
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
// src/zod/staticSchemaValidator.ts
|
|
228
|
+
var SchemaValidator = () => new ZodSchemaValidator();
|
|
229
|
+
var StaticSchemaValidator = SchemaValidator();
|
|
230
|
+
var string = StaticSchemaValidator.string;
|
|
231
|
+
var uuid = StaticSchemaValidator.uuid;
|
|
232
|
+
var email = StaticSchemaValidator.email;
|
|
233
|
+
var uri = StaticSchemaValidator.uri;
|
|
234
|
+
var number = StaticSchemaValidator.number;
|
|
235
|
+
var bigint = StaticSchemaValidator.bigint;
|
|
236
|
+
var boolean = StaticSchemaValidator.boolean;
|
|
237
|
+
var date = StaticSchemaValidator.date;
|
|
238
|
+
var symbol = StaticSchemaValidator.symbol;
|
|
239
|
+
var nullish = StaticSchemaValidator.nullish;
|
|
240
|
+
var any = StaticSchemaValidator.any;
|
|
241
|
+
var unknown = StaticSchemaValidator.unknown;
|
|
242
|
+
var never = StaticSchemaValidator.never;
|
|
243
|
+
var schemify = StaticSchemaValidator.schemify.bind(
|
|
244
|
+
StaticSchemaValidator
|
|
245
|
+
);
|
|
246
|
+
var optional = StaticSchemaValidator.optional.bind(
|
|
247
|
+
StaticSchemaValidator
|
|
248
|
+
);
|
|
249
|
+
var array = StaticSchemaValidator.array.bind(StaticSchemaValidator);
|
|
250
|
+
var union = StaticSchemaValidator.union.bind(StaticSchemaValidator);
|
|
251
|
+
var literal = StaticSchemaValidator.literal.bind(
|
|
252
|
+
StaticSchemaValidator
|
|
253
|
+
);
|
|
254
|
+
var enum_ = StaticSchemaValidator.enum_.bind(StaticSchemaValidator);
|
|
255
|
+
var isSchema = StaticSchemaValidator.isSchema.bind(
|
|
256
|
+
StaticSchemaValidator
|
|
257
|
+
);
|
|
258
|
+
var validate = StaticSchemaValidator.validate.bind(
|
|
259
|
+
StaticSchemaValidator
|
|
260
|
+
);
|
|
261
|
+
var parse = StaticSchemaValidator.parse.bind(StaticSchemaValidator);
|
|
262
|
+
var openapi = StaticSchemaValidator.openapi.bind(
|
|
263
|
+
StaticSchemaValidator
|
|
264
|
+
);
|
|
265
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
266
|
+
0 && (module.exports = {
|
|
267
|
+
SchemaValidator,
|
|
268
|
+
any,
|
|
269
|
+
array,
|
|
270
|
+
bigint,
|
|
271
|
+
boolean,
|
|
272
|
+
date,
|
|
273
|
+
email,
|
|
274
|
+
enum_,
|
|
275
|
+
isSchema,
|
|
276
|
+
literal,
|
|
277
|
+
never,
|
|
278
|
+
nullish,
|
|
279
|
+
number,
|
|
280
|
+
openapi,
|
|
281
|
+
optional,
|
|
282
|
+
parse,
|
|
283
|
+
schemify,
|
|
284
|
+
string,
|
|
285
|
+
symbol,
|
|
286
|
+
union,
|
|
287
|
+
unknown,
|
|
288
|
+
uri,
|
|
289
|
+
uuid,
|
|
290
|
+
validate
|
|
291
|
+
});
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
// src/zod/zodSchemaValidator.ts
|
|
2
|
+
import { generateSchema } from "@anatine/zod-openapi";
|
|
3
|
+
import {
|
|
4
|
+
ZodType,
|
|
5
|
+
z
|
|
6
|
+
} from "zod";
|
|
7
|
+
var ZodSchemaValidator = class {
|
|
8
|
+
_Type;
|
|
9
|
+
_SchemaCatchall;
|
|
10
|
+
_ValidSchemaObject;
|
|
11
|
+
string = z.string();
|
|
12
|
+
uuid = z.coerce.string().uuid();
|
|
13
|
+
email = z.coerce.string().email();
|
|
14
|
+
uri = z.coerce.string().url();
|
|
15
|
+
number = z.coerce.number();
|
|
16
|
+
bigint = z.coerce.bigint();
|
|
17
|
+
boolean = z.preprocess((val) => {
|
|
18
|
+
if (typeof val === "string") {
|
|
19
|
+
if (val.toLowerCase() === "true") return true;
|
|
20
|
+
if (val.toLowerCase() === "false") return false;
|
|
21
|
+
}
|
|
22
|
+
return val;
|
|
23
|
+
}, z.boolean());
|
|
24
|
+
date = z.coerce.date();
|
|
25
|
+
symbol = z.symbol();
|
|
26
|
+
nullish = z.union([z.void(), z.null(), z.undefined()]);
|
|
27
|
+
any = z.any();
|
|
28
|
+
unknown = z.unknown();
|
|
29
|
+
never = z.never();
|
|
30
|
+
/**
|
|
31
|
+
* Pretty print Zod errors.
|
|
32
|
+
*
|
|
33
|
+
* @param {ZodError} error - The Zod error to pretty print.
|
|
34
|
+
* @returns
|
|
35
|
+
*/
|
|
36
|
+
prettyPrintZodErrors(error) {
|
|
37
|
+
if (!error) return;
|
|
38
|
+
const errorMessages = error.errors.map((err, index) => {
|
|
39
|
+
const path = err.path.length > 0 ? err.path.join(" > ") : "root";
|
|
40
|
+
return `${index + 1}. Path: ${path}
|
|
41
|
+
Message: ${err.message}`;
|
|
42
|
+
});
|
|
43
|
+
return `Validation failed with the following errors:
|
|
44
|
+
${errorMessages.join("\n\n")}`;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Compiles schema if this exists, for optimal performance.
|
|
48
|
+
*
|
|
49
|
+
* @param {ZodObject<ZodRawShape>} schema - The schema to compile.
|
|
50
|
+
* @returns {ZodResolve<T>} - The compiled schema.
|
|
51
|
+
*/
|
|
52
|
+
compile(schema) {
|
|
53
|
+
return schema;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Convert a schema to a Zod schema.
|
|
57
|
+
* @param {ZodIdiomaticSchema} schema - The schema to convert.
|
|
58
|
+
* @returns {ZodResolve<T>} The resolved schema.
|
|
59
|
+
*/
|
|
60
|
+
schemify(schema) {
|
|
61
|
+
if (typeof schema === "string" || typeof schema === "number" || typeof schema === "boolean") {
|
|
62
|
+
return z.literal(schema);
|
|
63
|
+
}
|
|
64
|
+
if (schema instanceof ZodType) {
|
|
65
|
+
return schema;
|
|
66
|
+
}
|
|
67
|
+
const newSchema = {};
|
|
68
|
+
Object.getOwnPropertyNames(schema).forEach((key) => {
|
|
69
|
+
if (schema[key] instanceof ZodType) {
|
|
70
|
+
newSchema[key] = schema[key];
|
|
71
|
+
} else {
|
|
72
|
+
newSchema[key] = this.schemify(schema[key]);
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
return z.object(newSchema);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Make a schema optional.
|
|
79
|
+
* @param {ZodIdiomaticSchema} schema - The schema to make optional.
|
|
80
|
+
* @returns {ZodOptional<ZodResolve<T>>} The optional schema.
|
|
81
|
+
*/
|
|
82
|
+
optional(schema) {
|
|
83
|
+
if (schema instanceof ZodType) {
|
|
84
|
+
return schema.optional();
|
|
85
|
+
} else {
|
|
86
|
+
return this.schemify(schema).optional();
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Create an array schema.
|
|
91
|
+
* @param {ZodIdiomaticSchema} schema - The schema to use for array items.
|
|
92
|
+
* @returns {ZodArray<ZodResolve<T>>} The array schema.
|
|
93
|
+
*/
|
|
94
|
+
array(schema) {
|
|
95
|
+
if (schema instanceof ZodType) {
|
|
96
|
+
return schema.array();
|
|
97
|
+
} else {
|
|
98
|
+
return this.schemify(schema).array();
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Create a union schema.
|
|
103
|
+
* @param {ZodUnionContainer} schemas - The schemas to union.
|
|
104
|
+
* @returns {ZodUnion<UnionZodResolve<T>>} The union schema.
|
|
105
|
+
*/
|
|
106
|
+
union(schemas) {
|
|
107
|
+
const resolvedSchemas = schemas.map((schema) => {
|
|
108
|
+
if (schema instanceof ZodType) {
|
|
109
|
+
return schema;
|
|
110
|
+
}
|
|
111
|
+
return this.schemify(schema);
|
|
112
|
+
});
|
|
113
|
+
return z.union(
|
|
114
|
+
resolvedSchemas
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Create a literal schema.
|
|
119
|
+
* @param {LiteralSchema} value - The literal value.
|
|
120
|
+
* @returns {ZodLiteral<ZodResolve<T>>} The literal schema.
|
|
121
|
+
*/
|
|
122
|
+
literal(value) {
|
|
123
|
+
return z.literal(value);
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Create an enum schema.
|
|
127
|
+
* @param {Record<string, LiteralSchema>} schemaEnum - The enum schema.
|
|
128
|
+
* @returns {ZodUnion<UnionZodResolve<[T, T, ...T[]]>>} The enum schema.
|
|
129
|
+
*/
|
|
130
|
+
enum_(schemaEnum) {
|
|
131
|
+
return this.union(Object.values(schemaEnum));
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Checks if a value is a Zod schema.
|
|
135
|
+
* @param {unknown} value - The value to check.
|
|
136
|
+
* @returns {boolean} True if the value is a Zod schema.
|
|
137
|
+
*/
|
|
138
|
+
isSchema(value) {
|
|
139
|
+
return value instanceof ZodType;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Validate a value against a schema.
|
|
143
|
+
* @param {ZodCatchall} schema - The schema to validate against.
|
|
144
|
+
* @param {unknown} value - The value to validate.
|
|
145
|
+
* @returns {boolean} True if valid, otherwise false.
|
|
146
|
+
*/
|
|
147
|
+
validate(schema, value) {
|
|
148
|
+
return schema.safeParse(value).success;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Parses a value to a schema validation.
|
|
152
|
+
*
|
|
153
|
+
* @param {ZodCatchall} schema - The schema to validate against.
|
|
154
|
+
* @param {unknown} value - The value to validate.
|
|
155
|
+
* @returns {ParseResult} - The discrimintated parsed value if successful, the error if unsuccessful.
|
|
156
|
+
*/
|
|
157
|
+
parse(schema, value) {
|
|
158
|
+
try {
|
|
159
|
+
const result = schema.safeParse(value);
|
|
160
|
+
return result.success ? { ok: true, value: result.data } : {
|
|
161
|
+
ok: false,
|
|
162
|
+
error: this.prettyPrintZodErrors(result.error)
|
|
163
|
+
};
|
|
164
|
+
} catch (error) {
|
|
165
|
+
return {
|
|
166
|
+
ok: false,
|
|
167
|
+
error: `Unexpected zod safeParse error: ${error.message}`
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Convert a schema to an OpenAPI schema object.
|
|
173
|
+
* @param {ZodIdiomaticSchema} schema - The schema to convert.
|
|
174
|
+
* @returns {SchemaObject} The OpenAPI schema object.
|
|
175
|
+
*/
|
|
176
|
+
openapi(schema) {
|
|
177
|
+
return generateSchema(this.schemify(schema));
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
// src/zod/staticSchemaValidator.ts
|
|
182
|
+
var SchemaValidator = () => new ZodSchemaValidator();
|
|
183
|
+
var StaticSchemaValidator = SchemaValidator();
|
|
184
|
+
var string = StaticSchemaValidator.string;
|
|
185
|
+
var uuid = StaticSchemaValidator.uuid;
|
|
186
|
+
var email = StaticSchemaValidator.email;
|
|
187
|
+
var uri = StaticSchemaValidator.uri;
|
|
188
|
+
var number = StaticSchemaValidator.number;
|
|
189
|
+
var bigint = StaticSchemaValidator.bigint;
|
|
190
|
+
var boolean = StaticSchemaValidator.boolean;
|
|
191
|
+
var date = StaticSchemaValidator.date;
|
|
192
|
+
var symbol = StaticSchemaValidator.symbol;
|
|
193
|
+
var nullish = StaticSchemaValidator.nullish;
|
|
194
|
+
var any = StaticSchemaValidator.any;
|
|
195
|
+
var unknown = StaticSchemaValidator.unknown;
|
|
196
|
+
var never = StaticSchemaValidator.never;
|
|
197
|
+
var schemify = StaticSchemaValidator.schemify.bind(
|
|
198
|
+
StaticSchemaValidator
|
|
199
|
+
);
|
|
200
|
+
var optional = StaticSchemaValidator.optional.bind(
|
|
201
|
+
StaticSchemaValidator
|
|
202
|
+
);
|
|
203
|
+
var array = StaticSchemaValidator.array.bind(StaticSchemaValidator);
|
|
204
|
+
var union = StaticSchemaValidator.union.bind(StaticSchemaValidator);
|
|
205
|
+
var literal = StaticSchemaValidator.literal.bind(
|
|
206
|
+
StaticSchemaValidator
|
|
207
|
+
);
|
|
208
|
+
var enum_ = StaticSchemaValidator.enum_.bind(StaticSchemaValidator);
|
|
209
|
+
var isSchema = StaticSchemaValidator.isSchema.bind(
|
|
210
|
+
StaticSchemaValidator
|
|
211
|
+
);
|
|
212
|
+
var validate = StaticSchemaValidator.validate.bind(
|
|
213
|
+
StaticSchemaValidator
|
|
214
|
+
);
|
|
215
|
+
var parse = StaticSchemaValidator.parse.bind(StaticSchemaValidator);
|
|
216
|
+
var openapi = StaticSchemaValidator.openapi.bind(
|
|
217
|
+
StaticSchemaValidator
|
|
218
|
+
);
|
|
219
|
+
export {
|
|
220
|
+
SchemaValidator,
|
|
221
|
+
any,
|
|
222
|
+
array,
|
|
223
|
+
bigint,
|
|
224
|
+
boolean,
|
|
225
|
+
date,
|
|
226
|
+
email,
|
|
227
|
+
enum_,
|
|
228
|
+
isSchema,
|
|
229
|
+
literal,
|
|
230
|
+
never,
|
|
231
|
+
nullish,
|
|
232
|
+
number,
|
|
233
|
+
openapi,
|
|
234
|
+
optional,
|
|
235
|
+
parse,
|
|
236
|
+
schemify,
|
|
237
|
+
string,
|
|
238
|
+
symbol,
|
|
239
|
+
union,
|
|
240
|
+
unknown,
|
|
241
|
+
uri,
|
|
242
|
+
uuid,
|
|
243
|
+
validate
|
|
244
|
+
};
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { SchemaObject } from 'openapi3-ts/oas31';
|
|
2
|
+
import { S as SchemaValidator, L as LiteralSchema, P as ParseResult } from '../../index-V6qNDTlr.mjs';
|
|
3
|
+
import '@forklaunch/common';
|
|
4
|
+
import '@sinclair/typebox';
|
|
5
|
+
import '@sinclair/typebox/compiler';
|
|
6
|
+
import 'zod';
|
|
7
|
+
|
|
8
|
+
declare module '@forklaunch/validator' {
|
|
9
|
+
interface SchemaResolve<T> {
|
|
10
|
+
Mock: T;
|
|
11
|
+
}
|
|
12
|
+
interface SchemaTranslate<T> {
|
|
13
|
+
Mock: T;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
type RecursiveUnion<T extends readonly string[]> = T extends readonly [
|
|
17
|
+
infer F extends string,
|
|
18
|
+
...infer R extends readonly string[]
|
|
19
|
+
] ? R extends [] ? F : `${F} | ${RecursiveUnion<R>}` : '';
|
|
20
|
+
declare class MockSchemaValidator implements SchemaValidator<(<T extends string>(schema: T) => T), <T extends string>(schema: T) => T, <T extends string>(schema: T) => `optional ${T}`, <T extends string>(schema: T) => `array ${T}`, <T extends readonly string[]>(schemas: T) => RecursiveUnion<T>, <T extends LiteralSchema>(schema: T) => `literal ${T}`, <T extends LiteralSchema>(schemaEnum: Record<string, T>) => `enum ${T}`, (value: unknown) => value is string, <T extends string>(schema: T, value: string) => boolean, <T extends string>(schema: T, value: string) => ParseResult<T>, <T extends string>(schema: T) => SchemaObject> {
|
|
21
|
+
_Type: 'Mock';
|
|
22
|
+
_SchemaCatchall: string;
|
|
23
|
+
_ValidSchemaObject: string;
|
|
24
|
+
string: string;
|
|
25
|
+
uuid: string;
|
|
26
|
+
email: string;
|
|
27
|
+
uri: string;
|
|
28
|
+
number: string;
|
|
29
|
+
bigint: string;
|
|
30
|
+
boolean: string;
|
|
31
|
+
date: string;
|
|
32
|
+
symbol: string;
|
|
33
|
+
nullish: string;
|
|
34
|
+
any: string;
|
|
35
|
+
unknown: string;
|
|
36
|
+
never: string;
|
|
37
|
+
compile<T extends string>(schema: T): T;
|
|
38
|
+
schemify<T extends string>(schema: T): T;
|
|
39
|
+
optional<T extends string>(schema: T): `optional ${T}`;
|
|
40
|
+
array<T extends string>(schema: T): `array ${T}`;
|
|
41
|
+
union<T extends readonly string[]>(schemas: T): RecursiveUnion<T>;
|
|
42
|
+
literal<T extends LiteralSchema>(schema: T): `literal ${T}`;
|
|
43
|
+
enum_<T extends LiteralSchema>(schemaEnum: Record<string, T>): `enum ${T}`;
|
|
44
|
+
isSchema(value: unknown): value is string;
|
|
45
|
+
validate<T extends string>(schema: T, value: string): boolean;
|
|
46
|
+
parse<T extends string>(schema: T, value: string): ParseResult<T>;
|
|
47
|
+
openapi<T extends string>(_schema: T): SchemaObject;
|
|
48
|
+
}
|
|
49
|
+
declare const mockSchemaValidator: MockSchemaValidator;
|
|
50
|
+
declare const string: string;
|
|
51
|
+
declare const number: string;
|
|
52
|
+
declare const bigint: string;
|
|
53
|
+
declare const boolean: string;
|
|
54
|
+
declare const date: string;
|
|
55
|
+
declare const symbol: string;
|
|
56
|
+
declare const nullish: string;
|
|
57
|
+
declare const any: string;
|
|
58
|
+
declare const unknown: string;
|
|
59
|
+
declare const never: string;
|
|
60
|
+
declare const schemify: <T extends string>(schema: T) => T;
|
|
61
|
+
declare const optional: <T extends string>(schema: T) => `optional ${T}`;
|
|
62
|
+
declare const array: <T extends string>(schema: T) => `array ${T}`;
|
|
63
|
+
declare const union: <T extends readonly string[]>(schemas: T) => RecursiveUnion<T>;
|
|
64
|
+
declare const literal: <T extends LiteralSchema>(schema: T) => `literal ${T}`;
|
|
65
|
+
declare const enum_: <T extends LiteralSchema>(schemaEnum: Record<string, T>) => `enum ${T}`;
|
|
66
|
+
declare const validate: <T extends string>(schema: T, value: string) => boolean;
|
|
67
|
+
declare const openapi: <T extends string>(_schema: T) => SchemaObject;
|
|
68
|
+
|
|
69
|
+
export { MockSchemaValidator, any, array, bigint, boolean, date, enum_, literal, mockSchemaValidator, never, nullish, number, openapi, optional, schemify, string, symbol, union, unknown, validate };
|
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
import { SchemaObject } from 'openapi3-ts/oas31';
|
|
2
|
-
import { SchemaValidator } from '../../index';
|
|
3
|
-
import
|
|
4
|
-
|
|
2
|
+
import { S as SchemaValidator, L as LiteralSchema, P as ParseResult } from '../../index-V6qNDTlr.js';
|
|
3
|
+
import '@forklaunch/common';
|
|
4
|
+
import '@sinclair/typebox';
|
|
5
|
+
import '@sinclair/typebox/compiler';
|
|
6
|
+
import 'zod';
|
|
7
|
+
|
|
8
|
+
declare module '@forklaunch/validator' {
|
|
5
9
|
interface SchemaResolve<T> {
|
|
6
10
|
Mock: T;
|
|
7
11
|
}
|
|
@@ -13,7 +17,7 @@ type RecursiveUnion<T extends readonly string[]> = T extends readonly [
|
|
|
13
17
|
infer F extends string,
|
|
14
18
|
...infer R extends readonly string[]
|
|
15
19
|
] ? R extends [] ? F : `${F} | ${RecursiveUnion<R>}` : '';
|
|
16
|
-
|
|
20
|
+
declare class MockSchemaValidator implements SchemaValidator<(<T extends string>(schema: T) => T), <T extends string>(schema: T) => T, <T extends string>(schema: T) => `optional ${T}`, <T extends string>(schema: T) => `array ${T}`, <T extends readonly string[]>(schemas: T) => RecursiveUnion<T>, <T extends LiteralSchema>(schema: T) => `literal ${T}`, <T extends LiteralSchema>(schemaEnum: Record<string, T>) => `enum ${T}`, (value: unknown) => value is string, <T extends string>(schema: T, value: string) => boolean, <T extends string>(schema: T, value: string) => ParseResult<T>, <T extends string>(schema: T) => SchemaObject> {
|
|
17
21
|
_Type: 'Mock';
|
|
18
22
|
_SchemaCatchall: string;
|
|
19
23
|
_ValidSchemaObject: string;
|
|
@@ -42,24 +46,24 @@ export declare class MockSchemaValidator implements SchemaValidator<(<T extends
|
|
|
42
46
|
parse<T extends string>(schema: T, value: string): ParseResult<T>;
|
|
43
47
|
openapi<T extends string>(_schema: T): SchemaObject;
|
|
44
48
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
49
|
+
declare const mockSchemaValidator: MockSchemaValidator;
|
|
50
|
+
declare const string: string;
|
|
51
|
+
declare const number: string;
|
|
52
|
+
declare const bigint: string;
|
|
53
|
+
declare const boolean: string;
|
|
54
|
+
declare const date: string;
|
|
55
|
+
declare const symbol: string;
|
|
56
|
+
declare const nullish: string;
|
|
57
|
+
declare const any: string;
|
|
58
|
+
declare const unknown: string;
|
|
59
|
+
declare const never: string;
|
|
60
|
+
declare const schemify: <T extends string>(schema: T) => T;
|
|
61
|
+
declare const optional: <T extends string>(schema: T) => `optional ${T}`;
|
|
62
|
+
declare const array: <T extends string>(schema: T) => `array ${T}`;
|
|
63
|
+
declare const union: <T extends readonly string[]>(schemas: T) => RecursiveUnion<T>;
|
|
64
|
+
declare const literal: <T extends LiteralSchema>(schema: T) => `literal ${T}`;
|
|
65
|
+
declare const enum_: <T extends LiteralSchema>(schemaEnum: Record<string, T>) => `enum ${T}`;
|
|
66
|
+
declare const validate: <T extends string>(schema: T, value: string) => boolean;
|
|
67
|
+
declare const openapi: <T extends string>(_schema: T) => SchemaObject;
|
|
68
|
+
|
|
69
|
+
export { MockSchemaValidator, any, array, bigint, boolean, date, enum_, literal, mockSchemaValidator, never, nullish, number, openapi, optional, schemify, string, symbol, union, unknown, validate };
|