@orion-js/schema 4.0.0 → 4.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -33,6 +33,7 @@ __export(index_exports, {
33
33
  clean: () => clean2,
34
34
  cleanAndValidate: () => cleanAndValidate,
35
35
  cleanKey: () => cleanKey_default,
36
+ cloneSchema: () => cloneSchema,
36
37
  createEnum: () => createEnum,
37
38
  dotGetSchema: () => dotGetSchema_default2,
38
39
  fieldTypes: () => fieldTypes_default,
@@ -67,6 +68,35 @@ function type(input) {
67
68
  // ../../node_modules/.pnpm/rambdax@11.3.1/node_modules/rambdax/src/_internals/isArray.js
68
69
  var { isArray } = Array;
69
70
 
71
+ // ../../node_modules/.pnpm/rambdax@11.3.1/node_modules/rambdax/src/_internals/isInteger.js
72
+ function _isInteger(n) {
73
+ return n << 0 === n;
74
+ }
75
+ var isInteger = Number.isInteger || _isInteger;
76
+
77
+ // ../../node_modules/.pnpm/rambdax@11.3.1/node_modules/rambdax/src/_internals/createPath.js
78
+ function createPath(path, delimiter = ".") {
79
+ return typeof path === "string" ? path.split(delimiter).map((x) => isInteger(x) ? Number(x) : x) : path;
80
+ }
81
+
82
+ // ../../node_modules/.pnpm/rambdax@11.3.1/node_modules/rambdax/src/pick.js
83
+ function pick(propsToPick, input) {
84
+ if (arguments.length === 1) return (_input) => pick(propsToPick, _input);
85
+ if (input === null || input === void 0) {
86
+ return void 0;
87
+ }
88
+ const keys = createPath(propsToPick, ",");
89
+ const willReturn = {};
90
+ let counter = 0;
91
+ while (counter < keys.length) {
92
+ if (keys[counter] in input) {
93
+ willReturn[keys[counter]] = input[keys[counter]];
94
+ }
95
+ counter++;
96
+ }
97
+ return willReturn;
98
+ }
99
+
70
100
  // ../../node_modules/.pnpm/rambdax@11.3.1/node_modules/rambdax/src/isType.js
71
101
  function isType(xType, x) {
72
102
  if (arguments.length === 1) {
@@ -86,6 +116,21 @@ function clone(input) {
86
116
  return out;
87
117
  }
88
118
 
119
+ // ../../node_modules/.pnpm/rambdax@11.3.1/node_modules/rambdax/src/isEmpty.js
120
+ function isEmpty(input) {
121
+ const inputType = type(input);
122
+ if (["Undefined", "NaN", "Number", "Null"].includes(inputType))
123
+ return false;
124
+ if (!input) return true;
125
+ if (inputType === "Object") {
126
+ return Object.keys(input).length === 0;
127
+ }
128
+ if (inputType === "Array") {
129
+ return input.length === 0;
130
+ }
131
+ return false;
132
+ }
133
+
89
134
  // ../../node_modules/.pnpm/rambdax@11.3.1/node_modules/rambdax/src/isNil.js
90
135
  function isNil(x) {
91
136
  return x === void 0 || x === null;
@@ -503,8 +548,8 @@ function getSchemaWithMetadataFromAnyOrionForm(type2) {
503
548
 
504
549
  // src/getValidationErrors/getError/index.ts
505
550
  async function getValidationErrors(params) {
506
- const { schema, doc, currentDoc, value, currentSchema, keys, options = {}, args = [] } = params;
507
- const info = { schema, doc, currentDoc, keys, currentSchema, options };
551
+ const { schema: schema2, doc, currentDoc, value, currentSchema, keys, options = {}, args = [] } = params;
552
+ const info = { schema: schema2, doc, currentDoc, keys, currentSchema, options };
508
553
  if (isNil(value)) {
509
554
  if (!currentSchema.optional && !options.omitRequired) {
510
555
  return Errors_default.REQUIRED;
@@ -536,11 +581,11 @@ async function getValidationErrors(params) {
536
581
 
537
582
  // src/getSchemaFromTypedSchema.ts
538
583
  Symbol.metadata ?? (Symbol.metadata = Symbol("Symbol.metadata"));
539
- var getSchemaFromTypedSchema = (schema) => {
540
- const item = schema;
541
- if (!schema[Symbol.metadata]) return item;
542
- if (!schema[Symbol.metadata]._isTypedSchema) return item;
543
- return schema[Symbol.metadata]._getModel().getSchema();
584
+ var getSchemaFromTypedSchema = (schema2) => {
585
+ const item = schema2;
586
+ if (!schema2[Symbol.metadata]) return item;
587
+ if (!schema2[Symbol.metadata]._isTypedSchema) return item;
588
+ return schema2[Symbol.metadata]._getModel().getSchema();
544
589
  };
545
590
 
546
591
  // src/getValidationErrors/convertTypedSchema.ts
@@ -569,9 +614,9 @@ function clone2(value) {
569
614
  // src/getValidationErrors/doValidation.ts
570
615
  async function doValidation(params) {
571
616
  convertTypedSchema(params);
572
- const { schema, doc, currentDoc, value, currentSchema, keys = [], addError, options, args } = params;
617
+ const { schema: schema2, doc, currentDoc, value, currentSchema, keys = [], addError, options, args } = params;
573
618
  const info = {
574
- schema,
619
+ schema: schema2,
575
620
  doc,
576
621
  currentDoc,
577
622
  value,
@@ -653,8 +698,8 @@ function getValidationErrorsObject(validationErrors) {
653
698
  var defaultOptions = {
654
699
  omitRequired: false
655
700
  };
656
- async function getValidationErrors2(schema, doc, passedOptions = {}, ...args) {
657
- schema = getSchemaFromAnyOrionForm(schema);
701
+ async function getValidationErrors2(schema2, doc, passedOptions = {}, ...args) {
702
+ schema2 = getSchemaFromAnyOrionForm(schema2);
658
703
  const options = { ...defaultOptions, ...passedOptions };
659
704
  const errors = [];
660
705
  const addError = (keys, code) => {
@@ -664,11 +709,11 @@ async function getValidationErrors2(schema, doc, passedOptions = {}, ...args) {
664
709
  });
665
710
  };
666
711
  await doValidation({
667
- schema,
712
+ schema: schema2,
668
713
  doc,
669
714
  currentDoc: doc,
670
715
  value: doc,
671
- currentSchema: { type: schema },
716
+ currentSchema: { type: schema2 },
672
717
  addError,
673
718
  options,
674
719
  args
@@ -677,16 +722,16 @@ async function getValidationErrors2(schema, doc, passedOptions = {}, ...args) {
677
722
  }
678
723
 
679
724
  // src/validate.ts
680
- async function validate(schema, doc, passedOptions = {}, ...args) {
681
- const validationErrors = await getValidationErrors2(schema, doc, passedOptions, ...args);
725
+ async function validate(schema2, doc, passedOptions = {}, ...args) {
726
+ const validationErrors = await getValidationErrors2(schema2, doc, passedOptions, ...args);
682
727
  if (validationErrors) {
683
728
  throw new ValidationError(validationErrors);
684
729
  }
685
730
  }
686
731
 
687
732
  // src/isValid.ts
688
- async function isValid(schema, doc, passedOptions = {}, ...args) {
689
- const validationErrors = await getValidationErrors2(schema, doc, passedOptions, ...args);
733
+ async function isValid(schema2, doc, passedOptions = {}, ...args) {
734
+ const validationErrors = await getValidationErrors2(schema2, doc, passedOptions, ...args);
690
735
  return !validationErrors;
691
736
  }
692
737
 
@@ -698,9 +743,9 @@ function getFieldType(type2) {
698
743
  }
699
744
 
700
745
  // src/clean/getObjectNode.ts
701
- function getObjectNode(schema, value) {
702
- if (type(schema.type) === "Object" && type(value) === "Object") {
703
- const result = schema;
746
+ function getObjectNode(schema2, value) {
747
+ if (type(schema2.type) === "Object" && type(value) === "Object") {
748
+ const result = schema2;
704
749
  return result;
705
750
  }
706
751
  return null;
@@ -744,17 +789,17 @@ async function cleanType(type2, fieldSchema, value, info, ...args) {
744
789
 
745
790
  // src/clean/recursiveClean.ts
746
791
  var cleanObjectFields = async ({
747
- schema,
792
+ schema: schema2,
748
793
  value,
749
794
  ...other
750
795
  }) => {
751
- const keys = Object.keys(schema.type).filter((key) => !key.startsWith("__"));
796
+ const keys = Object.keys(schema2.type).filter((key) => !key.startsWith("__"));
752
797
  const newDoc = {};
753
798
  for (const key of keys) {
754
799
  try {
755
800
  const cleanOptions = {
756
801
  ...other,
757
- schema: schema.type[key],
802
+ schema: schema2.type[key],
758
803
  value: value[key],
759
804
  currentDoc: value
760
805
  };
@@ -769,11 +814,11 @@ var cleanObjectFields = async ({
769
814
  return newDoc;
770
815
  };
771
816
  var cleanArrayItems = async ({
772
- schema,
817
+ schema: schema2,
773
818
  value,
774
819
  ...other
775
820
  }) => {
776
- const schemaType = schema.type[0];
821
+ const schemaType = schema2.type[0];
777
822
  const promises = value.map(async (item) => {
778
823
  const newValue = await clean({
779
824
  ...other,
@@ -788,17 +833,17 @@ var cleanArrayItems = async ({
788
833
  const result = await Promise.all(promises);
789
834
  return result.filter((value2) => value2 !== void 0);
790
835
  };
791
- function getArrayNode(schema, value) {
792
- if (Array.isArray(schema.type) && !isNil(value)) {
793
- const result = schema;
836
+ function getArrayNode(schema2, value) {
837
+ if (Array.isArray(schema2.type) && !isNil(value)) {
838
+ const result = schema2;
794
839
  return result;
795
840
  }
796
841
  return null;
797
842
  }
798
843
  var clean = async (info) => {
799
844
  convertTypedSchema(info);
800
- const { schema, args = [], value } = info;
801
- const currSchema = schema.type === void 0 ? { type: schema } : schema;
845
+ const { schema: schema2, args = [], value } = info;
846
+ const currSchema = schema2.type === void 0 ? { type: schema2 } : schema2;
802
847
  const objectSchema = getObjectNode(currSchema, value);
803
848
  if (objectSchema) {
804
849
  const newDoc = await cleanObjectFields({
@@ -835,12 +880,12 @@ var defaultOptions2 = {
835
880
  trimStrings: true,
836
881
  removeEmptyStrings: false
837
882
  };
838
- async function clean2(schema, doc, opts = {}, ...args) {
883
+ async function clean2(schema2, doc, opts = {}, ...args) {
839
884
  if (!doc) return doc;
840
- schema = getSchemaFromAnyOrionForm(schema);
885
+ schema2 = getSchemaFromAnyOrionForm(schema2);
841
886
  const options = { ...defaultOptions2, ...opts };
842
887
  const params = {
843
- schema: { type: schema },
888
+ schema: { type: schema2 },
844
889
  value: doc,
845
890
  doc: options.forceDoc || doc,
846
891
  currentDoc: doc,
@@ -869,11 +914,11 @@ var dotGet = function dotGet2(object, path) {
869
914
  }
870
915
  return null;
871
916
  };
872
- function dotGetSchema_default(schema, path) {
873
- if (isNil(schema)) {
917
+ function dotGetSchema_default(schema2, path) {
918
+ if (isNil(schema2)) {
874
919
  throw new Error("You need to pass a schema");
875
920
  }
876
- return dotGet({ type: schema }, path);
921
+ return dotGet({ type: schema2 }, path);
877
922
  }
878
923
 
879
924
  // src/dotGetSchema.ts
@@ -883,9 +928,9 @@ var dotGetSchema_default2 = dotGetSchema_default;
883
928
  var defaultOptions3 = {
884
929
  filter: true
885
930
  };
886
- async function cleanKey_default(schema, key, value, passedOptions = {}, ...args) {
931
+ async function cleanKey_default(schema2, key, value, passedOptions = {}, ...args) {
887
932
  const options = { ...defaultOptions3, ...passedOptions };
888
- const keySchema = dotGetSchema_default2(schema, key);
933
+ const keySchema = dotGetSchema_default2(schema2, key);
889
934
  if (!keySchema) {
890
935
  if (options.filter) {
891
936
  return;
@@ -901,9 +946,9 @@ async function cleanKey_default(schema, key, value, passedOptions = {}, ...args)
901
946
  var defaultOptions4 = {
902
947
  filter: false
903
948
  };
904
- async function validateKey_default(schema, key, value, passedOptions = {}, ...args) {
949
+ async function validateKey_default(schema2, key, value, passedOptions = {}, ...args) {
905
950
  const options = { ...defaultOptions4, ...passedOptions };
906
- const keySchema = dotGetSchema_default(schema, key);
951
+ const keySchema = dotGetSchema_default(schema2, key);
907
952
  if (!keySchema) {
908
953
  if (options.filter) {
909
954
  return Errors_default.NOT_IN_SCHEMA;
@@ -966,17 +1011,93 @@ function createEnum(name, values) {
966
1011
  });
967
1012
  }
968
1013
 
1014
+ // src/types/fields.ts
1015
+ var subSchema = {
1016
+ name: {
1017
+ type: String
1018
+ }
1019
+ };
1020
+ var schema = {
1021
+ filter: {
1022
+ type: String
1023
+ },
1024
+ sub: {
1025
+ type: subSchema
1026
+ },
1027
+ gender: {
1028
+ type: createEnum("gender", ["male", "female"])
1029
+ },
1030
+ page: {
1031
+ type: "integer",
1032
+ defaultValue: 1,
1033
+ min: 1
1034
+ },
1035
+ limit: {
1036
+ type: "integer",
1037
+ defaultValue: 0,
1038
+ min: 0,
1039
+ max: 200
1040
+ },
1041
+ sortBy: {
1042
+ type: String,
1043
+ optional: true
1044
+ },
1045
+ sortType: {
1046
+ type: String,
1047
+ allowedValues: ["asc", "desc"],
1048
+ optional: true
1049
+ }
1050
+ };
1051
+
969
1052
  // src/cleanAndValidate.ts
970
- async function cleanAndValidate(schema, doc) {
971
- const cleaned = await clean2(schema, doc);
972
- await validate(schema, cleaned);
1053
+ async function cleanAndValidate(schema2, doc) {
1054
+ const cleaned = await clean2(schema2, doc);
1055
+ await validate(schema2, cleaned);
973
1056
  return cleaned;
974
1057
  }
975
1058
 
976
1059
  // src/schemaWithName/index.ts
977
- function schemaWithName(name, schema) {
978
- schema.__modelName = name;
979
- return schema;
1060
+ function schemaWithName(name, schema2) {
1061
+ schema2.__modelName = name;
1062
+ return schema2;
1063
+ }
1064
+
1065
+ // src/cloneSchema.ts
1066
+ function cloneSchema(options) {
1067
+ const { schema: schema2, extendSchema, mapFields, pickFields, omitFields } = options;
1068
+ const originalMetaKeys = Object.keys(schema2).filter((key) => key.startsWith("__"));
1069
+ const originalMetaFields = pick(originalMetaKeys, schema2);
1070
+ const cloned = { ...schema2 };
1071
+ if (pickFields == null ? void 0 : pickFields.length) {
1072
+ for (const key in cloned) {
1073
+ if (!pickFields.includes(key)) {
1074
+ delete cloned[key];
1075
+ }
1076
+ }
1077
+ }
1078
+ if (omitFields == null ? void 0 : omitFields.length) {
1079
+ for (const key in cloned) {
1080
+ if (omitFields.includes(key)) {
1081
+ delete cloned[key];
1082
+ }
1083
+ }
1084
+ }
1085
+ if (!isEmpty(extendSchema)) {
1086
+ for (const key in extendSchema) {
1087
+ cloned[key] = extendSchema[key];
1088
+ }
1089
+ }
1090
+ if (mapFields) {
1091
+ for (const key in cloned) {
1092
+ cloned[key] = mapFields(cloned[key], key);
1093
+ }
1094
+ }
1095
+ for (const key of originalMetaKeys) {
1096
+ if (originalMetaFields[key]) {
1097
+ cloned[key] = originalMetaFields[key];
1098
+ }
1099
+ }
1100
+ return cloned;
980
1101
  }
981
1102
  // Annotate the CommonJS export names for ESM import in node:
982
1103
  0 && (module.exports = {
@@ -984,6 +1105,7 @@ function schemaWithName(name, schema) {
984
1105
  clean,
985
1106
  cleanAndValidate,
986
1107
  cleanKey,
1108
+ cloneSchema,
987
1109
  createEnum,
988
1110
  dotGetSchema,
989
1111
  fieldTypes,