@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
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* This module provides a TypeScript-based schema definition using the TypeBox library.
|
|
4
|
+
* It includes various types, schema creation, validation, and OpenAPI integration.
|
|
5
|
+
*
|
|
6
|
+
* @module TypeboxSchemaValidator
|
|
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.TypeboxSchema = exports.TypeboxSchemaValidator = void 0;
|
|
10
|
+
const typebox_1 = require("@sinclair/typebox");
|
|
11
|
+
const value_1 = require("@sinclair/typebox/value");
|
|
12
|
+
/**
|
|
13
|
+
* Class representing a TypeBox schema definition.
|
|
14
|
+
* @implements {SchemaValidator}
|
|
15
|
+
*/
|
|
16
|
+
class TypeboxSchemaValidator {
|
|
17
|
+
_Type;
|
|
18
|
+
_SchemaCatchall;
|
|
19
|
+
_ValidSchemaObject;
|
|
20
|
+
string = typebox_1.Type.String();
|
|
21
|
+
number = typebox_1.Type.Number();
|
|
22
|
+
bigint = typebox_1.Type.BigInt();
|
|
23
|
+
boolean = typebox_1.Type.Boolean();
|
|
24
|
+
date = typebox_1.Type.Date();
|
|
25
|
+
symbol = typebox_1.Type.Symbol();
|
|
26
|
+
empty = typebox_1.Type.Union([typebox_1.Type.Void(), typebox_1.Type.Null(), typebox_1.Type.Undefined()]);
|
|
27
|
+
any = typebox_1.Type.Any();
|
|
28
|
+
unknown = typebox_1.Type.Unknown();
|
|
29
|
+
never = typebox_1.Type.Never();
|
|
30
|
+
/**
|
|
31
|
+
* Convert a schema to a TypeBox schema.
|
|
32
|
+
* @param {TIdiomaticSchema} schema - The schema to convert.
|
|
33
|
+
* @returns {TResolve<T>} The resolved schema.
|
|
34
|
+
*/
|
|
35
|
+
schemify(schema) {
|
|
36
|
+
if (typeof schema === 'string' ||
|
|
37
|
+
typeof schema === 'number' ||
|
|
38
|
+
typeof schema === 'boolean') {
|
|
39
|
+
return typebox_1.Type.Literal(schema);
|
|
40
|
+
}
|
|
41
|
+
if (typebox_1.Kind in schema) {
|
|
42
|
+
return schema;
|
|
43
|
+
}
|
|
44
|
+
const newSchema = {};
|
|
45
|
+
Object.getOwnPropertyNames(schema).forEach((key) => {
|
|
46
|
+
if (typeof schema[key] === 'object' && typebox_1.Kind in schema[key]) {
|
|
47
|
+
newSchema[key] = schema[key];
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
const schemified = this.schemify(schema[key]);
|
|
51
|
+
newSchema[key] = schemified;
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
return typebox_1.Type.Object(newSchema);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Make a schema optional.
|
|
58
|
+
* @param {TIdiomaticSchema} schema - The schema to make optional.
|
|
59
|
+
* @returns {TOptional<TResolve<T>>} The optional schema.
|
|
60
|
+
*/
|
|
61
|
+
optional(schema) {
|
|
62
|
+
if (typebox_1.Kind in schema) {
|
|
63
|
+
return typebox_1.Type.Optional(schema);
|
|
64
|
+
}
|
|
65
|
+
const schemified = this.schemify(schema);
|
|
66
|
+
return typebox_1.Type.Optional(schemified);
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Create an array schema.
|
|
70
|
+
* @param {TIdiomaticSchema} schema - The schema to use for array items.
|
|
71
|
+
* @returns {TArray<TResolve<T>>} The array schema.
|
|
72
|
+
*/
|
|
73
|
+
array(schema) {
|
|
74
|
+
if (typebox_1.Kind in schema) {
|
|
75
|
+
return typebox_1.Type.Array(schema);
|
|
76
|
+
}
|
|
77
|
+
const schemified = this.schemify(schema);
|
|
78
|
+
return typebox_1.Type.Array(schemified);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Create a union schema.
|
|
82
|
+
* @param {TUnionContainer} schemas - The schemas to union.
|
|
83
|
+
* @returns {TUnion<UnionTResolve<T>>} The union schema.
|
|
84
|
+
*
|
|
85
|
+
* WARNING: If "empty" or TUndefined is included in the union, the key will still be expected.
|
|
86
|
+
* This is a limitation of TypeBox. Consider using "optional" instead.
|
|
87
|
+
*/
|
|
88
|
+
union(schemas) {
|
|
89
|
+
const unionTypes = schemas.map((schema) => {
|
|
90
|
+
if (typebox_1.Kind in schema) {
|
|
91
|
+
return schema;
|
|
92
|
+
}
|
|
93
|
+
return this.schemify(schema);
|
|
94
|
+
});
|
|
95
|
+
return typebox_1.Type.Union(unionTypes);
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Create a literal schema.
|
|
99
|
+
* @param {LiteralSchema} value - The literal value.
|
|
100
|
+
* @returns {TLiteral<T>} The literal schema.
|
|
101
|
+
*/
|
|
102
|
+
literal(value) {
|
|
103
|
+
return typebox_1.Type.Literal(value);
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Validate a value against a schema.
|
|
107
|
+
* @param {TSchema} schema - The schema to validate against.
|
|
108
|
+
* @param {unknown} value - The value to validate.
|
|
109
|
+
* @returns {boolean} True if valid, otherwise false.
|
|
110
|
+
*/
|
|
111
|
+
validate(schema, value) {
|
|
112
|
+
if (typebox_1.Kind in schema) {
|
|
113
|
+
return value_1.Value.Check(schema, value);
|
|
114
|
+
}
|
|
115
|
+
const schemified = this.schemify(schema);
|
|
116
|
+
return value_1.Value.Check(schemified, value);
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Convert a schema to an OpenAPI schema object.
|
|
120
|
+
* @param {TIdiomaticSchema | TSchema} schema - The schema to convert.
|
|
121
|
+
* @returns {SchemaObject} The OpenAPI schema object.
|
|
122
|
+
*/
|
|
123
|
+
openapi(schema) {
|
|
124
|
+
return this.schemify(schema);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
exports.TypeboxSchemaValidator = TypeboxSchemaValidator;
|
|
128
|
+
/**
|
|
129
|
+
* Factory function for creating a TypeboxSchemaValidator instance.
|
|
130
|
+
* @returns {TypeboxSchemaValidator} The TypeboxSchemaValidator instance.
|
|
131
|
+
*/
|
|
132
|
+
const TypeboxSchema = () => new TypeboxSchemaValidator();
|
|
133
|
+
exports.TypeboxSchema = TypeboxSchema;
|
|
134
|
+
const SchemaValidator = (0, exports.TypeboxSchema)();
|
|
135
|
+
/**
|
|
136
|
+
* TypeBox schema definition for string type.
|
|
137
|
+
*/
|
|
138
|
+
exports.string = SchemaValidator.string;
|
|
139
|
+
/**
|
|
140
|
+
* TypeBox schema definition for number type.
|
|
141
|
+
*/
|
|
142
|
+
exports.number = SchemaValidator.number;
|
|
143
|
+
/**
|
|
144
|
+
* TypeBox schema definition for bigint type.
|
|
145
|
+
*/
|
|
146
|
+
exports.bigint = SchemaValidator.bigint;
|
|
147
|
+
/**
|
|
148
|
+
* TypeBox schema definition for boolean type.
|
|
149
|
+
*/
|
|
150
|
+
exports.boolean = SchemaValidator.boolean;
|
|
151
|
+
/**
|
|
152
|
+
* TypeBox schema definition for date type.
|
|
153
|
+
*/
|
|
154
|
+
exports.date = SchemaValidator.date;
|
|
155
|
+
/**
|
|
156
|
+
* TypeBox schema definition for symbol type.
|
|
157
|
+
*/
|
|
158
|
+
exports.symbol = SchemaValidator.symbol;
|
|
159
|
+
/**
|
|
160
|
+
* TypeBox schema definition for undefined, null, void types.
|
|
161
|
+
*/
|
|
162
|
+
exports.empty = SchemaValidator.empty;
|
|
163
|
+
/**
|
|
164
|
+
* TypeBox schema definition for any type.
|
|
165
|
+
*/
|
|
166
|
+
exports.any = SchemaValidator.any;
|
|
167
|
+
/**
|
|
168
|
+
* TypeBox schema definition for unknown type.
|
|
169
|
+
*/
|
|
170
|
+
exports.unknown = SchemaValidator.unknown;
|
|
171
|
+
/**
|
|
172
|
+
* TypeBox schema definition for never type.
|
|
173
|
+
*/
|
|
174
|
+
exports.never = SchemaValidator.never;
|
|
175
|
+
/**
|
|
176
|
+
* Transforms valid schema into TypeBox schema.
|
|
177
|
+
*/
|
|
178
|
+
exports.schemify = SchemaValidator.schemify.bind(SchemaValidator);
|
|
179
|
+
/**
|
|
180
|
+
* Makes a valid schema optional.
|
|
181
|
+
*/
|
|
182
|
+
exports.optional = SchemaValidator.optional.bind(SchemaValidator);
|
|
183
|
+
/**
|
|
184
|
+
* Defines an array for a valid schema.
|
|
185
|
+
*/
|
|
186
|
+
exports.array = SchemaValidator.array.bind(SchemaValidator);
|
|
187
|
+
/**
|
|
188
|
+
* Defines a union for a valid schema.
|
|
189
|
+
*/
|
|
190
|
+
exports.union = SchemaValidator.union.bind(SchemaValidator);
|
|
191
|
+
/**
|
|
192
|
+
* Defines a literal for a valid schema.
|
|
193
|
+
*/
|
|
194
|
+
exports.literal = SchemaValidator.literal.bind(SchemaValidator);
|
|
195
|
+
/**
|
|
196
|
+
* Validates a value against a valid schema.
|
|
197
|
+
*/
|
|
198
|
+
exports.validate = SchemaValidator.validate.bind(SchemaValidator);
|
|
199
|
+
/**
|
|
200
|
+
* Generates an OpenAPI schema object from a valid schema.
|
|
201
|
+
*/
|
|
202
|
+
exports.openapi = SchemaValidator.openapi.bind(SchemaValidator);
|
|
203
|
+
//# sourceMappingURL=typebox.schemaValidator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"typebox.schemaValidator.js","sourceRoot":"","sources":["../../typebox/typebox.schemaValidator.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEH,+CAS2B;AAC3B,mDAAgD;AAYhD;;;GAGG;AACH,MAAa,sBAAsB;IAYjC,KAAK,CAAa;IAClB,eAAe,CAAW;IAC1B,kBAAkB,CAAuD;IAEzE,MAAM,GAAG,cAAI,CAAC,MAAM,EAAE,CAAC;IACvB,MAAM,GAAG,cAAI,CAAC,MAAM,EAAE,CAAC;IACvB,MAAM,GAAG,cAAI,CAAC,MAAM,EAAE,CAAC;IACvB,OAAO,GAAG,cAAI,CAAC,OAAO,EAAE,CAAC;IACzB,IAAI,GAAG,cAAI,CAAC,IAAI,EAAE,CAAC;IACnB,MAAM,GAAG,cAAI,CAAC,MAAM,EAAE,CAAC;IACvB,KAAK,GAAG,cAAI,CAAC,KAAK,CAAC,CAAC,cAAI,CAAC,IAAI,EAAE,EAAE,cAAI,CAAC,IAAI,EAAE,EAAE,cAAI,CAAC,SAAS,EAAE,CAAC,CAAC,CAAC;IACjE,GAAG,GAAG,cAAI,CAAC,GAAG,EAAE,CAAC;IACjB,OAAO,GAAG,cAAI,CAAC,OAAO,EAAE,CAAC;IACzB,KAAK,GAAG,cAAI,CAAC,KAAK,EAAE,CAAC;IAErB;;;;OAIG;IACH,QAAQ,CAA6B,MAAS;QAC5C,IACE,OAAO,MAAM,K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AC,CAAC"}
|
|
@@ -53,11 +53,11 @@ export type UnionTResolve<T extends TUnionContainer> = T extends [
|
|
|
53
53
|
}
|
|
54
54
|
] : [];
|
|
55
55
|
/**
|
|
56
|
-
* Resolves a schema type T to its resolved type. The depth is limited to
|
|
56
|
+
* Resolves a schema type T to its resolved type. The depth is limited to 22 to prevent infinite recursion.
|
|
57
57
|
*
|
|
58
58
|
* @template T - The schema type to resolve.
|
|
59
59
|
* @template Depth - The current depth of the resolution.
|
|
60
60
|
*/
|
|
61
|
-
export type TResolve<T, Depth extends number = 0> = Depth extends
|
|
61
|
+
export type TResolve<T, Depth extends number = 0> = Depth extends 22 ? TUnknown : T extends LiteralSchema ? TLiteral<T> : T extends TKind ? T : T extends TObject<TObjectShape> ? T : T extends UnboxedTObjectSchema ? TObject<{
|
|
62
62
|
[K in keyof T]: TResolve<T[K], Increment<Depth>>;
|
|
63
63
|
}> : TNever;
|
|
@@ -5,13 +5,26 @@ import { ZodResolve, ZodSchemaTranslate } from '../zod/types/zod.schema.types';
|
|
|
5
5
|
/**
|
|
6
6
|
* Interface representing a schema validator.
|
|
7
7
|
*
|
|
8
|
-
* @template
|
|
9
|
-
* @template
|
|
10
|
-
* @template
|
|
8
|
+
* @template SchematicFunction - The function type for schemifying a schema.
|
|
9
|
+
* @template OptionalFunction - The function type for making a schema optional.
|
|
10
|
+
* @template ArrayFunction - The function type for converting a schema into an array.
|
|
11
|
+
* @template UnionFunction - The function type for unionizing multiple schemas.
|
|
12
|
+
* @template LiteralFunction - The function type for creating a literal schema.
|
|
13
|
+
* @template ValidationFunction - The function type for validating a value against a schema.
|
|
14
|
+
* @template OpenAPIFunction - The function type for converting a schema into an OpenAPI schema object.
|
|
11
15
|
*/
|
|
12
16
|
export interface SchemaValidator<SchematicFunction = <T>(schema: T) => unknown, OptionalFunction = <T>(schema: T) => unknown, ArrayFunction = <T>(schema: T) => unknown, UnionFunction = <T>(schemas: T[]) => unknown, LiteralFunction = <T extends LiteralSchema>(schema: T) => unknown, ValidationFunction = <T>(schema: T, value: unknown) => boolean, OpenAPIFunction = <T>(schema: T) => SchemaObject> {
|
|
17
|
+
/**
|
|
18
|
+
* The type of the schema validator. Meant to be used with non-null assertions.
|
|
19
|
+
*/
|
|
13
20
|
_Type: unknown;
|
|
21
|
+
/**
|
|
22
|
+
* The catch-all type for the schema. Meant to be used with non-null assertions.
|
|
23
|
+
*/
|
|
14
24
|
_SchemaCatchall: unknown;
|
|
25
|
+
/**
|
|
26
|
+
* The valid schema object type. Meant to be used with non-null assertions.
|
|
27
|
+
*/
|
|
15
28
|
_ValidSchemaObject: unknown;
|
|
16
29
|
/**
|
|
17
30
|
* Validator for string type.
|
|
@@ -56,7 +69,6 @@ export interface SchemaValidator<SchematicFunction = <T>(schema: T) => unknown,
|
|
|
56
69
|
/**
|
|
57
70
|
* Converts a valid schema input into a schemified form.
|
|
58
71
|
*
|
|
59
|
-
* @template T - The type of the idiomatic schema.
|
|
60
72
|
* @param {T} schema - The schema to schemify.
|
|
61
73
|
* @returns {unknown} - The schemified form of the schema.
|
|
62
74
|
*/
|
|
@@ -64,7 +76,6 @@ export interface SchemaValidator<SchematicFunction = <T>(schema: T) => unknown,
|
|
|
64
76
|
/**
|
|
65
77
|
* Converts a schema into an optional schema.
|
|
66
78
|
*
|
|
67
|
-
* @template T - The type of the idiomatic schema.
|
|
68
79
|
* @param {T} schema - The schema to make optional.
|
|
69
80
|
* @returns {unknown} - The optional form of the schema.
|
|
70
81
|
*/
|
|
@@ -72,7 +83,6 @@ export interface SchemaValidator<SchematicFunction = <T>(schema: T) => unknown,
|
|
|
72
83
|
/**
|
|
73
84
|
* Converts a schema into an array schema.
|
|
74
85
|
*
|
|
75
|
-
* @template T - The type of the idiomatic schema.
|
|
76
86
|
* @param {T} schema - The schema to convert into an array.
|
|
77
87
|
* @returns {unknown} - The array form of the schema.
|
|
78
88
|
*/
|
|
@@ -80,15 +90,13 @@ export interface SchemaValidator<SchematicFunction = <T>(schema: T) => unknown,
|
|
|
80
90
|
/**
|
|
81
91
|
* Converts multiple schemas into a union schema.
|
|
82
92
|
*
|
|
83
|
-
* @
|
|
84
|
-
* @param {T} schemas - The schemas to unionize.
|
|
93
|
+
* @param {T[]} schemas - The schemas to unionize.
|
|
85
94
|
* @returns {unknown} - The union form of the schemas.
|
|
86
95
|
*/
|
|
87
96
|
union: UnionFunction;
|
|
88
97
|
/**
|
|
89
98
|
* Creates a literal schema from a value.
|
|
90
99
|
*
|
|
91
|
-
* @template T - The type of the literal value.
|
|
92
100
|
* @param {T} value - The literal value.
|
|
93
101
|
* @returns {unknown} - The literal schema.
|
|
94
102
|
*/
|
|
@@ -96,7 +104,6 @@ export interface SchemaValidator<SchematicFunction = <T>(schema: T) => unknown,
|
|
|
96
104
|
/**
|
|
97
105
|
* Validates a value against a schema.
|
|
98
106
|
*
|
|
99
|
-
* @template T - The type of the catch-all schema.
|
|
100
107
|
* @param {T} schema - The schema to validate against.
|
|
101
108
|
* @param {unknown} value - The value to validate.
|
|
102
109
|
* @returns {boolean} - Whether the value is valid according to the schema.
|
|
@@ -105,29 +112,68 @@ export interface SchemaValidator<SchematicFunction = <T>(schema: T) => unknown,
|
|
|
105
112
|
/**
|
|
106
113
|
* Converts a schema into an OpenAPI schema object.
|
|
107
114
|
*
|
|
108
|
-
* @template T - The type of the idiomatic schema.
|
|
109
115
|
* @param {T} schema - The schema to convert.
|
|
110
116
|
* @returns {SchemaObject} - The OpenAPI schema object.
|
|
111
117
|
*/
|
|
112
118
|
openapi: OpenAPIFunction;
|
|
113
119
|
}
|
|
120
|
+
/**
|
|
121
|
+
* Type representing any schema validator.
|
|
122
|
+
*/
|
|
114
123
|
export type AnySchemaValidator = SchemaValidator<unknown, unknown, unknown, unknown, unknown, unknown, unknown> & {
|
|
124
|
+
/**
|
|
125
|
+
* The type of the schema resolver.
|
|
126
|
+
*/
|
|
115
127
|
_Type: keyof SchemaResolve<unknown>;
|
|
116
128
|
};
|
|
129
|
+
/**
|
|
130
|
+
* Interface representing schema resolution for different validation libraries.
|
|
131
|
+
*
|
|
132
|
+
* @template T - The type of the schema to resolve.
|
|
133
|
+
*/
|
|
117
134
|
export interface SchemaResolve<T> {
|
|
135
|
+
/**
|
|
136
|
+
* Schema resolution for Zod.
|
|
137
|
+
*/
|
|
118
138
|
Zod: ZodResolve<T>;
|
|
139
|
+
/**
|
|
140
|
+
* Schema resolution for TypeBox.
|
|
141
|
+
*/
|
|
119
142
|
TypeBox: TResolve<T>;
|
|
120
143
|
}
|
|
144
|
+
/**
|
|
145
|
+
* Interface representing schema translation for different validation libraries.
|
|
146
|
+
*
|
|
147
|
+
* @template T - The type of the schema to translate.
|
|
148
|
+
*/
|
|
121
149
|
export interface SchemaTranslate<T> {
|
|
150
|
+
/**
|
|
151
|
+
* Schema translation for Zod.
|
|
152
|
+
*/
|
|
122
153
|
Zod: ZodSchemaTranslate<T>;
|
|
154
|
+
/**
|
|
155
|
+
* Schema translation for TypeBox.
|
|
156
|
+
*/
|
|
123
157
|
TypeBox: TSchemaTranslate<T>;
|
|
124
158
|
}
|
|
159
|
+
/**
|
|
160
|
+
* Type representing the prettified schema translation.
|
|
161
|
+
*
|
|
162
|
+
* @template T - The type of the schema to translate.
|
|
163
|
+
* @template SV - The type of the schema validator.
|
|
164
|
+
*/
|
|
125
165
|
type SchemaPrettify<T, SV extends AnySchemaValidator> = Prettify<SchemaTranslate<T>[SV['_Type']]>;
|
|
166
|
+
/**
|
|
167
|
+
* Type representing a schema, which can be a valid schema object or an idiomatic schema.
|
|
168
|
+
*
|
|
169
|
+
* @template T - The type of the schema.
|
|
170
|
+
* @template SV - The type of the schema validator.
|
|
171
|
+
*/
|
|
126
172
|
export type Schema<T extends SV['_ValidSchemaObject'] | IdiomaticSchema<SV>, SV extends AnySchemaValidator> = SchemaPrettify<SchemaResolve<T>[SV['_Type']], SV>;
|
|
127
173
|
/**
|
|
128
174
|
* Represents a schema for an unboxed object where each key can have an idiomatic schema.
|
|
129
175
|
*
|
|
130
|
-
* @template
|
|
176
|
+
* @template SV - The type of the schema validator.
|
|
131
177
|
*/
|
|
132
178
|
export type UnboxedObjectSchema<SV extends AnySchemaValidator> = {
|
|
133
179
|
[key: KeyTypes]: IdiomaticSchema<SV>;
|
|
@@ -139,7 +185,7 @@ export type LiteralSchema = string | number | boolean;
|
|
|
139
185
|
/**
|
|
140
186
|
* Represents an idiomatic schema which can be an unboxed object schema, a literal schema, or a catch-all type.
|
|
141
187
|
*
|
|
142
|
-
* @template
|
|
188
|
+
* @template SV - The type of the schema validator.
|
|
143
189
|
*/
|
|
144
190
|
export type IdiomaticSchema<SV extends AnySchemaValidator> = UnboxedObjectSchema<SV> | LiteralSchema | SV['_SchemaCatchall'];
|
|
145
191
|
/**
|
package/dist/zod/index.d.ts
CHANGED
|
@@ -1,147 +1 @@
|
|
|
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 {};
|
|
1
|
+
export * from './zod.schemaValidator';
|