@forklaunch/validator 0.2.9 → 0.3.1
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/tests/mockSchemaValidator.d.ts +2 -2
- package/dist/tests/mockSchemaValidator.js +2 -2
- package/dist/tests/mockSchemaValidator.js.map +1 -1
- package/dist/tests/typebox/equality.test.js +82 -82
- package/dist/tests/typebox/equality.test.js.map +1 -1
- package/dist/tests/typebox/largeSchema.test.js +43 -43
- package/dist/tests/typebox/largeSchema.test.js.map +1 -1
- package/dist/typebox/index.d.ts +1 -150
- package/dist/typebox/index.js +15 -200
- package/dist/typebox/index.js.map +1 -1
- package/dist/typebox/typebox.schemaValidator.d.ts +150 -0
- package/dist/typebox/typebox.schemaValidator.js +203 -0
- package/dist/typebox/typebox.schemaValidator.js.map +1 -0
- package/dist/typebox/types/typebox.schema.types.d.ts +2 -2
- package/dist/types/schema.types.d.ts +59 -13
- package/dist/zod/index.d.ts +1 -147
- package/dist/zod/index.js +15 -193
- package/dist/zod/index.js.map +1 -1
- package/dist/zod/zod.schemaValidator.d.ts +147 -0
- package/dist/zod/zod.schemaValidator.js +196 -0
- package/dist/zod/zod.schemaValidator.js.map +1 -0
- package/package.json +1 -1
package/dist/zod/index.js
CHANGED
|
@@ -1,196 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
*/
|
|
8
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
-
exports.openapi = exports.validate = exports.literal = exports.union = exports.array = exports.optional = exports.schemify = exports.never = exports.unknown = exports.any = exports.empty = exports.symbol = exports.date = exports.boolean = exports.bigint = exports.number = exports.string = exports.ZodSchema = exports.ZodSchemaValidator = void 0;
|
|
10
|
-
const zod_openapi_1 = require("@anatine/zod-openapi");
|
|
11
|
-
const zod_1 = require("zod");
|
|
12
|
-
/**
|
|
13
|
-
* Class representing a Zod schema definition.
|
|
14
|
-
* @implements {SchemaValidator}
|
|
15
|
-
*/
|
|
16
|
-
class ZodSchemaValidator {
|
|
17
|
-
_Type;
|
|
18
|
-
_SchemaCatchall;
|
|
19
|
-
_ValidSchemaObject;
|
|
20
|
-
string = zod_1.z.string();
|
|
21
|
-
number = zod_1.z.number();
|
|
22
|
-
bigint = zod_1.z.bigint();
|
|
23
|
-
boolean = zod_1.z.boolean();
|
|
24
|
-
date = zod_1.z.date();
|
|
25
|
-
symbol = zod_1.z.symbol();
|
|
26
|
-
empty = zod_1.z.union([zod_1.z.void(), zod_1.z.null(), zod_1.z.undefined()]);
|
|
27
|
-
any = zod_1.z.any();
|
|
28
|
-
unknown = zod_1.z.unknown();
|
|
29
|
-
never = zod_1.z.never();
|
|
30
|
-
/**
|
|
31
|
-
* Convert a schema to a Zod schema.
|
|
32
|
-
* @param {ZodIdiomaticSchema} schema - The schema to convert.
|
|
33
|
-
* @returns {ZodResolve<T>} The resolved schema.
|
|
34
|
-
*/
|
|
35
|
-
schemify(schema) {
|
|
36
|
-
if (typeof schema === 'string' ||
|
|
37
|
-
typeof schema === 'number' ||
|
|
38
|
-
typeof schema === 'boolean') {
|
|
39
|
-
return zod_1.z.literal(schema);
|
|
40
|
-
}
|
|
41
|
-
if (schema instanceof zod_1.ZodType) {
|
|
42
|
-
return schema;
|
|
43
|
-
}
|
|
44
|
-
const newSchema = {};
|
|
45
|
-
Object.getOwnPropertyNames(schema).forEach((key) => {
|
|
46
|
-
if (schema[key] instanceof zod_1.ZodType) {
|
|
47
|
-
newSchema[key] = schema[key];
|
|
48
|
-
}
|
|
49
|
-
else {
|
|
50
|
-
newSchema[key] = this.schemify(schema[key]);
|
|
51
|
-
}
|
|
52
|
-
});
|
|
53
|
-
return zod_1.z.object(newSchema);
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* Make a schema optional.
|
|
57
|
-
* @param {ZodIdiomaticSchema} schema - The schema to make optional.
|
|
58
|
-
* @returns {ZodOptional<ZodResolve<T>>} The optional schema.
|
|
59
|
-
*/
|
|
60
|
-
optional(schema) {
|
|
61
|
-
if (schema instanceof zod_1.ZodType) {
|
|
62
|
-
return schema.optional();
|
|
63
|
-
}
|
|
64
|
-
return this.schemify(schema).optional();
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* Create an array schema.
|
|
68
|
-
* @param {ZodIdiomaticSchema} schema - The schema to use for array items.
|
|
69
|
-
* @returns {ZodArray<ZodResolve<T>>} The array schema.
|
|
70
|
-
*/
|
|
71
|
-
array(schema) {
|
|
72
|
-
if (schema instanceof zod_1.ZodType) {
|
|
73
|
-
return schema.array();
|
|
74
|
-
}
|
|
75
|
-
return this.schemify(schema).array();
|
|
76
|
-
}
|
|
77
|
-
/**
|
|
78
|
-
* Create a union schema.
|
|
79
|
-
* @param {ZodUnionContainer} schemas - The schemas to union.
|
|
80
|
-
* @returns {ZodUnion<UnionZodResolve<T>>} The union schema.
|
|
81
|
-
*/
|
|
82
|
-
union(schemas) {
|
|
83
|
-
if (schemas.length < 2) {
|
|
84
|
-
throw new Error('Union must have at least two schemas');
|
|
85
|
-
}
|
|
86
|
-
const unionTypes = schemas.map((schema) => {
|
|
87
|
-
if (schema instanceof zod_1.ZodType) {
|
|
88
|
-
return schema;
|
|
89
|
-
}
|
|
90
|
-
return this.schemify(schema);
|
|
91
|
-
});
|
|
92
|
-
return zod_1.z.union(unionTypes);
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
93
7
|
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
* @param {ZodCatchall} schema - The schema to validate against.
|
|
105
|
-
* @param {unknown} value - The value to validate.
|
|
106
|
-
* @returns {boolean} True if valid, otherwise false.
|
|
107
|
-
*/
|
|
108
|
-
validate(schema, value) {
|
|
109
|
-
return schema.safeParse(value).success;
|
|
110
|
-
}
|
|
111
|
-
/**
|
|
112
|
-
* Convert a schema to an OpenAPI schema object.
|
|
113
|
-
* @param {ZodIdiomaticSchema} schema - The schema to convert.
|
|
114
|
-
* @returns {SchemaObject} The OpenAPI schema object.
|
|
115
|
-
*/
|
|
116
|
-
openapi(schema) {
|
|
117
|
-
return (0, zod_openapi_1.generateSchema)(this.schemify(schema));
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
exports.ZodSchemaValidator = ZodSchemaValidator;
|
|
121
|
-
/**
|
|
122
|
-
* Factory function for creating a ZodSchemaValidator instance.
|
|
123
|
-
* @returns {ZodSchemaValidator} The ZodSchemaValidator instance.
|
|
124
|
-
*/
|
|
125
|
-
const ZodSchema = () => new ZodSchemaValidator();
|
|
126
|
-
exports.ZodSchema = ZodSchema;
|
|
127
|
-
const SchemaValidator = (0, exports.ZodSchema)();
|
|
128
|
-
/**
|
|
129
|
-
* Zod schema definition for string type.
|
|
130
|
-
*/
|
|
131
|
-
exports.string = SchemaValidator.string;
|
|
132
|
-
/**
|
|
133
|
-
* Zod schema definition for number type.
|
|
134
|
-
*/
|
|
135
|
-
exports.number = SchemaValidator.number;
|
|
136
|
-
/**
|
|
137
|
-
* Zod schema definition for bigint type.
|
|
138
|
-
*/
|
|
139
|
-
exports.bigint = SchemaValidator.bigint;
|
|
140
|
-
/**
|
|
141
|
-
* Zod schema definition for boolean type.
|
|
142
|
-
*/
|
|
143
|
-
exports.boolean = SchemaValidator.boolean;
|
|
144
|
-
/**
|
|
145
|
-
* Zod schema definition for date type.
|
|
146
|
-
*/
|
|
147
|
-
exports.date = SchemaValidator.date;
|
|
148
|
-
/**
|
|
149
|
-
* Zod schema definition for symbol type.
|
|
150
|
-
*/
|
|
151
|
-
exports.symbol = SchemaValidator.symbol;
|
|
152
|
-
/**
|
|
153
|
-
* Zod schema definition for undefined, null, void types.
|
|
154
|
-
*/
|
|
155
|
-
exports.empty = SchemaValidator.empty;
|
|
156
|
-
/**
|
|
157
|
-
* Zod schema definition for any type.
|
|
158
|
-
*/
|
|
159
|
-
exports.any = SchemaValidator.any;
|
|
160
|
-
/**
|
|
161
|
-
* Zod schema definition for unknown type.
|
|
162
|
-
*/
|
|
163
|
-
exports.unknown = SchemaValidator.unknown;
|
|
164
|
-
/**
|
|
165
|
-
* Zod schema definition for never type.
|
|
166
|
-
*/
|
|
167
|
-
exports.never = SchemaValidator.never;
|
|
168
|
-
/**
|
|
169
|
-
* Transforms valid schema into Zod schema.
|
|
170
|
-
*/
|
|
171
|
-
exports.schemify = SchemaValidator.schemify.bind(SchemaValidator);
|
|
172
|
-
/**
|
|
173
|
-
* Makes a valid schema optional.
|
|
174
|
-
*/
|
|
175
|
-
exports.optional = SchemaValidator.optional.bind(SchemaValidator);
|
|
176
|
-
/**
|
|
177
|
-
* Defines an array for a valid schema.
|
|
178
|
-
*/
|
|
179
|
-
exports.array = SchemaValidator.array.bind(SchemaValidator);
|
|
180
|
-
/**
|
|
181
|
-
* Defines a union for a valid schema.
|
|
182
|
-
*/
|
|
183
|
-
exports.union = SchemaValidator.union.bind(SchemaValidator);
|
|
184
|
-
/**
|
|
185
|
-
* Defines a literal for a valid schema.
|
|
186
|
-
*/
|
|
187
|
-
exports.literal = SchemaValidator.literal.bind(SchemaValidator);
|
|
188
|
-
/**
|
|
189
|
-
* Validates a value against a valid schema.
|
|
190
|
-
*/
|
|
191
|
-
exports.validate = SchemaValidator.validate.bind(SchemaValidator);
|
|
192
|
-
/**
|
|
193
|
-
* Generates an OpenAPI schema object from a valid schema.
|
|
194
|
-
*/
|
|
195
|
-
exports.openapi = SchemaValidator.openapi.bind(SchemaValidator);
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./zod.schemaValidator"), exports);
|
|
196
18
|
//# sourceMappingURL=index.js.map
|
package/dist/zod/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../zod/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../zod/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,wDAAsC"}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This module provides a Zod-based schema definition.
|
|
3
|
+
* It includes various types, schema creation, validation, and OpenAPI integration.
|
|
4
|
+
*
|
|
5
|
+
* @module ZodSchemaValidator
|
|
6
|
+
*/
|
|
7
|
+
import { SchemaObject } from 'openapi3-ts/oas31';
|
|
8
|
+
import { ZodArray, ZodLiteral, ZodObject, ZodOptional, ZodRawShape, ZodType, ZodUnion, z } from 'zod';
|
|
9
|
+
import { LiteralSchema, SchemaValidator } from '../types/schema.types';
|
|
10
|
+
import { UnionZodResolve, ZodCatchall, ZodIdiomaticSchema, ZodResolve, ZodUnionContainer } from './types/zod.schema.types';
|
|
11
|
+
/**
|
|
12
|
+
* Class representing a Zod schema definition.
|
|
13
|
+
* @implements {SchemaValidator}
|
|
14
|
+
*/
|
|
15
|
+
export declare class ZodSchemaValidator implements SchemaValidator<(<T extends ZodIdiomaticSchema>(schema: T) => ZodResolve<T>), <T extends ZodIdiomaticSchema>(schema: T) => ZodOptional<ZodResolve<T>>, <T extends ZodIdiomaticSchema>(schema: T) => ZodArray<ZodResolve<T>>, <T extends ZodUnionContainer>(schemas: T) => ZodUnion<UnionZodResolve<T>>, <T extends LiteralSchema>(value: T) => ZodLiteral<ZodResolve<T>>, <T extends ZodCatchall>(schema: T, value: unknown) => boolean, <T extends ZodIdiomaticSchema>(schema: T) => SchemaObject> {
|
|
16
|
+
_Type: 'Zod';
|
|
17
|
+
_SchemaCatchall: ZodType;
|
|
18
|
+
_ValidSchemaObject: ZodObject<ZodRawShape> | ZodArray<ZodObject<ZodRawShape>>;
|
|
19
|
+
string: z.ZodString;
|
|
20
|
+
number: z.ZodNumber;
|
|
21
|
+
bigint: z.ZodBigInt;
|
|
22
|
+
boolean: z.ZodBoolean;
|
|
23
|
+
date: z.ZodDate;
|
|
24
|
+
symbol: z.ZodSymbol;
|
|
25
|
+
empty: ZodUnion<[z.ZodVoid, z.ZodNull, z.ZodUndefined]>;
|
|
26
|
+
any: z.ZodAny;
|
|
27
|
+
unknown: z.ZodUnknown;
|
|
28
|
+
never: z.ZodNever;
|
|
29
|
+
/**
|
|
30
|
+
* Convert a schema to a Zod schema.
|
|
31
|
+
* @param {ZodIdiomaticSchema} schema - The schema to convert.
|
|
32
|
+
* @returns {ZodResolve<T>} The resolved schema.
|
|
33
|
+
*/
|
|
34
|
+
schemify<T extends ZodIdiomaticSchema>(schema: T): ZodResolve<T>;
|
|
35
|
+
/**
|
|
36
|
+
* Make a schema optional.
|
|
37
|
+
* @param {ZodIdiomaticSchema} schema - The schema to make optional.
|
|
38
|
+
* @returns {ZodOptional<ZodResolve<T>>} The optional schema.
|
|
39
|
+
*/
|
|
40
|
+
optional<T extends ZodIdiomaticSchema>(schema: T): ZodOptional<ZodResolve<T>>;
|
|
41
|
+
/**
|
|
42
|
+
* Create an array schema.
|
|
43
|
+
* @param {ZodIdiomaticSchema} schema - The schema to use for array items.
|
|
44
|
+
* @returns {ZodArray<ZodResolve<T>>} The array schema.
|
|
45
|
+
*/
|
|
46
|
+
array<T extends ZodIdiomaticSchema>(schema: T): ZodArray<ZodResolve<T>>;
|
|
47
|
+
/**
|
|
48
|
+
* Create a union schema.
|
|
49
|
+
* @param {ZodUnionContainer} schemas - The schemas to union.
|
|
50
|
+
* @returns {ZodUnion<UnionZodResolve<T>>} The union schema.
|
|
51
|
+
*/
|
|
52
|
+
union<T extends ZodUnionContainer>(schemas: T): ZodUnion<UnionZodResolve<T>>;
|
|
53
|
+
/**
|
|
54
|
+
* Create a literal schema.
|
|
55
|
+
* @param {LiteralSchema} value - The literal value.
|
|
56
|
+
* @returns {ZodLiteral<ZodResolve<T>>} The literal schema.
|
|
57
|
+
*/
|
|
58
|
+
literal<T extends LiteralSchema>(value: T): ZodLiteral<ZodResolve<T>>;
|
|
59
|
+
/**
|
|
60
|
+
* Validate a value against a schema.
|
|
61
|
+
* @param {ZodCatchall} schema - The schema to validate against.
|
|
62
|
+
* @param {unknown} value - The value to validate.
|
|
63
|
+
* @returns {boolean} True if valid, otherwise false.
|
|
64
|
+
*/
|
|
65
|
+
validate<T extends ZodCatchall>(schema: T, value: unknown): boolean;
|
|
66
|
+
/**
|
|
67
|
+
* Convert a schema to an OpenAPI schema object.
|
|
68
|
+
* @param {ZodIdiomaticSchema} schema - The schema to convert.
|
|
69
|
+
* @returns {SchemaObject} The OpenAPI schema object.
|
|
70
|
+
*/
|
|
71
|
+
openapi<T extends ZodIdiomaticSchema>(schema: T): SchemaObject;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Factory function for creating a ZodSchemaValidator instance.
|
|
75
|
+
* @returns {ZodSchemaValidator} The ZodSchemaValidator instance.
|
|
76
|
+
*/
|
|
77
|
+
export declare const ZodSchema: () => ZodSchemaValidator;
|
|
78
|
+
declare const SchemaValidator: ZodSchemaValidator;
|
|
79
|
+
/**
|
|
80
|
+
* Zod schema definition for string type.
|
|
81
|
+
*/
|
|
82
|
+
export declare const string: typeof SchemaValidator.string;
|
|
83
|
+
/**
|
|
84
|
+
* Zod schema definition for number type.
|
|
85
|
+
*/
|
|
86
|
+
export declare const number: typeof SchemaValidator.number;
|
|
87
|
+
/**
|
|
88
|
+
* Zod schema definition for bigint type.
|
|
89
|
+
*/
|
|
90
|
+
export declare const bigint: typeof SchemaValidator.bigint;
|
|
91
|
+
/**
|
|
92
|
+
* Zod schema definition for boolean type.
|
|
93
|
+
*/
|
|
94
|
+
export declare const boolean: typeof SchemaValidator.boolean;
|
|
95
|
+
/**
|
|
96
|
+
* Zod schema definition for date type.
|
|
97
|
+
*/
|
|
98
|
+
export declare const date: typeof SchemaValidator.date;
|
|
99
|
+
/**
|
|
100
|
+
* Zod schema definition for symbol type.
|
|
101
|
+
*/
|
|
102
|
+
export declare const symbol: typeof SchemaValidator.symbol;
|
|
103
|
+
/**
|
|
104
|
+
* Zod schema definition for undefined, null, void types.
|
|
105
|
+
*/
|
|
106
|
+
export declare const empty: typeof SchemaValidator.empty;
|
|
107
|
+
/**
|
|
108
|
+
* Zod schema definition for any type.
|
|
109
|
+
*/
|
|
110
|
+
export declare const any: typeof SchemaValidator.any;
|
|
111
|
+
/**
|
|
112
|
+
* Zod schema definition for unknown type.
|
|
113
|
+
*/
|
|
114
|
+
export declare const unknown: typeof SchemaValidator.unknown;
|
|
115
|
+
/**
|
|
116
|
+
* Zod schema definition for never type.
|
|
117
|
+
*/
|
|
118
|
+
export declare const never: typeof SchemaValidator.never;
|
|
119
|
+
/**
|
|
120
|
+
* Transforms valid schema into Zod schema.
|
|
121
|
+
*/
|
|
122
|
+
export declare const schemify: typeof SchemaValidator.schemify;
|
|
123
|
+
/**
|
|
124
|
+
* Makes a valid schema optional.
|
|
125
|
+
*/
|
|
126
|
+
export declare const optional: typeof SchemaValidator.optional;
|
|
127
|
+
/**
|
|
128
|
+
* Defines an array for a valid schema.
|
|
129
|
+
*/
|
|
130
|
+
export declare const array: typeof SchemaValidator.array;
|
|
131
|
+
/**
|
|
132
|
+
* Defines a union for a valid schema.
|
|
133
|
+
*/
|
|
134
|
+
export declare const union: typeof SchemaValidator.union;
|
|
135
|
+
/**
|
|
136
|
+
* Defines a literal for a valid schema.
|
|
137
|
+
*/
|
|
138
|
+
export declare const literal: typeof SchemaValidator.literal;
|
|
139
|
+
/**
|
|
140
|
+
* Validates a value against a valid schema.
|
|
141
|
+
*/
|
|
142
|
+
export declare const validate: typeof SchemaValidator.validate;
|
|
143
|
+
/**
|
|
144
|
+
* Generates an OpenAPI schema object from a valid schema.
|
|
145
|
+
*/
|
|
146
|
+
export declare const openapi: typeof SchemaValidator.openapi;
|
|
147
|
+
export {};
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* This module provides a Zod-based schema definition.
|
|
4
|
+
* It includes various types, schema creation, validation, and OpenAPI integration.
|
|
5
|
+
*
|
|
6
|
+
* @module ZodSchemaValidator
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.openapi = exports.validate = exports.literal = exports.union = exports.array = exports.optional = exports.schemify = exports.never = exports.unknown = exports.any = exports.empty = exports.symbol = exports.date = exports.boolean = exports.bigint = exports.number = exports.string = exports.ZodSchema = exports.ZodSchemaValidator = void 0;
|
|
10
|
+
const zod_openapi_1 = require("@anatine/zod-openapi");
|
|
11
|
+
const zod_1 = require("zod");
|
|
12
|
+
/**
|
|
13
|
+
* Class representing a Zod schema definition.
|
|
14
|
+
* @implements {SchemaValidator}
|
|
15
|
+
*/
|
|
16
|
+
class ZodSchemaValidator {
|
|
17
|
+
_Type;
|
|
18
|
+
_SchemaCatchall;
|
|
19
|
+
_ValidSchemaObject;
|
|
20
|
+
string = zod_1.z.string();
|
|
21
|
+
number = zod_1.z.number();
|
|
22
|
+
bigint = zod_1.z.bigint();
|
|
23
|
+
boolean = zod_1.z.boolean();
|
|
24
|
+
date = zod_1.z.date();
|
|
25
|
+
symbol = zod_1.z.symbol();
|
|
26
|
+
empty = zod_1.z.union([zod_1.z.void(), zod_1.z.null(), zod_1.z.undefined()]);
|
|
27
|
+
any = zod_1.z.any();
|
|
28
|
+
unknown = zod_1.z.unknown();
|
|
29
|
+
never = zod_1.z.never();
|
|
30
|
+
/**
|
|
31
|
+
* Convert a schema to a Zod schema.
|
|
32
|
+
* @param {ZodIdiomaticSchema} schema - The schema to convert.
|
|
33
|
+
* @returns {ZodResolve<T>} The resolved schema.
|
|
34
|
+
*/
|
|
35
|
+
schemify(schema) {
|
|
36
|
+
if (typeof schema === 'string' ||
|
|
37
|
+
typeof schema === 'number' ||
|
|
38
|
+
typeof schema === 'boolean') {
|
|
39
|
+
return zod_1.z.literal(schema);
|
|
40
|
+
}
|
|
41
|
+
if (schema instanceof zod_1.ZodType) {
|
|
42
|
+
return schema;
|
|
43
|
+
}
|
|
44
|
+
const newSchema = {};
|
|
45
|
+
Object.getOwnPropertyNames(schema).forEach((key) => {
|
|
46
|
+
if (schema[key] instanceof zod_1.ZodType) {
|
|
47
|
+
newSchema[key] = schema[key];
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
newSchema[key] = this.schemify(schema[key]);
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
return zod_1.z.object(newSchema);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Make a schema optional.
|
|
57
|
+
* @param {ZodIdiomaticSchema} schema - The schema to make optional.
|
|
58
|
+
* @returns {ZodOptional<ZodResolve<T>>} The optional schema.
|
|
59
|
+
*/
|
|
60
|
+
optional(schema) {
|
|
61
|
+
if (schema instanceof zod_1.ZodType) {
|
|
62
|
+
return schema.optional();
|
|
63
|
+
}
|
|
64
|
+
return this.schemify(schema).optional();
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Create an array schema.
|
|
68
|
+
* @param {ZodIdiomaticSchema} schema - The schema to use for array items.
|
|
69
|
+
* @returns {ZodArray<ZodResolve<T>>} The array schema.
|
|
70
|
+
*/
|
|
71
|
+
array(schema) {
|
|
72
|
+
if (schema instanceof zod_1.ZodType) {
|
|
73
|
+
return schema.array();
|
|
74
|
+
}
|
|
75
|
+
return this.schemify(schema).array();
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Create a union schema.
|
|
79
|
+
* @param {ZodUnionContainer} schemas - The schemas to union.
|
|
80
|
+
* @returns {ZodUnion<UnionZodResolve<T>>} The union schema.
|
|
81
|
+
*/
|
|
82
|
+
union(schemas) {
|
|
83
|
+
if (schemas.length < 2) {
|
|
84
|
+
throw new Error('Union must have at least two schemas');
|
|
85
|
+
}
|
|
86
|
+
const unionTypes = schemas.map((schema) => {
|
|
87
|
+
if (schema instanceof zod_1.ZodType) {
|
|
88
|
+
return schema;
|
|
89
|
+
}
|
|
90
|
+
return this.schemify(schema);
|
|
91
|
+
});
|
|
92
|
+
return zod_1.z.union(unionTypes);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Create a literal schema.
|
|
96
|
+
* @param {LiteralSchema} value - The literal value.
|
|
97
|
+
* @returns {ZodLiteral<ZodResolve<T>>} The literal schema.
|
|
98
|
+
*/
|
|
99
|
+
literal(value) {
|
|
100
|
+
return zod_1.z.literal(value);
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Validate a value against a schema.
|
|
104
|
+
* @param {ZodCatchall} schema - The schema to validate against.
|
|
105
|
+
* @param {unknown} value - The value to validate.
|
|
106
|
+
* @returns {boolean} True if valid, otherwise false.
|
|
107
|
+
*/
|
|
108
|
+
validate(schema, value) {
|
|
109
|
+
return schema.safeParse(value).success;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Convert a schema to an OpenAPI schema object.
|
|
113
|
+
* @param {ZodIdiomaticSchema} schema - The schema to convert.
|
|
114
|
+
* @returns {SchemaObject} The OpenAPI schema object.
|
|
115
|
+
*/
|
|
116
|
+
openapi(schema) {
|
|
117
|
+
return (0, zod_openapi_1.generateSchema)(this.schemify(schema));
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
exports.ZodSchemaValidator = ZodSchemaValidator;
|
|
121
|
+
/**
|
|
122
|
+
* Factory function for creating a ZodSchemaValidator instance.
|
|
123
|
+
* @returns {ZodSchemaValidator} The ZodSchemaValidator instance.
|
|
124
|
+
*/
|
|
125
|
+
const ZodSchema = () => new ZodSchemaValidator();
|
|
126
|
+
exports.ZodSchema = ZodSchema;
|
|
127
|
+
const SchemaValidator = (0, exports.ZodSchema)();
|
|
128
|
+
/**
|
|
129
|
+
* Zod schema definition for string type.
|
|
130
|
+
*/
|
|
131
|
+
exports.string = SchemaValidator.string;
|
|
132
|
+
/**
|
|
133
|
+
* Zod schema definition for number type.
|
|
134
|
+
*/
|
|
135
|
+
exports.number = SchemaValidator.number;
|
|
136
|
+
/**
|
|
137
|
+
* Zod schema definition for bigint type.
|
|
138
|
+
*/
|
|
139
|
+
exports.bigint = SchemaValidator.bigint;
|
|
140
|
+
/**
|
|
141
|
+
* Zod schema definition for boolean type.
|
|
142
|
+
*/
|
|
143
|
+
exports.boolean = SchemaValidator.boolean;
|
|
144
|
+
/**
|
|
145
|
+
* Zod schema definition for date type.
|
|
146
|
+
*/
|
|
147
|
+
exports.date = SchemaValidator.date;
|
|
148
|
+
/**
|
|
149
|
+
* Zod schema definition for symbol type.
|
|
150
|
+
*/
|
|
151
|
+
exports.symbol = SchemaValidator.symbol;
|
|
152
|
+
/**
|
|
153
|
+
* Zod schema definition for undefined, null, void types.
|
|
154
|
+
*/
|
|
155
|
+
exports.empty = SchemaValidator.empty;
|
|
156
|
+
/**
|
|
157
|
+
* Zod schema definition for any type.
|
|
158
|
+
*/
|
|
159
|
+
exports.any = SchemaValidator.any;
|
|
160
|
+
/**
|
|
161
|
+
* Zod schema definition for unknown type.
|
|
162
|
+
*/
|
|
163
|
+
exports.unknown = SchemaValidator.unknown;
|
|
164
|
+
/**
|
|
165
|
+
* Zod schema definition for never type.
|
|
166
|
+
*/
|
|
167
|
+
exports.never = SchemaValidator.never;
|
|
168
|
+
/**
|
|
169
|
+
* Transforms valid schema into Zod schema.
|
|
170
|
+
*/
|
|
171
|
+
exports.schemify = SchemaValidator.schemify.bind(SchemaValidator);
|
|
172
|
+
/**
|
|
173
|
+
* Makes a valid schema optional.
|
|
174
|
+
*/
|
|
175
|
+
exports.optional = SchemaValidator.optional.bind(SchemaValidator);
|
|
176
|
+
/**
|
|
177
|
+
* Defines an array for a valid schema.
|
|
178
|
+
*/
|
|
179
|
+
exports.array = SchemaValidator.array.bind(SchemaValidator);
|
|
180
|
+
/**
|
|
181
|
+
* Defines a union for a valid schema.
|
|
182
|
+
*/
|
|
183
|
+
exports.union = SchemaValidator.union.bind(SchemaValidator);
|
|
184
|
+
/**
|
|
185
|
+
* Defines a literal for a valid schema.
|
|
186
|
+
*/
|
|
187
|
+
exports.literal = SchemaValidator.literal.bind(SchemaValidator);
|
|
188
|
+
/**
|
|
189
|
+
* Validates a value against a valid schema.
|
|
190
|
+
*/
|
|
191
|
+
exports.validate = SchemaValidator.validate.bind(SchemaValidator);
|
|
192
|
+
/**
|
|
193
|
+
* Generates an OpenAPI schema object from a valid schema.
|
|
194
|
+
*/
|
|
195
|
+
exports.openapi = SchemaValidator.openapi.bind(SchemaValidator);
|
|
196
|
+
//# sourceMappingURL=zod.schemaValidator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"zod.schemaValidator.js","sourceRoot":"","sources":["../../zod/zod.schemaValidator.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEH,sDAAsD;AAEtD,6BASa;AAUb;;;GAGG;AACH,MAAa,kBAAkB;IAY7B,KAAK,CAAS;IACd,eAAe,CAAW;IAC1B,kBAAkB,CAEmB;IAErC,MAAM,GAAG,OAAC,CAAC,MAAM,EAAE,CAAC;IACpB,MAAM,GAAG,OAAC,CAAC,MAAM,EAAE,CAAC;IACpB,MAAM,GAAG,OAAC,CAAC,MAAM,EAAE,CAAC;IACpB,OAAO,GAAG,OAAC,C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