@cabloy/zod-query 2.1.8 → 2.1.12

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 +1,2 @@
1
1
  export * from './lib/query.ts';
2
+ export * from './lib/zod-is-type.ts';
package/dist/index.js CHANGED
@@ -43,6 +43,56 @@ function isAnyZodType(schema) {
43
43
  return 'def' in schema;
44
44
  }
45
45
 
46
+ /**
47
+ * The schema.isNullable() is deprecated. This is the suggested replacement.
48
+ */
49
+ function isNullableSchema(schema) {
50
+ return _isNullableSchema(schema);
51
+ }
52
+
53
+ /**
54
+ * The schema.isOptional() is deprecated. This is the suggested replacement.
55
+ */
56
+ function isOptionalSchema(schema) {
57
+ return _isOptionalSchema(schema);
58
+ }
59
+ function _isNullableSchema(schema) {
60
+ if (isZodType(schema, 'ZodNullable')) return true;
61
+ if (isZodType(schema, 'ZodNonOptional')) return false;
62
+ if (isZodType(schema, ['ZodOptional', 'ZodDefault', 'ZodReadonly']) && isAnyZodType(schema._zod.def.innerType)) {
63
+ return _isNullableSchema(schema._zod.def.innerType);
64
+ }
65
+ if (isZodType(schema, 'ZodPipe')) {
66
+ const inSchema = schema._zod.def.in;
67
+ const outSchema = schema._zod.def.out;
68
+ if (isZodType(inSchema, 'ZodTransform') && isAnyZodType(outSchema)) {
69
+ return _isNullableSchema(outSchema);
70
+ }
71
+ if (isAnyZodType(inSchema)) {
72
+ return _isNullableSchema(inSchema);
73
+ }
74
+ }
75
+ return false;
76
+ }
77
+ function _isOptionalSchema(schema) {
78
+ if (isZodType(schema, ['ZodOptional', 'ZodDefault'])) return true;
79
+ if (isZodType(schema, 'ZodNonOptional')) return false;
80
+ if (isZodType(schema, ['ZodNullable', 'ZodReadonly']) && isAnyZodType(schema._zod.def.innerType)) {
81
+ return _isOptionalSchema(schema._zod.def.innerType);
82
+ }
83
+ if (isZodType(schema, 'ZodPipe')) {
84
+ const inSchema = schema._zod.def.in;
85
+ const outSchema = schema._zod.def.out;
86
+ if (isZodType(inSchema, 'ZodTransform') && isAnyZodType(outSchema)) {
87
+ return _isOptionalSchema(outSchema);
88
+ }
89
+ if (isAnyZodType(inSchema)) {
90
+ return _isOptionalSchema(inSchema);
91
+ }
92
+ }
93
+ return false;
94
+ }
95
+
46
96
  class Metadata {
47
97
  static unwrapUntil(schema, typeName) {
48
98
  if (typeName && isZodType(schema, typeName)) {
@@ -95,6 +145,8 @@ function __parseAdapter(inst, parse) {
95
145
  return __parseObject(inst, parse, payload, _);
96
146
  case 'array':
97
147
  return __parseArray(inst, parse, payload, _);
148
+ case 'nullable':
149
+ return __parseNullable(inst, parse, payload, _);
98
150
  case 'optional':
99
151
  return __parseOptional(inst, parse, payload, _);
100
152
  case 'default':
@@ -118,6 +170,10 @@ function __parseString(inst, parse, payload, _) {
118
170
  // ignore upload file
119
171
  return payload;
120
172
  }
173
+ if (payload.value && payload.value instanceof Date) {
174
+ // ignore date
175
+ return payload;
176
+ }
121
177
  _coerceString(payload);
122
178
  return parse(payload, _);
123
179
  }
@@ -233,17 +289,27 @@ function __parseArray(_inst, parse, payload, _) {
233
289
  ///////////////////////////////////////////
234
290
 
235
291
  function __parseOptional(_inst, parse, payload, _) {
236
- if (isZodType(Metadata.unwrapUntil(_inst), 'ZodString')) {
237
- _coerceString(payload);
238
- } else {
239
- _coerceWithNil(payload);
240
- }
241
- if (payload.value === null) {
292
+ const nullable = isNullableSchema(_inst);
293
+ _coerceNullable(_inst, payload, nullable);
294
+ if (payload.value === null && !nullable) {
242
295
  payload.value = undefined;
243
296
  }
244
297
  return parse(payload, _);
245
298
  }
246
299
 
300
+ ////////////////////////////////////////////
301
+ ////////////////////////////////////////////
302
+ /// /////// //////////
303
+ /// /////// ZodNullable /////////
304
+ /// /////// //////////
305
+ ////////////////////////////////////////////
306
+ ////////////////////////////////////////////
307
+
308
+ function __parseNullable(_inst, parse, payload, _) {
309
+ _coerceNullable(_inst, payload, true);
310
+ return parse(payload, _);
311
+ }
312
+
247
313
  ////////////////////////////////////////////
248
314
  ////////////////////////////////////////////
249
315
  /// /////// /////////
@@ -253,11 +319,7 @@ function __parseOptional(_inst, parse, payload, _) {
253
319
  ////////////////////////////////////////////
254
320
 
255
321
  function __parseDefault(_inst, parse, payload, _) {
256
- if (isZodType(Metadata.unwrapUntil(_inst), 'ZodString')) {
257
- _coerceString(payload);
258
- } else {
259
- _coerceWithNil(payload);
260
- }
322
+ _coerceNullable(_inst, payload, isNullableSchema(_inst));
261
323
  return parse(payload, _);
262
324
  }
263
325
 
@@ -284,6 +346,16 @@ function _coerceString(payload, fn) {
284
346
  } else ;
285
347
  }
286
348
  }
349
+ function _coerceNullable(inst, payload, nullable) {
350
+ if (isZodType(Metadata.unwrapUntil(inst), 'ZodString')) {
351
+ _coerceString(payload);
352
+ if (nullable && payload.value === 'null') {
353
+ payload.value = null;
354
+ }
355
+ } else {
356
+ _coerceWithNil(payload);
357
+ }
358
+ }
287
359
  function _coerceWithNil(payload, fn) {
288
360
  if (!isNil(payload.value)) {
289
361
  if (payload.value === 'undefined' || payload.value === '') {
@@ -315,4 +387,4 @@ function getInnerTypeName(schema) {
315
387
  // }
316
388
  // }
317
389
 
318
- export { coerceWithNil, getInnerTypeName, setParseAdapter };
390
+ export { coerceWithNil, getInnerTypeName, isAnyZodType, isNullableSchema, isOptionalSchema, isZodType, setParseAdapter };
@@ -32,12 +32,10 @@ export declare function isZodType<TypeName extends keyof ZodTypes>(schema: objec
32
32
  export declare function isZodType<TypeName extends keyof ZodTypes>(schema: object, typeName: TypeName): schema is ZodTypes[TypeName];
33
33
  export declare function isAnyZodType(schema: object): schema is z.ZodType;
34
34
  /**
35
- * The schema.isNullable() is deprecated. This is the suggested replacement
36
- * as this was how isNullable operated beforehand.
35
+ * The schema.isNullable() is deprecated. This is the suggested replacement.
37
36
  */
38
37
  export declare function isNullableSchema(schema: z.ZodType): boolean;
39
38
  /**
40
- * The schema.isOptional() is deprecated. This is the suggested replacement
41
- * as this was how isOptional operated beforehand.
39
+ * The schema.isOptional() is deprecated. This is the suggested replacement.
42
40
  */
43
41
  export declare function isOptionalSchema(schema: z.ZodType): boolean;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@cabloy/zod-query",
3
- "version": "2.1.8",
4
- "gitHead": "3380f8c5d3989242b60dc2a34cbb330f74335cbe",
3
+ "version": "2.1.12",
4
+ "gitHead": "079e0f7eaebb585ff14ebdbdff9051229e8544db",
5
5
  "description": "zod-query",
6
6
  "keywords": [
7
7
  "ioc",