@orion-js/schema 4.0.0-next.2 → 4.0.0-next.3

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Orionjs Team
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
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 });
@@ -45,14 +44,14 @@ module.exports = __toCommonJS(index_exports);
45
44
 
46
45
  // src/ValidationError.ts
47
46
  var import_isPlainObject = __toESM(require("lodash/isPlainObject"), 1);
48
- var getPrintableError = /* @__PURE__ */ __name((validationErrors) => {
47
+ var getPrintableError = (validationErrors) => {
49
48
  const printableErrors = Object.keys(validationErrors).map((key) => {
50
49
  return `${key}: ${validationErrors[key]}`;
51
50
  }).join(", ");
52
51
  const message = `Validation Error: {${printableErrors}}`;
53
52
  return message;
54
- }, "getPrintableError");
55
- var _ValidationError = class _ValidationError extends Error {
53
+ };
54
+ var ValidationError = class _ValidationError extends Error {
56
55
  code;
57
56
  isValidationError;
58
57
  isOrionError;
@@ -69,24 +68,22 @@ var _ValidationError = class _ValidationError extends Error {
69
68
  this.validationErrors = validationErrors;
70
69
  this.getInfo;
71
70
  }
72
- getInfo = /* @__PURE__ */ __name(() => {
71
+ getInfo = () => {
73
72
  return {
74
73
  error: "validationError",
75
74
  message: "Validation Error",
76
75
  validationErrors: this.validationErrors
77
76
  };
78
- }, "getInfo");
79
- prependKey = /* @__PURE__ */ __name((prepend) => {
77
+ };
78
+ prependKey = (prepend) => {
80
79
  const newErrors = {};
81
80
  const keys = Object.keys(this.validationErrors);
82
81
  for (const key of keys) {
83
82
  newErrors[`${prepend}.${key}`] = this.validationErrors[key];
84
83
  }
85
84
  return new _ValidationError(newErrors);
86
- }, "prependKey");
85
+ };
87
86
  };
88
- __name(_ValidationError, "ValidationError");
89
- var ValidationError = _ValidationError;
90
87
 
91
88
  // src/getValidationErrors/convertTypedModel.ts
92
89
  function isClass(obj) {
@@ -97,8 +94,7 @@ function isClass(obj) {
97
94
  const isPrototypeCtorClass = obj.prototype.constructor && obj.prototype.constructor.toString && obj.prototype.constructor.toString().substring(0, 5) === "class";
98
95
  return isCtorClass || isPrototypeCtorClass;
99
96
  }
100
- __name(isClass, "isClass");
101
- var convertOnParam = /* @__PURE__ */ __name((info, paramName) => {
97
+ var convertOnParam = (info, paramName) => {
102
98
  if (!info[paramName]) return;
103
99
  const type = info[paramName].type;
104
100
  if (!type) return;
@@ -106,20 +102,20 @@ var convertOnParam = /* @__PURE__ */ __name((info, paramName) => {
106
102
  if (!isClass(type)) return;
107
103
  if (!type.getModel || !type.__schemaId) return;
108
104
  info[paramName].type = type.getModel().getCleanSchema();
109
- }, "convertOnParam");
110
- var convertTypedModel = /* @__PURE__ */ __name((info) => {
105
+ };
106
+ var convertTypedModel = (info) => {
111
107
  convertOnParam(info, "schema");
112
108
  convertOnParam(info, "currentSchema");
113
- }, "convertTypedModel");
109
+ };
114
110
 
115
111
  // src/getSchemaFromTypedModel.ts
116
- var getSchemaFromTypedModel = /* @__PURE__ */ __name((schema) => {
112
+ var getSchemaFromTypedModel = (schema) => {
117
113
  const item = schema;
118
114
  if (typeof item !== "function") return item;
119
115
  if (!isClass(item)) return item;
120
116
  if (!item.getModel || !item.__schemaId) return item;
121
117
  return item.getModel().getCleanSchema();
122
- }, "getSchemaFromTypedModel");
118
+ };
123
119
 
124
120
  // src/getValidationErrors/getError/index.ts
125
121
  var import_isNil2 = __toESM(require("lodash/isNil"), 1);
@@ -132,13 +128,13 @@ var import_isString5 = __toESM(require("lodash/isString"), 1);
132
128
  // src/fieldType.ts
133
129
  function fieldType(opts) {
134
130
  const { name, validate: validate2, clean: clean3, ...otherFields } = opts;
135
- const overwrittenValidate = /* @__PURE__ */ __name((value, info = {}) => {
131
+ const overwrittenValidate = (value, info = {}) => {
136
132
  if (!info.currentSchema) {
137
133
  info.currentSchema = {};
138
134
  }
139
135
  return validate2(value, info);
140
- }, "overwrittenValidate");
141
- const overwrittenClean = /* @__PURE__ */ __name((value, info = {}) => {
136
+ };
137
+ const overwrittenClean = (value, info = {}) => {
142
138
  if (!info.options) {
143
139
  info.options = {};
144
140
  }
@@ -146,7 +142,7 @@ function fieldType(opts) {
146
142
  return clean3(value, info);
147
143
  }
148
144
  return value;
149
- }, "overwrittenClean");
145
+ };
150
146
  return {
151
147
  ...otherFields,
152
148
  name,
@@ -155,7 +151,6 @@ function fieldType(opts) {
155
151
  _isFieldType: true
156
152
  };
157
153
  }
158
- __name(fieldType, "fieldType");
159
154
 
160
155
  // src/fieldTypes/array.ts
161
156
  var import_isArray = __toESM(require("lodash/isArray"), 1);
@@ -191,9 +186,7 @@ var array_default = fieldType({
191
186
  clean(value, { options }) {
192
187
  if (options.autoConvert) {
193
188
  if (!(0, import_isArray.default)(value)) {
194
- value = [
195
- value
196
- ];
189
+ value = [value];
197
190
  }
198
191
  }
199
192
  return value;
@@ -455,19 +448,11 @@ function getFieldValidator_default(type) {
455
448
  }
456
449
  return type;
457
450
  }
458
- __name(getFieldValidator_default, "default");
459
451
 
460
452
  // src/getValidationErrors/getError/index.ts
461
453
  async function getValidationErrors(params) {
462
454
  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
- };
455
+ const info = { schema, doc, currentDoc, keys, currentSchema, options };
471
456
  if ((0, import_isNil2.default)(value)) {
472
457
  if (!currentSchema.optional && !options.omitRequired) {
473
458
  return Errors_default.REQUIRED;
@@ -496,7 +481,6 @@ async function getValidationErrors(params) {
496
481
  }
497
482
  return null;
498
483
  }
499
- __name(getValidationErrors, "getValidationErrors");
500
484
 
501
485
  // src/getValidationErrors/doValidation.ts
502
486
  var import_isPlainObject5 = __toESM(require("lodash/isPlainObject"), 1);
@@ -507,17 +491,7 @@ var import_difference2 = __toESM(require("lodash/difference"), 1);
507
491
  async function doValidation(params) {
508
492
  convertTypedModel(params);
509
493
  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
- };
494
+ const info = { schema, doc, currentDoc, value, currentSchema, keys, options, args, addError };
521
495
  const error = await getValidationErrors(info);
522
496
  if (error) {
523
497
  addError(keys, error);
@@ -567,15 +541,12 @@ async function doValidation(params) {
567
541
  ...info,
568
542
  currentDoc: value,
569
543
  value: itemValue,
570
- currentSchema: {
571
- type: itemSchema
572
- },
544
+ currentSchema: { type: itemSchema },
573
545
  keys: keyItemKeys
574
546
  });
575
547
  }
576
548
  }
577
549
  }
578
- __name(doValidation, "doValidation");
579
550
 
580
551
  // src/getValidationErrors/getValidationErrorsObject.ts
581
552
  var import_dot_object = __toESM(require("dot-object"), 1);
@@ -588,7 +559,6 @@ function getValidationErrorsObject(validationErrors) {
588
559
  }
589
560
  return dot.dot(errors);
590
561
  }
591
- __name(getValidationErrorsObject, "getValidationErrorsObject");
592
562
 
593
563
  // src/getValidationErrors/index.ts
594
564
  var defaultOptions = {
@@ -596,32 +566,26 @@ var defaultOptions = {
596
566
  };
597
567
  async function getValidationErrors2(schema, doc, passedOptions = {}, ...args) {
598
568
  schema = getSchemaFromTypedModel(schema);
599
- const options = {
600
- ...defaultOptions,
601
- ...passedOptions
602
- };
569
+ const options = { ...defaultOptions, ...passedOptions };
603
570
  const errors = [];
604
- const addError = /* @__PURE__ */ __name(function(keys, code) {
571
+ const addError = function(keys, code) {
605
572
  errors.push({
606
573
  key: keys.join("."),
607
574
  code
608
575
  });
609
- }, "addError");
576
+ };
610
577
  await doValidation({
611
578
  schema,
612
579
  doc,
613
580
  currentDoc: doc,
614
581
  value: doc,
615
- currentSchema: {
616
- type: schema
617
- },
582
+ currentSchema: { type: schema },
618
583
  addError,
619
584
  options,
620
585
  args
621
586
  });
622
587
  return getValidationErrorsObject(errors);
623
588
  }
624
- __name(getValidationErrors2, "getValidationErrors");
625
589
 
626
590
  // src/validate.ts
627
591
  async function validate(schema, doc, passedOptions = {}, ...args) {
@@ -630,14 +594,12 @@ async function validate(schema, doc, passedOptions = {}, ...args) {
630
594
  throw new ValidationError(validationErrors);
631
595
  }
632
596
  }
633
- __name(validate, "validate");
634
597
 
635
598
  // src/isValid.ts
636
599
  async function isValid(schema, doc, passedOptions = {}, ...args) {
637
600
  const validationErrors = await getValidationErrors2(schema, doc, passedOptions, ...args);
638
601
  return !validationErrors;
639
602
  }
640
- __name(isValid, "isValid");
641
603
 
642
604
  // src/getValidationErrors/getError/getFieldType.ts
643
605
  function getFieldType(type) {
@@ -645,7 +607,6 @@ function getFieldType(type) {
645
607
  const validator = validatorKey === "custom" ? type : fieldTypes_default[validatorKey];
646
608
  return validator;
647
609
  }
648
- __name(getFieldType, "getFieldType");
649
610
 
650
611
  // src/clean/recursiveClean.ts
651
612
  var import_isUndefined = __toESM(require("lodash/isUndefined"), 1);
@@ -663,7 +624,6 @@ function getObjectNode(schema, value) {
663
624
  }
664
625
  return null;
665
626
  }
666
- __name(getObjectNode, "getObjectNode");
667
627
 
668
628
  // src/clean/cleanType.ts
669
629
  async function cleanType(type, fieldSchema, value, info, ...args) {
@@ -705,11 +665,14 @@ async function cleanType(type, fieldSchema, value, info, ...args) {
705
665
  }
706
666
  return value;
707
667
  }
708
- __name(cleanType, "cleanType");
709
668
 
710
669
  // src/clean/recursiveClean.ts
711
670
  var import_isNil5 = __toESM(require("lodash/isNil"), 1);
712
- var cleanObjectFields = /* @__PURE__ */ __name(async function({ schema, value, ...other }) {
671
+ var cleanObjectFields = async function({
672
+ schema,
673
+ value,
674
+ ...other
675
+ }) {
713
676
  const keys = Object.keys(schema.type).filter((key) => !key.startsWith("__"));
714
677
  const newDoc = {};
715
678
  for (const key of keys) {
@@ -729,8 +692,12 @@ var cleanObjectFields = /* @__PURE__ */ __name(async function({ schema, value, .
729
692
  }
730
693
  }
731
694
  return newDoc;
732
- }, "cleanObjectFields");
733
- var cleanArrayItems = /* @__PURE__ */ __name(async function({ schema, value, ...other }) {
695
+ };
696
+ var cleanArrayItems = async function({
697
+ schema,
698
+ value,
699
+ ...other
700
+ }) {
734
701
  const schemaType = schema.type[0];
735
702
  const promises = value.map(async (item, index) => {
736
703
  const newValue = await clean({
@@ -745,7 +712,7 @@ var cleanArrayItems = /* @__PURE__ */ __name(async function({ schema, value, ...
745
712
  });
746
713
  const result = await Promise.all(promises);
747
714
  return result.filter((value2) => !(0, import_isUndefined.default)(value2));
748
- }, "cleanArrayItems");
715
+ };
749
716
  function getArrayNode(schema, value) {
750
717
  if ((0, import_isArray5.default)(schema.type) && !(0, import_isNil5.default)(value)) {
751
718
  const result = schema;
@@ -753,13 +720,10 @@ function getArrayNode(schema, value) {
753
720
  }
754
721
  return null;
755
722
  }
756
- __name(getArrayNode, "getArrayNode");
757
- var clean = /* @__PURE__ */ __name(async function(info) {
723
+ var clean = async function(info) {
758
724
  convertTypedModel(info);
759
725
  let { schema, args = [], value } = info;
760
- const currSchema = schema.type === void 0 ? {
761
- type: schema
762
- } : schema;
726
+ const currSchema = schema.type === void 0 ? { type: schema } : schema;
763
727
  const objectSchema = getObjectNode(currSchema, value);
764
728
  if (objectSchema) {
765
729
  const newDoc = await cleanObjectFields({
@@ -774,9 +738,7 @@ var clean = /* @__PURE__ */ __name(async function(info) {
774
738
  if (arraySchema) {
775
739
  let updatedValue = value;
776
740
  if (!(0, import_isArray5.default)(value) && !Array.isArray(value)) {
777
- updatedValue = [
778
- value
779
- ];
741
+ updatedValue = [value];
780
742
  }
781
743
  const newDoc = await cleanArrayItems({
782
744
  ...info,
@@ -788,7 +750,7 @@ var clean = /* @__PURE__ */ __name(async function(info) {
788
750
  }
789
751
  const result = await cleanType(currSchema.type, currSchema, value, info, ...args);
790
752
  return result;
791
- }, "clean");
753
+ };
792
754
  var recursiveClean_default = clean;
793
755
 
794
756
  // src/clean/index.ts
@@ -801,14 +763,9 @@ var defaultOptions2 = {
801
763
  async function clean2(schema, doc, opts = {}, ...args) {
802
764
  if (!doc) return doc;
803
765
  schema = getSchemaFromTypedModel(schema);
804
- const options = {
805
- ...defaultOptions2,
806
- ...opts
807
- };
766
+ const options = { ...defaultOptions2, ...opts };
808
767
  const params = {
809
- schema: {
810
- type: schema
811
- },
768
+ schema: { type: schema },
812
769
  value: doc,
813
770
  doc: options.forceDoc || doc,
814
771
  currentDoc: doc,
@@ -818,42 +775,32 @@ async function clean2(schema, doc, opts = {}, ...args) {
818
775
  const cleanedResult = await recursiveClean_default(params);
819
776
  return cleanedResult;
820
777
  }
821
- __name(clean2, "clean");
822
778
 
823
779
  // src/validateKey/dotGetSchema.ts
824
780
  var import_isPlainObject7 = __toESM(require("lodash/isPlainObject"), 1);
825
781
  var import_isNil6 = __toESM(require("lodash/isNil"), 1);
826
- var dotGet = /* @__PURE__ */ __name(function dotGet2(object, path) {
782
+ var dotGet = function dotGet2(object, path) {
827
783
  if (path === "") return object;
828
784
  const pathParts = path.split(".");
829
785
  const first = pathParts.shift();
830
786
  const remainingPath = pathParts.join(".");
831
787
  const levelObject = object.type;
832
788
  if (first === "$" || /^[0-9]+$/.test(first)) {
833
- return dotGet2({
834
- type: levelObject[0]
835
- }, remainingPath);
789
+ return dotGet2({ type: levelObject[0] }, remainingPath);
836
790
  } else if ((0, import_isPlainObject7.default)(levelObject[first])) {
837
791
  return dotGet2(levelObject[first], remainingPath);
838
792
  }
839
793
  if (levelObject === "blackbox") {
840
- return {
841
- type: "blackbox",
842
- optional: true,
843
- isBlackboxChild: true
844
- };
794
+ return { type: "blackbox", optional: true, isBlackboxChild: true };
845
795
  }
846
796
  return null;
847
- }, "dotGet");
797
+ };
848
798
  function dotGetSchema_default(schema, path) {
849
799
  if ((0, import_isNil6.default)(schema)) {
850
800
  throw new Error("You need to pass a schema");
851
801
  }
852
- return dotGet({
853
- type: schema
854
- }, path);
802
+ return dotGet({ type: schema }, path);
855
803
  }
856
- __name(dotGetSchema_default, "default");
857
804
 
858
805
  // src/dotGetSchema.ts
859
806
  var dotGetSchema_default2 = dotGetSchema_default;
@@ -863,10 +810,7 @@ var defaultOptions3 = {
863
810
  filter: true
864
811
  };
865
812
  async function cleanKey_default(schema, key, value, passedOptions = {}, ...args) {
866
- const options = {
867
- ...defaultOptions3,
868
- ...passedOptions
869
- };
813
+ const options = { ...defaultOptions3, ...passedOptions };
870
814
  const keySchema = dotGetSchema_default2(schema, key);
871
815
  if (!keySchema) {
872
816
  if (options.filter) {
@@ -875,24 +819,16 @@ async function cleanKey_default(schema, key, value, passedOptions = {}, ...args)
875
819
  return value;
876
820
  }
877
821
  }
878
- const result = await clean2({
879
- clean: keySchema
880
- }, {
881
- clean: value
882
- }, options, ...args);
822
+ const result = await clean2({ clean: keySchema }, { clean: value }, options, ...args);
883
823
  return result.clean;
884
824
  }
885
- __name(cleanKey_default, "default");
886
825
 
887
826
  // src/validateKey/index.ts
888
827
  var defaultOptions4 = {
889
828
  filter: false
890
829
  };
891
830
  async function validateKey_default(schema, key, value, passedOptions = {}, ...args) {
892
- const options = {
893
- ...defaultOptions4,
894
- ...passedOptions
895
- };
831
+ const options = { ...defaultOptions4, ...passedOptions };
896
832
  const keySchema = dotGetSchema_default(schema, key);
897
833
  if (!keySchema) {
898
834
  if (options.filter) {
@@ -904,15 +840,15 @@ async function validateKey_default(schema, key, value, passedOptions = {}, ...ar
904
840
  if (keySchema.isBlackboxChild) {
905
841
  return null;
906
842
  }
907
- const result = await getValidationErrors2({
908
- validate: keySchema
909
- }, {
910
- validate: value
911
- }, options, ...args);
843
+ const result = await getValidationErrors2(
844
+ { validate: keySchema },
845
+ { validate: value },
846
+ options,
847
+ ...args
848
+ );
912
849
  if (!result) return null;
913
850
  return result.validate;
914
851
  }
915
- __name(validateKey_default, "default");
916
852
 
917
853
  // src/fieldTypes/enum.ts
918
854
  var import_isString6 = __toESM(require("lodash/isString"), 1);
@@ -926,19 +862,17 @@ function createEnum(name, values) {
926
862
  enumName: name,
927
863
  enumValues: values
928
864
  },
929
- toGraphQLType: /* @__PURE__ */ __name((GraphQL) => {
865
+ toGraphQLType: (GraphQL) => {
930
866
  global.GraphQLEnums = global.GraphQLEnums || {};
931
867
  global.GraphQLEnums[name] = global.GraphQLEnums[name] || new GraphQL.GraphQLEnumType({
932
868
  name,
933
869
  values: values.reduce((result, value) => {
934
- result[value] = {
935
- value
936
- };
870
+ result[value] = { value };
937
871
  return result;
938
872
  }, {})
939
873
  });
940
874
  return global.GraphQLEnums[name];
941
- }, "toGraphQLType"),
875
+ },
942
876
  validate(value, { currentSchema }) {
943
877
  if (!(0, import_isString6.default)(value)) return Errors_default.NOT_A_STRING;
944
878
  if (!(0, import_includes2.default)(values, value)) {
@@ -963,7 +897,6 @@ function createEnum(name, values) {
963
897
  })
964
898
  };
965
899
  }
966
- __name(createEnum, "createEnum");
967
900
  // Annotate the CommonJS export names for ESM import in node:
968
901
  0 && (module.exports = {
969
902
  ValidationError,