@forklaunch/validator 0.2.7 → 0.2.8

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.
Files changed (40) hide show
  1. package/dist/index.d.ts +1 -0
  2. package/dist/index.js +18 -0
  3. package/dist/index.js.map +1 -0
  4. package/dist/jest.config.d.ts +3 -0
  5. package/dist/jest.config.js +10 -0
  6. package/dist/jest.config.js.map +1 -0
  7. package/dist/tests/mockSchemaValidator.d.ts +50 -0
  8. package/dist/tests/mockSchemaValidator.js +59 -0
  9. package/dist/tests/mockSchemaValidator.js.map +1 -0
  10. package/dist/tests/typebox/equality.test.d.ts +1 -0
  11. package/dist/tests/typebox/equality.test.js +234 -0
  12. package/dist/tests/typebox/equality.test.js.map +1 -0
  13. package/dist/tests/typebox/largeSchema.test.d.ts +1 -0
  14. package/dist/tests/typebox/largeSchema.test.js +153 -0
  15. package/dist/tests/typebox/largeSchema.test.js.map +1 -0
  16. package/dist/tests/zod/equality.test.d.ts +1 -0
  17. package/dist/tests/zod/equality.test.js +238 -0
  18. package/dist/tests/zod/equality.test.js.map +1 -0
  19. package/dist/tests/zod/largeSchema.test.d.ts +1 -0
  20. package/dist/tests/zod/largeSchema.test.js +153 -0
  21. package/dist/tests/zod/largeSchema.test.js.map +1 -0
  22. package/dist/typebox/index.d.ts +150 -0
  23. package/dist/typebox/index.js +203 -0
  24. package/dist/typebox/index.js.map +1 -0
  25. package/dist/typebox/types/typebox.schema.types.d.ts +63 -0
  26. package/dist/typebox/types/typebox.schema.types.js +3 -0
  27. package/dist/typebox/types/typebox.schema.types.js.map +1 -0
  28. package/dist/types/index.d.ts +1 -0
  29. package/dist/types/index.js +18 -0
  30. package/dist/types/index.js.map +1 -0
  31. package/dist/types/schema.types.d.ts +157 -0
  32. package/dist/types/schema.types.js +3 -0
  33. package/dist/types/schema.types.js.map +1 -0
  34. package/dist/zod/index.d.ts +147 -0
  35. package/dist/zod/index.js +196 -0
  36. package/dist/zod/index.js.map +1 -0
  37. package/dist/zod/types/zod.schema.types.d.ts +70 -0
  38. package/dist/zod/types/zod.schema.types.js +3 -0
  39. package/dist/zod/types/zod.schema.types.js.map +1 -0
  40. 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=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../typebox/index.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,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AC;AAE9C;;GAEG;AACU,QAAA,OAAO,GAClB,eAAe,CAAC,OAAO,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC;AAEhD;;GAEG;AACU,QAAA,QAAQ,GACnB,eAAe,CAAC,QAAQ,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC;AAEjD;;GAEG;AACU,QAAA,OAAO,GAClB,eAAe,CAAC,OAAO,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC"}
