@orion-js/schema 4.0.0-next.2 → 4.0.0-next.4
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/LICENSE +21 -0
- package/dist/index.cjs +187 -184
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +116 -55
- package/dist/index.d.ts +116 -55
- package/dist/index.js +180 -186
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -4,7 +4,6 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
|
4
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
5
|
var __getProtoOf = Object.getPrototypeOf;
|
|
6
6
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
-
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
8
7
|
var __export = (target, all) => {
|
|
9
8
|
for (var name in all)
|
|
10
9
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -35,9 +34,16 @@ __export(index_exports, {
|
|
|
35
34
|
cleanKey: () => cleanKey_default,
|
|
36
35
|
createEnum: () => createEnum,
|
|
37
36
|
dotGetSchema: () => dotGetSchema_default2,
|
|
37
|
+
fieldTypes: () => fieldTypes_default,
|
|
38
38
|
getFieldType: () => getFieldType,
|
|
39
|
+
getSchemaFromAnyOrionForm: () => getSchemaFromAnyOrionForm,
|
|
40
|
+
getSchemaModelName: () => getSchemaModelName,
|
|
41
|
+
getSchemaWithMetadataFromAnyOrionForm: () => getSchemaWithMetadataFromAnyOrionForm,
|
|
39
42
|
getValidationErrors: () => getValidationErrors2,
|
|
43
|
+
isSchemaLike: () => isSchemaLike,
|
|
44
|
+
isSchemaOrFieldLike: () => isSchemaOrFieldLike,
|
|
40
45
|
isValid: () => isValid,
|
|
46
|
+
schemaWithName: () => schemaWithName,
|
|
41
47
|
validate: () => validate,
|
|
42
48
|
validateKey: () => validateKey_default
|
|
43
49
|
});
|
|
@@ -45,14 +51,14 @@ module.exports = __toCommonJS(index_exports);
|
|
|
45
51
|
|
|
46
52
|
// src/ValidationError.ts
|
|
47
53
|
var import_isPlainObject = __toESM(require("lodash/isPlainObject"), 1);
|
|
48
|
-
var getPrintableError =
|
|
54
|
+
var getPrintableError = (validationErrors) => {
|
|
49
55
|
const printableErrors = Object.keys(validationErrors).map((key) => {
|
|
50
56
|
return `${key}: ${validationErrors[key]}`;
|
|
51
57
|
}).join(", ");
|
|
52
58
|
const message = `Validation Error: {${printableErrors}}`;
|
|
53
59
|
return message;
|
|
54
|
-
}
|
|
55
|
-
var
|
|
60
|
+
};
|
|
61
|
+
var ValidationError = class _ValidationError extends Error {
|
|
56
62
|
code;
|
|
57
63
|
isValidationError;
|
|
58
64
|
isOrionError;
|
|
@@ -69,57 +75,31 @@ var _ValidationError = class _ValidationError extends Error {
|
|
|
69
75
|
this.validationErrors = validationErrors;
|
|
70
76
|
this.getInfo;
|
|
71
77
|
}
|
|
72
|
-
getInfo =
|
|
78
|
+
getInfo = () => {
|
|
73
79
|
return {
|
|
74
80
|
error: "validationError",
|
|
75
81
|
message: "Validation Error",
|
|
76
82
|
validationErrors: this.validationErrors
|
|
77
83
|
};
|
|
78
|
-
}
|
|
79
|
-
prependKey =
|
|
84
|
+
};
|
|
85
|
+
prependKey = (prepend) => {
|
|
80
86
|
const newErrors = {};
|
|
81
87
|
const keys = Object.keys(this.validationErrors);
|
|
82
88
|
for (const key of keys) {
|
|
83
89
|
newErrors[`${prepend}.${key}`] = this.validationErrors[key];
|
|
84
90
|
}
|
|
85
91
|
return new _ValidationError(newErrors);
|
|
86
|
-
}
|
|
92
|
+
};
|
|
87
93
|
};
|
|
88
|
-
__name(_ValidationError, "ValidationError");
|
|
89
|
-
var ValidationError = _ValidationError;
|
|
90
94
|
|
|
91
|
-
// src/
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
if (obj.prototype === void 0) {
|
|
95
|
-
return isCtorClass;
|
|
96
|
-
}
|
|
97
|
-
const isPrototypeCtorClass = obj.prototype.constructor && obj.prototype.constructor.toString && obj.prototype.constructor.toString().substring(0, 5) === "class";
|
|
98
|
-
return isCtorClass || isPrototypeCtorClass;
|
|
99
|
-
}
|
|
100
|
-
__name(isClass, "isClass");
|
|
101
|
-
var convertOnParam = /* @__PURE__ */ __name((info, paramName) => {
|
|
102
|
-
if (!info[paramName]) return;
|
|
103
|
-
const type = info[paramName].type;
|
|
104
|
-
if (!type) return;
|
|
105
|
-
if (typeof type !== "function") return;
|
|
106
|
-
if (!isClass(type)) return;
|
|
107
|
-
if (!type.getModel || !type.__schemaId) return;
|
|
108
|
-
info[paramName].type = type.getModel().getCleanSchema();
|
|
109
|
-
}, "convertOnParam");
|
|
110
|
-
var convertTypedModel = /* @__PURE__ */ __name((info) => {
|
|
111
|
-
convertOnParam(info, "schema");
|
|
112
|
-
convertOnParam(info, "currentSchema");
|
|
113
|
-
}, "convertTypedModel");
|
|
114
|
-
|
|
115
|
-
// src/getSchemaFromTypedModel.ts
|
|
116
|
-
var getSchemaFromTypedModel = /* @__PURE__ */ __name((schema) => {
|
|
95
|
+
// src/getSchemaFromTypedSchema.ts
|
|
96
|
+
Symbol.metadata ?? (Symbol.metadata = Symbol("Symbol.metadata"));
|
|
97
|
+
var getSchemaFromTypedSchema = (schema) => {
|
|
117
98
|
const item = schema;
|
|
118
|
-
if (
|
|
119
|
-
if (!
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
}, "getSchemaFromTypedModel");
|
|
99
|
+
if (!schema[Symbol.metadata]) return item;
|
|
100
|
+
if (!schema[Symbol.metadata]._isTypedSchema) return item;
|
|
101
|
+
return schema[Symbol.metadata]._getModel().getSchema();
|
|
102
|
+
};
|
|
123
103
|
|
|
124
104
|
// src/getValidationErrors/getError/index.ts
|
|
125
105
|
var import_isNil2 = __toESM(require("lodash/isNil"), 1);
|
|
@@ -132,13 +112,13 @@ var import_isString5 = __toESM(require("lodash/isString"), 1);
|
|
|
132
112
|
// src/fieldType.ts
|
|
133
113
|
function fieldType(opts) {
|
|
134
114
|
const { name, validate: validate2, clean: clean3, ...otherFields } = opts;
|
|
135
|
-
const overwrittenValidate =
|
|
115
|
+
const overwrittenValidate = (value, info = {}) => {
|
|
136
116
|
if (!info.currentSchema) {
|
|
137
117
|
info.currentSchema = {};
|
|
138
118
|
}
|
|
139
119
|
return validate2(value, info);
|
|
140
|
-
}
|
|
141
|
-
const overwrittenClean =
|
|
120
|
+
};
|
|
121
|
+
const overwrittenClean = (value, info = {}) => {
|
|
142
122
|
if (!info.options) {
|
|
143
123
|
info.options = {};
|
|
144
124
|
}
|
|
@@ -146,16 +126,16 @@ function fieldType(opts) {
|
|
|
146
126
|
return clean3(value, info);
|
|
147
127
|
}
|
|
148
128
|
return value;
|
|
149
|
-
}
|
|
129
|
+
};
|
|
150
130
|
return {
|
|
151
131
|
...otherFields,
|
|
152
132
|
name,
|
|
153
133
|
validate: overwrittenValidate,
|
|
154
134
|
clean: overwrittenClean,
|
|
155
|
-
|
|
135
|
+
__isFieldType: true,
|
|
136
|
+
__tsFieldType: null
|
|
156
137
|
};
|
|
157
138
|
}
|
|
158
|
-
__name(fieldType, "fieldType");
|
|
159
139
|
|
|
160
140
|
// src/fieldTypes/array.ts
|
|
161
141
|
var import_isArray = __toESM(require("lodash/isArray"), 1);
|
|
@@ -191,9 +171,7 @@ var array_default = fieldType({
|
|
|
191
171
|
clean(value, { options }) {
|
|
192
172
|
if (options.autoConvert) {
|
|
193
173
|
if (!(0, import_isArray.default)(value)) {
|
|
194
|
-
value = [
|
|
195
|
-
value
|
|
196
|
-
];
|
|
174
|
+
value = [value];
|
|
197
175
|
}
|
|
198
176
|
}
|
|
199
177
|
return value;
|
|
@@ -230,12 +208,12 @@ var string_default = fieldType({
|
|
|
230
208
|
name: "string",
|
|
231
209
|
validate(value, { currentSchema }) {
|
|
232
210
|
if (!(0, import_isString.default)(value)) return Errors_default.NOT_A_STRING;
|
|
233
|
-
if (isFinite(currentSchema.min)) {
|
|
211
|
+
if (Number.isFinite(currentSchema.min)) {
|
|
234
212
|
if (value.length < currentSchema.min) {
|
|
235
213
|
return Errors_default.STRING_TOO_SHORT;
|
|
236
214
|
}
|
|
237
215
|
}
|
|
238
|
-
if (isFinite(currentSchema.max)) {
|
|
216
|
+
if (Number.isFinite(currentSchema.max)) {
|
|
239
217
|
if (value.length > currentSchema.max) {
|
|
240
218
|
return Errors_default.STRING_TOO_LONG;
|
|
241
219
|
}
|
|
@@ -276,13 +254,13 @@ var date_default = fieldType({
|
|
|
276
254
|
if (options.autoConvert) {
|
|
277
255
|
if ((0, import_isString2.default)(value)) {
|
|
278
256
|
const result = new Date(value);
|
|
279
|
-
if (isNaN(result.getTime())) {
|
|
257
|
+
if (Number.isNaN(result.getTime())) {
|
|
280
258
|
return value;
|
|
281
259
|
}
|
|
282
260
|
value = result;
|
|
283
261
|
} else if ((0, import_isNumber.default)(value)) {
|
|
284
262
|
const result = new Date(value);
|
|
285
|
-
if (isNaN(result.getTime())) {
|
|
263
|
+
if (Number.isNaN(result.getTime())) {
|
|
286
264
|
return value;
|
|
287
265
|
}
|
|
288
266
|
value = result;
|
|
@@ -296,18 +274,17 @@ var date_default = fieldType({
|
|
|
296
274
|
var import_isInteger = __toESM(require("lodash/isInteger"), 1);
|
|
297
275
|
|
|
298
276
|
// src/fieldTypes/number.ts
|
|
299
|
-
var import_isFinite = __toESM(require("lodash/isFinite"), 1);
|
|
300
277
|
var import_toNumber = __toESM(require("lodash/toNumber"), 1);
|
|
301
278
|
var number_default = fieldType({
|
|
302
279
|
name: "number",
|
|
303
280
|
validate(value, { currentSchema }) {
|
|
304
|
-
if (!
|
|
305
|
-
if (
|
|
281
|
+
if (!Number.isFinite(value)) return Errors_default.NOT_A_NUMBER;
|
|
282
|
+
if (Number.isFinite(currentSchema.min)) {
|
|
306
283
|
if (value < currentSchema.min) {
|
|
307
284
|
return Errors_default.NUMBER_TOO_SMALL;
|
|
308
285
|
}
|
|
309
286
|
}
|
|
310
|
-
if (
|
|
287
|
+
if (Number.isFinite(currentSchema.max)) {
|
|
311
288
|
if (value > currentSchema.max) {
|
|
312
289
|
return Errors_default.NUMBER_TOO_BIG;
|
|
313
290
|
}
|
|
@@ -362,10 +339,11 @@ var boolean_default = fieldType({
|
|
|
362
339
|
clean(value, { options }) {
|
|
363
340
|
if (options.autoConvert) {
|
|
364
341
|
if (typeof value === "string") {
|
|
365
|
-
|
|
342
|
+
const stringValue = value;
|
|
343
|
+
if (stringValue === "true") {
|
|
366
344
|
value = true;
|
|
367
345
|
}
|
|
368
|
-
if (
|
|
346
|
+
if (stringValue === "false") {
|
|
369
347
|
value = false;
|
|
370
348
|
}
|
|
371
349
|
}
|
|
@@ -414,7 +392,7 @@ var blackbox_default = fieldType({
|
|
|
414
392
|
// src/fieldTypes/any.ts
|
|
415
393
|
var any_default = fieldType({
|
|
416
394
|
name: "any",
|
|
417
|
-
validate(
|
|
395
|
+
validate() {
|
|
418
396
|
}
|
|
419
397
|
});
|
|
420
398
|
|
|
@@ -435,19 +413,19 @@ var fieldTypes_default = {
|
|
|
435
413
|
|
|
436
414
|
// src/getValidationErrors/getError/getFieldValidator.ts
|
|
437
415
|
var import_has = __toESM(require("lodash/has"), 1);
|
|
438
|
-
function
|
|
416
|
+
function getFieldValidator(type) {
|
|
439
417
|
if ((0, import_isPlainObject4.default)(type)) {
|
|
440
|
-
if (type.
|
|
418
|
+
if (type.__isFieldType) return "custom";
|
|
441
419
|
return "plainObject";
|
|
442
420
|
}
|
|
443
421
|
if ((0, import_isArray3.default)(type)) return "array";
|
|
444
422
|
if (type === String) return "string";
|
|
445
|
-
if (type === Date) return "date";
|
|
423
|
+
if (typeof type === "function" && type.name === "Date") return "date";
|
|
446
424
|
if (type === Number) return "number";
|
|
447
425
|
if (type === Boolean) return "boolean";
|
|
448
426
|
if (type === "enum") return "string";
|
|
449
427
|
if (!(0, import_isString5.default)(type)) {
|
|
450
|
-
throw new Error(
|
|
428
|
+
throw new Error(`Field type is invalid. Pass a string or a custom field type. Got ${type}`);
|
|
451
429
|
}
|
|
452
430
|
const exists = (0, import_has.default)(fieldTypes_default, type);
|
|
453
431
|
if (!exists) {
|
|
@@ -455,25 +433,17 @@ function getFieldValidator_default(type) {
|
|
|
455
433
|
}
|
|
456
434
|
return type;
|
|
457
435
|
}
|
|
458
|
-
__name(getFieldValidator_default, "default");
|
|
459
436
|
|
|
460
437
|
// src/getValidationErrors/getError/index.ts
|
|
461
438
|
async function getValidationErrors(params) {
|
|
462
439
|
const { schema, doc, currentDoc, value, currentSchema, keys, options = {}, args = [] } = params;
|
|
463
|
-
const info = {
|
|
464
|
-
schema,
|
|
465
|
-
doc,
|
|
466
|
-
currentDoc,
|
|
467
|
-
keys,
|
|
468
|
-
currentSchema,
|
|
469
|
-
options
|
|
470
|
-
};
|
|
440
|
+
const info = { schema, doc, currentDoc, keys, currentSchema, options };
|
|
471
441
|
if ((0, import_isNil2.default)(value)) {
|
|
472
442
|
if (!currentSchema.optional && !options.omitRequired) {
|
|
473
443
|
return Errors_default.REQUIRED;
|
|
474
444
|
}
|
|
475
445
|
} else {
|
|
476
|
-
const validatorKey =
|
|
446
|
+
const validatorKey = getFieldValidator(currentSchema.type);
|
|
477
447
|
const validator = validatorKey === "custom" ? currentSchema.type : fieldTypes_default[validatorKey];
|
|
478
448
|
const error = await validator.validate(value, info, ...args);
|
|
479
449
|
if (error) {
|
|
@@ -496,7 +466,6 @@ async function getValidationErrors(params) {
|
|
|
496
466
|
}
|
|
497
467
|
return null;
|
|
498
468
|
}
|
|
499
|
-
__name(getValidationErrors, "getValidationErrors");
|
|
500
469
|
|
|
501
470
|
// src/getValidationErrors/doValidation.ts
|
|
502
471
|
var import_isPlainObject5 = __toESM(require("lodash/isPlainObject"), 1);
|
|
@@ -504,20 +473,24 @@ var import_isArray4 = __toESM(require("lodash/isArray"), 1);
|
|
|
504
473
|
var import_clone = __toESM(require("lodash/clone"), 1);
|
|
505
474
|
var import_isNil3 = __toESM(require("lodash/isNil"), 1);
|
|
506
475
|
var import_difference2 = __toESM(require("lodash/difference"), 1);
|
|
476
|
+
|
|
477
|
+
// src/getValidationErrors/convertTypedSchema.ts
|
|
478
|
+
var convertOnParam = (info, paramName) => {
|
|
479
|
+
if (!info[paramName]) return;
|
|
480
|
+
const type = info[paramName].type;
|
|
481
|
+
if (!type) return;
|
|
482
|
+
info[paramName].type = getSchemaFromTypedSchema(type);
|
|
483
|
+
};
|
|
484
|
+
var convertTypedSchema = (info) => {
|
|
485
|
+
convertOnParam(info, "schema");
|
|
486
|
+
convertOnParam(info, "currentSchema");
|
|
487
|
+
};
|
|
488
|
+
|
|
489
|
+
// src/getValidationErrors/doValidation.ts
|
|
507
490
|
async function doValidation(params) {
|
|
508
|
-
|
|
491
|
+
convertTypedSchema(params);
|
|
509
492
|
const { schema, doc, currentDoc, value, currentSchema, keys = [], addError, options, args } = params;
|
|
510
|
-
const info = {
|
|
511
|
-
schema,
|
|
512
|
-
doc,
|
|
513
|
-
currentDoc,
|
|
514
|
-
value,
|
|
515
|
-
currentSchema,
|
|
516
|
-
keys,
|
|
517
|
-
options,
|
|
518
|
-
args,
|
|
519
|
-
addError
|
|
520
|
-
};
|
|
493
|
+
const info = { schema, doc, currentDoc, value, currentSchema, keys, options, args, addError };
|
|
521
494
|
const error = await getValidationErrors(info);
|
|
522
495
|
if (error) {
|
|
523
496
|
addError(keys, error);
|
|
@@ -527,7 +500,7 @@ async function doValidation(params) {
|
|
|
527
500
|
if ((0, import_isPlainObject5.default)(currentSchema.type)) {
|
|
528
501
|
const type = currentSchema.type;
|
|
529
502
|
if (type) {
|
|
530
|
-
if (type.
|
|
503
|
+
if (type.__isFieldType) {
|
|
531
504
|
return;
|
|
532
505
|
}
|
|
533
506
|
if (typeof type.__skipChildValidation === "function") {
|
|
@@ -567,15 +540,12 @@ async function doValidation(params) {
|
|
|
567
540
|
...info,
|
|
568
541
|
currentDoc: value,
|
|
569
542
|
value: itemValue,
|
|
570
|
-
currentSchema: {
|
|
571
|
-
type: itemSchema
|
|
572
|
-
},
|
|
543
|
+
currentSchema: { type: itemSchema },
|
|
573
544
|
keys: keyItemKeys
|
|
574
545
|
});
|
|
575
546
|
}
|
|
576
547
|
}
|
|
577
548
|
}
|
|
578
|
-
__name(doValidation, "doValidation");
|
|
579
549
|
|
|
580
550
|
// src/getValidationErrors/getValidationErrorsObject.ts
|
|
581
551
|
var import_dot_object = __toESM(require("dot-object"), 1);
|
|
@@ -588,40 +558,33 @@ function getValidationErrorsObject(validationErrors) {
|
|
|
588
558
|
}
|
|
589
559
|
return dot.dot(errors);
|
|
590
560
|
}
|
|
591
|
-
__name(getValidationErrorsObject, "getValidationErrorsObject");
|
|
592
561
|
|
|
593
562
|
// src/getValidationErrors/index.ts
|
|
594
563
|
var defaultOptions = {
|
|
595
564
|
omitRequired: false
|
|
596
565
|
};
|
|
597
566
|
async function getValidationErrors2(schema, doc, passedOptions = {}, ...args) {
|
|
598
|
-
schema =
|
|
599
|
-
const options = {
|
|
600
|
-
...defaultOptions,
|
|
601
|
-
...passedOptions
|
|
602
|
-
};
|
|
567
|
+
schema = getSchemaFromTypedSchema(schema);
|
|
568
|
+
const options = { ...defaultOptions, ...passedOptions };
|
|
603
569
|
const errors = [];
|
|
604
|
-
const addError =
|
|
570
|
+
const addError = (keys, code) => {
|
|
605
571
|
errors.push({
|
|
606
572
|
key: keys.join("."),
|
|
607
573
|
code
|
|
608
574
|
});
|
|
609
|
-
}
|
|
575
|
+
};
|
|
610
576
|
await doValidation({
|
|
611
577
|
schema,
|
|
612
578
|
doc,
|
|
613
579
|
currentDoc: doc,
|
|
614
580
|
value: doc,
|
|
615
|
-
currentSchema: {
|
|
616
|
-
type: schema
|
|
617
|
-
},
|
|
581
|
+
currentSchema: { type: schema },
|
|
618
582
|
addError,
|
|
619
583
|
options,
|
|
620
584
|
args
|
|
621
585
|
});
|
|
622
586
|
return getValidationErrorsObject(errors);
|
|
623
587
|
}
|
|
624
|
-
__name(getValidationErrors2, "getValidationErrors");
|
|
625
588
|
|
|
626
589
|
// src/validate.ts
|
|
627
590
|
async function validate(schema, doc, passedOptions = {}, ...args) {
|
|
@@ -630,22 +593,19 @@ async function validate(schema, doc, passedOptions = {}, ...args) {
|
|
|
630
593
|
throw new ValidationError(validationErrors);
|
|
631
594
|
}
|
|
632
595
|
}
|
|
633
|
-
__name(validate, "validate");
|
|
634
596
|
|
|
635
597
|
// src/isValid.ts
|
|
636
598
|
async function isValid(schema, doc, passedOptions = {}, ...args) {
|
|
637
599
|
const validationErrors = await getValidationErrors2(schema, doc, passedOptions, ...args);
|
|
638
600
|
return !validationErrors;
|
|
639
601
|
}
|
|
640
|
-
__name(isValid, "isValid");
|
|
641
602
|
|
|
642
603
|
// src/getValidationErrors/getError/getFieldType.ts
|
|
643
604
|
function getFieldType(type) {
|
|
644
|
-
const validatorKey =
|
|
605
|
+
const validatorKey = getFieldValidator(type);
|
|
645
606
|
const validator = validatorKey === "custom" ? type : fieldTypes_default[validatorKey];
|
|
646
607
|
return validator;
|
|
647
608
|
}
|
|
648
|
-
__name(getFieldType, "getFieldType");
|
|
649
609
|
|
|
650
610
|
// src/clean/recursiveClean.ts
|
|
651
611
|
var import_isUndefined = __toESM(require("lodash/isUndefined"), 1);
|
|
@@ -663,7 +623,6 @@ function getObjectNode(schema, value) {
|
|
|
663
623
|
}
|
|
664
624
|
return null;
|
|
665
625
|
}
|
|
666
|
-
__name(getObjectNode, "getObjectNode");
|
|
667
626
|
|
|
668
627
|
// src/clean/cleanType.ts
|
|
669
628
|
async function cleanType(type, fieldSchema, value, info, ...args) {
|
|
@@ -690,11 +649,6 @@ async function cleanType(type, fieldSchema, value, info, ...args) {
|
|
|
690
649
|
value = defaultValue;
|
|
691
650
|
}
|
|
692
651
|
}
|
|
693
|
-
const { autoValue } = fieldSchema;
|
|
694
|
-
if (autoValue) {
|
|
695
|
-
needReClean = true;
|
|
696
|
-
value = await autoValue(value, info, ...args);
|
|
697
|
-
}
|
|
698
652
|
const { clean: clean3 } = fieldSchema;
|
|
699
653
|
if (clean3) {
|
|
700
654
|
needReClean = true;
|
|
@@ -705,11 +659,14 @@ async function cleanType(type, fieldSchema, value, info, ...args) {
|
|
|
705
659
|
}
|
|
706
660
|
return value;
|
|
707
661
|
}
|
|
708
|
-
__name(cleanType, "cleanType");
|
|
709
662
|
|
|
710
663
|
// src/clean/recursiveClean.ts
|
|
711
664
|
var import_isNil5 = __toESM(require("lodash/isNil"), 1);
|
|
712
|
-
var cleanObjectFields =
|
|
665
|
+
var cleanObjectFields = async ({
|
|
666
|
+
schema,
|
|
667
|
+
value,
|
|
668
|
+
...other
|
|
669
|
+
}) => {
|
|
713
670
|
const keys = Object.keys(schema.type).filter((key) => !key.startsWith("__"));
|
|
714
671
|
const newDoc = {};
|
|
715
672
|
for (const key of keys) {
|
|
@@ -729,10 +686,14 @@ var cleanObjectFields = /* @__PURE__ */ __name(async function({ schema, value, .
|
|
|
729
686
|
}
|
|
730
687
|
}
|
|
731
688
|
return newDoc;
|
|
732
|
-
}
|
|
733
|
-
var cleanArrayItems =
|
|
689
|
+
};
|
|
690
|
+
var cleanArrayItems = async ({
|
|
691
|
+
schema,
|
|
692
|
+
value,
|
|
693
|
+
...other
|
|
694
|
+
}) => {
|
|
734
695
|
const schemaType = schema.type[0];
|
|
735
|
-
const promises = value.map(async (item
|
|
696
|
+
const promises = value.map(async (item) => {
|
|
736
697
|
const newValue = await clean({
|
|
737
698
|
...other,
|
|
738
699
|
schema: {
|
|
@@ -745,7 +706,7 @@ var cleanArrayItems = /* @__PURE__ */ __name(async function({ schema, value, ...
|
|
|
745
706
|
});
|
|
746
707
|
const result = await Promise.all(promises);
|
|
747
708
|
return result.filter((value2) => !(0, import_isUndefined.default)(value2));
|
|
748
|
-
}
|
|
709
|
+
};
|
|
749
710
|
function getArrayNode(schema, value) {
|
|
750
711
|
if ((0, import_isArray5.default)(schema.type) && !(0, import_isNil5.default)(value)) {
|
|
751
712
|
const result = schema;
|
|
@@ -753,13 +714,10 @@ function getArrayNode(schema, value) {
|
|
|
753
714
|
}
|
|
754
715
|
return null;
|
|
755
716
|
}
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
const currSchema = schema.type === void 0 ? {
|
|
761
|
-
type: schema
|
|
762
|
-
} : schema;
|
|
717
|
+
var clean = async (info) => {
|
|
718
|
+
convertTypedSchema(info);
|
|
719
|
+
const { schema, args = [], value } = info;
|
|
720
|
+
const currSchema = schema.type === void 0 ? { type: schema } : schema;
|
|
763
721
|
const objectSchema = getObjectNode(currSchema, value);
|
|
764
722
|
if (objectSchema) {
|
|
765
723
|
const newDoc = await cleanObjectFields({
|
|
@@ -774,9 +732,7 @@ var clean = /* @__PURE__ */ __name(async function(info) {
|
|
|
774
732
|
if (arraySchema) {
|
|
775
733
|
let updatedValue = value;
|
|
776
734
|
if (!(0, import_isArray5.default)(value) && !Array.isArray(value)) {
|
|
777
|
-
updatedValue = [
|
|
778
|
-
value
|
|
779
|
-
];
|
|
735
|
+
updatedValue = [value];
|
|
780
736
|
}
|
|
781
737
|
const newDoc = await cleanArrayItems({
|
|
782
738
|
...info,
|
|
@@ -788,7 +744,7 @@ var clean = /* @__PURE__ */ __name(async function(info) {
|
|
|
788
744
|
}
|
|
789
745
|
const result = await cleanType(currSchema.type, currSchema, value, info, ...args);
|
|
790
746
|
return result;
|
|
791
|
-
}
|
|
747
|
+
};
|
|
792
748
|
var recursiveClean_default = clean;
|
|
793
749
|
|
|
794
750
|
// src/clean/index.ts
|
|
@@ -800,15 +756,10 @@ var defaultOptions2 = {
|
|
|
800
756
|
};
|
|
801
757
|
async function clean2(schema, doc, opts = {}, ...args) {
|
|
802
758
|
if (!doc) return doc;
|
|
803
|
-
schema =
|
|
804
|
-
const options = {
|
|
805
|
-
...defaultOptions2,
|
|
806
|
-
...opts
|
|
807
|
-
};
|
|
759
|
+
schema = getSchemaFromTypedSchema(schema);
|
|
760
|
+
const options = { ...defaultOptions2, ...opts };
|
|
808
761
|
const params = {
|
|
809
|
-
schema: {
|
|
810
|
-
type: schema
|
|
811
|
-
},
|
|
762
|
+
schema: { type: schema },
|
|
812
763
|
value: doc,
|
|
813
764
|
doc: options.forceDoc || doc,
|
|
814
765
|
currentDoc: doc,
|
|
@@ -818,42 +769,33 @@ async function clean2(schema, doc, opts = {}, ...args) {
|
|
|
818
769
|
const cleanedResult = await recursiveClean_default(params);
|
|
819
770
|
return cleanedResult;
|
|
820
771
|
}
|
|
821
|
-
__name(clean2, "clean");
|
|
822
772
|
|
|
823
773
|
// src/validateKey/dotGetSchema.ts
|
|
824
774
|
var import_isPlainObject7 = __toESM(require("lodash/isPlainObject"), 1);
|
|
825
775
|
var import_isNil6 = __toESM(require("lodash/isNil"), 1);
|
|
826
|
-
var dotGet =
|
|
776
|
+
var dotGet = function dotGet2(object, path) {
|
|
827
777
|
if (path === "") return object;
|
|
828
778
|
const pathParts = path.split(".");
|
|
829
779
|
const first = pathParts.shift();
|
|
830
780
|
const remainingPath = pathParts.join(".");
|
|
831
781
|
const levelObject = object.type;
|
|
832
782
|
if (first === "$" || /^[0-9]+$/.test(first)) {
|
|
833
|
-
return dotGet2({
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
} else if ((0, import_isPlainObject7.default)(levelObject[first])) {
|
|
783
|
+
return dotGet2({ type: levelObject[0] }, remainingPath);
|
|
784
|
+
}
|
|
785
|
+
if ((0, import_isPlainObject7.default)(levelObject[first])) {
|
|
837
786
|
return dotGet2(levelObject[first], remainingPath);
|
|
838
787
|
}
|
|
839
788
|
if (levelObject === "blackbox") {
|
|
840
|
-
return {
|
|
841
|
-
type: "blackbox",
|
|
842
|
-
optional: true,
|
|
843
|
-
isBlackboxChild: true
|
|
844
|
-
};
|
|
789
|
+
return { type: "blackbox", optional: true, isBlackboxChild: true };
|
|
845
790
|
}
|
|
846
791
|
return null;
|
|
847
|
-
}
|
|
792
|
+
};
|
|
848
793
|
function dotGetSchema_default(schema, path) {
|
|
849
794
|
if ((0, import_isNil6.default)(schema)) {
|
|
850
795
|
throw new Error("You need to pass a schema");
|
|
851
796
|
}
|
|
852
|
-
return dotGet({
|
|
853
|
-
type: schema
|
|
854
|
-
}, path);
|
|
797
|
+
return dotGet({ type: schema }, path);
|
|
855
798
|
}
|
|
856
|
-
__name(dotGetSchema_default, "default");
|
|
857
799
|
|
|
858
800
|
// src/dotGetSchema.ts
|
|
859
801
|
var dotGetSchema_default2 = dotGetSchema_default;
|
|
@@ -863,10 +805,7 @@ var defaultOptions3 = {
|
|
|
863
805
|
filter: true
|
|
864
806
|
};
|
|
865
807
|
async function cleanKey_default(schema, key, value, passedOptions = {}, ...args) {
|
|
866
|
-
const options = {
|
|
867
|
-
...defaultOptions3,
|
|
868
|
-
...passedOptions
|
|
869
|
-
};
|
|
808
|
+
const options = { ...defaultOptions3, ...passedOptions };
|
|
870
809
|
const keySchema = dotGetSchema_default2(schema, key);
|
|
871
810
|
if (!keySchema) {
|
|
872
811
|
if (options.filter) {
|
|
@@ -875,44 +814,35 @@ async function cleanKey_default(schema, key, value, passedOptions = {}, ...args)
|
|
|
875
814
|
return value;
|
|
876
815
|
}
|
|
877
816
|
}
|
|
878
|
-
const result = await clean2({
|
|
879
|
-
clean: keySchema
|
|
880
|
-
}, {
|
|
881
|
-
clean: value
|
|
882
|
-
}, options, ...args);
|
|
817
|
+
const result = await clean2({ clean: keySchema }, { clean: value }, options, ...args);
|
|
883
818
|
return result.clean;
|
|
884
819
|
}
|
|
885
|
-
__name(cleanKey_default, "default");
|
|
886
820
|
|
|
887
821
|
// src/validateKey/index.ts
|
|
888
822
|
var defaultOptions4 = {
|
|
889
823
|
filter: false
|
|
890
824
|
};
|
|
891
825
|
async function validateKey_default(schema, key, value, passedOptions = {}, ...args) {
|
|
892
|
-
const options = {
|
|
893
|
-
...defaultOptions4,
|
|
894
|
-
...passedOptions
|
|
895
|
-
};
|
|
826
|
+
const options = { ...defaultOptions4, ...passedOptions };
|
|
896
827
|
const keySchema = dotGetSchema_default(schema, key);
|
|
897
828
|
if (!keySchema) {
|
|
898
829
|
if (options.filter) {
|
|
899
830
|
return Errors_default.NOT_IN_SCHEMA;
|
|
900
|
-
} else {
|
|
901
|
-
return null;
|
|
902
831
|
}
|
|
832
|
+
return null;
|
|
903
833
|
}
|
|
904
834
|
if (keySchema.isBlackboxChild) {
|
|
905
835
|
return null;
|
|
906
836
|
}
|
|
907
|
-
const result = await getValidationErrors2(
|
|
908
|
-
validate: keySchema
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
837
|
+
const result = await getValidationErrors2(
|
|
838
|
+
{ validate: keySchema },
|
|
839
|
+
{ validate: value },
|
|
840
|
+
options,
|
|
841
|
+
...args
|
|
842
|
+
);
|
|
912
843
|
if (!result) return null;
|
|
913
844
|
return result.validate;
|
|
914
845
|
}
|
|
915
|
-
__name(validateKey_default, "default");
|
|
916
846
|
|
|
917
847
|
// src/fieldTypes/enum.ts
|
|
918
848
|
var import_isString6 = __toESM(require("lodash/isString"), 1);
|
|
@@ -926,19 +856,17 @@ function createEnum(name, values) {
|
|
|
926
856
|
enumName: name,
|
|
927
857
|
enumValues: values
|
|
928
858
|
},
|
|
929
|
-
toGraphQLType:
|
|
859
|
+
toGraphQLType: (GraphQL) => {
|
|
930
860
|
global.GraphQLEnums = global.GraphQLEnums || {};
|
|
931
861
|
global.GraphQLEnums[name] = global.GraphQLEnums[name] || new GraphQL.GraphQLEnumType({
|
|
932
862
|
name,
|
|
933
863
|
values: values.reduce((result, value) => {
|
|
934
|
-
result[value] = {
|
|
935
|
-
value
|
|
936
|
-
};
|
|
864
|
+
result[value] = { value };
|
|
937
865
|
return result;
|
|
938
866
|
}, {})
|
|
939
867
|
});
|
|
940
868
|
return global.GraphQLEnums[name];
|
|
941
|
-
},
|
|
869
|
+
},
|
|
942
870
|
validate(value, { currentSchema }) {
|
|
943
871
|
if (!(0, import_isString6.default)(value)) return Errors_default.NOT_A_STRING;
|
|
944
872
|
if (!(0, import_includes2.default)(values, value)) {
|
|
@@ -963,7 +891,75 @@ function createEnum(name, values) {
|
|
|
963
891
|
})
|
|
964
892
|
};
|
|
965
893
|
}
|
|
966
|
-
|
|
894
|
+
|
|
895
|
+
// src/models.ts
|
|
896
|
+
Symbol.metadata ?? (Symbol.metadata = Symbol("Symbol.metadata"));
|
|
897
|
+
function isSchemaLike(type) {
|
|
898
|
+
var _a;
|
|
899
|
+
if (!type) return false;
|
|
900
|
+
if (objectHasSubObjectWithKey(type, "type")) return true;
|
|
901
|
+
if ((_a = type == null ? void 0 : type[Symbol.metadata]) == null ? void 0 : _a._getModel) return true;
|
|
902
|
+
if (type.getModel) return true;
|
|
903
|
+
if (type.getSchema) return true;
|
|
904
|
+
if (type.getCleanSchema) return true;
|
|
905
|
+
if (type.__isModel) return true;
|
|
906
|
+
if (type.__modelName) return true;
|
|
907
|
+
return false;
|
|
908
|
+
}
|
|
909
|
+
function isSchemaOrFieldLike(type) {
|
|
910
|
+
if (Array.isArray(type)) {
|
|
911
|
+
if (type.length !== 1) return false;
|
|
912
|
+
return isSchemaOrFieldLike(type[0]);
|
|
913
|
+
}
|
|
914
|
+
if (isSchemaLike(type)) return true;
|
|
915
|
+
try {
|
|
916
|
+
if (getFieldValidator(type)) return true;
|
|
917
|
+
} catch {
|
|
918
|
+
return false;
|
|
919
|
+
}
|
|
920
|
+
return false;
|
|
921
|
+
}
|
|
922
|
+
function getSchemaModelName(type) {
|
|
923
|
+
if (!type) return null;
|
|
924
|
+
if (type.__modelName) return type.__modelName;
|
|
925
|
+
if (type.getModel) return type.getModel().name;
|
|
926
|
+
if (type.getSchema) return type.getSchema().__modelName;
|
|
927
|
+
return null;
|
|
928
|
+
}
|
|
929
|
+
function getSchemaFromAnyOrionForm(type) {
|
|
930
|
+
var _a, _b;
|
|
931
|
+
if ((_a = type == null ? void 0 : type[Symbol.metadata]) == null ? void 0 : _a._getModel) {
|
|
932
|
+
return (_b = type == null ? void 0 : type[Symbol.metadata]) == null ? void 0 : _b._getModel().getSchema();
|
|
933
|
+
}
|
|
934
|
+
if (type == null ? void 0 : type.getModel) return type.getModel().getSchema();
|
|
935
|
+
if (type.getSchema) {
|
|
936
|
+
return type.getSchema();
|
|
937
|
+
}
|
|
938
|
+
if (type.getSchema) {
|
|
939
|
+
return type.getSchema();
|
|
940
|
+
}
|
|
941
|
+
if (objectHasSubObjectWithKey(type, "type")) return type;
|
|
942
|
+
return null;
|
|
943
|
+
}
|
|
944
|
+
function objectHasSubObjectWithKey(object, key) {
|
|
945
|
+
if (!object || typeof object !== "object") return false;
|
|
946
|
+
for (const key1 in object) {
|
|
947
|
+
const value = object[key1];
|
|
948
|
+
if (value && typeof value === "object" && key in value) {
|
|
949
|
+
return true;
|
|
950
|
+
}
|
|
951
|
+
}
|
|
952
|
+
return false;
|
|
953
|
+
}
|
|
954
|
+
function getSchemaWithMetadataFromAnyOrionForm(type) {
|
|
955
|
+
return getSchemaFromAnyOrionForm(type);
|
|
956
|
+
}
|
|
957
|
+
|
|
958
|
+
// src/schemaWithName/index.ts
|
|
959
|
+
function schemaWithName(name, schema) {
|
|
960
|
+
schema.__modelName = name;
|
|
961
|
+
return schema;
|
|
962
|
+
}
|
|
967
963
|
// Annotate the CommonJS export names for ESM import in node:
|
|
968
964
|
0 && (module.exports = {
|
|
969
965
|
ValidationError,
|
|
@@ -971,9 +967,16 @@ __name(createEnum, "createEnum");
|
|
|
971
967
|
cleanKey,
|
|
972
968
|
createEnum,
|
|
973
969
|
dotGetSchema,
|
|
970
|
+
fieldTypes,
|
|
974
971
|
getFieldType,
|
|
972
|
+
getSchemaFromAnyOrionForm,
|
|
973
|
+
getSchemaModelName,
|
|
974
|
+
getSchemaWithMetadataFromAnyOrionForm,
|
|
975
975
|
getValidationErrors,
|
|
976
|
+
isSchemaLike,
|
|
977
|
+
isSchemaOrFieldLike,
|
|
976
978
|
isValid,
|
|
979
|
+
schemaWithName,
|
|
977
980
|
validate,
|
|
978
981
|
validateKey
|
|
979
982
|
});
|