@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
package/lib/src/zod/index.mjs
CHANGED
|
@@ -1,53 +1,155 @@
|
|
|
1
1
|
// src/zod/zodSchemaValidator.ts
|
|
2
|
-
import { generateSchema } from "@anatine/zod-openapi";
|
|
2
|
+
import { extendZodWithOpenApi, generateSchema } from "@anatine/zod-openapi";
|
|
3
3
|
import {
|
|
4
4
|
z,
|
|
5
5
|
ZodType
|
|
6
6
|
} from "zod";
|
|
7
|
+
extendZodWithOpenApi(z);
|
|
7
8
|
var ZodSchemaValidator = class {
|
|
8
9
|
_Type = "Zod";
|
|
9
10
|
_SchemaCatchall;
|
|
10
11
|
_ValidSchemaObject;
|
|
11
|
-
string = z.string()
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
12
|
+
string = z.string().openapi({
|
|
13
|
+
title: "String",
|
|
14
|
+
example: "a string"
|
|
15
|
+
});
|
|
16
|
+
uuid = z.string().uuid().openapi({
|
|
17
|
+
title: "UUID",
|
|
18
|
+
format: "uuid",
|
|
19
|
+
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}$",
|
|
20
|
+
example: "a8b2c3d4-e5f6-g7h8-i9j0-k1l2m3n4o5p6"
|
|
21
|
+
});
|
|
22
|
+
email = z.string().email().openapi({
|
|
23
|
+
title: "Email",
|
|
24
|
+
format: "email",
|
|
25
|
+
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])+)])`,
|
|
26
|
+
example: "a@b.com"
|
|
27
|
+
});
|
|
28
|
+
uri = z.string().url().openapi({
|
|
29
|
+
title: "URI",
|
|
30
|
+
format: "uri",
|
|
31
|
+
pattern: "^[a-zA-Z][a-zA-Z\\d+-.]*:[^\\s]*$",
|
|
32
|
+
example: "https://forklaunch.com"
|
|
33
|
+
});
|
|
34
|
+
number = z.preprocess((value) => {
|
|
16
35
|
try {
|
|
17
36
|
return Number(value);
|
|
18
37
|
} catch {
|
|
19
38
|
return value;
|
|
20
39
|
}
|
|
21
|
-
}
|
|
22
|
-
|
|
40
|
+
}, z.number()).openapi({
|
|
41
|
+
title: "Number",
|
|
42
|
+
example: 123
|
|
43
|
+
});
|
|
44
|
+
bigint = z.preprocess((value) => {
|
|
23
45
|
try {
|
|
24
|
-
|
|
46
|
+
if (value instanceof Date) {
|
|
47
|
+
return BigInt(value.getTime());
|
|
48
|
+
}
|
|
49
|
+
switch (typeof value) {
|
|
50
|
+
case "number":
|
|
51
|
+
case "string":
|
|
52
|
+
return BigInt(value);
|
|
53
|
+
case "boolean":
|
|
54
|
+
return BigInt(value ? 1 : 0);
|
|
55
|
+
default:
|
|
56
|
+
return value;
|
|
57
|
+
}
|
|
25
58
|
} catch {
|
|
26
59
|
return value;
|
|
27
60
|
}
|
|
28
|
-
}
|
|
61
|
+
}, z.bigint()).openapi({
|
|
62
|
+
title: "BigInt",
|
|
63
|
+
type: "integer",
|
|
64
|
+
format: "int64",
|
|
65
|
+
example: 123n
|
|
66
|
+
});
|
|
29
67
|
boolean = z.preprocess((val) => {
|
|
30
68
|
if (typeof val === "string") {
|
|
31
69
|
if (val.toLowerCase() === "true") return true;
|
|
32
70
|
if (val.toLowerCase() === "false") return false;
|
|
33
71
|
}
|
|
34
72
|
return val;
|
|
35
|
-
}, z.boolean())
|
|
36
|
-
|
|
73
|
+
}, z.boolean()).openapi({
|
|
74
|
+
title: "Boolean",
|
|
75
|
+
example: true
|
|
76
|
+
});
|
|
77
|
+
date = z.preprocess((value) => {
|
|
37
78
|
try {
|
|
38
|
-
|
|
79
|
+
switch (typeof value) {
|
|
80
|
+
case "string":
|
|
81
|
+
return new Date(value);
|
|
82
|
+
case "number":
|
|
83
|
+
return new Date(value);
|
|
84
|
+
default:
|
|
85
|
+
return value;
|
|
86
|
+
}
|
|
39
87
|
} catch {
|
|
40
88
|
return value;
|
|
41
89
|
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
90
|
+
}, z.date()).openapi({
|
|
91
|
+
title: "Date",
|
|
92
|
+
type: "string",
|
|
93
|
+
format: "date-time",
|
|
94
|
+
example: "2025-05-16T21:13:04.123Z"
|
|
95
|
+
});
|
|
96
|
+
symbol = z.symbol().openapi({
|
|
97
|
+
title: "Symbol",
|
|
98
|
+
example: Symbol("symbol")
|
|
99
|
+
});
|
|
100
|
+
nullish = z.union([z.void(), z.null(), z.undefined()]).openapi({
|
|
101
|
+
title: "Nullish",
|
|
102
|
+
type: "null",
|
|
103
|
+
example: null
|
|
104
|
+
});
|
|
105
|
+
void = z.void().openapi({
|
|
106
|
+
title: "Void",
|
|
107
|
+
type: "null",
|
|
108
|
+
example: void 0
|
|
109
|
+
});
|
|
110
|
+
null = z.null().openapi({
|
|
111
|
+
title: "Null",
|
|
112
|
+
type: "null",
|
|
113
|
+
example: null
|
|
114
|
+
});
|
|
115
|
+
undefined = z.undefined().openapi({
|
|
116
|
+
title: "Undefined",
|
|
117
|
+
type: "null",
|
|
118
|
+
example: void 0
|
|
119
|
+
});
|
|
120
|
+
any = z.any().openapi({
|
|
121
|
+
title: "Any",
|
|
122
|
+
type: "object",
|
|
123
|
+
example: "any"
|
|
124
|
+
});
|
|
125
|
+
unknown = z.unknown().openapi({
|
|
126
|
+
title: "Unknown",
|
|
127
|
+
type: "object",
|
|
128
|
+
example: "unknown"
|
|
129
|
+
});
|
|
130
|
+
never = z.never().openapi({
|
|
131
|
+
title: "Never",
|
|
132
|
+
type: "null",
|
|
133
|
+
example: "never"
|
|
134
|
+
});
|
|
135
|
+
binary = z.string().transform(Buffer.from).openapi({
|
|
136
|
+
title: "Binary",
|
|
137
|
+
type: "string",
|
|
138
|
+
format: "binary",
|
|
139
|
+
example: "a utf-8 encodable string"
|
|
140
|
+
});
|
|
141
|
+
file = (name, type) => z.string().transform((val) => {
|
|
142
|
+
return new File([val], name, {
|
|
143
|
+
type,
|
|
144
|
+
lastModified: Date.now()
|
|
145
|
+
});
|
|
146
|
+
}).openapi({
|
|
147
|
+
title: "File",
|
|
148
|
+
type: "string",
|
|
149
|
+
format: "binary",
|
|
150
|
+
contentMediaType: type,
|
|
151
|
+
example: "a utf-8 encodable string"
|
|
152
|
+
});
|
|
51
153
|
/**
|
|
52
154
|
* Compiles schema if this exists, for optimal performance.
|
|
53
155
|
*
|
|
@@ -164,6 +266,15 @@ var ZodSchemaValidator = class {
|
|
|
164
266
|
isSchema(value) {
|
|
165
267
|
return value instanceof ZodType;
|
|
166
268
|
}
|
|
269
|
+
/**
|
|
270
|
+
* Checks if a value is an instance of a Zod schema.
|
|
271
|
+
* @param {object} value - The value to check.
|
|
272
|
+
* @param {ZodType} type - The schema to check against.
|
|
273
|
+
* @returns {boolean} True if the value is an instance of the schema.
|
|
274
|
+
*/
|
|
275
|
+
isInstanceOf(value, type) {
|
|
276
|
+
return this.isSchema(value) && type._def.typeName === value._def.typeName;
|
|
277
|
+
}
|
|
167
278
|
/**
|
|
168
279
|
* Validate a value against a schema.
|
|
169
280
|
* @param {ZodCatchall} schema - The schema to validate against.
|
|
@@ -239,6 +350,8 @@ var undefined_ = StaticSchemaValidator.undefined;
|
|
|
239
350
|
var any = StaticSchemaValidator.any;
|
|
240
351
|
var unknown = StaticSchemaValidator.unknown;
|
|
241
352
|
var never = StaticSchemaValidator.never;
|
|
353
|
+
var binary = StaticSchemaValidator.binary;
|
|
354
|
+
var file = StaticSchemaValidator.file;
|
|
242
355
|
var schemify = StaticSchemaValidator.schemify.bind(
|
|
243
356
|
StaticSchemaValidator
|
|
244
357
|
);
|
|
@@ -273,10 +386,12 @@ export {
|
|
|
273
386
|
any,
|
|
274
387
|
array,
|
|
275
388
|
bigint,
|
|
389
|
+
binary,
|
|
276
390
|
boolean,
|
|
277
391
|
date,
|
|
278
392
|
email,
|
|
279
393
|
enum_,
|
|
394
|
+
file,
|
|
280
395
|
function_,
|
|
281
396
|
isSchema,
|
|
282
397
|
literal,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@forklaunch/validator",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "Schema validator for ForkLaunch components.",
|
|
5
5
|
"homepage": "https://github.com/forklaunch/forklaunch-js#readme",
|
|
6
6
|
"bugs": {
|
|
@@ -60,23 +60,24 @@
|
|
|
60
60
|
"dependencies": {
|
|
61
61
|
"@anatine/zod-openapi": "^2.2.8",
|
|
62
62
|
"@sinclair/typebox": "^0.34.33",
|
|
63
|
-
"zod": "^3.24
|
|
64
|
-
"@forklaunch/common": "0.
|
|
63
|
+
"zod": "^3.25.24",
|
|
64
|
+
"@forklaunch/common": "0.3.0"
|
|
65
65
|
},
|
|
66
66
|
"devDependencies": {
|
|
67
|
-
"@eslint/js": "^9.
|
|
67
|
+
"@eslint/js": "^9.27.0",
|
|
68
68
|
"@types/jest": "^29.5.14",
|
|
69
|
-
"@
|
|
70
|
-
"
|
|
71
|
-
"eslint-
|
|
72
|
-
"
|
|
69
|
+
"@types/node": "^22.15.21",
|
|
70
|
+
"@vitest/coverage-v8": "^3.1.4",
|
|
71
|
+
"eslint-config-prettier": "^10.1.5",
|
|
72
|
+
"eslint-plugin-prettier": "^5.4.0",
|
|
73
|
+
"globals": "^16.1.0",
|
|
73
74
|
"jest": "^29.7.0",
|
|
74
75
|
"openapi3-ts": "^4.4.0",
|
|
75
|
-
"ts-jest": "^29.3.
|
|
76
|
+
"ts-jest": "^29.3.4",
|
|
76
77
|
"ts-node": "^10.9.2",
|
|
77
|
-
"tsup": "^8.
|
|
78
|
-
"typedoc": "^0.28.
|
|
79
|
-
"typescript-eslint": "^8.
|
|
78
|
+
"tsup": "^8.5.0",
|
|
79
|
+
"typedoc": "^0.28.4",
|
|
80
|
+
"typescript-eslint": "^8.32.1"
|
|
80
81
|
},
|
|
81
82
|
"scripts": {
|
|
82
83
|
"build": "tsc --noEmit && tsup index.ts src/typebox/index.ts src/zod/index.ts __test__/utils/mockSchemaValidator.ts --format cjs,esm --no-splitting --tsconfig tsconfig.json --outDir lib --dts --clean",
|