@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/dist/index.js CHANGED
@@ -1,16 +1,13 @@
1
- var __defProp = Object.defineProperty;
2
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
-
4
1
  // src/ValidationError.ts
5
2
  import isPlainObject from "lodash/isPlainObject";
6
- var getPrintableError = /* @__PURE__ */ __name((validationErrors) => {
3
+ var getPrintableError = (validationErrors) => {
7
4
  const printableErrors = Object.keys(validationErrors).map((key) => {
8
5
  return `${key}: ${validationErrors[key]}`;
9
6
  }).join(", ");
10
7
  const message = `Validation Error: {${printableErrors}}`;
11
8
  return message;
12
- }, "getPrintableError");
13
- var _ValidationError = class _ValidationError extends Error {
9
+ };
10
+ var ValidationError = class _ValidationError extends Error {
14
11
  code;
15
12
  isValidationError;
16
13
  isOrionError;
@@ -27,57 +24,31 @@ var _ValidationError = class _ValidationError extends Error {
27
24
  this.validationErrors = validationErrors;
28
25
  this.getInfo;
29
26
  }
30
- getInfo = /* @__PURE__ */ __name(() => {
27
+ getInfo = () => {
31
28
  return {
32
29
  error: "validationError",
33
30
  message: "Validation Error",
34
31
  validationErrors: this.validationErrors
35
32
  };
36
- }, "getInfo");
37
- prependKey = /* @__PURE__ */ __name((prepend) => {
33
+ };
34
+ prependKey = (prepend) => {
38
35
  const newErrors = {};
39
36
  const keys = Object.keys(this.validationErrors);
40
37
  for (const key of keys) {
41
38
  newErrors[`${prepend}.${key}`] = this.validationErrors[key];
42
39
  }
43
40
  return new _ValidationError(newErrors);
44
- }, "prependKey");
41
+ };
45
42
  };
46
- __name(_ValidationError, "ValidationError");
47
- var ValidationError = _ValidationError;
48
43
 
49
- // src/getValidationErrors/convertTypedModel.ts
50
- function isClass(obj) {
51
- const isCtorClass = obj.constructor && obj.constructor.toString().substring(0, 5) === "class";
52
- if (obj.prototype === void 0) {
53
- return isCtorClass;
54
- }
55
- const isPrototypeCtorClass = obj.prototype.constructor && obj.prototype.constructor.toString && obj.prototype.constructor.toString().substring(0, 5) === "class";
56
- return isCtorClass || isPrototypeCtorClass;
57
- }
58
- __name(isClass, "isClass");
59
- var convertOnParam = /* @__PURE__ */ __name((info, paramName) => {
60
- if (!info[paramName]) return;
61
- const type = info[paramName].type;
62
- if (!type) return;
63
- if (typeof type !== "function") return;
64
- if (!isClass(type)) return;
65
- if (!type.getModel || !type.__schemaId) return;
66
- info[paramName].type = type.getModel().getCleanSchema();
67
- }, "convertOnParam");
68
- var convertTypedModel = /* @__PURE__ */ __name((info) => {
69
- convertOnParam(info, "schema");
70
- convertOnParam(info, "currentSchema");
71
- }, "convertTypedModel");
72
-
73
- // src/getSchemaFromTypedModel.ts
74
- var getSchemaFromTypedModel = /* @__PURE__ */ __name((schema) => {
44
+ // src/getSchemaFromTypedSchema.ts
45
+ Symbol.metadata ?? (Symbol.metadata = Symbol("Symbol.metadata"));
46
+ var getSchemaFromTypedSchema = (schema) => {
75
47
  const item = schema;
76
- if (typeof item !== "function") return item;
77
- if (!isClass(item)) return item;
78
- if (!item.getModel || !item.__schemaId) return item;
79
- return item.getModel().getCleanSchema();
80
- }, "getSchemaFromTypedModel");
48
+ if (!schema[Symbol.metadata]) return item;
49
+ if (!schema[Symbol.metadata]._isTypedSchema) return item;
50
+ return schema[Symbol.metadata]._getModel().getSchema();
51
+ };
81
52
 
82
53
  // src/getValidationErrors/getError/index.ts
83
54
  import isNil2 from "lodash/isNil";
@@ -90,13 +61,13 @@ import isString5 from "lodash/isString";
90
61
  // src/fieldType.ts
91
62
  function fieldType(opts) {
92
63
  const { name, validate: validate2, clean: clean3, ...otherFields } = opts;
93
- const overwrittenValidate = /* @__PURE__ */ __name((value, info = {}) => {
64
+ const overwrittenValidate = (value, info = {}) => {
94
65
  if (!info.currentSchema) {
95
66
  info.currentSchema = {};
96
67
  }
97
68
  return validate2(value, info);
98
- }, "overwrittenValidate");
99
- const overwrittenClean = /* @__PURE__ */ __name((value, info = {}) => {
69
+ };
70
+ const overwrittenClean = (value, info = {}) => {
100
71
  if (!info.options) {
101
72
  info.options = {};
102
73
  }
@@ -104,16 +75,16 @@ function fieldType(opts) {
104
75
  return clean3(value, info);
105
76
  }
106
77
  return value;
107
- }, "overwrittenClean");
78
+ };
108
79
  return {
109
80
  ...otherFields,
110
81
  name,
111
82
  validate: overwrittenValidate,
112
83
  clean: overwrittenClean,
113
- _isFieldType: true
84
+ __isFieldType: true,
85
+ __tsFieldType: null
114
86
  };
115
87
  }
116
- __name(fieldType, "fieldType");
117
88
 
118
89
  // src/fieldTypes/array.ts
119
90
  import isArray from "lodash/isArray";
@@ -149,9 +120,7 @@ var array_default = fieldType({
149
120
  clean(value, { options }) {
150
121
  if (options.autoConvert) {
151
122
  if (!isArray(value)) {
152
- value = [
153
- value
154
- ];
123
+ value = [value];
155
124
  }
156
125
  }
157
126
  return value;
@@ -188,12 +157,12 @@ var string_default = fieldType({
188
157
  name: "string",
189
158
  validate(value, { currentSchema }) {
190
159
  if (!isString(value)) return Errors_default.NOT_A_STRING;
191
- if (isFinite(currentSchema.min)) {
160
+ if (Number.isFinite(currentSchema.min)) {
192
161
  if (value.length < currentSchema.min) {
193
162
  return Errors_default.STRING_TOO_SHORT;
194
163
  }
195
164
  }
196
- if (isFinite(currentSchema.max)) {
165
+ if (Number.isFinite(currentSchema.max)) {
197
166
  if (value.length > currentSchema.max) {
198
167
  return Errors_default.STRING_TOO_LONG;
199
168
  }
@@ -234,13 +203,13 @@ var date_default = fieldType({
234
203
  if (options.autoConvert) {
235
204
  if (isString2(value)) {
236
205
  const result = new Date(value);
237
- if (isNaN(result.getTime())) {
206
+ if (Number.isNaN(result.getTime())) {
238
207
  return value;
239
208
  }
240
209
  value = result;
241
210
  } else if (isNumber(value)) {
242
211
  const result = new Date(value);
243
- if (isNaN(result.getTime())) {
212
+ if (Number.isNaN(result.getTime())) {
244
213
  return value;
245
214
  }
246
215
  value = result;
@@ -254,18 +223,17 @@ var date_default = fieldType({
254
223
  import isInteger from "lodash/isInteger";
255
224
 
256
225
  // src/fieldTypes/number.ts
257
- import isFinite2 from "lodash/isFinite";
258
226
  import toNumber from "lodash/toNumber";
259
227
  var number_default = fieldType({
260
228
  name: "number",
261
229
  validate(value, { currentSchema }) {
262
- if (!isFinite2(value)) return Errors_default.NOT_A_NUMBER;
263
- if (isFinite2(currentSchema.min)) {
230
+ if (!Number.isFinite(value)) return Errors_default.NOT_A_NUMBER;
231
+ if (Number.isFinite(currentSchema.min)) {
264
232
  if (value < currentSchema.min) {
265
233
  return Errors_default.NUMBER_TOO_SMALL;
266
234
  }
267
235
  }
268
- if (isFinite2(currentSchema.max)) {
236
+ if (Number.isFinite(currentSchema.max)) {
269
237
  if (value > currentSchema.max) {
270
238
  return Errors_default.NUMBER_TOO_BIG;
271
239
  }
@@ -320,10 +288,11 @@ var boolean_default = fieldType({
320
288
  clean(value, { options }) {
321
289
  if (options.autoConvert) {
322
290
  if (typeof value === "string") {
323
- if (value === "true") {
291
+ const stringValue = value;
292
+ if (stringValue === "true") {
324
293
  value = true;
325
294
  }
326
- if (value === "false") {
295
+ if (stringValue === "false") {
327
296
  value = false;
328
297
  }
329
298
  }
@@ -372,7 +341,7 @@ var blackbox_default = fieldType({
372
341
  // src/fieldTypes/any.ts
373
342
  var any_default = fieldType({
374
343
  name: "any",
375
- validate(value) {
344
+ validate() {
376
345
  }
377
346
  });
378
347
 
@@ -393,19 +362,19 @@ var fieldTypes_default = {
393
362
 
394
363
  // src/getValidationErrors/getError/getFieldValidator.ts
395
364
  import has from "lodash/has";
396
- function getFieldValidator_default(type) {
365
+ function getFieldValidator(type) {
397
366
  if (isPlainObject4(type)) {
398
- if (type._isFieldType) return "custom";
367
+ if (type.__isFieldType) return "custom";
399
368
  return "plainObject";
400
369
  }
401
370
  if (isArray3(type)) return "array";
402
371
  if (type === String) return "string";
403
- if (type === Date) return "date";
372
+ if (typeof type === "function" && type.name === "Date") return "date";
404
373
  if (type === Number) return "number";
405
374
  if (type === Boolean) return "boolean";
406
375
  if (type === "enum") return "string";
407
376
  if (!isString5(type)) {
408
- throw new Error("Field type is invalid. Pass a string or a custom field type. Got " + type);
377
+ throw new Error(`Field type is invalid. Pass a string or a custom field type. Got ${type}`);
409
378
  }
410
379
  const exists = has(fieldTypes_default, type);
411
380
  if (!exists) {
@@ -413,25 +382,17 @@ function getFieldValidator_default(type) {
413
382
  }
414
383
  return type;
415
384
  }
416
- __name(getFieldValidator_default, "default");
417
385
 
418
386
  // src/getValidationErrors/getError/index.ts
419
387
  async function getValidationErrors(params) {
420
388
  const { schema, doc, currentDoc, value, currentSchema, keys, options = {}, args = [] } = params;
421
- const info = {
422
- schema,
423
- doc,
424
- currentDoc,
425
- keys,
426
- currentSchema,
427
- options
428
- };
389
+ const info = { schema, doc, currentDoc, keys, currentSchema, options };
429
390
  if (isNil2(value)) {
430
391
  if (!currentSchema.optional && !options.omitRequired) {
431
392
  return Errors_default.REQUIRED;
432
393
  }
433
394
  } else {
434
- const validatorKey = getFieldValidator_default(currentSchema.type);
395
+ const validatorKey = getFieldValidator(currentSchema.type);
435
396
  const validator = validatorKey === "custom" ? currentSchema.type : fieldTypes_default[validatorKey];
436
397
  const error = await validator.validate(value, info, ...args);
437
398
  if (error) {
@@ -454,7 +415,6 @@ async function getValidationErrors(params) {
454
415
  }
455
416
  return null;
456
417
  }
457
- __name(getValidationErrors, "getValidationErrors");
458
418
 
459
419
  // src/getValidationErrors/doValidation.ts
460
420
  import isPlainObject5 from "lodash/isPlainObject";
@@ -462,20 +422,24 @@ import isArray4 from "lodash/isArray";
462
422
  import clone from "lodash/clone";
463
423
  import isNil3 from "lodash/isNil";
464
424
  import difference2 from "lodash/difference";
425
+
426
+ // src/getValidationErrors/convertTypedSchema.ts
427
+ var convertOnParam = (info, paramName) => {
428
+ if (!info[paramName]) return;
429
+ const type = info[paramName].type;
430
+ if (!type) return;
431
+ info[paramName].type = getSchemaFromTypedSchema(type);
432
+ };
433
+ var convertTypedSchema = (info) => {
434
+ convertOnParam(info, "schema");
435
+ convertOnParam(info, "currentSchema");
436
+ };
437
+
438
+ // src/getValidationErrors/doValidation.ts
465
439
  async function doValidation(params) {
466
- convertTypedModel(params);
440
+ convertTypedSchema(params);
467
441
  const { schema, doc, currentDoc, value, currentSchema, keys = [], addError, options, args } = params;
468
- const info = {
469
- schema,
470
- doc,
471
- currentDoc,
472
- value,
473
- currentSchema,
474
- keys,
475
- options,
476
- args,
477
- addError
478
- };
442
+ const info = { schema, doc, currentDoc, value, currentSchema, keys, options, args, addError };
479
443
  const error = await getValidationErrors(info);
480
444
  if (error) {
481
445
  addError(keys, error);
@@ -485,7 +449,7 @@ async function doValidation(params) {
485
449
  if (isPlainObject5(currentSchema.type)) {
486
450
  const type = currentSchema.type;
487
451
  if (type) {
488
- if (type._isFieldType) {
452
+ if (type.__isFieldType) {
489
453
  return;
490
454
  }
491
455
  if (typeof type.__skipChildValidation === "function") {
@@ -525,15 +489,12 @@ async function doValidation(params) {
525
489
  ...info,
526
490
  currentDoc: value,
527
491
  value: itemValue,
528
- currentSchema: {
529
- type: itemSchema
530
- },
492
+ currentSchema: { type: itemSchema },
531
493
  keys: keyItemKeys
532
494
  });
533
495
  }
534
496
  }
535
497
  }
536
- __name(doValidation, "doValidation");
537
498
 
538
499
  // src/getValidationErrors/getValidationErrorsObject.ts
539
500
  import Dot from "dot-object";
@@ -546,40 +507,33 @@ function getValidationErrorsObject(validationErrors) {
546
507
  }
547
508
  return dot.dot(errors);
548
509
  }
549
- __name(getValidationErrorsObject, "getValidationErrorsObject");
550
510
 
551
511
  // src/getValidationErrors/index.ts
552
512
  var defaultOptions = {
553
513
  omitRequired: false
554
514
  };
555
515
  async function getValidationErrors2(schema, doc, passedOptions = {}, ...args) {
556
- schema = getSchemaFromTypedModel(schema);
557
- const options = {
558
- ...defaultOptions,
559
- ...passedOptions
560
- };
516
+ schema = getSchemaFromTypedSchema(schema);
517
+ const options = { ...defaultOptions, ...passedOptions };
561
518
  const errors = [];
562
- const addError = /* @__PURE__ */ __name(function(keys, code) {
519
+ const addError = (keys, code) => {
563
520
  errors.push({
564
521
  key: keys.join("."),
565
522
  code
566
523
  });
567
- }, "addError");
524
+ };
568
525
  await doValidation({
569
526
  schema,
570
527
  doc,
571
528
  currentDoc: doc,
572
529
  value: doc,
573
- currentSchema: {
574
- type: schema
575
- },
530
+ currentSchema: { type: schema },
576
531
  addError,
577
532
  options,
578
533
  args
579
534
  });
580
535
  return getValidationErrorsObject(errors);
581
536
  }
582
- __name(getValidationErrors2, "getValidationErrors");
583
537
 
584
538
  // src/validate.ts
585
539
  async function validate(schema, doc, passedOptions = {}, ...args) {
@@ -588,22 +542,19 @@ async function validate(schema, doc, passedOptions = {}, ...args) {
588
542
  throw new ValidationError(validationErrors);
589
543
  }
590
544
  }
591
- __name(validate, "validate");
592
545
 
593
546
  // src/isValid.ts
594
547
  async function isValid(schema, doc, passedOptions = {}, ...args) {
595
548
  const validationErrors = await getValidationErrors2(schema, doc, passedOptions, ...args);
596
549
  return !validationErrors;
597
550
  }
598
- __name(isValid, "isValid");
599
551
 
600
552
  // src/getValidationErrors/getError/getFieldType.ts
601
553
  function getFieldType(type) {
602
- const validatorKey = getFieldValidator_default(type);
554
+ const validatorKey = getFieldValidator(type);
603
555
  const validator = validatorKey === "custom" ? type : fieldTypes_default[validatorKey];
604
556
  return validator;
605
557
  }
606
- __name(getFieldType, "getFieldType");
607
558
 
608
559
  // src/clean/recursiveClean.ts
609
560
  import isUndefined from "lodash/isUndefined";
@@ -621,7 +572,6 @@ function getObjectNode(schema, value) {
621
572
  }
622
573
  return null;
623
574
  }
624
- __name(getObjectNode, "getObjectNode");
625
575
 
626
576
  // src/clean/cleanType.ts
627
577
  async function cleanType(type, fieldSchema, value, info, ...args) {
@@ -648,11 +598,6 @@ async function cleanType(type, fieldSchema, value, info, ...args) {
648
598
  value = defaultValue;
649
599
  }
650
600
  }
651
- const { autoValue } = fieldSchema;
652
- if (autoValue) {
653
- needReClean = true;
654
- value = await autoValue(value, info, ...args);
655
- }
656
601
  const { clean: clean3 } = fieldSchema;
657
602
  if (clean3) {
658
603
  needReClean = true;
@@ -663,11 +608,14 @@ async function cleanType(type, fieldSchema, value, info, ...args) {
663
608
  }
664
609
  return value;
665
610
  }
666
- __name(cleanType, "cleanType");
667
611
 
668
612
  // src/clean/recursiveClean.ts
669
613
  import isNil5 from "lodash/isNil";
670
- var cleanObjectFields = /* @__PURE__ */ __name(async function({ schema, value, ...other }) {
614
+ var cleanObjectFields = async ({
615
+ schema,
616
+ value,
617
+ ...other
618
+ }) => {
671
619
  const keys = Object.keys(schema.type).filter((key) => !key.startsWith("__"));
672
620
  const newDoc = {};
673
621
  for (const key of keys) {
@@ -687,10 +635,14 @@ var cleanObjectFields = /* @__PURE__ */ __name(async function({ schema, value, .
687
635
  }
688
636
  }
689
637
  return newDoc;
690
- }, "cleanObjectFields");
691
- var cleanArrayItems = /* @__PURE__ */ __name(async function({ schema, value, ...other }) {
638
+ };
639
+ var cleanArrayItems = async ({
640
+ schema,
641
+ value,
642
+ ...other
643
+ }) => {
692
644
  const schemaType = schema.type[0];
693
- const promises = value.map(async (item, index) => {
645
+ const promises = value.map(async (item) => {
694
646
  const newValue = await clean({
695
647
  ...other,
696
648
  schema: {
@@ -703,7 +655,7 @@ var cleanArrayItems = /* @__PURE__ */ __name(async function({ schema, value, ...
703
655
  });
704
656
  const result = await Promise.all(promises);
705
657
  return result.filter((value2) => !isUndefined(value2));
706
- }, "cleanArrayItems");
658
+ };
707
659
  function getArrayNode(schema, value) {
708
660
  if (isArray5(schema.type) && !isNil5(value)) {
709
661
  const result = schema;
@@ -711,13 +663,10 @@ function getArrayNode(schema, value) {
711
663
  }
712
664
  return null;
713
665
  }
714
- __name(getArrayNode, "getArrayNode");
715
- var clean = /* @__PURE__ */ __name(async function(info) {
716
- convertTypedModel(info);
717
- let { schema, args = [], value } = info;
718
- const currSchema = schema.type === void 0 ? {
719
- type: schema
720
- } : schema;
666
+ var clean = async (info) => {
667
+ convertTypedSchema(info);
668
+ const { schema, args = [], value } = info;
669
+ const currSchema = schema.type === void 0 ? { type: schema } : schema;
721
670
  const objectSchema = getObjectNode(currSchema, value);
722
671
  if (objectSchema) {
723
672
  const newDoc = await cleanObjectFields({
@@ -732,9 +681,7 @@ var clean = /* @__PURE__ */ __name(async function(info) {
732
681
  if (arraySchema) {
733
682
  let updatedValue = value;
734
683
  if (!isArray5(value) && !Array.isArray(value)) {
735
- updatedValue = [
736
- value
737
- ];
684
+ updatedValue = [value];
738
685
  }
739
686
  const newDoc = await cleanArrayItems({
740
687
  ...info,
@@ -746,7 +693,7 @@ var clean = /* @__PURE__ */ __name(async function(info) {
746
693
  }
747
694
  const result = await cleanType(currSchema.type, currSchema, value, info, ...args);
748
695
  return result;
749
- }, "clean");
696
+ };
750
697
  var recursiveClean_default = clean;
751
698
 
752
699
  // src/clean/index.ts
@@ -758,15 +705,10 @@ var defaultOptions2 = {
758
705
  };
759
706
  async function clean2(schema, doc, opts = {}, ...args) {
760
707
  if (!doc) return doc;
761
- schema = getSchemaFromTypedModel(schema);
762
- const options = {
763
- ...defaultOptions2,
764
- ...opts
765
- };
708
+ schema = getSchemaFromTypedSchema(schema);
709
+ const options = { ...defaultOptions2, ...opts };
766
710
  const params = {
767
- schema: {
768
- type: schema
769
- },
711
+ schema: { type: schema },
770
712
  value: doc,
771
713
  doc: options.forceDoc || doc,
772
714
  currentDoc: doc,
@@ -776,42 +718,33 @@ async function clean2(schema, doc, opts = {}, ...args) {
776
718
  const cleanedResult = await recursiveClean_default(params);
777
719
  return cleanedResult;
778
720
  }
779
- __name(clean2, "clean");
780
721
 
781
722
  // src/validateKey/dotGetSchema.ts
782
723
  import isPlainObject7 from "lodash/isPlainObject";
783
724
  import isNil6 from "lodash/isNil";
784
- var dotGet = /* @__PURE__ */ __name(function dotGet2(object, path) {
725
+ var dotGet = function dotGet2(object, path) {
785
726
  if (path === "") return object;
786
727
  const pathParts = path.split(".");
787
728
  const first = pathParts.shift();
788
729
  const remainingPath = pathParts.join(".");
789
730
  const levelObject = object.type;
790
731
  if (first === "$" || /^[0-9]+$/.test(first)) {
791
- return dotGet2({
792
- type: levelObject[0]
793
- }, remainingPath);
794
- } else if (isPlainObject7(levelObject[first])) {
732
+ return dotGet2({ type: levelObject[0] }, remainingPath);
733
+ }
734
+ if (isPlainObject7(levelObject[first])) {
795
735
  return dotGet2(levelObject[first], remainingPath);
796
736
  }
797
737
  if (levelObject === "blackbox") {
798
- return {
799
- type: "blackbox",
800
- optional: true,
801
- isBlackboxChild: true
802
- };
738
+ return { type: "blackbox", optional: true, isBlackboxChild: true };
803
739
  }
804
740
  return null;
805
- }, "dotGet");
741
+ };
806
742
  function dotGetSchema_default(schema, path) {
807
743
  if (isNil6(schema)) {
808
744
  throw new Error("You need to pass a schema");
809
745
  }
810
- return dotGet({
811
- type: schema
812
- }, path);
746
+ return dotGet({ type: schema }, path);
813
747
  }
814
- __name(dotGetSchema_default, "default");
815
748
 
816
749
  // src/dotGetSchema.ts
817
750
  var dotGetSchema_default2 = dotGetSchema_default;
@@ -821,10 +754,7 @@ var defaultOptions3 = {
821
754
  filter: true
822
755
  };
823
756
  async function cleanKey_default(schema, key, value, passedOptions = {}, ...args) {
824
- const options = {
825
- ...defaultOptions3,
826
- ...passedOptions
827
- };
757
+ const options = { ...defaultOptions3, ...passedOptions };
828
758
  const keySchema = dotGetSchema_default2(schema, key);
829
759
  if (!keySchema) {
830
760
  if (options.filter) {
@@ -833,44 +763,35 @@ async function cleanKey_default(schema, key, value, passedOptions = {}, ...args)
833
763
  return value;
834
764
  }
835
765
  }
836
- const result = await clean2({
837
- clean: keySchema
838
- }, {
839
- clean: value
840
- }, options, ...args);
766
+ const result = await clean2({ clean: keySchema }, { clean: value }, options, ...args);
841
767
  return result.clean;
842
768
  }
843
- __name(cleanKey_default, "default");
844
769
 
845
770
  // src/validateKey/index.ts
846
771
  var defaultOptions4 = {
847
772
  filter: false
848
773
  };
849
774
  async function validateKey_default(schema, key, value, passedOptions = {}, ...args) {
850
- const options = {
851
- ...defaultOptions4,
852
- ...passedOptions
853
- };
775
+ const options = { ...defaultOptions4, ...passedOptions };
854
776
  const keySchema = dotGetSchema_default(schema, key);
855
777
  if (!keySchema) {
856
778
  if (options.filter) {
857
779
  return Errors_default.NOT_IN_SCHEMA;
858
- } else {
859
- return null;
860
780
  }
781
+ return null;
861
782
  }
862
783
  if (keySchema.isBlackboxChild) {
863
784
  return null;
864
785
  }
865
- const result = await getValidationErrors2({
866
- validate: keySchema
867
- }, {
868
- validate: value
869
- }, options, ...args);
786
+ const result = await getValidationErrors2(
787
+ { validate: keySchema },
788
+ { validate: value },
789
+ options,
790
+ ...args
791
+ );
870
792
  if (!result) return null;
871
793
  return result.validate;
872
794
  }
873
- __name(validateKey_default, "default");
874
795
 
875
796
  // src/fieldTypes/enum.ts
876
797
  import isString6 from "lodash/isString";
@@ -884,19 +805,17 @@ function createEnum(name, values) {
884
805
  enumName: name,
885
806
  enumValues: values
886
807
  },
887
- toGraphQLType: /* @__PURE__ */ __name((GraphQL) => {
808
+ toGraphQLType: (GraphQL) => {
888
809
  global.GraphQLEnums = global.GraphQLEnums || {};
889
810
  global.GraphQLEnums[name] = global.GraphQLEnums[name] || new GraphQL.GraphQLEnumType({
890
811
  name,
891
812
  values: values.reduce((result, value) => {
892
- result[value] = {
893
- value
894
- };
813
+ result[value] = { value };
895
814
  return result;
896
815
  }, {})
897
816
  });
898
817
  return global.GraphQLEnums[name];
899
- }, "toGraphQLType"),
818
+ },
900
819
  validate(value, { currentSchema }) {
901
820
  if (!isString6(value)) return Errors_default.NOT_A_STRING;
902
821
  if (!includes2(values, value)) {
@@ -921,16 +840,91 @@ function createEnum(name, values) {
921
840
  })
922
841
  };
923
842
  }
924
- __name(createEnum, "createEnum");
843
+
844
+ // src/models.ts
845
+ Symbol.metadata ?? (Symbol.metadata = Symbol("Symbol.metadata"));
846
+ function isSchemaLike(type) {
847
+ var _a;
848
+ if (!type) return false;
849
+ if (objectHasSubObjectWithKey(type, "type")) return true;
850
+ if ((_a = type == null ? void 0 : type[Symbol.metadata]) == null ? void 0 : _a._getModel) return true;
851
+ if (type.getModel) return true;
852
+ if (type.getSchema) return true;
853
+ if (type.getCleanSchema) return true;
854
+ if (type.__isModel) return true;
855
+ if (type.__modelName) return true;
856
+ return false;
857
+ }
858
+ function isSchemaOrFieldLike(type) {
859
+ if (Array.isArray(type)) {
860
+ if (type.length !== 1) return false;
861
+ return isSchemaOrFieldLike(type[0]);
862
+ }
863
+ if (isSchemaLike(type)) return true;
864
+ try {
865
+ if (getFieldValidator(type)) return true;
866
+ } catch {
867
+ return false;
868
+ }
869
+ return false;
870
+ }
871
+ function getSchemaModelName(type) {
872
+ if (!type) return null;
873
+ if (type.__modelName) return type.__modelName;
874
+ if (type.getModel) return type.getModel().name;
875
+ if (type.getSchema) return type.getSchema().__modelName;
876
+ return null;
877
+ }
878
+ function getSchemaFromAnyOrionForm(type) {
879
+ var _a, _b;
880
+ if ((_a = type == null ? void 0 : type[Symbol.metadata]) == null ? void 0 : _a._getModel) {
881
+ return (_b = type == null ? void 0 : type[Symbol.metadata]) == null ? void 0 : _b._getModel().getSchema();
882
+ }
883
+ if (type == null ? void 0 : type.getModel) return type.getModel().getSchema();
884
+ if (type.getSchema) {
885
+ return type.getSchema();
886
+ }
887
+ if (type.getSchema) {
888
+ return type.getSchema();
889
+ }
890
+ if (objectHasSubObjectWithKey(type, "type")) return type;
891
+ return null;
892
+ }
893
+ function objectHasSubObjectWithKey(object, key) {
894
+ if (!object || typeof object !== "object") return false;
895
+ for (const key1 in object) {
896
+ const value = object[key1];
897
+ if (value && typeof value === "object" && key in value) {
898
+ return true;
899
+ }
900
+ }
901
+ return false;
902
+ }
903
+ function getSchemaWithMetadataFromAnyOrionForm(type) {
904
+ return getSchemaFromAnyOrionForm(type);
905
+ }
906
+
907
+ // src/schemaWithName/index.ts
908
+ function schemaWithName(name, schema) {
909
+ schema.__modelName = name;
910
+ return schema;
911
+ }
925
912
  export {
926
913
  ValidationError,
927
914
  clean2 as clean,
928
915
  cleanKey_default as cleanKey,
929
916
  createEnum,
930
917
  dotGetSchema_default2 as dotGetSchema,
918
+ fieldTypes_default as fieldTypes,
931
919
  getFieldType,
920
+ getSchemaFromAnyOrionForm,
921
+ getSchemaModelName,
922
+ getSchemaWithMetadataFromAnyOrionForm,
932
923
  getValidationErrors2 as getValidationErrors,
924
+ isSchemaLike,
925
+ isSchemaOrFieldLike,
933
926
  isValid,
927
+ schemaWithName,
934
928
  validate,
935
929
  validateKey_default as validateKey
936
930
  };