@cabloy/zod-query 1.0.0 → 1.0.2
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 +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/lib/query.d.ts +2 -0
- package/dist/lib/query.d.ts.map +1 -0
- package/dist/lib/query.js +194 -0
- package/dist/lib/query.js.map +1 -0
- package/dist/lib/utils.d.ts +3 -0
- package/dist/lib/utils.d.ts.map +1 -0
- package/dist/lib/utils.js +3 -0
- package/dist/lib/utils.js.map +1 -0
- package/package.json +1 -1
- package/src/index.ts +1 -0
- package/src/lib/query.ts +211 -0
- package/src/lib/utils.ts +2 -0
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export
|
|
1
|
+
export * from './lib/query.js';
|
|
2
2
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":""}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export
|
|
1
|
+
export * from './lib/query.js';
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":""}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"query.d.ts","sourceRoot":"","sources":["../../src/lib/query.ts"],"names":[],"mappings":"AA6LA,wBAAgB,aAAa,CAAC,IAAI,EAAE,GAAG,EAAE,EAAE,CAAC,EAAE,QAAQ,OAIrD"}
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { isNil } from './utils.js';
|
|
3
|
+
/////////////////////////////////////////
|
|
4
|
+
/////////////////////////////////////////
|
|
5
|
+
////////// //////////
|
|
6
|
+
////////// ZodString //////////
|
|
7
|
+
////////// //////////
|
|
8
|
+
/////////////////////////////////////////
|
|
9
|
+
/////////////////////////////////////////
|
|
10
|
+
const _parseString = z.ZodString.prototype._parse;
|
|
11
|
+
z.ZodString.prototype._parse = function (input) {
|
|
12
|
+
_coerce(input, () => {
|
|
13
|
+
input.data = String(input.data);
|
|
14
|
+
});
|
|
15
|
+
return _parseString.call(this, input);
|
|
16
|
+
};
|
|
17
|
+
/////////////////////////////////////////
|
|
18
|
+
/////////////////////////////////////////
|
|
19
|
+
////////// //////////
|
|
20
|
+
////////// ZodNumber //////////
|
|
21
|
+
////////// //////////
|
|
22
|
+
/////////////////////////////////////////
|
|
23
|
+
/////////////////////////////////////////
|
|
24
|
+
const _parseNumber = z.ZodNumber.prototype._parse;
|
|
25
|
+
z.ZodNumber.prototype._parse = function (input) {
|
|
26
|
+
_coerceWithNil(input, () => {
|
|
27
|
+
input.data = Number(input.data);
|
|
28
|
+
});
|
|
29
|
+
return _parseNumber.call(this, input);
|
|
30
|
+
};
|
|
31
|
+
/////////////////////////////////////////
|
|
32
|
+
/////////////////////////////////////////
|
|
33
|
+
////////// //////////
|
|
34
|
+
////////// ZodBigInt //////////
|
|
35
|
+
////////// //////////
|
|
36
|
+
/////////////////////////////////////////
|
|
37
|
+
/////////////////////////////////////////
|
|
38
|
+
const _parseBigInt = z.ZodBigInt.prototype._parse;
|
|
39
|
+
z.ZodBigInt.prototype._parse = function (input) {
|
|
40
|
+
_coerceWithNil(input, () => {
|
|
41
|
+
input.data = BigInt(input.data);
|
|
42
|
+
});
|
|
43
|
+
return _parseBigInt.call(this, input);
|
|
44
|
+
};
|
|
45
|
+
//////////////////////////////////////////
|
|
46
|
+
//////////////////////////////////////////
|
|
47
|
+
////////// ///////////
|
|
48
|
+
////////// ZodBoolean //////////
|
|
49
|
+
////////// ///////////
|
|
50
|
+
//////////////////////////////////////////
|
|
51
|
+
//////////////////////////////////////////
|
|
52
|
+
const _parseBoolean = z.ZodBoolean.prototype._parse;
|
|
53
|
+
z.ZodBoolean.prototype._parse = function (input) {
|
|
54
|
+
_coerceWithNil(input, () => {
|
|
55
|
+
if (input.data === 'false' || input.data === '0') {
|
|
56
|
+
input.data = false;
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
input.data = Boolean(input.data);
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
return _parseBoolean.call(this, input);
|
|
63
|
+
};
|
|
64
|
+
///////////////////////////////////////
|
|
65
|
+
///////////////////////////////////////
|
|
66
|
+
////////// ////////
|
|
67
|
+
////////// ZodDate ////////
|
|
68
|
+
////////// ////////
|
|
69
|
+
///////////////////////////////////////
|
|
70
|
+
///////////////////////////////////////
|
|
71
|
+
const _parseDate = z.ZodDate.prototype._parse;
|
|
72
|
+
z.ZodDate.prototype._parse = function (input) {
|
|
73
|
+
_coerceWithNil(input, () => {
|
|
74
|
+
input.data = new Date(input.data);
|
|
75
|
+
});
|
|
76
|
+
return _parseDate.call(this, input);
|
|
77
|
+
};
|
|
78
|
+
/////////////////////////////////////////
|
|
79
|
+
/////////////////////////////////////////
|
|
80
|
+
////////// //////////
|
|
81
|
+
////////// ZodObject //////////
|
|
82
|
+
////////// //////////
|
|
83
|
+
/////////////////////////////////////////
|
|
84
|
+
/////////////////////////////////////////
|
|
85
|
+
const _parseObject = z.ZodObject.prototype._parse;
|
|
86
|
+
z.ZodObject.prototype._parse = function (input) {
|
|
87
|
+
_coerceWithNil(input, () => {
|
|
88
|
+
if (typeof input.data === 'string') {
|
|
89
|
+
input.data = JSON.parse(input.data);
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
return _parseObject.call(this, input);
|
|
93
|
+
};
|
|
94
|
+
////////////////////////////////////////
|
|
95
|
+
////////////////////////////////////////
|
|
96
|
+
////////// //////////
|
|
97
|
+
////////// ZodArray //////////
|
|
98
|
+
////////// //////////
|
|
99
|
+
////////////////////////////////////////
|
|
100
|
+
////////////////////////////////////////
|
|
101
|
+
const _parseArray = z.ZodArray.prototype._parse;
|
|
102
|
+
z.ZodArray.prototype._parse = function (input) {
|
|
103
|
+
_coerceWithNil(input, () => {
|
|
104
|
+
if (typeof input.data === 'string') {
|
|
105
|
+
if (input.data[0] === '[') {
|
|
106
|
+
input.data = JSON.parse(input.data);
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
input.data = input.data.split(',');
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
return _parseArray.call(this, input);
|
|
114
|
+
};
|
|
115
|
+
///////////////////////////////////////////
|
|
116
|
+
///////////////////////////////////////////
|
|
117
|
+
////////// //////////
|
|
118
|
+
////////// ZodOptional //////////
|
|
119
|
+
////////// //////////
|
|
120
|
+
///////////////////////////////////////////
|
|
121
|
+
///////////////////////////////////////////
|
|
122
|
+
const _parseOptional = z.ZodOptional.prototype._parse;
|
|
123
|
+
z.ZodOptional.prototype._parse = function (input) {
|
|
124
|
+
if (_getInnerType(this).typeName === 'ZodString') {
|
|
125
|
+
_coerce(input);
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
_coerceWithNil(input);
|
|
129
|
+
}
|
|
130
|
+
return _parseOptional.call(this, input);
|
|
131
|
+
};
|
|
132
|
+
////////////////////////////////////////////
|
|
133
|
+
////////////////////////////////////////////
|
|
134
|
+
////////// //////////
|
|
135
|
+
////////// ZodDefault //////////
|
|
136
|
+
////////// //////////
|
|
137
|
+
////////////////////////////////////////////
|
|
138
|
+
////////////////////////////////////////////
|
|
139
|
+
const _parseDefault = z.ZodDefault.prototype._parse;
|
|
140
|
+
z.ZodDefault.prototype._parse = function (input) {
|
|
141
|
+
if (_getInnerType(this).typeName === 'ZodString') {
|
|
142
|
+
_coerce(input);
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
_coerceWithNil(input);
|
|
146
|
+
}
|
|
147
|
+
return _parseDefault.call(this, input);
|
|
148
|
+
};
|
|
149
|
+
///////////////////////////////////////
|
|
150
|
+
///////////////////////////////////////
|
|
151
|
+
////////// ////////
|
|
152
|
+
////////// coerce ////////
|
|
153
|
+
////////// ////////
|
|
154
|
+
///////////////////////////////////////
|
|
155
|
+
///////////////////////////////////////
|
|
156
|
+
function _coerce(input, fn) {
|
|
157
|
+
if (!isNil(input.data)) {
|
|
158
|
+
fn?.();
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
function _coerceWithNil(input, fn) {
|
|
162
|
+
if (!isNil(input.data)) {
|
|
163
|
+
if (input.data === 'undefined' || input.data === '') {
|
|
164
|
+
input.data = undefined;
|
|
165
|
+
}
|
|
166
|
+
else if (input.data === 'null') {
|
|
167
|
+
input.data = null;
|
|
168
|
+
}
|
|
169
|
+
else {
|
|
170
|
+
fn?.();
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
export function coerceWithNil(data, fn) {
|
|
175
|
+
const input = { data };
|
|
176
|
+
_coerceWithNil(input, fn);
|
|
177
|
+
return data;
|
|
178
|
+
}
|
|
179
|
+
function _getInnerType(schema) {
|
|
180
|
+
let innerType = schema._def.innerType;
|
|
181
|
+
while (innerType._def.innerType) {
|
|
182
|
+
innerType = innerType._def.innerType;
|
|
183
|
+
}
|
|
184
|
+
return innerType;
|
|
185
|
+
}
|
|
186
|
+
// function _coerce(instance, input, fn) {
|
|
187
|
+
// if (instance._def.coerce !== false) {
|
|
188
|
+
// if (instance._def.coerce === true) instance._def.coerce = undefined as any;
|
|
189
|
+
// if (!isNil(input.data)) {
|
|
190
|
+
// fn();
|
|
191
|
+
// }
|
|
192
|
+
// }
|
|
193
|
+
// }
|
|
194
|
+
//# sourceMappingURL=query.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"query.js","sourceRoot":"","sources":["../../src/lib/query.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAEnC,yCAAyC;AACzC,yCAAyC;AACzC,yCAAyC;AACzC,yCAAyC;AACzC,yCAAyC;AACzC,yCAAyC;AACzC,yCAAyC;AAEzC,MAAM,YAAY,GAAG,CAAC,CAAC,SAAS,CAAC,SAAS,CAAC,MAAM,CAAC;AAClD,CAAC,CAAC,SAAS,CAAC,SAAS,CAAC,MAAM,GAAG,UAA6B,KAAK;IAC/D,OAAO,CAAC,KAAK,EAAE,GAAG,EAAE;QAClB,KAAK,CAAC,IAAI,GAAG,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IAClC,CAAC,CAAC,CAAC;IACH,OAAO,YAAY,CAAC,IAAI,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;AACxC,CAAC,CAAC;AAEF,yCAAyC;AACzC,yCAAyC;AACzC,yCAAyC;AACzC,yCAAyC;AACzC,yCAAyC;AACzC,yCAAyC;AACzC,yCAAyC;AAEzC,MAAM,YAAY,GAAG,CAAC,CAAC,SAAS,CAAC,SAAS,CAAC,MAAM,CAAC;AAClD,CAAC,CAAC,SAAS,CAAC,SAAS,CAAC,MAAM,GAAG,UAA6B,KAAK;IAC/D,cAAc,CAAC,KAAK,EAAE,GAAG,EAAE;QACzB,KAAK,CAAC,IAAI,GAAG,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IAClC,CAAC,CAAC,CAAC;IACH,OAAO,YAAY,CAAC,IAAI,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;AACxC,CAAC,CAAC;AAEF,yCAAyC;AACzC,yCAAyC;AACzC,yCAAyC;AACzC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ACD,OAAO,SAAS,CAAC;AACnB,CAAC;AAED,0CAA0C;AAC1C,0CAA0C;AAC1C,kFAAkF;AAClF,gCAAgC;AAChC,cAAc;AACd,QAAQ;AACR,MAAM;AACN,IAAI"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/lib/utils.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,WAAW,QAAS,GAAG,KAAG,GAAG,IAAI,SAAuC,CAAC;AACtF,eAAO,MAAM,KAAK,QAAS,GAAG,KAAG,GAAG,IAAI,IAAI,GAAG,SAA6C,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/lib/utils.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,GAAQ,EAAoB,EAAE,CAAC,OAAO,GAAG,KAAK,WAAW,CAAC;AACtF,MAAM,CAAC,MAAM,KAAK,GAAG,CAAC,GAAQ,EAA2B,EAAE,CAAC,WAAW,CAAC,GAAG,CAAC,IAAI,GAAG,KAAK,IAAI,CAAC"}
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './lib/query.js';
|
package/src/lib/query.ts
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { isNil } from './utils.js';
|
|
3
|
+
|
|
4
|
+
/////////////////////////////////////////
|
|
5
|
+
/////////////////////////////////////////
|
|
6
|
+
////////// //////////
|
|
7
|
+
////////// ZodString //////////
|
|
8
|
+
////////// //////////
|
|
9
|
+
/////////////////////////////////////////
|
|
10
|
+
/////////////////////////////////////////
|
|
11
|
+
|
|
12
|
+
const _parseString = z.ZodString.prototype._parse;
|
|
13
|
+
z.ZodString.prototype._parse = function (this: z.ZodString, input) {
|
|
14
|
+
_coerce(input, () => {
|
|
15
|
+
input.data = String(input.data);
|
|
16
|
+
});
|
|
17
|
+
return _parseString.call(this, input);
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
/////////////////////////////////////////
|
|
21
|
+
/////////////////////////////////////////
|
|
22
|
+
////////// //////////
|
|
23
|
+
////////// ZodNumber //////////
|
|
24
|
+
////////// //////////
|
|
25
|
+
/////////////////////////////////////////
|
|
26
|
+
/////////////////////////////////////////
|
|
27
|
+
|
|
28
|
+
const _parseNumber = z.ZodNumber.prototype._parse;
|
|
29
|
+
z.ZodNumber.prototype._parse = function (this: z.ZodNumber, input) {
|
|
30
|
+
_coerceWithNil(input, () => {
|
|
31
|
+
input.data = Number(input.data);
|
|
32
|
+
});
|
|
33
|
+
return _parseNumber.call(this, input);
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/////////////////////////////////////////
|
|
37
|
+
/////////////////////////////////////////
|
|
38
|
+
////////// //////////
|
|
39
|
+
////////// ZodBigInt //////////
|
|
40
|
+
////////// //////////
|
|
41
|
+
/////////////////////////////////////////
|
|
42
|
+
/////////////////////////////////////////
|
|
43
|
+
|
|
44
|
+
const _parseBigInt = z.ZodBigInt.prototype._parse;
|
|
45
|
+
z.ZodBigInt.prototype._parse = function (this: z.ZodBigInt, input) {
|
|
46
|
+
_coerceWithNil(input, () => {
|
|
47
|
+
input.data = BigInt(input.data);
|
|
48
|
+
});
|
|
49
|
+
return _parseBigInt.call(this, input);
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
//////////////////////////////////////////
|
|
53
|
+
//////////////////////////////////////////
|
|
54
|
+
////////// ///////////
|
|
55
|
+
////////// ZodBoolean //////////
|
|
56
|
+
////////// ///////////
|
|
57
|
+
//////////////////////////////////////////
|
|
58
|
+
//////////////////////////////////////////
|
|
59
|
+
|
|
60
|
+
const _parseBoolean = z.ZodBoolean.prototype._parse;
|
|
61
|
+
z.ZodBoolean.prototype._parse = function (this: z.ZodBoolean, input) {
|
|
62
|
+
_coerceWithNil(input, () => {
|
|
63
|
+
if (input.data === 'false' || input.data === '0') {
|
|
64
|
+
input.data = false;
|
|
65
|
+
} else {
|
|
66
|
+
input.data = Boolean(input.data);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
return _parseBoolean.call(this, input);
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
///////////////////////////////////////
|
|
73
|
+
///////////////////////////////////////
|
|
74
|
+
////////// ////////
|
|
75
|
+
////////// ZodDate ////////
|
|
76
|
+
////////// ////////
|
|
77
|
+
///////////////////////////////////////
|
|
78
|
+
///////////////////////////////////////
|
|
79
|
+
|
|
80
|
+
const _parseDate = z.ZodDate.prototype._parse;
|
|
81
|
+
z.ZodDate.prototype._parse = function (this: z.ZodDate, input) {
|
|
82
|
+
_coerceWithNil(input, () => {
|
|
83
|
+
input.data = new Date(input.data);
|
|
84
|
+
});
|
|
85
|
+
return _parseDate.call(this, input);
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
/////////////////////////////////////////
|
|
89
|
+
/////////////////////////////////////////
|
|
90
|
+
////////// //////////
|
|
91
|
+
////////// ZodObject //////////
|
|
92
|
+
////////// //////////
|
|
93
|
+
/////////////////////////////////////////
|
|
94
|
+
/////////////////////////////////////////
|
|
95
|
+
|
|
96
|
+
const _parseObject = z.ZodObject.prototype._parse;
|
|
97
|
+
z.ZodObject.prototype._parse = function (input) {
|
|
98
|
+
_coerceWithNil(input, () => {
|
|
99
|
+
if (typeof input.data === 'string') {
|
|
100
|
+
input.data = JSON.parse(input.data);
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
return _parseObject.call(this, input);
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
////////////////////////////////////////
|
|
107
|
+
////////////////////////////////////////
|
|
108
|
+
////////// //////////
|
|
109
|
+
////////// ZodArray //////////
|
|
110
|
+
////////// //////////
|
|
111
|
+
////////////////////////////////////////
|
|
112
|
+
////////////////////////////////////////
|
|
113
|
+
|
|
114
|
+
const _parseArray = z.ZodArray.prototype._parse;
|
|
115
|
+
z.ZodArray.prototype._parse = function (input) {
|
|
116
|
+
_coerceWithNil(input, () => {
|
|
117
|
+
if (typeof input.data === 'string') {
|
|
118
|
+
if (input.data[0] === '[') {
|
|
119
|
+
input.data = JSON.parse(input.data);
|
|
120
|
+
} else {
|
|
121
|
+
input.data = input.data.split(',');
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
return _parseArray.call(this, input);
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
///////////////////////////////////////////
|
|
129
|
+
///////////////////////////////////////////
|
|
130
|
+
////////// //////////
|
|
131
|
+
////////// ZodOptional //////////
|
|
132
|
+
////////// //////////
|
|
133
|
+
///////////////////////////////////////////
|
|
134
|
+
///////////////////////////////////////////
|
|
135
|
+
|
|
136
|
+
const _parseOptional = z.ZodOptional.prototype._parse;
|
|
137
|
+
z.ZodOptional.prototype._parse = function (this: z.ZodOptional<any>, input) {
|
|
138
|
+
if (_getInnerType(this).typeName === 'ZodString') {
|
|
139
|
+
_coerce(input);
|
|
140
|
+
} else {
|
|
141
|
+
_coerceWithNil(input);
|
|
142
|
+
}
|
|
143
|
+
return _parseOptional.call(this, input);
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
////////////////////////////////////////////
|
|
147
|
+
////////////////////////////////////////////
|
|
148
|
+
////////// //////////
|
|
149
|
+
////////// ZodDefault //////////
|
|
150
|
+
////////// //////////
|
|
151
|
+
////////////////////////////////////////////
|
|
152
|
+
////////////////////////////////////////////
|
|
153
|
+
|
|
154
|
+
const _parseDefault = z.ZodDefault.prototype._parse;
|
|
155
|
+
z.ZodDefault.prototype._parse = function (this: z.ZodDefault<any>, input) {
|
|
156
|
+
if (_getInnerType(this).typeName === 'ZodString') {
|
|
157
|
+
_coerce(input);
|
|
158
|
+
} else {
|
|
159
|
+
_coerceWithNil(input);
|
|
160
|
+
}
|
|
161
|
+
return _parseDefault.call(this, input);
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
///////////////////////////////////////
|
|
165
|
+
///////////////////////////////////////
|
|
166
|
+
////////// ////////
|
|
167
|
+
////////// coerce ////////
|
|
168
|
+
////////// ////////
|
|
169
|
+
///////////////////////////////////////
|
|
170
|
+
///////////////////////////////////////
|
|
171
|
+
|
|
172
|
+
function _coerce(input, fn?: Function) {
|
|
173
|
+
if (!isNil(input.data)) {
|
|
174
|
+
fn?.();
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function _coerceWithNil(input, fn?: Function) {
|
|
179
|
+
if (!isNil(input.data)) {
|
|
180
|
+
if (input.data === 'undefined' || input.data === '') {
|
|
181
|
+
input.data = undefined;
|
|
182
|
+
} else if (input.data === 'null') {
|
|
183
|
+
input.data = null;
|
|
184
|
+
} else {
|
|
185
|
+
fn?.();
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export function coerceWithNil(data: any, fn?: Function) {
|
|
191
|
+
const input = { data };
|
|
192
|
+
_coerceWithNil(input, fn);
|
|
193
|
+
return data;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function _getInnerType(schema: any) {
|
|
197
|
+
let innerType = schema._def.innerType;
|
|
198
|
+
while (innerType._def.innerType) {
|
|
199
|
+
innerType = innerType._def.innerType;
|
|
200
|
+
}
|
|
201
|
+
return innerType;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// function _coerce(instance, input, fn) {
|
|
205
|
+
// if (instance._def.coerce !== false) {
|
|
206
|
+
// if (instance._def.coerce === true) instance._def.coerce = undefined as any;
|
|
207
|
+
// if (!isNil(input.data)) {
|
|
208
|
+
// fn();
|
|
209
|
+
// }
|
|
210
|
+
// }
|
|
211
|
+
// }
|
package/src/lib/utils.ts
ADDED