@cabloy/zod-query 1.0.16 → 2.0.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/index.d.ts CHANGED
@@ -1,2 +1 @@
1
1
  export * from './lib/query.ts';
2
- export * from './lib/zodMetadata.ts';
package/dist/index.js CHANGED
@@ -1,2 +1 @@
1
1
  export * from "./lib/query.js";
2
- export * from "./lib/zodMetadata.js";
@@ -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
+ }
@@ -1,2 +1,4 @@
1
- export declare function coerceWithNil(data: any, fn?: Function): any;
2
- export declare function getTypeName(schema: any): any;
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,17 @@ import { isNil } from "./utils.js";
7
31
  /// /////// /////////
8
32
  /////////////////////////////////////////
9
33
  /////////////////////////////////////////
10
- const _parseString = z.ZodString.prototype._parse;
11
- z.ZodString.prototype._parse = function (input) {
12
- if (this._def?.openapi?.metadata?.format === 'binary' && !isNil(input.data)) {
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 { status: 'valid', value: input.data };
38
+ return payload;
15
39
  }
16
- _coerce(input, () => {
17
- input.data = String(input.data);
40
+ _coerce(payload, () => {
41
+ payload.value = String(payload.value);
18
42
  });
19
- return _parseString.call(this, input);
20
- };
43
+ return parse(payload, _);
44
+ }
21
45
  /////////////////////////////////////////
22
46
  /////////////////////////////////////////
23
47
  /// /////// /////////
@@ -25,13 +49,12 @@ z.ZodString.prototype._parse = function (input) {
25
49
  /// /////// /////////
26
50
  /////////////////////////////////////////
27
51
  /////////////////////////////////////////
28
- const _parseNumber = z.ZodNumber.prototype._parse;
29
- z.ZodNumber.prototype._parse = function (input) {
30
- _coerceWithNil(input, () => {
31
- input.data = Number(input.data);
52
+ function __parseNumber(_inst, parse, payload, _) {
53
+ _coerceWithNil(payload, () => {
54
+ payload.value = Number(payload.value);
32
55
  });
33
- return _parseNumber.call(this, input);
34
- };
56
+ return parse(payload, _);
57
+ }
35
58
  /////////////////////////////////////////
36
59
  /////////////////////////////////////////
37
60
  /// /////// /////////
@@ -39,13 +62,12 @@ z.ZodNumber.prototype._parse = function (input) {
39
62
  /// /////// /////////
40
63
  /////////////////////////////////////////
41
64
  /////////////////////////////////////////
42
- const _parseBigInt = z.ZodBigInt.prototype._parse;
43
- z.ZodBigInt.prototype._parse = function (input) {
44
- _coerceWithNil(input, () => {
45
- input.data = BigInt(input.data);
65
+ function __parseBigInt(_inst, parse, payload, _) {
66
+ _coerceWithNil(payload, () => {
67
+ payload.value = BigInt(payload.value);
46
68
  });
47
- return _parseBigInt.call(this, input);
48
- };
69
+ return parse(payload, _);
70
+ }
49
71
  //////////////////////////////////////////
50
72
  //////////////////////////////////////////
51
73
  /// /////// //////////
@@ -53,18 +75,17 @@ z.ZodBigInt.prototype._parse = function (input) {
53
75
  /// /////// //////////
54
76
  //////////////////////////////////////////
55
77
  //////////////////////////////////////////
56
- const _parseBoolean = z.ZodBoolean.prototype._parse;
57
- z.ZodBoolean.prototype._parse = function (input) {
58
- _coerceWithNil(input, () => {
59
- if (input.data === 'false' || input.data === '0') {
60
- input.data = false;
78
+ function __parseBoolean(_inst, parse, payload, _) {
79
+ _coerceWithNil(payload, () => {
80
+ if (payload.value === 'false' || payload.value === '0') {
81
+ payload.value = false;
61
82
  }
62
83
  else {
63
- input.data = Boolean(input.data);
84
+ payload.value = Boolean(payload.value);
64
85
  }
65
86
  });
66
- return _parseBoolean.call(this, input);
67
- };
87
+ return parse(payload, _);
88
+ }
68
89
  ///////////////////////////////////////
69
90
  ///////////////////////////////////////
70
91
  /// /////// ///////
@@ -72,13 +93,12 @@ z.ZodBoolean.prototype._parse = function (input) {
72
93
  /// /////// ///////
73
94
  ///////////////////////////////////////
74
95
  ///////////////////////////////////////
75
- const _parseDate = z.ZodDate.prototype._parse;
76
- z.ZodDate.prototype._parse = function (input) {
77
- _coerceWithNil(input, () => {
78
- input.data = new Date(input.data);
96
+ function __parseDate(_inst, parse, payload, _) {
97
+ _coerceWithNil(payload, () => {
98
+ payload.value = new Date(payload.value);
79
99
  });
80
- return _parseDate.call(this, input);
81
- };
100
+ return parse(payload, _);
101
+ }
82
102
  /////////////////////////////////////////
83
103
  /////////////////////////////////////////
84
104
  /// /////// /////////
@@ -86,15 +106,14 @@ z.ZodDate.prototype._parse = function (input) {
86
106
  /// /////// /////////
87
107
  /////////////////////////////////////////
88
108
  /////////////////////////////////////////
89
- const _parseObject = z.ZodObject.prototype._parse;
90
- z.ZodObject.prototype._parse = function (input) {
91
- _coerceWithNil(input, () => {
92
- if (typeof input.data === 'string') {
93
- input.data = JSON.parse(input.data);
109
+ function __parseObject(_inst, parse, payload, _) {
110
+ _coerceWithNil(payload, () => {
111
+ if (typeof payload.value === 'string') {
112
+ payload.value = JSON.parse(payload.value);
94
113
  }
95
114
  });
96
- return _parseObject.call(this, input);
97
- };
115
+ return parse(payload, _);
116
+ }
98
117
  ////////////////////////////////////////
99
118
  ////////////////////////////////////////
100
119
  /// /////// /////////
@@ -102,20 +121,19 @@ z.ZodObject.prototype._parse = function (input) {
102
121
  /// /////// /////////
103
122
  ////////////////////////////////////////
104
123
  ////////////////////////////////////////
105
- const _parseArray = z.ZodArray.prototype._parse;
106
- z.ZodArray.prototype._parse = function (input) {
107
- _coerceWithNil(input, () => {
108
- if (typeof input.data === 'string') {
109
- if (input.data[0] === '[') {
110
- input.data = JSON.parse(input.data);
124
+ function __parseArray(_inst, parse, payload, _) {
125
+ _coerceWithNil(payload, () => {
126
+ if (typeof payload.value === 'string') {
127
+ if (payload.value.startsWith('[') && payload.value.endsWith(']')) {
128
+ payload.value = JSON.parse(payload.value);
111
129
  }
112
130
  else {
113
- input.data = input.data.split(',');
131
+ payload.value = payload.value.split(',');
114
132
  }
115
133
  }
116
134
  });
117
- return _parseArray.call(this, input);
118
- };
135
+ return parse(payload, _);
136
+ }
119
137
  ///////////////////////////////////////////
120
138
  ///////////////////////////////////////////
121
139
  /// /////// /////////
@@ -123,19 +141,18 @@ z.ZodArray.prototype._parse = function (input) {
123
141
  /// /////// /////////
124
142
  ///////////////////////////////////////////
125
143
  ///////////////////////////////////////////
126
- const _parseOptional = z.ZodOptional.prototype._parse;
127
- z.ZodOptional.prototype._parse = function (input) {
128
- if (_getTypeName(_getInnerType(this)) === 'ZodString') {
129
- _coerce(input);
144
+ function __parseOptional(_inst, parse, payload, _) {
145
+ if (isZodType(Metadata.unwrapUntil(_inst), 'ZodString')) {
146
+ _coerce(payload);
130
147
  }
131
148
  else {
132
- _coerceWithNil(input);
149
+ _coerceWithNil(payload);
133
150
  }
134
- if (input.data === null) {
135
- input.data = undefined;
151
+ if (payload.value === null) {
152
+ payload.value = undefined;
136
153
  }
137
- return _parseOptional.call(this, input);
138
- };
154
+ return parse(payload, _);
155
+ }
139
156
  ////////////////////////////////////////////
140
157
  ////////////////////////////////////////////
141
158
  /// /////// /////////
@@ -143,16 +160,15 @@ z.ZodOptional.prototype._parse = function (input) {
143
160
  /// /////// /////////
144
161
  ////////////////////////////////////////////
145
162
  ////////////////////////////////////////////
146
- const _parseDefault = z.ZodDefault.prototype._parse;
147
- z.ZodDefault.prototype._parse = function (input) {
148
- if (_getTypeName(_getInnerType(this)) === 'ZodString') {
149
- _coerce(input);
163
+ function __parseDefault(_inst, parse, payload, _) {
164
+ if (isZodType(Metadata.unwrapUntil(_inst), 'ZodString')) {
165
+ _coerce(payload);
150
166
  }
151
167
  else {
152
- _coerceWithNil(input);
168
+ _coerceWithNil(payload);
153
169
  }
154
- return _parseDefault.call(this, input);
155
- };
170
+ return parse(payload, _);
171
+ }
156
172
  ///////////////////////////////////////
157
173
  ///////////////////////////////////////
158
174
  /// /////// ///////
@@ -160,53 +176,36 @@ z.ZodDefault.prototype._parse = function (input) {
160
176
  /// /////// ///////
161
177
  ///////////////////////////////////////
162
178
  ///////////////////////////////////////
163
- function _coerce(input, fn) {
164
- if (!isNil(input.data)) {
165
- fn?.();
179
+ function _coerce(payload, fn) {
180
+ if (!isNil(payload.value)) {
181
+ fn?.(payload);
166
182
  }
167
183
  }
168
- function _coerceWithNil(input, fn) {
169
- if (!isNil(input.data)) {
170
- if (input.data === 'undefined' || input.data === '') {
171
- input.data = undefined;
184
+ function _coerceWithNil(payload, fn) {
185
+ if (!isNil(payload.value)) {
186
+ if (payload.value === 'undefined' || payload.value === '') {
187
+ payload.value = undefined;
172
188
  }
173
- else if (input.data === 'null') {
174
- input.data = null;
189
+ else if (payload.value === 'null') {
190
+ payload.value = null;
175
191
  }
176
192
  else {
177
- fn?.();
193
+ fn?.(payload);
178
194
  }
179
195
  }
180
196
  }
181
- export function coerceWithNil(data, fn) {
182
- const input = { data };
183
- _coerceWithNil(input, fn);
184
- return data;
185
- }
186
- function _getInnerType(schema) {
187
- let innerType = schema._def.innerType;
188
- if (!innerType)
189
- return schema;
190
- while (innerType._def.innerType) {
191
- innerType = innerType._def.innerType;
192
- }
193
- return innerType;
194
- }
195
- function _getTypeName(schema) {
196
- let typeName = schema.typeName;
197
- while (!typeName) {
198
- schema = schema._def;
199
- typeName = schema.typeName;
200
- }
201
- return typeName;
197
+ export function coerceWithNil(value, fn) {
198
+ const payload = { value, issues: [] };
199
+ _coerceWithNil(payload, fn);
200
+ return payload.value;
202
201
  }
203
- export function getTypeName(schema) {
204
- return _getTypeName(_getInnerType(schema));
202
+ export function getInnerTypeName(schema) {
203
+ return Metadata.unwrapUntil(schema).type;
205
204
  }
206
205
  // function _coerce(instance, input, fn) {
207
206
  // if (instance._def.coerce !== false) {
208
207
  // if (instance._def.coerce === true) instance._def.coerce = undefined as any;
209
- // if (!isNil(input.data)) {
208
+ // if (!isNil(payload.value)) {
210
209
  // fn();
211
210
  // }
212
211
  // }
@@ -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,7 +1,7 @@
1
1
  {
2
2
  "name": "@cabloy/zod-query",
3
3
  "type": "module",
4
- "version": "1.0.16",
4
+ "version": "2.0.1",
5
5
  "description": "zod-query",
6
6
  "publishConfig": {
7
7
  "access": "public"
@@ -1,9 +0,0 @@
1
- export declare class ZodMetadata {
2
- static unwrapUntil(schema: any, typeName?: any): any;
3
- static unwrapChained(schema: any): any;
4
- static getDefaultValue(zodSchema: any): any;
5
- static getInternalMetadata(zodSchema: any): any;
6
- static getLazySchema(zodSchema: any): any;
7
- static getRefId(zodSchema: any): any;
8
- }
9
- export declare function isZodType(schema: any, typeName: any): boolean;
@@ -1,50 +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
- }
46
- export function isZodType(schema, typeName) {
47
- let _a;
48
- // eslint-disable-next-line
49
- return ((_a = schema === null || schema === void 0 ? void 0 : schema._def) === null || _a === void 0 ? void 0 : _a.typeName) === typeName;
50
- }