@cabloy/zod-query 2.1.8 → 2.1.11

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':
@@ -233,17 +285,27 @@ function __parseArray(_inst, parse, payload, _) {
233
285
  ///////////////////////////////////////////
234
286
 
235
287
  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) {
288
+ const nullable = isNullableSchema(_inst);
289
+ _coerceNullable(_inst, payload, nullable);
290
+ if (payload.value === null && !nullable) {
242
291
  payload.value = undefined;
243
292
  }
244
293
  return parse(payload, _);
245
294
  }
246
295
 
296
+ ////////////////////////////////////////////
297
+ ////////////////////////////////////////////
298
+ /// /////// //////////
299
+ /// /////// ZodNullable /////////
300
+ /// /////// //////////
301
+ ////////////////////////////////////////////
302
+ ////////////////////////////////////////////
303
+
304
+ function __parseNullable(_inst, parse, payload, _) {
305
+ _coerceNullable(_inst, payload, true);
306
+ return parse(payload, _);
307
+ }
308
+
247
309
  ////////////////////////////////////////////
248
310
  ////////////////////////////////////////////
249
311
  /// /////// /////////
@@ -253,11 +315,7 @@ function __parseOptional(_inst, parse, payload, _) {
253
315
  ////////////////////////////////////////////
254
316
 
255
317
  function __parseDefault(_inst, parse, payload, _) {
256
- if (isZodType(Metadata.unwrapUntil(_inst), 'ZodString')) {
257
- _coerceString(payload);
258
- } else {
259
- _coerceWithNil(payload);
260
- }
318
+ _coerceNullable(_inst, payload, isNullableSchema(_inst));
261
319
  return parse(payload, _);
262
320
  }
263
321
 
@@ -284,6 +342,16 @@ function _coerceString(payload, fn) {
284
342
  } else ;
285
343
  }
286
344
  }
345
+ function _coerceNullable(inst, payload, nullable) {
346
+ if (isZodType(Metadata.unwrapUntil(inst), 'ZodString')) {
347
+ _coerceString(payload);
348
+ if (nullable && payload.value === 'null') {
349
+ payload.value = null;
350
+ }
351
+ } else {
352
+ _coerceWithNil(payload);
353
+ }
354
+ }
287
355
  function _coerceWithNil(payload, fn) {
288
356
  if (!isNil(payload.value)) {
289
357
  if (payload.value === 'undefined' || payload.value === '') {
@@ -315,4 +383,4 @@ function getInnerTypeName(schema) {
315
383
  // }
316
384
  // }
317
385
 
318
- export { coerceWithNil, getInnerTypeName, setParseAdapter };
386
+ 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.11",
4
+ "gitHead": "c7429e0d6a259341ae6e23d018b3238fb2483fdf",
5
5
  "description": "zod-query",
6
6
  "keywords": [
7
7
  "ioc",