@forklaunch/validator 0.5.3 → 0.6.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/__test__/utils/mockSchemaValidator.d.mts +12 -2
- package/lib/__test__/utils/mockSchemaValidator.d.ts +12 -2
- package/lib/__test__/utils/mockSchemaValidator.js +17 -2
- package/lib/__test__/utils/mockSchemaValidator.mjs +17 -2
- package/lib/index.d.mts +2 -2
- package/lib/index.d.ts +2 -2
- package/lib/{schema.types-BL6n8u4w.d.mts → schema.types-MvSNOCj2.d.mts} +50 -16
- package/lib/{schema.types-BL6n8u4w.d.ts → schema.types-MvSNOCj2.d.ts} +50 -16
- package/lib/src/typebox/index.d.mts +15 -7
- package/lib/src/typebox/index.d.ts +15 -7
- package/lib/src/typebox/index.js +133 -42
- package/lib/src/typebox/index.mjs +132 -42
- package/lib/src/zod/index.d.mts +16 -8
- package/lib/src/zod/index.d.ts +16 -8
- package/lib/src/zod/index.js +138 -21
- package/lib/src/zod/index.mjs +137 -22
- package/package.json +13 -12
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
// src/typebox/typeboxSchemaValidator.ts
|
|
2
|
+
import { InMemoryFile } from "@forklaunch/common";
|
|
2
3
|
import {
|
|
4
|
+
FormatRegistry,
|
|
3
5
|
Kind,
|
|
4
6
|
KindGuard,
|
|
5
7
|
Type
|
|
@@ -11,6 +13,7 @@ import {
|
|
|
11
13
|
ValueErrorType
|
|
12
14
|
} from "@sinclair/typebox/errors";
|
|
13
15
|
import { Value } from "@sinclair/typebox/value";
|
|
16
|
+
FormatRegistry.Set("binary", (value) => typeof value === "string");
|
|
14
17
|
SetErrorFunction((params) => {
|
|
15
18
|
switch (params.errorType) {
|
|
16
19
|
case ValueErrorType.Union:
|
|
@@ -26,18 +29,27 @@ var TypeboxSchemaValidator = class {
|
|
|
26
29
|
_Type = "TypeBox";
|
|
27
30
|
_SchemaCatchall;
|
|
28
31
|
_ValidSchemaObject;
|
|
29
|
-
string = Type.String(
|
|
32
|
+
string = Type.String({
|
|
33
|
+
example: "a string",
|
|
34
|
+
title: "String"
|
|
35
|
+
});
|
|
30
36
|
uuid = Type.String({
|
|
31
37
|
pattern: "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$",
|
|
32
|
-
errorType: "uuid"
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
pattern: "^[a-zA-Z][a-zA-Z\\d+-.]*:[^\\s]*$",
|
|
36
|
-
errorType: "uri"
|
|
38
|
+
errorType: "uuid",
|
|
39
|
+
example: "a8b2c3d4-e5f6-g7h8-i9j0-k1l2m3n4o5p6",
|
|
40
|
+
title: "UUID"
|
|
37
41
|
});
|
|
38
42
|
email = Type.String({
|
|
39
43
|
pattern: `(?:[a-z0-9!#$%&'*+/=?^_{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_{|}~-]+)*|"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)])`,
|
|
40
|
-
errorType: "email"
|
|
44
|
+
errorType: "email",
|
|
45
|
+
example: "a@b.com",
|
|
46
|
+
title: "Email"
|
|
47
|
+
});
|
|
48
|
+
uri = Type.String({
|
|
49
|
+
pattern: "^[a-zA-Z][a-zA-Z\\d+-.]*:[^\\s]*$",
|
|
50
|
+
errorType: "uri",
|
|
51
|
+
example: "https://forklaunch.com",
|
|
52
|
+
title: "URI"
|
|
41
53
|
});
|
|
42
54
|
number = Type.Transform(
|
|
43
55
|
Type.Union(
|
|
@@ -46,12 +58,13 @@ var TypeboxSchemaValidator = class {
|
|
|
46
58
|
Type.String({ pattern: "^[0-9]+$" }),
|
|
47
59
|
Type.Boolean(),
|
|
48
60
|
Type.Null(),
|
|
49
|
-
Type.
|
|
50
|
-
Type.
|
|
61
|
+
Type.BigInt(),
|
|
62
|
+
Type.Date()
|
|
51
63
|
],
|
|
52
64
|
{
|
|
53
65
|
errorType: "number-like",
|
|
54
|
-
|
|
66
|
+
example: 123,
|
|
67
|
+
title: "Number"
|
|
55
68
|
}
|
|
56
69
|
)
|
|
57
70
|
).Decode((value) => {
|
|
@@ -70,19 +83,23 @@ var TypeboxSchemaValidator = class {
|
|
|
70
83
|
[
|
|
71
84
|
Type.BigInt(),
|
|
72
85
|
Type.Number(),
|
|
73
|
-
Type.String({ pattern: "^[0-9]
|
|
86
|
+
Type.String({ pattern: "^[0-9]+n?$" }),
|
|
74
87
|
Type.Boolean(),
|
|
75
88
|
Type.Date()
|
|
76
89
|
],
|
|
77
90
|
{
|
|
78
91
|
errorType: "BigInt-like",
|
|
79
|
-
|
|
92
|
+
example: 123n,
|
|
93
|
+
title: "BigInt"
|
|
80
94
|
}
|
|
81
95
|
)
|
|
82
96
|
).Decode((value) => {
|
|
83
97
|
if (typeof value !== "bigint") {
|
|
84
98
|
try {
|
|
85
|
-
|
|
99
|
+
if (value instanceof Date) {
|
|
100
|
+
return BigInt(value.getTime());
|
|
101
|
+
}
|
|
102
|
+
return BigInt(value);
|
|
86
103
|
} catch {
|
|
87
104
|
throw new Error("Invalid bigint");
|
|
88
105
|
}
|
|
@@ -99,7 +116,8 @@ var TypeboxSchemaValidator = class {
|
|
|
99
116
|
],
|
|
100
117
|
{
|
|
101
118
|
errorType: "boolean-like",
|
|
102
|
-
|
|
119
|
+
example: true,
|
|
120
|
+
title: "Boolean"
|
|
103
121
|
}
|
|
104
122
|
)
|
|
105
123
|
).Decode((value) => {
|
|
@@ -113,38 +131,80 @@ var TypeboxSchemaValidator = class {
|
|
|
113
131
|
date = Type.Transform(
|
|
114
132
|
Type.Union(
|
|
115
133
|
[
|
|
116
|
-
Type.Date(),
|
|
117
|
-
Type.Number(),
|
|
118
134
|
Type.String({
|
|
119
135
|
pattern: "^\\d{4}(-\\d{2}){0,2}(T\\d{2}:\\d{2}(:\\d{2}(\\.\\d{1,3})?)?(Z|([+-]\\d{2}:\\d{2}))?)?$|^\\d{1,2}\\/\\d{1,2}\\/\\d{4}$|^\\d{4}\\/\\d{1,2}\\/\\d{1,2}$|^\\d+$"
|
|
120
136
|
}),
|
|
121
|
-
Type.
|
|
122
|
-
Type.
|
|
137
|
+
Type.Number(),
|
|
138
|
+
Type.Date()
|
|
123
139
|
],
|
|
124
140
|
{
|
|
125
141
|
errorType: "date",
|
|
126
|
-
|
|
142
|
+
example: "2025-05-16T21:13:04.123Z",
|
|
143
|
+
title: "Date"
|
|
127
144
|
}
|
|
128
145
|
)
|
|
129
146
|
).Decode((value) => {
|
|
130
|
-
if (
|
|
131
|
-
|
|
132
|
-
return /* @__PURE__ */ new Date(value ? 1 : 0);
|
|
133
|
-
}
|
|
134
|
-
return new Date(value);
|
|
147
|
+
if (value === null || typeof value === "boolean") {
|
|
148
|
+
return /* @__PURE__ */ new Date(value ? 1 : 0);
|
|
135
149
|
}
|
|
136
|
-
return value;
|
|
137
|
-
}).Encode((value) => new Date(value));
|
|
138
|
-
symbol = Type.Symbol(
|
|
150
|
+
return new Date(value);
|
|
151
|
+
}).Encode((value) => new Date(value).toISOString());
|
|
152
|
+
symbol = Type.Symbol({
|
|
153
|
+
title: "Symbol"
|
|
154
|
+
});
|
|
139
155
|
nullish = Type.Union([Type.Void(), Type.Null(), Type.Undefined()], {
|
|
140
|
-
errorType: "nullish"
|
|
156
|
+
errorType: "nullish",
|
|
157
|
+
type: "null",
|
|
158
|
+
example: "null",
|
|
159
|
+
title: "Nullish"
|
|
160
|
+
});
|
|
161
|
+
void = Type.Void({
|
|
162
|
+
type: "null",
|
|
163
|
+
example: "void",
|
|
164
|
+
title: "Void"
|
|
141
165
|
});
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
166
|
+
null = Type.Null({
|
|
167
|
+
type: "null",
|
|
168
|
+
example: "null",
|
|
169
|
+
title: "Null"
|
|
170
|
+
});
|
|
171
|
+
undefined = Type.Undefined({
|
|
172
|
+
type: "null",
|
|
173
|
+
example: "undefined",
|
|
174
|
+
title: "Undefined"
|
|
175
|
+
});
|
|
176
|
+
any = Type.Any({
|
|
177
|
+
type: "object",
|
|
178
|
+
example: "any",
|
|
179
|
+
title: "Any"
|
|
180
|
+
});
|
|
181
|
+
unknown = Type.Unknown({
|
|
182
|
+
type: "object",
|
|
183
|
+
example: "unknown",
|
|
184
|
+
title: "Unknown"
|
|
185
|
+
});
|
|
186
|
+
never = Type.Never({
|
|
187
|
+
type: "null",
|
|
188
|
+
example: "never",
|
|
189
|
+
title: "Never"
|
|
190
|
+
});
|
|
191
|
+
binary = Type.Transform(
|
|
192
|
+
Type.String({
|
|
193
|
+
errorType: "binary",
|
|
194
|
+
format: "binary",
|
|
195
|
+
example: "a utf-8 encodable string",
|
|
196
|
+
title: "Binary"
|
|
197
|
+
})
|
|
198
|
+
).Decode(Buffer.from).Encode((value) => value.toString());
|
|
199
|
+
file = (name, type) => Type.Transform(
|
|
200
|
+
Type.String({
|
|
201
|
+
errorType: "binary",
|
|
202
|
+
format: "binary",
|
|
203
|
+
contentMediaType: type,
|
|
204
|
+
example: "a utf-8 encodable string",
|
|
205
|
+
title: "File"
|
|
206
|
+
})
|
|
207
|
+
).Decode((value) => new InMemoryFile(value, name, { type })).Encode((value) => value.content);
|
|
148
208
|
/**
|
|
149
209
|
* Extracts the error type of a schema for error messages.
|
|
150
210
|
*
|
|
@@ -174,12 +234,12 @@ var TypeboxSchemaValidator = class {
|
|
|
174
234
|
* @returns {TResolve<T>} The resolved schema.
|
|
175
235
|
*/
|
|
176
236
|
schemify(schema) {
|
|
177
|
-
if (typeof schema === "string" || typeof schema === "number" || typeof schema === "boolean") {
|
|
178
|
-
return Type.Literal(schema);
|
|
179
|
-
}
|
|
180
237
|
if (KindGuard.IsSchema(schema) || schema instanceof TypeCheck) {
|
|
181
238
|
return schema;
|
|
182
239
|
}
|
|
240
|
+
if (typeof schema === "string" || typeof schema === "number" || typeof schema === "boolean") {
|
|
241
|
+
return Type.Literal(schema);
|
|
242
|
+
}
|
|
183
243
|
const newSchema = {};
|
|
184
244
|
Object.getOwnPropertyNames(schema).forEach((key) => {
|
|
185
245
|
if (KindGuard.IsSchema(schema[key])) {
|
|
@@ -288,6 +348,15 @@ var TypeboxSchemaValidator = class {
|
|
|
288
348
|
isSchema(value) {
|
|
289
349
|
return KindGuard.IsSchema(value);
|
|
290
350
|
}
|
|
351
|
+
/**
|
|
352
|
+
* Check if a value is an instance of a TypeBox schema.
|
|
353
|
+
* @param {object} value - The value to check.
|
|
354
|
+
* @param {TCatchall} type - The schema to check against.
|
|
355
|
+
* @returns {boolean} True if the value is an instance of the schema.
|
|
356
|
+
*/
|
|
357
|
+
isInstanceOf(value, type) {
|
|
358
|
+
return typeof value === "object" && value != null && Kind in value && value[Kind] === type[Kind];
|
|
359
|
+
}
|
|
291
360
|
/**
|
|
292
361
|
* Validate a value against a schema.
|
|
293
362
|
*
|
|
@@ -361,21 +430,38 @@ var TypeboxSchemaValidator = class {
|
|
|
361
430
|
* @returns {SchemaObject} The OpenAPI schema object.
|
|
362
431
|
*/
|
|
363
432
|
openapi(schema) {
|
|
364
|
-
|
|
365
|
-
if (
|
|
366
|
-
|
|
433
|
+
let schemified = this.schemify(schema);
|
|
434
|
+
if (KindGuard.IsDate(schemified)) {
|
|
435
|
+
schemified = Type.String({
|
|
436
|
+
format: "date-time"
|
|
437
|
+
});
|
|
367
438
|
}
|
|
368
439
|
const newSchema = Object.assign({}, schemified);
|
|
369
440
|
if (Object.hasOwn(newSchema, "properties")) {
|
|
370
|
-
newSchema.properties = { ...schemified.properties };
|
|
371
441
|
if (newSchema.properties) {
|
|
372
|
-
Object.entries(
|
|
442
|
+
Object.entries({ ...schemified.properties }).forEach(([key, value]) => {
|
|
373
443
|
if (KindGuard.IsSchema(value) && newSchema.properties) {
|
|
374
444
|
newSchema.properties[key] = this.openapi(value);
|
|
375
445
|
}
|
|
376
446
|
});
|
|
377
447
|
}
|
|
378
448
|
}
|
|
449
|
+
if (Object.hasOwn(newSchema, "items")) {
|
|
450
|
+
newSchema.items = this.openapi(newSchema.items);
|
|
451
|
+
}
|
|
452
|
+
if (Array.isArray(newSchema.anyOf)) {
|
|
453
|
+
newSchema.anyOf = newSchema.anyOf.map(
|
|
454
|
+
(item) => this.openapi(item)
|
|
455
|
+
);
|
|
456
|
+
}
|
|
457
|
+
if (Array.isArray(newSchema.oneOf)) {
|
|
458
|
+
newSchema.oneOf = newSchema.oneOf.map(
|
|
459
|
+
(item) => this.openapi(item)
|
|
460
|
+
);
|
|
461
|
+
}
|
|
462
|
+
if ("errorType" in newSchema) {
|
|
463
|
+
delete newSchema["errorType"];
|
|
464
|
+
}
|
|
379
465
|
return newSchema;
|
|
380
466
|
}
|
|
381
467
|
};
|
|
@@ -399,6 +485,8 @@ var undefined_ = StaticSchemaValidator.undefined;
|
|
|
399
485
|
var any = StaticSchemaValidator.any;
|
|
400
486
|
var unknown = StaticSchemaValidator.unknown;
|
|
401
487
|
var never = StaticSchemaValidator.never;
|
|
488
|
+
var binary = StaticSchemaValidator.binary;
|
|
489
|
+
var file = StaticSchemaValidator.file;
|
|
402
490
|
var schemify = StaticSchemaValidator.schemify.bind(
|
|
403
491
|
StaticSchemaValidator
|
|
404
492
|
);
|
|
@@ -433,10 +521,12 @@ export {
|
|
|
433
521
|
any,
|
|
434
522
|
array,
|
|
435
523
|
bigint,
|
|
524
|
+
binary,
|
|
436
525
|
boolean,
|
|
437
526
|
date,
|
|
438
527
|
email,
|
|
439
528
|
enum_,
|
|
529
|
+
file,
|
|
440
530
|
function_,
|
|
441
531
|
isSchema,
|
|
442
532
|
literal,
|
package/lib/src/zod/index.d.mts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import * as openapi3_ts_oas31 from 'openapi3-ts/oas31';
|
|
2
|
-
import { Z as ZodSchemaValidator, p as ZodIdiomaticSchema, q as ZodResolve, r as ZodUnionContainer, s as UnionZodResolve, L as LiteralSchema, t as ZodTupleContainer, u as TupleZodResolve, v as ZodRecordKey, w as ZodCatchall, a as ParseResult } from '../../schema.types-
|
|
3
|
-
export { C as UnboxedZodObjectSchema,
|
|
2
|
+
import { Z as ZodSchemaValidator, p as ZodIdiomaticSchema, q as ZodResolve, r as ZodUnionContainer, s as UnionZodResolve, L as LiteralSchema, t as ZodTupleContainer, u as TupleZodResolve, v as ZodRecordKey, w as ZodCatchall, a as ParseResult, x as ZodSchemaTranslate } from '../../schema.types-MvSNOCj2.mjs';
|
|
3
|
+
export { C as UnboxedZodObjectSchema, B as ZodObject, z as ZodObjectShape, y as ZodOuterArray } from '../../schema.types-MvSNOCj2.mjs';
|
|
4
|
+
import * as _forklaunch_common from '@forklaunch/common';
|
|
4
5
|
import * as zod from 'zod';
|
|
5
|
-
import '@forklaunch/common';
|
|
6
6
|
import '@sinclair/typebox';
|
|
7
7
|
import '@sinclair/typebox/compiler';
|
|
8
8
|
|
|
@@ -30,11 +30,11 @@ declare const uri: zod.ZodString;
|
|
|
30
30
|
/**
|
|
31
31
|
* Zod schema definition for number type.
|
|
32
32
|
*/
|
|
33
|
-
declare const number: zod.
|
|
33
|
+
declare const number: zod.ZodEffects<zod.ZodNumber, number, unknown>;
|
|
34
34
|
/**
|
|
35
35
|
* Zod schema definition for bigint type.
|
|
36
36
|
*/
|
|
37
|
-
declare const bigint: zod.
|
|
37
|
+
declare const bigint: zod.ZodEffects<zod.ZodBigInt, bigint, unknown>;
|
|
38
38
|
/**
|
|
39
39
|
* Zod schema definition for boolean type.
|
|
40
40
|
*/
|
|
@@ -42,7 +42,7 @@ declare const boolean: zod.ZodEffects<zod.ZodBoolean, boolean, unknown>;
|
|
|
42
42
|
/**
|
|
43
43
|
* Zod schema definition for date type.
|
|
44
44
|
*/
|
|
45
|
-
declare const date: zod.
|
|
45
|
+
declare const date: zod.ZodEffects<zod.ZodDate, Date, unknown>;
|
|
46
46
|
/**
|
|
47
47
|
* Zod schema definition for symbol type.
|
|
48
48
|
*/
|
|
@@ -75,6 +75,14 @@ declare const unknown: zod.ZodUnknown;
|
|
|
75
75
|
* Zod schema definition for never type.
|
|
76
76
|
*/
|
|
77
77
|
declare const never: zod.ZodNever;
|
|
78
|
+
/**
|
|
79
|
+
* Zod schema definition for blob type.
|
|
80
|
+
*/
|
|
81
|
+
declare const binary: zod.ZodEffects<zod.ZodString, Buffer<ArrayBuffer>, string>;
|
|
82
|
+
/**
|
|
83
|
+
* Zod schema definition for file type.
|
|
84
|
+
*/
|
|
85
|
+
declare const file: (name: string, type: _forklaunch_common.MimeType) => zod.ZodEffects<zod.ZodString, File, string>;
|
|
78
86
|
/**
|
|
79
87
|
* Transforms valid schema into Zod schema.
|
|
80
88
|
*/
|
|
@@ -122,10 +130,10 @@ declare const validate: <T extends ZodIdiomaticSchema | ZodCatchall>(schema: T,
|
|
|
122
130
|
/**
|
|
123
131
|
* Parses a value to be conformant to a particular schema.
|
|
124
132
|
*/
|
|
125
|
-
declare const parse: <T extends ZodIdiomaticSchema | ZodCatchall>(schema: T, value: unknown) => ParseResult<ZodResolve<T
|
|
133
|
+
declare const parse: <T extends ZodIdiomaticSchema | ZodCatchall>(schema: T, value: unknown) => ParseResult<ZodSchemaTranslate<ZodResolve<T>>>;
|
|
126
134
|
/**
|
|
127
135
|
* Generates an OpenAPI schema object from a valid schema.
|
|
128
136
|
*/
|
|
129
137
|
declare const openapi: <T extends ZodIdiomaticSchema | ZodCatchall>(schema: T) => openapi3_ts_oas31.SchemaObject;
|
|
130
138
|
|
|
131
|
-
export { SchemaValidator, TupleZodResolve, UnionZodResolve, ZodCatchall, ZodIdiomaticSchema, ZodRecordKey, ZodResolve, ZodSchemaValidator, ZodTupleContainer, ZodUnionContainer, any, array, bigint, boolean, date, email, enum_, function_, isSchema, literal, never, null_, nullish, number, openapi, optional, parse, promise, record, schemify, string, symbol, undefined_, union, unknown, uri, uuid, validate, void_ };
|
|
139
|
+
export { SchemaValidator, TupleZodResolve, UnionZodResolve, ZodCatchall, ZodIdiomaticSchema, ZodRecordKey, ZodResolve, ZodSchemaTranslate, ZodSchemaValidator, ZodTupleContainer, ZodUnionContainer, any, array, bigint, binary, boolean, date, email, enum_, file, function_, isSchema, literal, never, null_, nullish, number, openapi, optional, parse, promise, record, schemify, string, symbol, undefined_, union, unknown, uri, uuid, validate, void_ };
|
package/lib/src/zod/index.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import * as openapi3_ts_oas31 from 'openapi3-ts/oas31';
|
|
2
|
-
import { Z as ZodSchemaValidator, p as ZodIdiomaticSchema, q as ZodResolve, r as ZodUnionContainer, s as UnionZodResolve, L as LiteralSchema, t as ZodTupleContainer, u as TupleZodResolve, v as ZodRecordKey, w as ZodCatchall, a as ParseResult } from '../../schema.types-
|
|
3
|
-
export { C as UnboxedZodObjectSchema,
|
|
2
|
+
import { Z as ZodSchemaValidator, p as ZodIdiomaticSchema, q as ZodResolve, r as ZodUnionContainer, s as UnionZodResolve, L as LiteralSchema, t as ZodTupleContainer, u as TupleZodResolve, v as ZodRecordKey, w as ZodCatchall, a as ParseResult, x as ZodSchemaTranslate } from '../../schema.types-MvSNOCj2.js';
|
|
3
|
+
export { C as UnboxedZodObjectSchema, B as ZodObject, z as ZodObjectShape, y as ZodOuterArray } from '../../schema.types-MvSNOCj2.js';
|
|
4
|
+
import * as _forklaunch_common from '@forklaunch/common';
|
|
4
5
|
import * as zod from 'zod';
|
|
5
|
-
import '@forklaunch/common';
|
|
6
6
|
import '@sinclair/typebox';
|
|
7
7
|
import '@sinclair/typebox/compiler';
|
|
8
8
|
|
|
@@ -30,11 +30,11 @@ declare const uri: zod.ZodString;
|
|
|
30
30
|
/**
|
|
31
31
|
* Zod schema definition for number type.
|
|
32
32
|
*/
|
|
33
|
-
declare const number: zod.
|
|
33
|
+
declare const number: zod.ZodEffects<zod.ZodNumber, number, unknown>;
|
|
34
34
|
/**
|
|
35
35
|
* Zod schema definition for bigint type.
|
|
36
36
|
*/
|
|
37
|
-
declare const bigint: zod.
|
|
37
|
+
declare const bigint: zod.ZodEffects<zod.ZodBigInt, bigint, unknown>;
|
|
38
38
|
/**
|
|
39
39
|
* Zod schema definition for boolean type.
|
|
40
40
|
*/
|
|
@@ -42,7 +42,7 @@ declare const boolean: zod.ZodEffects<zod.ZodBoolean, boolean, unknown>;
|
|
|
42
42
|
/**
|
|
43
43
|
* Zod schema definition for date type.
|
|
44
44
|
*/
|
|
45
|
-
declare const date: zod.
|
|
45
|
+
declare const date: zod.ZodEffects<zod.ZodDate, Date, unknown>;
|
|
46
46
|
/**
|
|
47
47
|
* Zod schema definition for symbol type.
|
|
48
48
|
*/
|
|
@@ -75,6 +75,14 @@ declare const unknown: zod.ZodUnknown;
|
|
|
75
75
|
* Zod schema definition for never type.
|
|
76
76
|
*/
|
|
77
77
|
declare const never: zod.ZodNever;
|
|
78
|
+
/**
|
|
79
|
+
* Zod schema definition for blob type.
|
|
80
|
+
*/
|
|
81
|
+
declare const binary: zod.ZodEffects<zod.ZodString, Buffer<ArrayBuffer>, string>;
|
|
82
|
+
/**
|
|
83
|
+
* Zod schema definition for file type.
|
|
84
|
+
*/
|
|
85
|
+
declare const file: (name: string, type: _forklaunch_common.MimeType) => zod.ZodEffects<zod.ZodString, File, string>;
|
|
78
86
|
/**
|
|
79
87
|
* Transforms valid schema into Zod schema.
|
|
80
88
|
*/
|
|
@@ -122,10 +130,10 @@ declare const validate: <T extends ZodIdiomaticSchema | ZodCatchall>(schema: T,
|
|
|
122
130
|
/**
|
|
123
131
|
* Parses a value to be conformant to a particular schema.
|
|
124
132
|
*/
|
|
125
|
-
declare const parse: <T extends ZodIdiomaticSchema | ZodCatchall>(schema: T, value: unknown) => ParseResult<ZodResolve<T
|
|
133
|
+
declare const parse: <T extends ZodIdiomaticSchema | ZodCatchall>(schema: T, value: unknown) => ParseResult<ZodSchemaTranslate<ZodResolve<T>>>;
|
|
126
134
|
/**
|
|
127
135
|
* Generates an OpenAPI schema object from a valid schema.
|
|
128
136
|
*/
|
|
129
137
|
declare const openapi: <T extends ZodIdiomaticSchema | ZodCatchall>(schema: T) => openapi3_ts_oas31.SchemaObject;
|
|
130
138
|
|
|
131
|
-
export { SchemaValidator, TupleZodResolve, UnionZodResolve, ZodCatchall, ZodIdiomaticSchema, ZodRecordKey, ZodResolve, ZodSchemaValidator, ZodTupleContainer, ZodUnionContainer, any, array, bigint, boolean, date, email, enum_, function_, isSchema, literal, never, null_, nullish, number, openapi, optional, parse, promise, record, schemify, string, symbol, undefined_, union, unknown, uri, uuid, validate, void_ };
|
|
139
|
+
export { SchemaValidator, TupleZodResolve, UnionZodResolve, ZodCatchall, ZodIdiomaticSchema, ZodRecordKey, ZodResolve, ZodSchemaTranslate, ZodSchemaValidator, ZodTupleContainer, ZodUnionContainer, any, array, bigint, binary, boolean, date, email, enum_, file, function_, isSchema, literal, never, null_, nullish, number, openapi, optional, parse, promise, record, schemify, string, symbol, undefined_, union, unknown, uri, uuid, validate, void_ };
|
package/lib/src/zod/index.js
CHANGED
|
@@ -24,10 +24,12 @@ __export(zod_exports, {
|
|
|
24
24
|
any: () => any,
|
|
25
25
|
array: () => array,
|
|
26
26
|
bigint: () => bigint,
|
|
27
|
+
binary: () => binary,
|
|
27
28
|
boolean: () => boolean,
|
|
28
29
|
date: () => date,
|
|
29
30
|
email: () => email,
|
|
30
31
|
enum_: () => enum_,
|
|
32
|
+
file: () => file,
|
|
31
33
|
function_: () => function_,
|
|
32
34
|
isSchema: () => isSchema,
|
|
33
35
|
literal: () => literal,
|
|
@@ -56,50 +58,152 @@ module.exports = __toCommonJS(zod_exports);
|
|
|
56
58
|
// src/zod/zodSchemaValidator.ts
|
|
57
59
|
var import_zod_openapi = require("@anatine/zod-openapi");
|
|
58
60
|
var import_zod = require("zod");
|
|
61
|
+
(0, import_zod_openapi.extendZodWithOpenApi)(import_zod.z);
|
|
59
62
|
var ZodSchemaValidator = class {
|
|
60
63
|
_Type = "Zod";
|
|
61
64
|
_SchemaCatchall;
|
|
62
65
|
_ValidSchemaObject;
|
|
63
|
-
string = import_zod.z.string()
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
66
|
+
string = import_zod.z.string().openapi({
|
|
67
|
+
title: "String",
|
|
68
|
+
example: "a string"
|
|
69
|
+
});
|
|
70
|
+
uuid = import_zod.z.string().uuid().openapi({
|
|
71
|
+
title: "UUID",
|
|
72
|
+
format: "uuid",
|
|
73
|
+
pattern: "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$",
|
|
74
|
+
example: "a8b2c3d4-e5f6-g7h8-i9j0-k1l2m3n4o5p6"
|
|
75
|
+
});
|
|
76
|
+
email = import_zod.z.string().email().openapi({
|
|
77
|
+
title: "Email",
|
|
78
|
+
format: "email",
|
|
79
|
+
pattern: `(?:[a-z0-9!#$%&'*+/=?^_{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_{|}~-]+)*|"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)])`,
|
|
80
|
+
example: "a@b.com"
|
|
81
|
+
});
|
|
82
|
+
uri = import_zod.z.string().url().openapi({
|
|
83
|
+
title: "URI",
|
|
84
|
+
format: "uri",
|
|
85
|
+
pattern: "^[a-zA-Z][a-zA-Z\\d+-.]*:[^\\s]*$",
|
|
86
|
+
example: "https://forklaunch.com"
|
|
87
|
+
});
|
|
88
|
+
number = import_zod.z.preprocess((value) => {
|
|
68
89
|
try {
|
|
69
90
|
return Number(value);
|
|
70
91
|
} catch {
|
|
71
92
|
return value;
|
|
72
93
|
}
|
|
73
|
-
}
|
|
74
|
-
|
|
94
|
+
}, import_zod.z.number()).openapi({
|
|
95
|
+
title: "Number",
|
|
96
|
+
example: 123
|
|
97
|
+
});
|
|
98
|
+
bigint = import_zod.z.preprocess((value) => {
|
|
75
99
|
try {
|
|
76
|
-
|
|
100
|
+
if (value instanceof Date) {
|
|
101
|
+
return BigInt(value.getTime());
|
|
102
|
+
}
|
|
103
|
+
switch (typeof value) {
|
|
104
|
+
case "number":
|
|
105
|
+
case "string":
|
|
106
|
+
return BigInt(value);
|
|
107
|
+
case "boolean":
|
|
108
|
+
return BigInt(value ? 1 : 0);
|
|
109
|
+
default:
|
|
110
|
+
return value;
|
|
111
|
+
}
|
|
77
112
|
} catch {
|
|
78
113
|
return value;
|
|
79
114
|
}
|
|
80
|
-
}
|
|
115
|
+
}, import_zod.z.bigint()).openapi({
|
|
116
|
+
title: "BigInt",
|
|
117
|
+
type: "integer",
|
|
118
|
+
format: "int64",
|
|
119
|
+
example: 123n
|
|
120
|
+
});
|
|
81
121
|
boolean = import_zod.z.preprocess((val) => {
|
|
82
122
|
if (typeof val === "string") {
|
|
83
123
|
if (val.toLowerCase() === "true") return true;
|
|
84
124
|
if (val.toLowerCase() === "false") return false;
|
|
85
125
|
}
|
|
86
126
|
return val;
|
|
87
|
-
}, import_zod.z.boolean())
|
|
88
|
-
|
|
127
|
+
}, import_zod.z.boolean()).openapi({
|
|
128
|
+
title: "Boolean",
|
|
129
|
+
example: true
|
|
130
|
+
});
|
|
131
|
+
date = import_zod.z.preprocess((value) => {
|
|
89
132
|
try {
|
|
90
|
-
|
|
133
|
+
switch (typeof value) {
|
|
134
|
+
case "string":
|
|
135
|
+
return new Date(value);
|
|
136
|
+
case "number":
|
|
137
|
+
return new Date(value);
|
|
138
|
+
default:
|
|
139
|
+
return value;
|
|
140
|
+
}
|
|
91
141
|
} catch {
|
|
92
142
|
return value;
|
|
93
143
|
}
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
144
|
+
}, import_zod.z.date()).openapi({
|
|
145
|
+
title: "Date",
|
|
146
|
+
type: "string",
|
|
147
|
+
format: "date-time",
|
|
148
|
+
example: "2025-05-16T21:13:04.123Z"
|
|
149
|
+
});
|
|
150
|
+
symbol = import_zod.z.symbol().openapi({
|
|
151
|
+
title: "Symbol",
|
|
152
|
+
example: Symbol("symbol")
|
|
153
|
+
});
|
|
154
|
+
nullish = import_zod.z.union([import_zod.z.void(), import_zod.z.null(), import_zod.z.undefined()]).openapi({
|
|
155
|
+
title: "Nullish",
|
|
156
|
+
type: "null",
|
|
157
|
+
example: null
|
|
158
|
+
});
|
|
159
|
+
void = import_zod.z.void().openapi({
|
|
160
|
+
title: "Void",
|
|
161
|
+
type: "null",
|
|
162
|
+
example: void 0
|
|
163
|
+
});
|
|
164
|
+
null = import_zod.z.null().openapi({
|
|
165
|
+
title: "Null",
|
|
166
|
+
type: "null",
|
|
167
|
+
example: null
|
|
168
|
+
});
|
|
169
|
+
undefined = import_zod.z.undefined().openapi({
|
|
170
|
+
title: "Undefined",
|
|
171
|
+
type: "null",
|
|
172
|
+
example: void 0
|
|
173
|
+
});
|
|
174
|
+
any = import_zod.z.any().openapi({
|
|
175
|
+
title: "Any",
|
|
176
|
+
type: "object",
|
|
177
|
+
example: "any"
|
|
178
|
+
});
|
|
179
|
+
unknown = import_zod.z.unknown().openapi({
|
|
180
|
+
title: "Unknown",
|
|
181
|
+
type: "object",
|
|
182
|
+
example: "unknown"
|
|
183
|
+
});
|
|
184
|
+
never = import_zod.z.never().openapi({
|
|
185
|
+
title: "Never",
|
|
186
|
+
type: "null",
|
|
187
|
+
example: "never"
|
|
188
|
+
});
|
|
189
|
+
binary = import_zod.z.string().transform(Buffer.from).openapi({
|
|
190
|
+
title: "Binary",
|
|
191
|
+
type: "string",
|
|
192
|
+
format: "binary",
|
|
193
|
+
example: "a utf-8 encodable string"
|
|
194
|
+
});
|
|
195
|
+
file = (name, type) => import_zod.z.string().transform((val) => {
|
|
196
|
+
return new File([val], name, {
|
|
197
|
+
type,
|
|
198
|
+
lastModified: Date.now()
|
|
199
|
+
});
|
|
200
|
+
}).openapi({
|
|
201
|
+
title: "File",
|
|
202
|
+
type: "string",
|
|
203
|
+
format: "binary",
|
|
204
|
+
contentMediaType: type,
|
|
205
|
+
example: "a utf-8 encodable string"
|
|
206
|
+
});
|
|
103
207
|
/**
|
|
104
208
|
* Compiles schema if this exists, for optimal performance.
|
|
105
209
|
*
|
|
@@ -216,6 +320,15 @@ var ZodSchemaValidator = class {
|
|
|
216
320
|
isSchema(value) {
|
|
217
321
|
return value instanceof import_zod.ZodType;
|
|
218
322
|
}
|
|
323
|
+
/**
|
|
324
|
+
* Checks if a value is an instance of a Zod schema.
|
|
325
|
+
* @param {object} value - The value to check.
|
|
326
|
+
* @param {ZodType} type - The schema to check against.
|
|
327
|
+
* @returns {boolean} True if the value is an instance of the schema.
|
|
328
|
+
*/
|
|
329
|
+
isInstanceOf(value, type) {
|
|
330
|
+
return this.isSchema(value) && type._def.typeName === value._def.typeName;
|
|
331
|
+
}
|
|
219
332
|
/**
|
|
220
333
|
* Validate a value against a schema.
|
|
221
334
|
* @param {ZodCatchall} schema - The schema to validate against.
|
|
@@ -291,6 +404,8 @@ var undefined_ = StaticSchemaValidator.undefined;
|
|
|
291
404
|
var any = StaticSchemaValidator.any;
|
|
292
405
|
var unknown = StaticSchemaValidator.unknown;
|
|
293
406
|
var never = StaticSchemaValidator.never;
|
|
407
|
+
var binary = StaticSchemaValidator.binary;
|
|
408
|
+
var file = StaticSchemaValidator.file;
|
|
294
409
|
var schemify = StaticSchemaValidator.schemify.bind(
|
|
295
410
|
StaticSchemaValidator
|
|
296
411
|
);
|
|
@@ -326,10 +441,12 @@ var openapi = StaticSchemaValidator.openapi.bind(
|
|
|
326
441
|
any,
|
|
327
442
|
array,
|
|
328
443
|
bigint,
|
|
444
|
+
binary,
|
|
329
445
|
boolean,
|
|
330
446
|
date,
|
|
331
447
|
email,
|
|
332
448
|
enum_,
|
|
449
|
+
file,
|
|
333
450
|
function_,
|
|
334
451
|
isSchema,
|
|
335
452
|
literal,
|