@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.js CHANGED
@@ -14,6 +14,35 @@ function type(input) {
14
14
  // ../../node_modules/.pnpm/rambdax@11.3.1/node_modules/rambdax/src/_internals/isArray.js
15
15
  var { isArray } = Array;
16
16
 
17
+ // ../../node_modules/.pnpm/rambdax@11.3.1/node_modules/rambdax/src/_internals/isInteger.js
18
+ function _isInteger(n) {
19
+ return n << 0 === n;
20
+ }
21
+ var isInteger = Number.isInteger || _isInteger;
22
+
23
+ // ../../node_modules/.pnpm/rambdax@11.3.1/node_modules/rambdax/src/_internals/createPath.js
24
+ function createPath(path, delimiter = ".") {
25
+ return typeof path === "string" ? path.split(delimiter).map((x) => isInteger(x) ? Number(x) : x) : path;
26
+ }
27
+
28
+ // ../../node_modules/.pnpm/rambdax@11.3.1/node_modules/rambdax/src/pick.js
29
+ function pick(propsToPick, input) {
30
+ if (arguments.length === 1) return (_input) => pick(propsToPick, _input);
31
+ if (input === null || input === void 0) {
32
+ return void 0;
33
+ }
34
+ const keys = createPath(propsToPick, ",");
35
+ const willReturn = {};
36
+ let counter = 0;
37
+ while (counter < keys.length) {
38
+ if (keys[counter] in input) {
39
+ willReturn[keys[counter]] = input[keys[counter]];
40
+ }
41
+ counter++;
42
+ }
43
+ return willReturn;
44
+ }
45
+
17
46
  // ../../node_modules/.pnpm/rambdax@11.3.1/node_modules/rambdax/src/isType.js
18
47
  function isType(xType, x) {
19
48
  if (arguments.length === 1) {
@@ -33,6 +62,21 @@ function clone(input) {
33
62
  return out;
34
63
  }
35
64
 
65
+ // ../../node_modules/.pnpm/rambdax@11.3.1/node_modules/rambdax/src/isEmpty.js
66
+ function isEmpty(input) {
67
+ const inputType = type(input);
68
+ if (["Undefined", "NaN", "Number", "Null"].includes(inputType))
69
+ return false;
70
+ if (!input) return true;
71
+ if (inputType === "Object") {
72
+ return Object.keys(input).length === 0;
73
+ }
74
+ if (inputType === "Array") {
75
+ return input.length === 0;
76
+ }
77
+ return false;
78
+ }
79
+
36
80
  // ../../node_modules/.pnpm/rambdax@11.3.1/node_modules/rambdax/src/isNil.js
37
81
  function isNil(x) {
38
82
  return x === void 0 || x === null;
@@ -450,8 +494,8 @@ function getSchemaWithMetadataFromAnyOrionForm(type2) {
450
494
 
451
495
  // src/getValidationErrors/getError/index.ts
452
496
  async function getValidationErrors(params) {
453
- const { schema, doc, currentDoc, value, currentSchema, keys, options = {}, args = [] } = params;
454
- const info = { schema, doc, currentDoc, keys, currentSchema, options };
497
+ const { schema: schema2, doc, currentDoc, value, currentSchema, keys, options = {}, args = [] } = params;
498
+ const info = { schema: schema2, doc, currentDoc, keys, currentSchema, options };
455
499
  if (isNil(value)) {
456
500
  if (!currentSchema.optional && !options.omitRequired) {
457
501
  return Errors_default.REQUIRED;
@@ -483,11 +527,11 @@ async function getValidationErrors(params) {
483
527
 
484
528
  // src/getSchemaFromTypedSchema.ts
485
529
  Symbol.metadata ?? (Symbol.metadata = Symbol("Symbol.metadata"));
486
- var getSchemaFromTypedSchema = (schema) => {
487
- const item = schema;
488
- if (!schema[Symbol.metadata]) return item;
489
- if (!schema[Symbol.metadata]._isTypedSchema) return item;
490
- return schema[Symbol.metadata]._getModel().getSchema();
530
+ var getSchemaFromTypedSchema = (schema2) => {
531
+ const item = schema2;
532
+ if (!schema2[Symbol.metadata]) return item;
533
+ if (!schema2[Symbol.metadata]._isTypedSchema) return item;
534
+ return schema2[Symbol.metadata]._getModel().getSchema();
491
535
  };
492
536
 
493
537
  // src/getValidationErrors/convertTypedSchema.ts
@@ -516,9 +560,9 @@ function clone2(value) {
516
560
  // src/getValidationErrors/doValidation.ts
517
561
  async function doValidation(params) {
518
562
  convertTypedSchema(params);
519
- const { schema, doc, currentDoc, value, currentSchema, keys = [], addError, options, args } = params;
563
+ const { schema: schema2, doc, currentDoc, value, currentSchema, keys = [], addError, options, args } = params;
520
564
  const info = {
521
- schema,
565
+ schema: schema2,
522
566
  doc,
523
567
  currentDoc,
524
568
  value,
@@ -600,8 +644,8 @@ function getValidationErrorsObject(validationErrors) {
600
644
  var defaultOptions = {
601
645
  omitRequired: false
602
646
  };
603
- async function getValidationErrors2(schema, doc, passedOptions = {}, ...args) {
604
- schema = getSchemaFromAnyOrionForm(schema);
647
+ async function getValidationErrors2(schema2, doc, passedOptions = {}, ...args) {
648
+ schema2 = getSchemaFromAnyOrionForm(schema2);
605
649
  const options = { ...defaultOptions, ...passedOptions };
606
650
  const errors = [];
607
651
  const addError = (keys, code) => {
@@ -611,11 +655,11 @@ async function getValidationErrors2(schema, doc, passedOptions = {}, ...args) {
611
655
  });
612
656
  };
613
657
  await doValidation({
614
- schema,
658
+ schema: schema2,
615
659
  doc,
616
660
  currentDoc: doc,
617
661
  value: doc,
618
- currentSchema: { type: schema },
662
+ currentSchema: { type: schema2 },
619
663
  addError,
620
664
  options,
621
665
  args
@@ -624,16 +668,16 @@ async function getValidationErrors2(schema, doc, passedOptions = {}, ...args) {
624
668
  }
625
669
 
626
670
  // src/validate.ts
627
- async function validate(schema, doc, passedOptions = {}, ...args) {
628
- const validationErrors = await getValidationErrors2(schema, doc, passedOptions, ...args);
671
+ async function validate(schema2, doc, passedOptions = {}, ...args) {
672
+ const validationErrors = await getValidationErrors2(schema2, doc, passedOptions, ...args);
629
673
  if (validationErrors) {
630
674
  throw new ValidationError(validationErrors);
631
675
  }
632
676
  }
633
677
 
634
678
  // src/isValid.ts
635
- async function isValid(schema, doc, passedOptions = {}, ...args) {
636
- const validationErrors = await getValidationErrors2(schema, doc, passedOptions, ...args);
679
+ async function isValid(schema2, doc, passedOptions = {}, ...args) {
680
+ const validationErrors = await getValidationErrors2(schema2, doc, passedOptions, ...args);
637
681
  return !validationErrors;
638
682
  }
639
683
 
@@ -645,9 +689,9 @@ function getFieldType(type2) {
645
689
  }
646
690
 
647
691
  // src/clean/getObjectNode.ts
648
- function getObjectNode(schema, value) {
649
- if (type(schema.type) === "Object" && type(value) === "Object") {
650
- const result = schema;
692
+ function getObjectNode(schema2, value) {
693
+ if (type(schema2.type) === "Object" && type(value) === "Object") {
694
+ const result = schema2;
651
695
  return result;
652
696
  }
653
697
  return null;
@@ -691,17 +735,17 @@ async function cleanType(type2, fieldSchema, value, info, ...args) {
691
735
 
692
736
  // src/clean/recursiveClean.ts
693
737
  var cleanObjectFields = async ({
694
- schema,
738
+ schema: schema2,
695
739
  value,
696
740
  ...other
697
741
  }) => {
698
- const keys = Object.keys(schema.type).filter((key) => !key.startsWith("__"));
742
+ const keys = Object.keys(schema2.type).filter((key) => !key.startsWith("__"));
699
743
  const newDoc = {};
700
744
  for (const key of keys) {
701
745
  try {
702
746
  const cleanOptions = {
703
747
  ...other,
704
- schema: schema.type[key],
748
+ schema: schema2.type[key],
705
749
  value: value[key],
706
750
  currentDoc: value
707
751
  };
@@ -716,11 +760,11 @@ var cleanObjectFields = async ({
716
760
  return newDoc;
717
761
  };
718
762
  var cleanArrayItems = async ({
719
- schema,
763
+ schema: schema2,
720
764
  value,
721
765
  ...other
722
766
  }) => {
723
- const schemaType = schema.type[0];
767
+ const schemaType = schema2.type[0];
724
768
  const promises = value.map(async (item) => {
725
769
  const newValue = await clean({
726
770
  ...other,
@@ -735,17 +779,17 @@ var cleanArrayItems = async ({
735
779
  const result = await Promise.all(promises);
736
780
  return result.filter((value2) => value2 !== void 0);
737
781
  };
738
- function getArrayNode(schema, value) {
739
- if (Array.isArray(schema.type) && !isNil(value)) {
740
- const result = schema;
782
+ function getArrayNode(schema2, value) {
783
+ if (Array.isArray(schema2.type) && !isNil(value)) {
784
+ const result = schema2;
741
785
  return result;
742
786
  }
743
787
  return null;
744
788
  }
745
789
  var clean = async (info) => {
746
790
  convertTypedSchema(info);
747
- const { schema, args = [], value } = info;
748
- const currSchema = schema.type === void 0 ? { type: schema } : schema;
791
+ const { schema: schema2, args = [], value } = info;
792
+ const currSchema = schema2.type === void 0 ? { type: schema2 } : schema2;
749
793
  const objectSchema = getObjectNode(currSchema, value);
750
794
  if (objectSchema) {
751
795
  const newDoc = await cleanObjectFields({
@@ -782,12 +826,12 @@ var defaultOptions2 = {
782
826
  trimStrings: true,
783
827
  removeEmptyStrings: false
784
828
  };
785
- async function clean2(schema, doc, opts = {}, ...args) {
829
+ async function clean2(schema2, doc, opts = {}, ...args) {
786
830
  if (!doc) return doc;
787
- schema = getSchemaFromAnyOrionForm(schema);
831
+ schema2 = getSchemaFromAnyOrionForm(schema2);
788
832
  const options = { ...defaultOptions2, ...opts };
789
833
  const params = {
790
- schema: { type: schema },
834
+ schema: { type: schema2 },
791
835
  value: doc,
792
836
  doc: options.forceDoc || doc,
793
837
  currentDoc: doc,
@@ -816,11 +860,11 @@ var dotGet = function dotGet2(object, path) {
816
860
  }
817
861
  return null;
818
862
  };
819
- function dotGetSchema_default(schema, path) {
820
- if (isNil(schema)) {
863
+ function dotGetSchema_default(schema2, path) {
864
+ if (isNil(schema2)) {
821
865
  throw new Error("You need to pass a schema");
822
866
  }
823
- return dotGet({ type: schema }, path);
867
+ return dotGet({ type: schema2 }, path);
824
868
  }
825
869
 
826
870
  // src/dotGetSchema.ts
@@ -830,9 +874,9 @@ var dotGetSchema_default2 = dotGetSchema_default;
830
874
  var defaultOptions3 = {
831
875
  filter: true
832
876
  };
833
- async function cleanKey_default(schema, key, value, passedOptions = {}, ...args) {
877
+ async function cleanKey_default(schema2, key, value, passedOptions = {}, ...args) {
834
878
  const options = { ...defaultOptions3, ...passedOptions };
835
- const keySchema = dotGetSchema_default2(schema, key);
879
+ const keySchema = dotGetSchema_default2(schema2, key);
836
880
  if (!keySchema) {
837
881
  if (options.filter) {
838
882
  return;
@@ -848,9 +892,9 @@ async function cleanKey_default(schema, key, value, passedOptions = {}, ...args)
848
892
  var defaultOptions4 = {
849
893
  filter: false
850
894
  };
851
- async function validateKey_default(schema, key, value, passedOptions = {}, ...args) {
895
+ async function validateKey_default(schema2, key, value, passedOptions = {}, ...args) {
852
896
  const options = { ...defaultOptions4, ...passedOptions };
853
- const keySchema = dotGetSchema_default(schema, key);
897
+ const keySchema = dotGetSchema_default(schema2, key);
854
898
  if (!keySchema) {
855
899
  if (options.filter) {
856
900
  return Errors_default.NOT_IN_SCHEMA;
@@ -913,23 +957,100 @@ function createEnum(name, values) {
913
957
  });
914
958
  }
915
959
 
960
+ // src/types/fields.ts
961
+ var subSchema = {
962
+ name: {
963
+ type: String
964
+ }
965
+ };
966
+ var schema = {
967
+ filter: {
968
+ type: String
969
+ },
970
+ sub: {
971
+ type: subSchema
972
+ },
973
+ gender: {
974
+ type: createEnum("gender", ["male", "female"])
975
+ },
976
+ page: {
977
+ type: "integer",
978
+ defaultValue: 1,
979
+ min: 1
980
+ },
981
+ limit: {
982
+ type: "integer",
983
+ defaultValue: 0,
984
+ min: 0,
985
+ max: 200
986
+ },
987
+ sortBy: {
988
+ type: String,
989
+ optional: true
990
+ },
991
+ sortType: {
992
+ type: String,
993
+ allowedValues: ["asc", "desc"],
994
+ optional: true
995
+ }
996
+ };
997
+
916
998
  // src/cleanAndValidate.ts
917
- async function cleanAndValidate(schema, doc) {
918
- const cleaned = await clean2(schema, doc);
919
- await validate(schema, cleaned);
999
+ async function cleanAndValidate(schema2, doc) {
1000
+ const cleaned = await clean2(schema2, doc);
1001
+ await validate(schema2, cleaned);
920
1002
  return cleaned;
921
1003
  }
922
1004
 
923
1005
  // src/schemaWithName/index.ts
924
- function schemaWithName(name, schema) {
925
- schema.__modelName = name;
926
- return schema;
1006
+ function schemaWithName(name, schema2) {
1007
+ schema2.__modelName = name;
1008
+ return schema2;
1009
+ }
1010
+
1011
+ // src/cloneSchema.ts
1012
+ function cloneSchema(options) {
1013
+ const { schema: schema2, extendSchema, mapFields, pickFields, omitFields } = options;
1014
+ const originalMetaKeys = Object.keys(schema2).filter((key) => key.startsWith("__"));
1015
+ const originalMetaFields = pick(originalMetaKeys, schema2);
1016
+ const cloned = { ...schema2 };
1017
+ if (pickFields == null ? void 0 : pickFields.length) {
1018
+ for (const key in cloned) {
1019
+ if (!pickFields.includes(key)) {
1020
+ delete cloned[key];
1021
+ }
1022
+ }
1023
+ }
1024
+ if (omitFields == null ? void 0 : omitFields.length) {
1025
+ for (const key in cloned) {
1026
+ if (omitFields.includes(key)) {
1027
+ delete cloned[key];
1028
+ }
1029
+ }
1030
+ }
1031
+ if (!isEmpty(extendSchema)) {
1032
+ for (const key in extendSchema) {
1033
+ cloned[key] = extendSchema[key];
1034
+ }
1035
+ }
1036
+ if (mapFields) {
1037
+ for (const key in cloned) {
1038
+ cloned[key] = mapFields(cloned[key], key);
1039
+ }
1040
+ }
1041
+ for (const key of originalMetaKeys) {
1042
+ if (originalMetaFields[key]) {
1043
+ cloned[key] = originalMetaFields[key];
1044
+ }
1045
+ }
1046
+ return cloned;
927
1047
  }
928
1048
  export {
929
1049
  ValidationError,
930
1050
  clean2 as clean,
931
1051
  cleanAndValidate,
932
1052
  cleanKey_default as cleanKey,
1053
+ cloneSchema,
933
1054
  createEnum,
934
1055
  dotGetSchema_default2 as dotGetSchema,
935
1056
  fieldTypes_default as fieldTypes,