@orion-js/schema 4.0.0-next.4 → 4.0.0-next.6
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.cjs +153 -153
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +152 -152
- package/dist/index.js.map +1 -1
- package/package.json +3 -5
package/dist/index.js
CHANGED
|
@@ -1,5 +1,44 @@
|
|
|
1
|
+
// ../../node_modules/.pnpm/rambdax@11.3.1/node_modules/rambdax/src/type.js
|
|
2
|
+
function type(input) {
|
|
3
|
+
if (input === null) {
|
|
4
|
+
return "Null";
|
|
5
|
+
} else if (input === void 0) {
|
|
6
|
+
return "Undefined";
|
|
7
|
+
} else if (Number.isNaN(input)) {
|
|
8
|
+
return "NaN";
|
|
9
|
+
}
|
|
10
|
+
const typeResult = Object.prototype.toString.call(input).slice(8, -1);
|
|
11
|
+
return typeResult === "AsyncFunction" ? "Promise" : typeResult;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// ../../node_modules/.pnpm/rambdax@11.3.1/node_modules/rambdax/src/_internals/isArray.js
|
|
15
|
+
var { isArray } = Array;
|
|
16
|
+
|
|
17
|
+
// ../../node_modules/.pnpm/rambdax@11.3.1/node_modules/rambdax/src/isType.js
|
|
18
|
+
function isType(xType, x) {
|
|
19
|
+
if (arguments.length === 1) {
|
|
20
|
+
return (xHolder) => isType(xType, xHolder);
|
|
21
|
+
}
|
|
22
|
+
return type(x) === xType;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// ../../node_modules/.pnpm/rambdax@11.3.1/node_modules/rambdax/src/clone.js
|
|
26
|
+
function clone(input) {
|
|
27
|
+
const out = isArray(input) ? Array(input.length) : {};
|
|
28
|
+
if (input && input.getTime) return new Date(input.getTime());
|
|
29
|
+
for (const key in input) {
|
|
30
|
+
const v = input[key];
|
|
31
|
+
out[key] = typeof v === "object" && v !== null ? v.getTime ? new Date(v.getTime()) : clone(v) : v;
|
|
32
|
+
}
|
|
33
|
+
return out;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// ../../node_modules/.pnpm/rambdax@11.3.1/node_modules/rambdax/src/isNil.js
|
|
37
|
+
function isNil(x) {
|
|
38
|
+
return x === void 0 || x === null;
|
|
39
|
+
}
|
|
40
|
+
|
|
1
41
|
// src/ValidationError.ts
|
|
2
|
-
import isPlainObject from "lodash/isPlainObject";
|
|
3
42
|
var getPrintableError = (validationErrors) => {
|
|
4
43
|
const printableErrors = Object.keys(validationErrors).map((key) => {
|
|
5
44
|
return `${key}: ${validationErrors[key]}`;
|
|
@@ -14,7 +53,7 @@ var ValidationError = class _ValidationError extends Error {
|
|
|
14
53
|
validationErrors;
|
|
15
54
|
constructor(validationErrors) {
|
|
16
55
|
super(getPrintableError(validationErrors));
|
|
17
|
-
if (
|
|
56
|
+
if (type(validationErrors) !== "Object") {
|
|
18
57
|
throw new Error("ValidationError must be initialized with an errors object");
|
|
19
58
|
}
|
|
20
59
|
Error.captureStackTrace(this, this.constructor);
|
|
@@ -50,14 +89,6 @@ var getSchemaFromTypedSchema = (schema) => {
|
|
|
50
89
|
return schema[Symbol.metadata]._getModel().getSchema();
|
|
51
90
|
};
|
|
52
91
|
|
|
53
|
-
// src/getValidationErrors/getError/index.ts
|
|
54
|
-
import isNil2 from "lodash/isNil";
|
|
55
|
-
|
|
56
|
-
// src/getValidationErrors/getError/getFieldValidator.ts
|
|
57
|
-
import isPlainObject4 from "lodash/isPlainObject";
|
|
58
|
-
import isArray3 from "lodash/isArray";
|
|
59
|
-
import isString5 from "lodash/isString";
|
|
60
|
-
|
|
61
92
|
// src/fieldType.ts
|
|
62
93
|
function fieldType(opts) {
|
|
63
94
|
const { name, validate: validate2, clean: clean3, ...otherFields } = opts;
|
|
@@ -86,9 +117,6 @@ function fieldType(opts) {
|
|
|
86
117
|
};
|
|
87
118
|
}
|
|
88
119
|
|
|
89
|
-
// src/fieldTypes/array.ts
|
|
90
|
-
import isArray from "lodash/isArray";
|
|
91
|
-
|
|
92
120
|
// src/Errors.ts
|
|
93
121
|
var Errors_default = {
|
|
94
122
|
NOT_IN_SCHEMA: "notInSchema",
|
|
@@ -115,11 +143,11 @@ var Errors_default = {
|
|
|
115
143
|
var array_default = fieldType({
|
|
116
144
|
name: "array",
|
|
117
145
|
validate(value) {
|
|
118
|
-
if (!isArray(value)) return Errors_default.NOT_AN_ARRAY;
|
|
146
|
+
if (!Array.isArray(value)) return Errors_default.NOT_AN_ARRAY;
|
|
119
147
|
},
|
|
120
148
|
clean(value, { options }) {
|
|
121
149
|
if (options.autoConvert) {
|
|
122
|
-
if (!isArray(value)) {
|
|
150
|
+
if (!Array.isArray(value)) {
|
|
123
151
|
value = [value];
|
|
124
152
|
}
|
|
125
153
|
}
|
|
@@ -128,19 +156,17 @@ var array_default = fieldType({
|
|
|
128
156
|
});
|
|
129
157
|
|
|
130
158
|
// src/fieldTypes/plainObject.ts
|
|
131
|
-
import isPlainObject2 from "lodash/isPlainObject";
|
|
132
|
-
import difference from "lodash/difference";
|
|
133
159
|
var plainObject_default = fieldType({
|
|
134
160
|
name: "plainObject",
|
|
135
161
|
validate(value) {
|
|
136
|
-
if (
|
|
162
|
+
if (type(value) !== "Object") return Errors_default.NOT_AN_OBJECT;
|
|
137
163
|
},
|
|
138
|
-
clean(value, { type, options }) {
|
|
139
|
-
if (
|
|
164
|
+
clean(value, { type: typeObj, options }) {
|
|
165
|
+
if (type(value) !== "Object") return value;
|
|
140
166
|
if (options.filter) {
|
|
141
167
|
const documentKeys = Object.keys(value);
|
|
142
|
-
const schemaKeys = Object.keys(
|
|
143
|
-
const notInSchemaKeys =
|
|
168
|
+
const schemaKeys = Object.keys(typeObj);
|
|
169
|
+
const notInSchemaKeys = documentKeys.filter((key) => !schemaKeys.includes(key));
|
|
144
170
|
for (const key of notInSchemaKeys) {
|
|
145
171
|
delete value[key];
|
|
146
172
|
}
|
|
@@ -150,13 +176,10 @@ var plainObject_default = fieldType({
|
|
|
150
176
|
});
|
|
151
177
|
|
|
152
178
|
// src/fieldTypes/string.ts
|
|
153
|
-
import isString from "lodash/isString";
|
|
154
|
-
import includes from "lodash/includes";
|
|
155
|
-
import isArray2 from "lodash/isArray";
|
|
156
179
|
var string_default = fieldType({
|
|
157
180
|
name: "string",
|
|
158
181
|
validate(value, { currentSchema }) {
|
|
159
|
-
if (
|
|
182
|
+
if (typeof value !== "string") return Errors_default.NOT_A_STRING;
|
|
160
183
|
if (Number.isFinite(currentSchema.min)) {
|
|
161
184
|
if (value.length < currentSchema.min) {
|
|
162
185
|
return Errors_default.STRING_TOO_SHORT;
|
|
@@ -167,8 +190,8 @@ var string_default = fieldType({
|
|
|
167
190
|
return Errors_default.STRING_TOO_LONG;
|
|
168
191
|
}
|
|
169
192
|
}
|
|
170
|
-
if (
|
|
171
|
-
if (!
|
|
193
|
+
if (Array.isArray(currentSchema.allowedValues)) {
|
|
194
|
+
if (!currentSchema.allowedValues.includes(value)) {
|
|
172
195
|
return Errors_default.NOT_AN_ALLOWED_VALUE;
|
|
173
196
|
}
|
|
174
197
|
}
|
|
@@ -191,23 +214,20 @@ var string_default = fieldType({
|
|
|
191
214
|
});
|
|
192
215
|
|
|
193
216
|
// src/fieldTypes/date.ts
|
|
194
|
-
import isDate from "lodash/isDate";
|
|
195
|
-
import isString2 from "lodash/isString";
|
|
196
|
-
import isNumber from "lodash/isNumber";
|
|
197
217
|
var date_default = fieldType({
|
|
198
218
|
name: "date",
|
|
199
219
|
validate(value) {
|
|
200
|
-
if (!
|
|
220
|
+
if (!(value instanceof Date)) return Errors_default.NOT_A_DATE;
|
|
201
221
|
},
|
|
202
222
|
clean(value, { options }) {
|
|
203
223
|
if (options.autoConvert) {
|
|
204
|
-
if (
|
|
224
|
+
if (typeof value === "string") {
|
|
205
225
|
const result = new Date(value);
|
|
206
226
|
if (Number.isNaN(result.getTime())) {
|
|
207
227
|
return value;
|
|
208
228
|
}
|
|
209
229
|
value = result;
|
|
210
|
-
} else if (
|
|
230
|
+
} else if (typeof value === "number") {
|
|
211
231
|
const result = new Date(value);
|
|
212
232
|
if (Number.isNaN(result.getTime())) {
|
|
213
233
|
return value;
|
|
@@ -219,11 +239,7 @@ var date_default = fieldType({
|
|
|
219
239
|
}
|
|
220
240
|
});
|
|
221
241
|
|
|
222
|
-
// src/fieldTypes/integer.ts
|
|
223
|
-
import isInteger from "lodash/isInteger";
|
|
224
|
-
|
|
225
242
|
// src/fieldTypes/number.ts
|
|
226
|
-
import toNumber from "lodash/toNumber";
|
|
227
243
|
var number_default = fieldType({
|
|
228
244
|
name: "number",
|
|
229
245
|
validate(value, { currentSchema }) {
|
|
@@ -241,7 +257,7 @@ var number_default = fieldType({
|
|
|
241
257
|
},
|
|
242
258
|
clean(value, { options: { autoConvert } }) {
|
|
243
259
|
if (typeof value === "string" && autoConvert) {
|
|
244
|
-
value =
|
|
260
|
+
value = Number(value);
|
|
245
261
|
}
|
|
246
262
|
return value;
|
|
247
263
|
}
|
|
@@ -251,21 +267,19 @@ var number_default = fieldType({
|
|
|
251
267
|
var integer_default = fieldType({
|
|
252
268
|
name: "integer",
|
|
253
269
|
validate(value, info) {
|
|
254
|
-
if (!isInteger(value)) return Errors_default.NOT_AN_INTEGER;
|
|
270
|
+
if (!Number.isInteger(value)) return Errors_default.NOT_AN_INTEGER;
|
|
255
271
|
return number_default.validate(value, info);
|
|
256
272
|
}
|
|
257
273
|
});
|
|
258
274
|
|
|
259
275
|
// src/fieldTypes/ID.ts
|
|
260
|
-
import isString3 from "lodash/isString";
|
|
261
|
-
import isInteger2 from "lodash/isInteger";
|
|
262
276
|
var ID_default = fieldType({
|
|
263
277
|
name: "ID",
|
|
264
278
|
validate(value) {
|
|
265
|
-
if (
|
|
279
|
+
if (typeof value !== "string" && !Number.isInteger(value)) return Errors_default.NOT_AN_ID;
|
|
266
280
|
},
|
|
267
281
|
clean(value, { options }) {
|
|
268
|
-
if (
|
|
282
|
+
if (typeof value !== "string" && !Number.isInteger(value)) return value;
|
|
269
283
|
const { trimStrings, removeEmptyStrings } = options;
|
|
270
284
|
value = String(value);
|
|
271
285
|
if (trimStrings) {
|
|
@@ -279,11 +293,10 @@ var ID_default = fieldType({
|
|
|
279
293
|
});
|
|
280
294
|
|
|
281
295
|
// src/fieldTypes/boolean.ts
|
|
282
|
-
import isBoolean from "lodash/isBoolean";
|
|
283
296
|
var boolean_default = fieldType({
|
|
284
297
|
name: "boolean",
|
|
285
298
|
validate(value) {
|
|
286
|
-
if (
|
|
299
|
+
if (typeof value !== "boolean") return Errors_default.NOT_A_BOOLEAN;
|
|
287
300
|
},
|
|
288
301
|
clean(value, { options }) {
|
|
289
302
|
if (options.autoConvert) {
|
|
@@ -309,15 +322,13 @@ var boolean_default = fieldType({
|
|
|
309
322
|
});
|
|
310
323
|
|
|
311
324
|
// src/fieldTypes/email.ts
|
|
312
|
-
import isString4 from "lodash/isString";
|
|
313
|
-
import isNil from "lodash/isNil";
|
|
314
325
|
var email_default = fieldType({
|
|
315
326
|
name: "email",
|
|
316
327
|
validate(value, { currentSchema }) {
|
|
317
328
|
if ((value === "" || isNil(value)) && !currentSchema.optional) {
|
|
318
329
|
return Errors_default.REQUIRED;
|
|
319
330
|
}
|
|
320
|
-
if (value &&
|
|
331
|
+
if (value && typeof value !== "string") return Errors_default.NOT_A_STRING;
|
|
321
332
|
const regex = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
|
|
322
333
|
if (value && !regex.test(value)) return Errors_default.NOT_AN_EMAIL;
|
|
323
334
|
},
|
|
@@ -330,11 +341,10 @@ var email_default = fieldType({
|
|
|
330
341
|
});
|
|
331
342
|
|
|
332
343
|
// src/fieldTypes/blackbox.ts
|
|
333
|
-
import isPlainObject3 from "lodash/isPlainObject";
|
|
334
344
|
var blackbox_default = fieldType({
|
|
335
345
|
name: "blackbox",
|
|
336
346
|
validate(value) {
|
|
337
|
-
if (
|
|
347
|
+
if (type(value) !== "Object") return Errors_default.NOT_AN_OBJECT;
|
|
338
348
|
}
|
|
339
349
|
});
|
|
340
350
|
|
|
@@ -361,33 +371,32 @@ var fieldTypes_default = {
|
|
|
361
371
|
};
|
|
362
372
|
|
|
363
373
|
// src/getValidationErrors/getError/getFieldValidator.ts
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
if (type.__isFieldType) return "custom";
|
|
374
|
+
function getFieldValidator(type2) {
|
|
375
|
+
if (isType("Object", type2)) {
|
|
376
|
+
if (type2.__isFieldType) return "custom";
|
|
368
377
|
return "plainObject";
|
|
369
378
|
}
|
|
370
|
-
if (
|
|
371
|
-
if (
|
|
372
|
-
if (typeof
|
|
373
|
-
if (
|
|
374
|
-
if (
|
|
375
|
-
if (
|
|
376
|
-
if (
|
|
377
|
-
throw new Error(`Field type is invalid. Pass a string or a custom field type. Got ${
|
|
379
|
+
if (Array.isArray(type2)) return "array";
|
|
380
|
+
if (type2 === String) return "string";
|
|
381
|
+
if (typeof type2 === "function" && type2.name === "Date") return "date";
|
|
382
|
+
if (type2 === Number) return "number";
|
|
383
|
+
if (type2 === Boolean) return "boolean";
|
|
384
|
+
if (type2 === "enum") return "string";
|
|
385
|
+
if (typeof type2 !== "string") {
|
|
386
|
+
throw new Error(`Field type is invalid. Pass a string or a custom field type. Got ${type2}`);
|
|
378
387
|
}
|
|
379
|
-
const exists =
|
|
388
|
+
const exists = fieldTypes_default[type2];
|
|
380
389
|
if (!exists) {
|
|
381
390
|
throw new Error("Field type does not exist");
|
|
382
391
|
}
|
|
383
|
-
return
|
|
392
|
+
return type2;
|
|
384
393
|
}
|
|
385
394
|
|
|
386
395
|
// src/getValidationErrors/getError/index.ts
|
|
387
396
|
async function getValidationErrors(params) {
|
|
388
397
|
const { schema, doc, currentDoc, value, currentSchema, keys, options = {}, args = [] } = params;
|
|
389
398
|
const info = { schema, doc, currentDoc, keys, currentSchema, options };
|
|
390
|
-
if (
|
|
399
|
+
if (isNil(value)) {
|
|
391
400
|
if (!currentSchema.optional && !options.omitRequired) {
|
|
392
401
|
return Errors_default.REQUIRED;
|
|
393
402
|
}
|
|
@@ -406,9 +415,9 @@ async function getValidationErrors(params) {
|
|
|
406
415
|
return customError;
|
|
407
416
|
}
|
|
408
417
|
}
|
|
409
|
-
const
|
|
410
|
-
if (
|
|
411
|
-
const typeError = await
|
|
418
|
+
const type2 = currentSchema.type;
|
|
419
|
+
if (type2.__validate) {
|
|
420
|
+
const typeError = await type2.__validate(value, info, ...args);
|
|
412
421
|
if (typeError) {
|
|
413
422
|
return typeError;
|
|
414
423
|
}
|
|
@@ -416,25 +425,29 @@ async function getValidationErrors(params) {
|
|
|
416
425
|
return null;
|
|
417
426
|
}
|
|
418
427
|
|
|
419
|
-
// src/getValidationErrors/doValidation.ts
|
|
420
|
-
import isPlainObject5 from "lodash/isPlainObject";
|
|
421
|
-
import isArray4 from "lodash/isArray";
|
|
422
|
-
import clone from "lodash/clone";
|
|
423
|
-
import isNil3 from "lodash/isNil";
|
|
424
|
-
import difference2 from "lodash/difference";
|
|
425
|
-
|
|
426
428
|
// src/getValidationErrors/convertTypedSchema.ts
|
|
427
429
|
var convertOnParam = (info, paramName) => {
|
|
428
430
|
if (!info[paramName]) return;
|
|
429
|
-
const
|
|
430
|
-
if (!
|
|
431
|
-
info[paramName].type = getSchemaFromTypedSchema(
|
|
431
|
+
const type2 = info[paramName].type;
|
|
432
|
+
if (!type2) return;
|
|
433
|
+
info[paramName].type = getSchemaFromTypedSchema(type2);
|
|
432
434
|
};
|
|
433
435
|
var convertTypedSchema = (info) => {
|
|
434
436
|
convertOnParam(info, "schema");
|
|
435
437
|
convertOnParam(info, "currentSchema");
|
|
436
438
|
};
|
|
437
439
|
|
|
440
|
+
// src/clone.ts
|
|
441
|
+
function clone2(value) {
|
|
442
|
+
if (isType("Object", value)) {
|
|
443
|
+
return clone(value);
|
|
444
|
+
}
|
|
445
|
+
if (Array.isArray(value)) {
|
|
446
|
+
return clone(value);
|
|
447
|
+
}
|
|
448
|
+
return value;
|
|
449
|
+
}
|
|
450
|
+
|
|
438
451
|
// src/getValidationErrors/doValidation.ts
|
|
439
452
|
async function doValidation(params) {
|
|
440
453
|
convertTypedSchema(params);
|
|
@@ -445,15 +458,15 @@ async function doValidation(params) {
|
|
|
445
458
|
addError(keys, error);
|
|
446
459
|
return;
|
|
447
460
|
}
|
|
448
|
-
if (
|
|
449
|
-
if (
|
|
450
|
-
const
|
|
451
|
-
if (
|
|
452
|
-
if (
|
|
461
|
+
if (isNil(value)) return;
|
|
462
|
+
if (type(currentSchema.type) === "Object") {
|
|
463
|
+
const type2 = currentSchema.type;
|
|
464
|
+
if (type2) {
|
|
465
|
+
if (type2.__isFieldType) {
|
|
453
466
|
return;
|
|
454
467
|
}
|
|
455
|
-
if (typeof
|
|
456
|
-
if (await
|
|
468
|
+
if (typeof type2.__skipChildValidation === "function") {
|
|
469
|
+
if (await type2.__skipChildValidation(value, info)) {
|
|
457
470
|
return;
|
|
458
471
|
}
|
|
459
472
|
}
|
|
@@ -462,7 +475,7 @@ async function doValidation(params) {
|
|
|
462
475
|
for (const key of schemaKeys) {
|
|
463
476
|
const itemSchema = currentSchema.type[key];
|
|
464
477
|
const itemValue = value[key];
|
|
465
|
-
const keyItemKeys =
|
|
478
|
+
const keyItemKeys = clone2(keys);
|
|
466
479
|
keyItemKeys.push(key);
|
|
467
480
|
await doValidation({
|
|
468
481
|
...info,
|
|
@@ -473,17 +486,17 @@ async function doValidation(params) {
|
|
|
473
486
|
});
|
|
474
487
|
}
|
|
475
488
|
const documentKeys = Object.keys(value);
|
|
476
|
-
const notInSchemaKeys =
|
|
489
|
+
const notInSchemaKeys = documentKeys.filter((key) => !schemaKeys.includes(key));
|
|
477
490
|
for (const key of notInSchemaKeys) {
|
|
478
|
-
const keyItemKeys =
|
|
491
|
+
const keyItemKeys = clone2(keys);
|
|
479
492
|
keyItemKeys.push(key);
|
|
480
493
|
addError(keyItemKeys, Errors_default.NOT_IN_SCHEMA);
|
|
481
494
|
}
|
|
482
|
-
} else if (
|
|
495
|
+
} else if (Array.isArray(currentSchema.type)) {
|
|
483
496
|
const itemSchema = currentSchema.type[0];
|
|
484
497
|
for (let i = 0; i < value.length; i++) {
|
|
485
498
|
const itemValue = value[i];
|
|
486
|
-
const keyItemKeys =
|
|
499
|
+
const keyItemKeys = clone2(keys);
|
|
487
500
|
keyItemKeys.push(i.toString());
|
|
488
501
|
await doValidation({
|
|
489
502
|
...info,
|
|
@@ -550,23 +563,15 @@ async function isValid(schema, doc, passedOptions = {}, ...args) {
|
|
|
550
563
|
}
|
|
551
564
|
|
|
552
565
|
// src/getValidationErrors/getError/getFieldType.ts
|
|
553
|
-
function getFieldType(
|
|
554
|
-
const validatorKey = getFieldValidator(
|
|
555
|
-
const validator = validatorKey === "custom" ?
|
|
566
|
+
function getFieldType(type2) {
|
|
567
|
+
const validatorKey = getFieldValidator(type2);
|
|
568
|
+
const validator = validatorKey === "custom" ? type2 : fieldTypes_default[validatorKey];
|
|
556
569
|
return validator;
|
|
557
570
|
}
|
|
558
571
|
|
|
559
|
-
// src/clean/recursiveClean.ts
|
|
560
|
-
import isUndefined from "lodash/isUndefined";
|
|
561
|
-
import isArray5 from "lodash/isArray";
|
|
562
|
-
|
|
563
|
-
// src/clean/cleanType.ts
|
|
564
|
-
import isNil4 from "lodash/isNil";
|
|
565
|
-
|
|
566
572
|
// src/clean/getObjectNode.ts
|
|
567
|
-
import isPlainObject6 from "lodash/isPlainObject";
|
|
568
573
|
function getObjectNode(schema, value) {
|
|
569
|
-
if (
|
|
574
|
+
if (type(schema.type) === "Object" && type(value) === "Object") {
|
|
570
575
|
const result = schema;
|
|
571
576
|
return result;
|
|
572
577
|
}
|
|
@@ -574,13 +579,13 @@ function getObjectNode(schema, value) {
|
|
|
574
579
|
}
|
|
575
580
|
|
|
576
581
|
// src/clean/cleanType.ts
|
|
577
|
-
async function cleanType(
|
|
582
|
+
async function cleanType(type2, fieldSchema, value, info, ...args) {
|
|
578
583
|
info.type = fieldSchema.type;
|
|
579
584
|
if (!info.type) {
|
|
580
585
|
throw new Error("Cleaning field with no type");
|
|
581
586
|
}
|
|
582
|
-
const { clean: rootFieldClean } = await getFieldType(
|
|
583
|
-
if (rootFieldClean && !
|
|
587
|
+
const { clean: rootFieldClean } = await getFieldType(type2);
|
|
588
|
+
if (rootFieldClean && !isNil(value)) {
|
|
584
589
|
value = await rootFieldClean(value, info, ...args);
|
|
585
590
|
}
|
|
586
591
|
let needReClean = false;
|
|
@@ -590,7 +595,7 @@ async function cleanType(type, fieldSchema, value, info, ...args) {
|
|
|
590
595
|
value = await objectTypeSchema.type.__clean(value, info, ...args);
|
|
591
596
|
}
|
|
592
597
|
const { defaultValue } = fieldSchema;
|
|
593
|
-
if (
|
|
598
|
+
if (isNil(value) && !isNil(defaultValue)) {
|
|
594
599
|
needReClean = true;
|
|
595
600
|
if (typeof defaultValue === "function") {
|
|
596
601
|
value = await defaultValue(info, ...args);
|
|
@@ -603,14 +608,13 @@ async function cleanType(type, fieldSchema, value, info, ...args) {
|
|
|
603
608
|
needReClean = true;
|
|
604
609
|
value = await clean3(value, info, ...args);
|
|
605
610
|
}
|
|
606
|
-
if (needReClean && rootFieldClean && !
|
|
611
|
+
if (needReClean && rootFieldClean && !isNil(value)) {
|
|
607
612
|
value = await rootFieldClean(value, info, ...args);
|
|
608
613
|
}
|
|
609
614
|
return value;
|
|
610
615
|
}
|
|
611
616
|
|
|
612
617
|
// src/clean/recursiveClean.ts
|
|
613
|
-
import isNil5 from "lodash/isNil";
|
|
614
618
|
var cleanObjectFields = async ({
|
|
615
619
|
schema,
|
|
616
620
|
value,
|
|
@@ -627,7 +631,7 @@ var cleanObjectFields = async ({
|
|
|
627
631
|
currentDoc: value
|
|
628
632
|
};
|
|
629
633
|
const newValue = await clean(cleanOptions);
|
|
630
|
-
if (
|
|
634
|
+
if (newValue !== void 0) {
|
|
631
635
|
newDoc[key] = newValue;
|
|
632
636
|
}
|
|
633
637
|
} catch (error) {
|
|
@@ -654,10 +658,10 @@ var cleanArrayItems = async ({
|
|
|
654
658
|
return newValue;
|
|
655
659
|
});
|
|
656
660
|
const result = await Promise.all(promises);
|
|
657
|
-
return result.filter((value2) =>
|
|
661
|
+
return result.filter((value2) => value2 !== void 0);
|
|
658
662
|
};
|
|
659
663
|
function getArrayNode(schema, value) {
|
|
660
|
-
if (
|
|
664
|
+
if (Array.isArray(schema.type) && !isNil(value)) {
|
|
661
665
|
const result = schema;
|
|
662
666
|
return result;
|
|
663
667
|
}
|
|
@@ -680,7 +684,7 @@ var clean = async (info) => {
|
|
|
680
684
|
const arraySchema = getArrayNode(currSchema, value);
|
|
681
685
|
if (arraySchema) {
|
|
682
686
|
let updatedValue = value;
|
|
683
|
-
if (!
|
|
687
|
+
if (!Array.isArray(value)) {
|
|
684
688
|
updatedValue = [value];
|
|
685
689
|
}
|
|
686
690
|
const newDoc = await cleanArrayItems({
|
|
@@ -720,8 +724,6 @@ async function clean2(schema, doc, opts = {}, ...args) {
|
|
|
720
724
|
}
|
|
721
725
|
|
|
722
726
|
// src/validateKey/dotGetSchema.ts
|
|
723
|
-
import isPlainObject7 from "lodash/isPlainObject";
|
|
724
|
-
import isNil6 from "lodash/isNil";
|
|
725
727
|
var dotGet = function dotGet2(object, path) {
|
|
726
728
|
if (path === "") return object;
|
|
727
729
|
const pathParts = path.split(".");
|
|
@@ -731,7 +733,7 @@ var dotGet = function dotGet2(object, path) {
|
|
|
731
733
|
if (first === "$" || /^[0-9]+$/.test(first)) {
|
|
732
734
|
return dotGet2({ type: levelObject[0] }, remainingPath);
|
|
733
735
|
}
|
|
734
|
-
if (
|
|
736
|
+
if (isType("Object", levelObject[first])) {
|
|
735
737
|
return dotGet2(levelObject[first], remainingPath);
|
|
736
738
|
}
|
|
737
739
|
if (levelObject === "blackbox") {
|
|
@@ -740,7 +742,7 @@ var dotGet = function dotGet2(object, path) {
|
|
|
740
742
|
return null;
|
|
741
743
|
};
|
|
742
744
|
function dotGetSchema_default(schema, path) {
|
|
743
|
-
if (
|
|
745
|
+
if (isNil(schema)) {
|
|
744
746
|
throw new Error("You need to pass a schema");
|
|
745
747
|
}
|
|
746
748
|
return dotGet({ type: schema }, path);
|
|
@@ -794,8 +796,6 @@ async function validateKey_default(schema, key, value, passedOptions = {}, ...ar
|
|
|
794
796
|
}
|
|
795
797
|
|
|
796
798
|
// src/fieldTypes/enum.ts
|
|
797
|
-
import isString6 from "lodash/isString";
|
|
798
|
-
import includes2 from "lodash/includes";
|
|
799
799
|
function createEnum(name, values) {
|
|
800
800
|
return {
|
|
801
801
|
type: values[0],
|
|
@@ -817,8 +817,8 @@ function createEnum(name, values) {
|
|
|
817
817
|
return global.GraphQLEnums[name];
|
|
818
818
|
},
|
|
819
819
|
validate(value, { currentSchema }) {
|
|
820
|
-
if (
|
|
821
|
-
if (!
|
|
820
|
+
if (typeof value !== "string") return Errors_default.NOT_A_STRING;
|
|
821
|
+
if (!values.includes(value)) {
|
|
822
822
|
return Errors_default.NOT_AN_ALLOWED_VALUE;
|
|
823
823
|
}
|
|
824
824
|
if (value === "" && !currentSchema.optional) {
|
|
@@ -843,51 +843,51 @@ function createEnum(name, values) {
|
|
|
843
843
|
|
|
844
844
|
// src/models.ts
|
|
845
845
|
Symbol.metadata ?? (Symbol.metadata = Symbol("Symbol.metadata"));
|
|
846
|
-
function isSchemaLike(
|
|
846
|
+
function isSchemaLike(type2) {
|
|
847
847
|
var _a;
|
|
848
|
-
if (!
|
|
849
|
-
if (objectHasSubObjectWithKey(
|
|
850
|
-
if ((_a =
|
|
851
|
-
if (
|
|
852
|
-
if (
|
|
853
|
-
if (
|
|
854
|
-
if (
|
|
855
|
-
if (
|
|
848
|
+
if (!type2) return false;
|
|
849
|
+
if (objectHasSubObjectWithKey(type2, "type")) return true;
|
|
850
|
+
if ((_a = type2 == null ? void 0 : type2[Symbol.metadata]) == null ? void 0 : _a._getModel) return true;
|
|
851
|
+
if (type2.getModel) return true;
|
|
852
|
+
if (type2.getSchema) return true;
|
|
853
|
+
if (type2.getCleanSchema) return true;
|
|
854
|
+
if (type2.__isModel) return true;
|
|
855
|
+
if (type2.__modelName) return true;
|
|
856
856
|
return false;
|
|
857
857
|
}
|
|
858
|
-
function isSchemaOrFieldLike(
|
|
859
|
-
if (Array.isArray(
|
|
860
|
-
if (
|
|
861
|
-
return isSchemaOrFieldLike(
|
|
858
|
+
function isSchemaOrFieldLike(type2) {
|
|
859
|
+
if (Array.isArray(type2)) {
|
|
860
|
+
if (type2.length !== 1) return false;
|
|
861
|
+
return isSchemaOrFieldLike(type2[0]);
|
|
862
862
|
}
|
|
863
|
-
if (isSchemaLike(
|
|
863
|
+
if (isSchemaLike(type2)) return true;
|
|
864
864
|
try {
|
|
865
|
-
if (getFieldValidator(
|
|
865
|
+
if (getFieldValidator(type2)) return true;
|
|
866
866
|
} catch {
|
|
867
867
|
return false;
|
|
868
868
|
}
|
|
869
869
|
return false;
|
|
870
870
|
}
|
|
871
|
-
function getSchemaModelName(
|
|
872
|
-
if (!
|
|
873
|
-
if (
|
|
874
|
-
if (
|
|
875
|
-
if (
|
|
871
|
+
function getSchemaModelName(type2) {
|
|
872
|
+
if (!type2) return null;
|
|
873
|
+
if (type2.__modelName) return type2.__modelName;
|
|
874
|
+
if (type2.getModel) return type2.getModel().name;
|
|
875
|
+
if (type2.getSchema) return type2.getSchema().__modelName;
|
|
876
876
|
return null;
|
|
877
877
|
}
|
|
878
|
-
function getSchemaFromAnyOrionForm(
|
|
878
|
+
function getSchemaFromAnyOrionForm(type2) {
|
|
879
879
|
var _a, _b;
|
|
880
|
-
if ((_a =
|
|
881
|
-
return (_b =
|
|
880
|
+
if ((_a = type2 == null ? void 0 : type2[Symbol.metadata]) == null ? void 0 : _a._getModel) {
|
|
881
|
+
return (_b = type2 == null ? void 0 : type2[Symbol.metadata]) == null ? void 0 : _b._getModel().getSchema();
|
|
882
882
|
}
|
|
883
|
-
if (
|
|
884
|
-
if (
|
|
885
|
-
return
|
|
883
|
+
if (type2 == null ? void 0 : type2.getModel) return type2.getModel().getSchema();
|
|
884
|
+
if (type2.getSchema) {
|
|
885
|
+
return type2.getSchema();
|
|
886
886
|
}
|
|
887
|
-
if (
|
|
888
|
-
return
|
|
887
|
+
if (type2.getSchema) {
|
|
888
|
+
return type2.getSchema();
|
|
889
889
|
}
|
|
890
|
-
if (objectHasSubObjectWithKey(
|
|
890
|
+
if (objectHasSubObjectWithKey(type2, "type")) return type2;
|
|
891
891
|
return null;
|
|
892
892
|
}
|
|
893
893
|
function objectHasSubObjectWithKey(object, key) {
|
|
@@ -900,8 +900,8 @@ function objectHasSubObjectWithKey(object, key) {
|
|
|
900
900
|
}
|
|
901
901
|
return false;
|
|
902
902
|
}
|
|
903
|
-
function getSchemaWithMetadataFromAnyOrionForm(
|
|
904
|
-
return getSchemaFromAnyOrionForm(
|
|
903
|
+
function getSchemaWithMetadataFromAnyOrionForm(type2) {
|
|
904
|
+
return getSchemaFromAnyOrionForm(type2);
|
|
905
905
|
}
|
|
906
906
|
|
|
907
907
|
// src/schemaWithName/index.ts
|