@cabloy/zod-query 1.0.17 → 2.0.3
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/index.d.ts +0 -1
- package/dist/index.js +0 -1
- package/dist/lib/metadata.d.ts +6 -0
- package/dist/lib/metadata.js +31 -0
- package/dist/lib/query.d.ts +4 -2
- package/dist/lib/query.js +111 -101
- package/dist/lib/zod-is-type.d.ts +43 -0
- package/dist/lib/zod-is-type.js +58 -0
- package/package.json +1 -1
- package/dist/lib/zodMetadata.d.ts +0 -12
- package/dist/lib/zodMetadata.js +0 -56
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type z from 'zod';
|
|
2
|
+
import type { ZodTypes } from './zod-is-type.ts';
|
|
3
|
+
export declare class Metadata {
|
|
4
|
+
static unwrapUntil(schema: z.ZodType): z.ZodType;
|
|
5
|
+
static unwrapUntil<TypeName extends keyof ZodTypes>(schema: z.ZodType, typeName: TypeName | undefined): ZodTypes[TypeName] | undefined;
|
|
6
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { isAnyZodType, isZodType } from "./zod-is-type.js";
|
|
2
|
+
export class Metadata {
|
|
3
|
+
static unwrapUntil(schema, typeName) {
|
|
4
|
+
if (typeName && isZodType(schema, typeName)) {
|
|
5
|
+
return schema;
|
|
6
|
+
}
|
|
7
|
+
if (isZodType(schema, [
|
|
8
|
+
'ZodOptional',
|
|
9
|
+
'ZodNullable',
|
|
10
|
+
'ZodDefault',
|
|
11
|
+
'ZodReadonly',
|
|
12
|
+
'ZodNonOptional',
|
|
13
|
+
]) &&
|
|
14
|
+
isAnyZodType(schema._zod.def.innerType)) {
|
|
15
|
+
return this.unwrapUntil(schema._zod.def.innerType, typeName);
|
|
16
|
+
}
|
|
17
|
+
if (isZodType(schema, 'ZodPipe')) {
|
|
18
|
+
const inSchema = schema._zod.def.in;
|
|
19
|
+
const outSchema = schema._zod.def.out;
|
|
20
|
+
// meaning preprocess
|
|
21
|
+
if (isZodType(inSchema, 'ZodTransform') && isAnyZodType(outSchema)) {
|
|
22
|
+
return this.unwrapUntil(outSchema, typeName);
|
|
23
|
+
}
|
|
24
|
+
// meaning transform
|
|
25
|
+
if (isAnyZodType(inSchema)) {
|
|
26
|
+
return this.unwrapUntil(inSchema, typeName);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return typeName ? undefined : schema;
|
|
30
|
+
}
|
|
31
|
+
}
|
package/dist/lib/query.d.ts
CHANGED
|
@@ -1,2 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
export declare function
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
export declare function setParseAdapter(zodMetadata: any): void;
|
|
3
|
+
export declare function coerceWithNil(value: any, fn?: Function): any;
|
|
4
|
+
export declare function getInnerTypeName(schema: z.ZodType): "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function" | "any" | "array" | "default" | "transform" | "enum" | "intersection" | "literal" | "never" | "null" | "nullable" | "nonoptional" | "optional" | "pipe" | "readonly" | "record" | "tuple" | "union" | "unknown" | "void" | "date" | "map" | "int" | "file" | "set" | "success" | "prefault" | "catch" | "nan" | "template_literal" | "promise" | "lazy" | "custom";
|
package/dist/lib/query.js
CHANGED
|
@@ -1,5 +1,29 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
|
+
import { Metadata } from "./metadata.js";
|
|
2
3
|
import { isNil } from "./utils.js";
|
|
4
|
+
import { isZodType } from "./zod-is-type.js";
|
|
5
|
+
let ZodMetadata;
|
|
6
|
+
export function setParseAdapter(zodMetadata) {
|
|
7
|
+
ZodMetadata = zodMetadata;
|
|
8
|
+
(z.core).setParseAdapter(__parseAdapter);
|
|
9
|
+
}
|
|
10
|
+
function __parseAdapter(inst, parse) {
|
|
11
|
+
return (payload, _) => {
|
|
12
|
+
const type = inst.type;
|
|
13
|
+
switch (type) {
|
|
14
|
+
case 'string': return __parseString(inst, parse, payload, _);
|
|
15
|
+
case 'number': return __parseNumber(inst, parse, payload, _);
|
|
16
|
+
case 'bigint': return __parseBigInt(inst, parse, payload, _);
|
|
17
|
+
case 'boolean': return __parseBoolean(inst, parse, payload, _);
|
|
18
|
+
case 'date': return __parseDate(inst, parse, payload, _);
|
|
19
|
+
case 'object': return __parseObject(inst, parse, payload, _);
|
|
20
|
+
case 'array': return __parseArray(inst, parse, payload, _);
|
|
21
|
+
case 'optional': return __parseOptional(inst, parse, payload, _);
|
|
22
|
+
case 'default': return __parseDefault(inst, parse, payload, _);
|
|
23
|
+
}
|
|
24
|
+
return parse(payload, _);
|
|
25
|
+
};
|
|
26
|
+
}
|
|
3
27
|
/////////////////////////////////////////
|
|
4
28
|
/////////////////////////////////////////
|
|
5
29
|
/// /////// /////////
|
|
@@ -7,17 +31,15 @@ import { isNil } from "./utils.js";
|
|
|
7
31
|
/// /////// /////////
|
|
8
32
|
/////////////////////////////////////////
|
|
9
33
|
/////////////////////////////////////////
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
if (
|
|
34
|
+
function __parseString(inst, parse, payload, _) {
|
|
35
|
+
const metadata = ZodMetadata?.getOpenapiMetadata(inst);
|
|
36
|
+
if (metadata?.format === 'binary' && !isNil(payload.value)) {
|
|
13
37
|
// ignore upload file
|
|
14
|
-
return
|
|
38
|
+
return payload;
|
|
15
39
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
return _parseString.call(this, input);
|
|
20
|
-
};
|
|
40
|
+
_coerceString(payload);
|
|
41
|
+
return parse(payload, _);
|
|
42
|
+
}
|
|
21
43
|
/////////////////////////////////////////
|
|
22
44
|
/////////////////////////////////////////
|
|
23
45
|
/// /////// /////////
|
|
@@ -25,13 +47,12 @@ z.ZodString.prototype._parse = function (input) {
|
|
|
25
47
|
/// /////// /////////
|
|
26
48
|
/////////////////////////////////////////
|
|
27
49
|
/////////////////////////////////////////
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
input.data = Number(input.data);
|
|
50
|
+
function __parseNumber(_inst, parse, payload, _) {
|
|
51
|
+
_coerceWithNil(payload, () => {
|
|
52
|
+
payload.value = Number(payload.value);
|
|
32
53
|
});
|
|
33
|
-
return
|
|
34
|
-
}
|
|
54
|
+
return parse(payload, _);
|
|
55
|
+
}
|
|
35
56
|
/////////////////////////////////////////
|
|
36
57
|
/////////////////////////////////////////
|
|
37
58
|
/// /////// /////////
|
|
@@ -39,13 +60,12 @@ z.ZodNumber.prototype._parse = function (input) {
|
|
|
39
60
|
/// /////// /////////
|
|
40
61
|
/////////////////////////////////////////
|
|
41
62
|
/////////////////////////////////////////
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
input.data = BigInt(input.data);
|
|
63
|
+
function __parseBigInt(_inst, parse, payload, _) {
|
|
64
|
+
_coerceWithNil(payload, () => {
|
|
65
|
+
payload.value = BigInt(payload.value);
|
|
46
66
|
});
|
|
47
|
-
return
|
|
48
|
-
}
|
|
67
|
+
return parse(payload, _);
|
|
68
|
+
}
|
|
49
69
|
//////////////////////////////////////////
|
|
50
70
|
//////////////////////////////////////////
|
|
51
71
|
/// /////// //////////
|
|
@@ -53,18 +73,17 @@ z.ZodBigInt.prototype._parse = function (input) {
|
|
|
53
73
|
/// /////// //////////
|
|
54
74
|
//////////////////////////////////////////
|
|
55
75
|
//////////////////////////////////////////
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
input.data = false;
|
|
76
|
+
function __parseBoolean(_inst, parse, payload, _) {
|
|
77
|
+
_coerceWithNil(payload, () => {
|
|
78
|
+
if (payload.value === 'false' || payload.value === '0') {
|
|
79
|
+
payload.value = false;
|
|
61
80
|
}
|
|
62
81
|
else {
|
|
63
|
-
|
|
82
|
+
payload.value = Boolean(payload.value);
|
|
64
83
|
}
|
|
65
84
|
});
|
|
66
|
-
return
|
|
67
|
-
}
|
|
85
|
+
return parse(payload, _);
|
|
86
|
+
}
|
|
68
87
|
///////////////////////////////////////
|
|
69
88
|
///////////////////////////////////////
|
|
70
89
|
/// /////// ///////
|
|
@@ -72,13 +91,12 @@ z.ZodBoolean.prototype._parse = function (input) {
|
|
|
72
91
|
/// /////// ///////
|
|
73
92
|
///////////////////////////////////////
|
|
74
93
|
///////////////////////////////////////
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
input.data = new Date(input.data);
|
|
94
|
+
function __parseDate(_inst, parse, payload, _) {
|
|
95
|
+
_coerceWithNil(payload, () => {
|
|
96
|
+
payload.value = new Date(payload.value);
|
|
79
97
|
});
|
|
80
|
-
return
|
|
81
|
-
}
|
|
98
|
+
return parse(payload, _);
|
|
99
|
+
}
|
|
82
100
|
/////////////////////////////////////////
|
|
83
101
|
/////////////////////////////////////////
|
|
84
102
|
/// /////// /////////
|
|
@@ -86,15 +104,14 @@ z.ZodDate.prototype._parse = function (input) {
|
|
|
86
104
|
/// /////// /////////
|
|
87
105
|
/////////////////////////////////////////
|
|
88
106
|
/////////////////////////////////////////
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
input.data = JSON.parse(input.data);
|
|
107
|
+
function __parseObject(_inst, parse, payload, _) {
|
|
108
|
+
_coerceWithNil(payload, () => {
|
|
109
|
+
if (typeof payload.value === 'string') {
|
|
110
|
+
payload.value = JSON.parse(payload.value);
|
|
94
111
|
}
|
|
95
112
|
});
|
|
96
|
-
return
|
|
97
|
-
}
|
|
113
|
+
return parse(payload, _);
|
|
114
|
+
}
|
|
98
115
|
////////////////////////////////////////
|
|
99
116
|
////////////////////////////////////////
|
|
100
117
|
/// /////// /////////
|
|
@@ -102,20 +119,19 @@ z.ZodObject.prototype._parse = function (input) {
|
|
|
102
119
|
/// /////// /////////
|
|
103
120
|
////////////////////////////////////////
|
|
104
121
|
////////////////////////////////////////
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
input.data = JSON.parse(input.data);
|
|
122
|
+
function __parseArray(_inst, parse, payload, _) {
|
|
123
|
+
_coerceWithNil(payload, () => {
|
|
124
|
+
if (typeof payload.value === 'string') {
|
|
125
|
+
if (payload.value.startsWith('[') && payload.value.endsWith(']')) {
|
|
126
|
+
payload.value = JSON.parse(payload.value);
|
|
111
127
|
}
|
|
112
128
|
else {
|
|
113
|
-
|
|
129
|
+
payload.value = payload.value.split(',');
|
|
114
130
|
}
|
|
115
131
|
}
|
|
116
132
|
});
|
|
117
|
-
return
|
|
118
|
-
}
|
|
133
|
+
return parse(payload, _);
|
|
134
|
+
}
|
|
119
135
|
///////////////////////////////////////////
|
|
120
136
|
///////////////////////////////////////////
|
|
121
137
|
/// /////// /////////
|
|
@@ -123,19 +139,18 @@ z.ZodArray.prototype._parse = function (input) {
|
|
|
123
139
|
/// /////// /////////
|
|
124
140
|
///////////////////////////////////////////
|
|
125
141
|
///////////////////////////////////////////
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
_coerce(input);
|
|
142
|
+
function __parseOptional(_inst, parse, payload, _) {
|
|
143
|
+
if (isZodType(Metadata.unwrapUntil(_inst), 'ZodString')) {
|
|
144
|
+
_coerceString(payload);
|
|
130
145
|
}
|
|
131
146
|
else {
|
|
132
|
-
_coerceWithNil(
|
|
147
|
+
_coerceWithNil(payload);
|
|
133
148
|
}
|
|
134
|
-
if (
|
|
135
|
-
|
|
149
|
+
if (payload.value === null) {
|
|
150
|
+
payload.value = undefined;
|
|
136
151
|
}
|
|
137
|
-
return
|
|
138
|
-
}
|
|
152
|
+
return parse(payload, _);
|
|
153
|
+
}
|
|
139
154
|
////////////////////////////////////////////
|
|
140
155
|
////////////////////////////////////////////
|
|
141
156
|
/// /////// /////////
|
|
@@ -143,16 +158,15 @@ z.ZodOptional.prototype._parse = function (input) {
|
|
|
143
158
|
/// /////// /////////
|
|
144
159
|
////////////////////////////////////////////
|
|
145
160
|
////////////////////////////////////////////
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
_coerce(input);
|
|
161
|
+
function __parseDefault(_inst, parse, payload, _) {
|
|
162
|
+
if (isZodType(Metadata.unwrapUntil(_inst), 'ZodString')) {
|
|
163
|
+
_coerceString(payload);
|
|
150
164
|
}
|
|
151
165
|
else {
|
|
152
|
-
_coerceWithNil(
|
|
166
|
+
_coerceWithNil(payload);
|
|
153
167
|
}
|
|
154
|
-
return
|
|
155
|
-
}
|
|
168
|
+
return parse(payload, _);
|
|
169
|
+
}
|
|
156
170
|
///////////////////////////////////////
|
|
157
171
|
///////////////////////////////////////
|
|
158
172
|
/// /////// ///////
|
|
@@ -160,53 +174,49 @@ z.ZodDefault.prototype._parse = function (input) {
|
|
|
160
174
|
/// /////// ///////
|
|
161
175
|
///////////////////////////////////////
|
|
162
176
|
///////////////////////////////////////
|
|
163
|
-
function _coerce(
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
}
|
|
168
|
-
function
|
|
169
|
-
if (!isNil(
|
|
170
|
-
if (
|
|
171
|
-
|
|
177
|
+
// function _coerce(payload: IParsePayload, fn?: Function) {
|
|
178
|
+
// if (!isNil(payload.value)) {
|
|
179
|
+
// fn?.(payload);
|
|
180
|
+
// }
|
|
181
|
+
// }
|
|
182
|
+
function _coerceString(payload, fn) {
|
|
183
|
+
if (!isNil(payload.value)) {
|
|
184
|
+
if (payload.value === '') {
|
|
185
|
+
payload.value = undefined;
|
|
172
186
|
}
|
|
173
|
-
else if (
|
|
174
|
-
|
|
187
|
+
else if (typeof payload.value !== 'string') {
|
|
188
|
+
payload.value = String(payload.value);
|
|
175
189
|
}
|
|
176
190
|
else {
|
|
177
|
-
fn?.();
|
|
191
|
+
fn?.(payload);
|
|
178
192
|
}
|
|
179
193
|
}
|
|
180
194
|
}
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
195
|
+
function _coerceWithNil(payload, fn) {
|
|
196
|
+
if (!isNil(payload.value)) {
|
|
197
|
+
if (payload.value === 'undefined' || payload.value === '') {
|
|
198
|
+
payload.value = undefined;
|
|
199
|
+
}
|
|
200
|
+
else if (payload.value === 'null') {
|
|
201
|
+
payload.value = null;
|
|
202
|
+
}
|
|
203
|
+
else {
|
|
204
|
+
fn?.(payload);
|
|
205
|
+
}
|
|
192
206
|
}
|
|
193
|
-
return innerType;
|
|
194
207
|
}
|
|
195
|
-
function
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
typeName = schema.typeName;
|
|
200
|
-
}
|
|
201
|
-
return typeName;
|
|
208
|
+
export function coerceWithNil(value, fn) {
|
|
209
|
+
const payload = { value, issues: [] };
|
|
210
|
+
_coerceWithNil(payload, fn);
|
|
211
|
+
return payload.value;
|
|
202
212
|
}
|
|
203
|
-
export function
|
|
204
|
-
return
|
|
213
|
+
export function getInnerTypeName(schema) {
|
|
214
|
+
return Metadata.unwrapUntil(schema).type;
|
|
205
215
|
}
|
|
206
216
|
// function _coerce(instance, input, fn) {
|
|
207
217
|
// if (instance._def.coerce !== false) {
|
|
208
218
|
// if (instance._def.coerce === true) instance._def.coerce = undefined as any;
|
|
209
|
-
// if (!isNil(
|
|
219
|
+
// if (!isNil(payload.value)) {
|
|
210
220
|
// fn();
|
|
211
221
|
// }
|
|
212
222
|
// }
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { z } from 'zod';
|
|
2
|
+
export interface ZodTypes {
|
|
3
|
+
ZodAny: z.ZodAny;
|
|
4
|
+
ZodArray: z.ZodArray;
|
|
5
|
+
ZodBigInt: z.ZodBigInt;
|
|
6
|
+
ZodBoolean: z.ZodBoolean;
|
|
7
|
+
ZodDefault: z.ZodDefault;
|
|
8
|
+
ZodTransform: z.ZodTransform;
|
|
9
|
+
ZodEnum: z.ZodEnum;
|
|
10
|
+
ZodIntersection: z.ZodIntersection;
|
|
11
|
+
ZodLiteral: z.ZodLiteral;
|
|
12
|
+
ZodNever: z.ZodNever;
|
|
13
|
+
ZodNull: z.ZodNull;
|
|
14
|
+
ZodNullable: z.ZodNullable;
|
|
15
|
+
ZodNumber: z.ZodNumber;
|
|
16
|
+
ZodNonOptional: z.ZodNonOptional;
|
|
17
|
+
ZodObject: z.ZodObject;
|
|
18
|
+
ZodOptional: z.ZodOptional;
|
|
19
|
+
ZodPipe: z.ZodPipe;
|
|
20
|
+
ZodReadonly: z.ZodReadonly;
|
|
21
|
+
ZodRecord: z.ZodRecord;
|
|
22
|
+
ZodString: z.ZodString;
|
|
23
|
+
ZodTuple: z.ZodTuple;
|
|
24
|
+
ZodType: z.ZodType;
|
|
25
|
+
ZodUnion: z.ZodUnion;
|
|
26
|
+
ZodDiscriminatedUnion: z.ZodDiscriminatedUnion;
|
|
27
|
+
ZodUnknown: z.ZodUnknown;
|
|
28
|
+
ZodVoid: z.ZodVoid;
|
|
29
|
+
ZodDate: z.ZodDate;
|
|
30
|
+
}
|
|
31
|
+
export declare function isZodType<TypeName extends keyof ZodTypes>(schema: object, typeNames: TypeName[]): schema is ZodTypes[TypeName];
|
|
32
|
+
export declare function isZodType<TypeName extends keyof ZodTypes>(schema: object, typeName: TypeName): schema is ZodTypes[TypeName];
|
|
33
|
+
export declare function isAnyZodType(schema: object): schema is z.ZodType;
|
|
34
|
+
/**
|
|
35
|
+
* The schema.isNullable() is deprecated. This is the suggested replacement
|
|
36
|
+
* as this was how isNullable operated beforehand.
|
|
37
|
+
*/
|
|
38
|
+
export declare function isNullableSchema(schema: z.ZodType): boolean;
|
|
39
|
+
/**
|
|
40
|
+
* The schema.isOptional() is deprecated. This is the suggested replacement
|
|
41
|
+
* as this was how isOptional operated beforehand.
|
|
42
|
+
*/
|
|
43
|
+
export declare function isOptionalSchema(schema: z.ZodType): boolean;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
;
|
|
2
|
+
const ZodTypeKeys = {
|
|
3
|
+
ZodAny: 'any',
|
|
4
|
+
ZodArray: 'array',
|
|
5
|
+
ZodBigInt: 'bigint',
|
|
6
|
+
ZodBoolean: 'boolean',
|
|
7
|
+
ZodDefault: 'default',
|
|
8
|
+
ZodTransform: 'transform',
|
|
9
|
+
ZodEnum: 'enum',
|
|
10
|
+
ZodIntersection: 'intersection',
|
|
11
|
+
ZodLiteral: 'literal',
|
|
12
|
+
ZodNever: 'never',
|
|
13
|
+
ZodNull: 'null',
|
|
14
|
+
ZodNullable: 'nullable',
|
|
15
|
+
ZodNumber: 'number',
|
|
16
|
+
ZodNonOptional: 'nonoptional',
|
|
17
|
+
ZodObject: 'object',
|
|
18
|
+
ZodOptional: 'optional',
|
|
19
|
+
ZodPipe: 'pipe',
|
|
20
|
+
ZodReadonly: 'readonly',
|
|
21
|
+
ZodRecord: 'record',
|
|
22
|
+
ZodString: 'string',
|
|
23
|
+
ZodTuple: 'tuple',
|
|
24
|
+
ZodType: 'type',
|
|
25
|
+
ZodUnion: 'union',
|
|
26
|
+
ZodDiscriminatedUnion: 'union',
|
|
27
|
+
ZodUnknown: 'unknown',
|
|
28
|
+
ZodVoid: 'void',
|
|
29
|
+
ZodDate: 'date',
|
|
30
|
+
};
|
|
31
|
+
export function isZodType(schema, typeNames) {
|
|
32
|
+
const typeNamesArray = Array.isArray(typeNames) ? typeNames : [typeNames];
|
|
33
|
+
return typeNamesArray.some(typeName => {
|
|
34
|
+
const typeNameMatch = schema?.def?.type === ZodTypeKeys[typeName];
|
|
35
|
+
if (typeName === 'ZodDiscriminatedUnion') {
|
|
36
|
+
return (typeNameMatch &&
|
|
37
|
+
'discriminator' in schema.def);
|
|
38
|
+
}
|
|
39
|
+
return typeNameMatch;
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
export function isAnyZodType(schema) {
|
|
43
|
+
return 'def' in schema;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* The schema.isNullable() is deprecated. This is the suggested replacement
|
|
47
|
+
* as this was how isNullable operated beforehand.
|
|
48
|
+
*/
|
|
49
|
+
export function isNullableSchema(schema) {
|
|
50
|
+
return schema.safeParse(null).success;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* The schema.isOptional() is deprecated. This is the suggested replacement
|
|
54
|
+
* as this was how isOptional operated beforehand.
|
|
55
|
+
*/
|
|
56
|
+
export function isOptionalSchema(schema) {
|
|
57
|
+
return schema.safeParse(undefined).success;
|
|
58
|
+
}
|
package/package.json
CHANGED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import type z from 'zod';
|
|
2
|
-
export declare class ZodMetadata {
|
|
3
|
-
static unwrapUntil(schema: any, typeName?: any): any;
|
|
4
|
-
static unwrapChained(schema: any): any;
|
|
5
|
-
static getDefaultValue(zodSchema: any): any;
|
|
6
|
-
static getInternalMetadata(zodSchema: any): any;
|
|
7
|
-
static getLazySchema(zodSchema: any): any;
|
|
8
|
-
static getRefId(zodSchema: any): any;
|
|
9
|
-
static getFieldSchema(schema: any, key: string): z.ZodSchema;
|
|
10
|
-
static getOpenapiMetadata(schema: any): any;
|
|
11
|
-
}
|
|
12
|
-
export declare function isZodType(schema: any, typeName: any): boolean;
|
package/dist/lib/zodMetadata.js
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
export class ZodMetadata {
|
|
2
|
-
static unwrapUntil(schema, typeName) {
|
|
3
|
-
if (typeName && isZodType(schema, typeName)) {
|
|
4
|
-
return schema;
|
|
5
|
-
}
|
|
6
|
-
if (isZodType(schema, 'ZodOptional') ||
|
|
7
|
-
isZodType(schema, 'ZodNullable') ||
|
|
8
|
-
isZodType(schema, 'ZodBranded')) {
|
|
9
|
-
return this.unwrapUntil(schema.unwrap(), typeName);
|
|
10
|
-
}
|
|
11
|
-
if (isZodType(schema, 'ZodDefault') || isZodType(schema, 'ZodReadonly')) {
|
|
12
|
-
return this.unwrapUntil(schema._def.innerType, typeName);
|
|
13
|
-
}
|
|
14
|
-
if (isZodType(schema, 'ZodEffects')) {
|
|
15
|
-
return this.unwrapUntil(schema._def.schema, typeName);
|
|
16
|
-
}
|
|
17
|
-
if (isZodType(schema, 'ZodPipeline')) {
|
|
18
|
-
return this.unwrapUntil(schema._def.in, typeName);
|
|
19
|
-
}
|
|
20
|
-
return typeName ? undefined : schema;
|
|
21
|
-
}
|
|
22
|
-
static unwrapChained(schema) {
|
|
23
|
-
return this.unwrapUntil(schema);
|
|
24
|
-
}
|
|
25
|
-
static getDefaultValue(zodSchema) {
|
|
26
|
-
const unwrapped = this.unwrapUntil(zodSchema, 'ZodDefault');
|
|
27
|
-
return unwrapped === null || unwrapped === void 0 ? void 0 : unwrapped._def.defaultValue();
|
|
28
|
-
}
|
|
29
|
-
static getInternalMetadata(zodSchema) {
|
|
30
|
-
const innerSchema = this.unwrapChained(zodSchema);
|
|
31
|
-
const openapi = zodSchema._def.openapi
|
|
32
|
-
? zodSchema._def.openapi
|
|
33
|
-
: innerSchema._def.openapi;
|
|
34
|
-
return openapi === null || openapi === void 0 ? void 0 : openapi._internal;
|
|
35
|
-
}
|
|
36
|
-
static getLazySchema(zodSchema) {
|
|
37
|
-
const innerSchema = this.unwrapChained(zodSchema);
|
|
38
|
-
return zodSchema._def?.getter ?? innerSchema._def?.getter;
|
|
39
|
-
}
|
|
40
|
-
static getRefId(zodSchema) {
|
|
41
|
-
let _a;
|
|
42
|
-
// eslint-disable-next-line
|
|
43
|
-
return (_a = this.getInternalMetadata(zodSchema)) === null || _a === void 0 ? void 0 : _a.refId;
|
|
44
|
-
}
|
|
45
|
-
static getFieldSchema(schema, key) {
|
|
46
|
-
return schema.shape[key];
|
|
47
|
-
}
|
|
48
|
-
static getOpenapiMetadata(schema) {
|
|
49
|
-
return schema._def.openapi?.metadata;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
export function isZodType(schema, typeName) {
|
|
53
|
-
let _a;
|
|
54
|
-
// eslint-disable-next-line
|
|
55
|
-
return ((_a = schema === null || schema === void 0 ? void 0 : schema._def) === null || _a === void 0 ? void 0 : _a.typeName) === typeName;
|
|
56
|
-
}
|