@@ -0,0 +1,63 @@
1
+ import { TObject as OriginalTObject, Static, TArray, TKind, TLiteral, TNever, TProperties, TSchema, TUnknown } from '@sinclair/typebox';
2
+ import { Increment, KeyTypes, LiteralSchema } from '../../types/schema.types';
3
+ /**
4
+ * Represents a catch-all schema type.
5
+ */
6
+ export type TCatchall = TSchema;
7
+ /**
8
+ * Represents an outer array schema type. If the type T is an object shape, it will return an array schema of T. Otherwise, it returns TNever.
9
+ *
10
+ * @template T - The type to check and possibly convert to an array schema.
11
+ */
12
+ export type TOuterArray<T> = T extends TObject<TObjectShape> ? TArray<T> : TNever;
13
+ /**
14
+ * Represents the shape of an object schema.
15
+ */
16
+ export type TObjectShape = TProperties;
17
+ /**
18
+ * Represents an object schema type. If the type T is an object shape, it will return the original TObject type of T. Otherwise, it returns TNever.
19
+ *
20
+ * @template T - The type to check and possibly convert to an object schema.
21
+ */
22
+ export type TObject<T> = T extends TObjectShape ? OriginalTObject<T> : TNever;
23
+ /**
24
+ * Translates a schema type T to its static type if T extends TCatchall. Otherwise, it returns TNever.
25
+ *
26
+ * @template T - The schema type to translate.
27
+ */
28
+ export type TSchemaTranslate<T> = T extends TCatchall ? Static<T> : TNever;
29
+ /**
30
+ * Represents an unboxed object schema where each key can have an idiomatic schema.
31
+ */
32
+ export type UnboxedTObjectSchema = {
33
+ [key: KeyTypes]: TIdiomaticSchema;
34
+ };
35
+ /**
36
+ * Represents an idiomatic schema which can be an unboxed object schema or a literal schema.
37
+ */
38
+ export type TIdiomaticSchema = UnboxedTObjectSchema | LiteralSchema;
39
+ /**
40
+ * Represents a container for a union of idiomatic schemas.
41
+ */
42
+ export type TUnionContainer = [...TIdiomaticSchema[]];
43
+ /**
44
+ * Resolves a union container to a tuple of resolved idiomatic schemas.
45
+ *
46
+ * @template T - The union container to resolve.
47
+ */
48
+ export type UnionTResolve<T extends TUnionContainer> = T extends [
49
+ ...infer A extends TIdiomaticSchema[]
50
+ ] ? [
51
+ ...{
52
+ [K in keyof A]: TResolve<A[K]>;
53
+ }
54
+ ] : [];
55
+ /**
56
+ * Resolves a schema type T to its resolved type. The depth is limited to 45 to prevent infinite recursion.
57
+ *
58
+ * @template T - The schema type to resolve.
59
+ * @template Depth - The current depth of the resolution.
60
+ */
61
+ export type TResolve<T, Depth extends number = 0> = Depth extends 45 ? TUnknown : T extends LiteralSchema ? TLiteral<T> : T extends TObject<TObjectShape> ? T : T extends TSchema ? T : T extends TKind ? T : T extends UnboxedTObjectSchema ? TObject<{
62
+ [K in keyof T]: TResolve<T[K], Increment<Depth>>;
63
+ }> : TNever;
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=typebox.schema.types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"typebox.schema.types.js","sourceRoot":"","sources":["../../../typebox/types/typebox.schema.types.ts"],"names":[],"mappings":""}
@@ -0,0 +1 @@
1
+ export * from './schema.types';
@@ -0,0 +1,18 @@
1
+ "use strict";
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]; } };
7
+ }
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("./schema.types"), exports);
18
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../types/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,iDAA+B"}
@@ -0,0 +1,157 @@
1
+ import { Prettify } from '@forklaunch/common';
2
+ import { SchemaObject } from 'openapi3-ts/oas31';
3
+ import { TResolve, TSchemaTranslate } from '../typebox/types/typebox.schema.types';
4
+ import { ZodResolve, ZodSchemaTranslate } from '../zod/types/zod.schema.types';
5
+ /**
6
+ * Interface representing a schema validator.
7
+ *
8
+ * @template UnionContainer - The type for union schemas.
9
+ * @template IdiomaticSchema<unknown> - The type for idiomatic schemas.
10
+ * @template Catchall - The catch-all type for all schemas.
11
+ */
12
+ 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> {
13
+ _Type: unknown;
14
+ _SchemaCatchall: unknown;
15
+ _ValidSchemaObject: unknown;
16
+ /**
17
+ * Validator for string type.
18
+ */
19
+ string: unknown;
20
+ /**
21
+ * Validator for number type.
22
+ */
23
+ number: unknown;
24
+ /**
25
+ * Validator for bigint type.
26
+ */
27
+ bigint: unknown;
28
+ /**
29
+ * Validator for boolean type.
30
+ */
31
+ boolean: unknown;
32
+ /**
33
+ * Validator for date type.
34
+ */
35
+ date: unknown;
36
+ /**
37
+ * Validator for symbol type.
38
+ */
39
+ symbol: unknown;
40
+ /**
41
+ * Validator for empty type.
42
+ */
43
+ empty: unknown;
44
+ /**
45
+ * Validator for any type.
46
+ */
47
+ any: unknown;
48
+ /**
49
+ * Validator for unknown type.
50
+ */
51
+ unknown: unknown;
52
+ /**
53
+ * Validator for never type.
54
+ */
55
+ never: unknown;
56
+ /**
57
+ * Converts a valid schema input into a schemified form.
58
+ *
59
+ * @template T - The type of the idiomatic schema.
60
+ * @param {T} schema - The schema to schemify.
61
+ * @returns {unknown} - The schemified form of the schema.
62
+ */
63
+ schemify: SchematicFunction;
64
+ /**
65
+ * Converts a schema into an optional schema.
66
+ *
67
+ * @template T - The type of the idiomatic schema.
68
+ * @param {T} schema - The schema to make optional.
69
+ * @returns {unknown} - The optional form of the schema.
70
+ */
71
+ optional: OptionalFunction;
72
+ /**
73
+ * Converts a schema into an array schema.
74
+ *
75
+ * @template T - The type of the idiomatic schema.
76
+ * @param {T} schema - The schema to convert into an array.
77
+ * @returns {unknown} - The array form of the schema.
78
+ */
79
+ array: ArrayFunction;
80
+ /**
81
+ * Converts multiple schemas into a union schema.
82
+ *
83
+ * @template T - The type of the union container.
84
+ * @param {T} schemas - The schemas to unionize.
85
+ * @returns {unknown} - The union form of the schemas.
86
+ */
87
+ union: UnionFunction;
88
+ /**
89
+ * Creates a literal schema from a value.
90
+ *
91
+ * @template T - The type of the literal value.
92
+ * @param {T} value - The literal value.
93
+ * @returns {unknown} - The literal schema.
94
+ */
95
+ literal: LiteralFunction;
96
+ /**
97
+ * Validates a value against a schema.
98
+ *
99
+ * @template T - The type of the catch-all schema.
100
+ * @param {T} schema - The schema to validate against.
101
+ * @param {unknown} value - The value to validate.
102
+ * @returns {boolean} - Whether the value is valid according to the schema.
103
+ */
104
+ validate: ValidationFunction;
105
+ /**
106
+ * Converts a schema into an OpenAPI schema object.
107
+ *
108
+ * @template T - The type of the idiomatic schema.
109
+ * @param {T} schema - The schema to convert.
110
+ * @returns {SchemaObject} - The OpenAPI schema object.
111
+ */
112
+ openapi: OpenAPIFunction;
113
+ }
114
+ export type AnySchemaValidator = SchemaValidator<unknown, unknown, unknown, unknown, unknown, unknown, unknown>;
115
+ export interface SchemaResolve<T> {
116
+ Zod: ZodResolve<T>;
117
+ TypeBox: TResolve<T>;
118
+ }
119
+ export interface SchemaTranslate<T> {
120
+ Zod: ZodSchemaTranslate<T>;
121
+ TypeBox: TSchemaTranslate<T>;
122
+ }
123
+ type SchemaPrettify<T, SV extends {
124
+ _Type: keyof SchemaResolve<T>;
125
+ } & AnySchemaValidator> = Prettify<SchemaTranslate<T>[SV['_Type']]>;
126
+ export type Schema<T extends SV['_ValidSchemaObject'] | IdiomaticSchema<SV>, SV extends {
127
+ _Type: keyof SchemaResolve<T>;
128
+ } & AnySchemaValidator> = SchemaPrettify<SchemaResolve<T>[SV['_Type']], SV>;
129
+ /**
130
+ * Represents a schema for an unboxed object where each key can have an idiomatic schema.
131
+ *
132
+ * @template Catchall - The type to use for catch-all cases in the schema.
133
+ */
134
+ export type UnboxedObjectSchema<SV extends AnySchemaValidator> = {
135
+ [key: KeyTypes]: IdiomaticSchema<SV>;
136
+ };
137
+ /**
138
+ * Represents a schema that can be a literal value.
139
+ */
140
+ export type LiteralSchema = string | number | boolean;
141
+ /**
142
+ * Represents an idiomatic schema which can be an unboxed object schema, a literal schema, or a catch-all type.
143
+ *
144
+ * @template Catchall - The type to use for catch-all cases in the schema.
145
+ */
146
+ export type IdiomaticSchema<SV extends AnySchemaValidator> = UnboxedObjectSchema<SV> | LiteralSchema | SV['_SchemaCatchall'];
147
+ /**
148
+ * Increments a number type by one, with support up to 50.
149
+ *
150
+ * @template T - The number type to increment.
151
+ */
152
+ export type Increment<T extends number> = T extends 0 ? 1 : T extends 1 ? 2 : T extends 2 ? 3 : T extends 3 ? 4 : T extends 4 ? 5 : T extends 5 ? 6 : T extends 6 ? 7 : T extends 7 ? 8 : T extends 8 ? 9 : T extends 9 ? 10 : T extends 10 ? 11 : T extends 11 ? 12 : T extends 12 ? 13 : T extends 13 ? 14 : T extends 14 ? 15 : T extends 15 ? 16 : T extends 16 ? 17 : T extends 17 ? 18 : T extends 18 ? 19 : T extends 19 ? 20 : T extends 20 ? 21 : T extends 21 ? 22 : T extends 22 ? 23 : T extends 23 ? 24 : T extends 24 ? 25 : T extends 25 ? 26 : T extends 26 ? 27 : T extends 27 ? 28 : T extends 28 ? 29 : T extends 29 ? 30 : T extends 30 ? 31 : T extends 31 ? 32 : T extends 32 ? 33 : T extends 33 ? 34 : T extends 34 ? 35 : T extends 35 ? 36 : T extends 36 ? 37 : T extends 37 ? 38 : T extends 38 ? 39 : T extends 39 ? 40 : T extends 40 ? 41 : T extends 41 ? 42 : T extends 42 ? 43 : T extends 43 ? 44 : T extends 44 ? 45 : T extends 45 ? 46 : T extends 46 ? 47 : T extends 47 ? 48 : T extends 48 ? 49 : T extends 49 ? 50 : 50;
153
+ /**
154
+ * Represents key types that can be used in the schema.
155
+ */
156
+ export type KeyTypes = string | number;
157
+ export {};
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=schema.types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.types.js","sourceRoot":"","sources":["../../types/schema.types.ts"],"names":[],"mappings":""}
@@ -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 {};