@orion-js/schema 4.0.0-next.7 → 4.0.1

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
@@ -31,6 +31,7 @@ var index_exports = {};
31
31
  __export(index_exports, {
32
32
  ValidationError: () => ValidationError,
33
33
  clean: () => clean2,
34
+ cleanAndValidate: () => cleanAndValidate,
34
35
  cleanKey: () => cleanKey_default,
35
36
  createEnum: () => createEnum,
36
37
  dotGetSchema: () => dotGetSchema_default2,
@@ -42,6 +43,7 @@ __export(index_exports, {
42
43
  getValidationErrors: () => getValidationErrors2,
43
44
  isSchemaLike: () => isSchemaLike,
44
45
  isSchemaOrFieldLike: () => isSchemaOrFieldLike,
46
+ isStrictSchemaLike: () => isStrictSchemaLike,
45
47
  isValid: () => isValid,
46
48
  schemaWithName: () => schemaWithName,
47
49
  validate: () => validate,
@@ -131,15 +133,6 @@ var ValidationError = class _ValidationError extends Error {
131
133
  };
132
134
  };
133
135
 
134
- // src/getSchemaFromTypedSchema.ts
135
- Symbol.metadata ?? (Symbol.metadata = Symbol("Symbol.metadata"));
136
- var getSchemaFromTypedSchema = (schema) => {
137
- const item = schema;
138
- if (!schema[Symbol.metadata]) return item;
139
- if (!schema[Symbol.metadata]._isTypedSchema) return item;
140
- return schema[Symbol.metadata]._getModel().getSchema();
141
- };
142
-
143
136
  // src/fieldType.ts
144
137
  function fieldType(opts) {
145
138
  const { name, validate: validate2, clean: clean3, ...otherFields } = opts;
@@ -438,15 +431,80 @@ function getFieldValidator(type2) {
438
431
  }
439
432
  const exists = fieldTypes_default[type2];
440
433
  if (!exists) {
441
- throw new Error("Field type does not exist");
434
+ throw new Error(`Field type does not exist. Got ${type2}`);
442
435
  }
443
436
  return type2;
444
437
  }
445
438
 
439
+ // src/models.ts
440
+ Symbol.metadata ?? (Symbol.metadata = Symbol("Symbol.metadata"));
441
+ function isSchemaLike(type2) {
442
+ var _a;
443
+ if (!type2) return false;
444
+ if (objectHasSubObjectWithKey(type2, "type")) return true;
445
+ if ((_a = type2 == null ? void 0 : type2[Symbol.metadata]) == null ? void 0 : _a._getModel) return true;
446
+ if (type2.getModel) return true;
447
+ if (type2.getSchema) return true;
448
+ if (type2.getCleanSchema) return true;
449
+ if (type2.__isModel) return true;
450
+ if (type2.__modelName) return true;
451
+ return false;
452
+ }
453
+ function isStrictSchemaLike(type2) {
454
+ if (isSchemaLike(type2)) return true;
455
+ return false;
456
+ }
457
+ function isSchemaOrFieldLike(type2) {
458
+ if (Array.isArray(type2)) {
459
+ if (type2.length !== 1) return false;
460
+ return isSchemaOrFieldLike(type2[0]);
461
+ }
462
+ if (isSchemaLike(type2)) return true;
463
+ try {
464
+ if (getFieldValidator(type2)) return true;
465
+ } catch {
466
+ return false;
467
+ }
468
+ return false;
469
+ }
470
+ function getSchemaModelName(type2) {
471
+ if (!type2) return null;
472
+ if (type2.__modelName) return type2.__modelName;
473
+ if (type2.getModel) return type2.getModel().name;
474
+ if (type2.getSchema) return type2.getSchema().__modelName;
475
+ return null;
476
+ }
477
+ function getSchemaFromAnyOrionForm(type2) {
478
+ var _a, _b;
479
+ if (!type2) return type2;
480
+ if ((_a = type2 == null ? void 0 : type2[Symbol.metadata]) == null ? void 0 : _a._getModel) {
481
+ return (_b = type2 == null ? void 0 : type2[Symbol.metadata]) == null ? void 0 : _b._getModel().getSchema();
482
+ }
483
+ if (type2 == null ? void 0 : type2.getModel) return type2.getModel().getSchema();
484
+ if (type2.getSchema) {
485
+ return type2.getSchema();
486
+ }
487
+ if (objectHasSubObjectWithKey(type2, "type")) return type2;
488
+ return type2;
489
+ }
490
+ function objectHasSubObjectWithKey(object, key) {
491
+ if (!object || typeof object !== "object") return false;
492
+ for (const key1 in object) {
493
+ const value = object[key1];
494
+ if (value && typeof value === "object" && key in value) {
495
+ return true;
496
+ }
497
+ }
498
+ return false;
499
+ }
500
+ function getSchemaWithMetadataFromAnyOrionForm(type2) {
501
+ return getSchemaFromAnyOrionForm(type2);
502
+ }
503
+
446
504
  // src/getValidationErrors/getError/index.ts
447
505
  async function getValidationErrors(params) {
448
- const { schema, doc, currentDoc, value, currentSchema, keys, options = {}, args = [] } = params;
449
- const info = { schema, doc, currentDoc, keys, currentSchema, options };
506
+ const { schema: schema2, doc, currentDoc, value, currentSchema, keys, options = {}, args = [] } = params;
507
+ const info = { schema: schema2, doc, currentDoc, keys, currentSchema, options };
450
508
  if (isNil(value)) {
451
509
  if (!currentSchema.optional && !options.omitRequired) {
452
510
  return Errors_default.REQUIRED;
@@ -476,6 +534,15 @@ async function getValidationErrors(params) {
476
534
  return null;
477
535
  }
478
536
 
537
+ // src/getSchemaFromTypedSchema.ts
538
+ Symbol.metadata ?? (Symbol.metadata = Symbol("Symbol.metadata"));
539
+ var getSchemaFromTypedSchema = (schema2) => {
540
+ const item = schema2;
541
+ if (!schema2[Symbol.metadata]) return item;
542
+ if (!schema2[Symbol.metadata]._isTypedSchema) return item;
543
+ return schema2[Symbol.metadata]._getModel().getSchema();
544
+ };
545
+
479
546
  // src/getValidationErrors/convertTypedSchema.ts
480
547
  var convertOnParam = (info, paramName) => {
481
548
  if (!info[paramName]) return;
@@ -502,8 +569,18 @@ function clone2(value) {
502
569
  // src/getValidationErrors/doValidation.ts
503
570
  async function doValidation(params) {
504
571
  convertTypedSchema(params);
505
- const { schema, doc, currentDoc, value, currentSchema, keys = [], addError, options, args } = params;
506
- const info = { schema, doc, currentDoc, value, currentSchema, keys, options, args, addError };
572
+ const { schema: schema2, doc, currentDoc, value, currentSchema, keys = [], addError, options, args } = params;
573
+ const info = {
574
+ schema: schema2,
575
+ doc,
576
+ currentDoc,
577
+ value,
578
+ currentSchema,
579
+ keys,
580
+ options,
581
+ args,
582
+ addError
583
+ };
507
584
  const error = await getValidationErrors(info);
508
585
  if (error) {
509
586
  addError(keys, error);
@@ -576,8 +653,8 @@ function getValidationErrorsObject(validationErrors) {
576
653
  var defaultOptions = {
577
654
  omitRequired: false
578
655
  };
579
- async function getValidationErrors2(schema, doc, passedOptions = {}, ...args) {
580
- schema = getSchemaFromTypedSchema(schema);
656
+ async function getValidationErrors2(schema2, doc, passedOptions = {}, ...args) {
657
+ schema2 = getSchemaFromAnyOrionForm(schema2);
581
658
  const options = { ...defaultOptions, ...passedOptions };
582
659
  const errors = [];
583
660
  const addError = (keys, code) => {
@@ -587,11 +664,11 @@ async function getValidationErrors2(schema, doc, passedOptions = {}, ...args) {
587
664
  });
588
665
  };
589
666
  await doValidation({
590
- schema,
667
+ schema: schema2,
591
668
  doc,
592
669
  currentDoc: doc,
593
670
  value: doc,
594
- currentSchema: { type: schema },
671
+ currentSchema: { type: schema2 },
595
672
  addError,
596
673
  options,
597
674
  args
@@ -600,16 +677,16 @@ async function getValidationErrors2(schema, doc, passedOptions = {}, ...args) {
600
677
  }
601
678
 
602
679
  // src/validate.ts
603
- async function validate(schema, doc, passedOptions = {}, ...args) {
604
- const validationErrors = await getValidationErrors2(schema, doc, passedOptions, ...args);
680
+ async function validate(schema2, doc, passedOptions = {}, ...args) {
681
+ const validationErrors = await getValidationErrors2(schema2, doc, passedOptions, ...args);
605
682
  if (validationErrors) {
606
683
  throw new ValidationError(validationErrors);
607
684
  }
608
685
  }
609
686
 
610
687
  // src/isValid.ts
611
- async function isValid(schema, doc, passedOptions = {}, ...args) {
612
- const validationErrors = await getValidationErrors2(schema, doc, passedOptions, ...args);
688
+ async function isValid(schema2, doc, passedOptions = {}, ...args) {
689
+ const validationErrors = await getValidationErrors2(schema2, doc, passedOptions, ...args);
613
690
  return !validationErrors;
614
691
  }
615
692
 
@@ -621,9 +698,9 @@ function getFieldType(type2) {
621
698
  }
622
699
 
623
700
  // src/clean/getObjectNode.ts
624
- function getObjectNode(schema, value) {
625
- if (type(schema.type) === "Object" && type(value) === "Object") {
626
- const result = schema;
701
+ function getObjectNode(schema2, value) {
702
+ if (type(schema2.type) === "Object" && type(value) === "Object") {
703
+ const result = schema2;
627
704
  return result;
628
705
  }
629
706
  return null;
@@ -667,17 +744,17 @@ async function cleanType(type2, fieldSchema, value, info, ...args) {
667
744
 
668
745
  // src/clean/recursiveClean.ts
669
746
  var cleanObjectFields = async ({
670
- schema,
747
+ schema: schema2,
671
748
  value,
672
749
  ...other
673
750
  }) => {
674
- const keys = Object.keys(schema.type).filter((key) => !key.startsWith("__"));
751
+ const keys = Object.keys(schema2.type).filter((key) => !key.startsWith("__"));
675
752
  const newDoc = {};
676
753
  for (const key of keys) {
677
754
  try {
678
755
  const cleanOptions = {
679
756
  ...other,
680
- schema: schema.type[key],
757
+ schema: schema2.type[key],
681
758
  value: value[key],
682
759
  currentDoc: value
683
760
  };
@@ -692,11 +769,11 @@ var cleanObjectFields = async ({
692
769
  return newDoc;
693
770
  };
694
771
  var cleanArrayItems = async ({
695
- schema,
772
+ schema: schema2,
696
773
  value,
697
774
  ...other
698
775
  }) => {
699
- const schemaType = schema.type[0];
776
+ const schemaType = schema2.type[0];
700
777
  const promises = value.map(async (item) => {
701
778
  const newValue = await clean({
702
779
  ...other,
@@ -711,17 +788,17 @@ var cleanArrayItems = async ({
711
788
  const result = await Promise.all(promises);
712
789
  return result.filter((value2) => value2 !== void 0);
713
790
  };
714
- function getArrayNode(schema, value) {
715
- if (Array.isArray(schema.type) && !isNil(value)) {
716
- const result = schema;
791
+ function getArrayNode(schema2, value) {
792
+ if (Array.isArray(schema2.type) && !isNil(value)) {
793
+ const result = schema2;
717
794
  return result;
718
795
  }
719
796
  return null;
720
797
  }
721
798
  var clean = async (info) => {
722
799
  convertTypedSchema(info);
723
- const { schema, args = [], value } = info;
724
- const currSchema = schema.type === void 0 ? { type: schema } : schema;
800
+ const { schema: schema2, args = [], value } = info;
801
+ const currSchema = schema2.type === void 0 ? { type: schema2 } : schema2;
725
802
  const objectSchema = getObjectNode(currSchema, value);
726
803
  if (objectSchema) {
727
804
  const newDoc = await cleanObjectFields({
@@ -758,12 +835,12 @@ var defaultOptions2 = {
758
835
  trimStrings: true,
759
836
  removeEmptyStrings: false
760
837
  };
761
- async function clean2(schema, doc, opts = {}, ...args) {
838
+ async function clean2(schema2, doc, opts = {}, ...args) {
762
839
  if (!doc) return doc;
763
- schema = getSchemaFromTypedSchema(schema);
840
+ schema2 = getSchemaFromAnyOrionForm(schema2);
764
841
  const options = { ...defaultOptions2, ...opts };
765
842
  const params = {
766
- schema: { type: schema },
843
+ schema: { type: schema2 },
767
844
  value: doc,
768
845
  doc: options.forceDoc || doc,
769
846
  currentDoc: doc,
@@ -792,11 +869,11 @@ var dotGet = function dotGet2(object, path) {
792
869
  }
793
870
  return null;
794
871
  };
795
- function dotGetSchema_default(schema, path) {
796
- if (isNil(schema)) {
872
+ function dotGetSchema_default(schema2, path) {
873
+ if (isNil(schema2)) {
797
874
  throw new Error("You need to pass a schema");
798
875
  }
799
- return dotGet({ type: schema }, path);
876
+ return dotGet({ type: schema2 }, path);
800
877
  }
801
878
 
802
879
  // src/dotGetSchema.ts
@@ -806,9 +883,9 @@ var dotGetSchema_default2 = dotGetSchema_default;
806
883
  var defaultOptions3 = {
807
884
  filter: true
808
885
  };
809
- async function cleanKey_default(schema, key, value, passedOptions = {}, ...args) {
886
+ async function cleanKey_default(schema2, key, value, passedOptions = {}, ...args) {
810
887
  const options = { ...defaultOptions3, ...passedOptions };
811
- const keySchema = dotGetSchema_default2(schema, key);
888
+ const keySchema = dotGetSchema_default2(schema2, key);
812
889
  if (!keySchema) {
813
890
  if (options.filter) {
814
891
  return;
@@ -824,9 +901,9 @@ async function cleanKey_default(schema, key, value, passedOptions = {}, ...args)
824
901
  var defaultOptions4 = {
825
902
  filter: false
826
903
  };
827
- async function validateKey_default(schema, key, value, passedOptions = {}, ...args) {
904
+ async function validateKey_default(schema2, key, value, passedOptions = {}, ...args) {
828
905
  const options = { ...defaultOptions4, ...passedOptions };
829
- const keySchema = dotGetSchema_default(schema, key);
906
+ const keySchema = dotGetSchema_default(schema2, key);
830
907
  if (!keySchema) {
831
908
  if (options.filter) {
832
909
  return Errors_default.NOT_IN_SCHEMA;
@@ -848,122 +925,102 @@ async function validateKey_default(schema, key, value, passedOptions = {}, ...ar
848
925
 
849
926
  // src/fieldTypes/enum.ts
850
927
  function createEnum(name, values) {
851
- return {
852
- type: values[0],
853
- ...fieldType({
854
- name: "enum",
855
- meta: {
856
- enumName: name,
857
- enumValues: values
858
- },
859
- toGraphQLType: (GraphQL) => {
860
- global.GraphQLEnums = global.GraphQLEnums || {};
861
- global.GraphQLEnums[name] = global.GraphQLEnums[name] || new GraphQL.GraphQLEnumType({
862
- name,
863
- values: values.reduce((result, value) => {
864
- result[value] = { value };
865
- return result;
866
- }, {})
867
- });
868
- return global.GraphQLEnums[name];
869
- },
870
- validate(value, { currentSchema }) {
871
- if (typeof value !== "string") return Errors_default.NOT_A_STRING;
872
- if (!values.includes(value)) {
873
- return Errors_default.NOT_AN_ALLOWED_VALUE;
874
- }
875
- if (value === "" && !currentSchema.optional) {
876
- return Errors_default.REQUIRED;
877
- }
878
- },
879
- clean(value, { options: { autoConvert, trimStrings, removeEmptyStrings } }) {
880
- if (autoConvert) {
881
- value = String(value);
882
- }
883
- if (trimStrings) {
884
- value = value.trim();
885
- }
886
- if (removeEmptyStrings && value === "") {
887
- return void 0;
888
- }
889
- return value;
928
+ return fieldType({
929
+ name: "enum",
930
+ meta: {
931
+ enumName: name,
932
+ enumValues: values
933
+ },
934
+ toGraphQLType: (GraphQL) => {
935
+ global.GraphQLEnums = global.GraphQLEnums || {};
936
+ global.GraphQLEnums[name] = global.GraphQLEnums[name] || new GraphQL.GraphQLEnumType({
937
+ name,
938
+ values: values.reduce((result, value) => {
939
+ result[value] = { value };
940
+ return result;
941
+ }, {})
942
+ });
943
+ return global.GraphQLEnums[name];
944
+ },
945
+ validate(value, { currentSchema }) {
946
+ if (typeof value !== "string") return Errors_default.NOT_A_STRING;
947
+ if (!values.includes(value)) {
948
+ return Errors_default.NOT_AN_ALLOWED_VALUE;
890
949
  }
891
- })
892
- };
950
+ if (value === "" && !currentSchema.optional) {
951
+ return Errors_default.REQUIRED;
952
+ }
953
+ },
954
+ clean(value, { options: { autoConvert, trimStrings, removeEmptyStrings } }) {
955
+ if (autoConvert) {
956
+ value = String(value);
957
+ }
958
+ if (trimStrings) {
959
+ value = value.trim();
960
+ }
961
+ if (removeEmptyStrings && value === "") {
962
+ return void 0;
963
+ }
964
+ return value;
965
+ }
966
+ });
893
967
  }
894
968
 
895
- // src/models.ts
896
- Symbol.metadata ?? (Symbol.metadata = Symbol("Symbol.metadata"));
897
- function isSchemaLike(type2) {
898
- var _a;
899
- if (!type2) return false;
900
- if (objectHasSubObjectWithKey(type2, "type")) return true;
901
- if ((_a = type2 == null ? void 0 : type2[Symbol.metadata]) == null ? void 0 : _a._getModel) return true;
902
- if (type2.getModel) return true;
903
- if (type2.getSchema) return true;
904
- if (type2.getCleanSchema) return true;
905
- if (type2.__isModel) return true;
906
- if (type2.__modelName) return true;
907
- return false;
908
- }
909
- function isSchemaOrFieldLike(type2) {
910
- if (Array.isArray(type2)) {
911
- if (type2.length !== 1) return false;
912
- return isSchemaOrFieldLike(type2[0]);
913
- }
914
- if (isSchemaLike(type2)) return true;
915
- try {
916
- if (getFieldValidator(type2)) return true;
917
- } catch {
918
- return false;
919
- }
920
- return false;
921
- }
922
- function getSchemaModelName(type2) {
923
- if (!type2) return null;
924
- if (type2.__modelName) return type2.__modelName;
925
- if (type2.getModel) return type2.getModel().name;
926
- if (type2.getSchema) return type2.getSchema().__modelName;
927
- return null;
928
- }
929
- function getSchemaFromAnyOrionForm(type2) {
930
- var _a, _b;
931
- if ((_a = type2 == null ? void 0 : type2[Symbol.metadata]) == null ? void 0 : _a._getModel) {
932
- return (_b = type2 == null ? void 0 : type2[Symbol.metadata]) == null ? void 0 : _b._getModel().getSchema();
969
+ // src/types/fields.ts
970
+ var subSchema = {
971
+ name: {
972
+ type: String
933
973
  }
934
- if (type2 == null ? void 0 : type2.getModel) return type2.getModel().getSchema();
935
- if (type2.getSchema) {
936
- return type2.getSchema();
937
- }
938
- if (type2.getSchema) {
939
- return type2.getSchema();
940
- }
941
- if (objectHasSubObjectWithKey(type2, "type")) return type2;
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
- }
974
+ };
975
+ var schema = {
976
+ filter: {
977
+ type: String
978
+ },
979
+ sub: {
980
+ type: subSchema
981
+ },
982
+ gender: {
983
+ type: createEnum("gender", ["male", "female"])
984
+ },
985
+ page: {
986
+ type: "integer",
987
+ defaultValue: 1,
988
+ min: 1
989
+ },
990
+ limit: {
991
+ type: "integer",
992
+ defaultValue: 0,
993
+ min: 0,
994
+ max: 200
995
+ },
996
+ sortBy: {
997
+ type: String,
998
+ optional: true
999
+ },
1000
+ sortType: {
1001
+ type: String,
1002
+ allowedValues: ["asc", "desc"],
1003
+ optional: true
951
1004
  }
952
- return false;
953
- }
954
- function getSchemaWithMetadataFromAnyOrionForm(type2) {
955
- return getSchemaFromAnyOrionForm(type2);
1005
+ };
1006
+
1007
+ // src/cleanAndValidate.ts
1008
+ async function cleanAndValidate(schema2, doc) {
1009
+ const cleaned = await clean2(schema2, doc);
1010
+ await validate(schema2, cleaned);
1011
+ return cleaned;
956
1012
  }
957
1013
 
958
1014
  // src/schemaWithName/index.ts
959
- function schemaWithName(name, schema) {
960
- schema.__modelName = name;
961
- return schema;
1015
+ function schemaWithName(name, schema2) {
1016
+ schema2.__modelName = name;
1017
+ return schema2;
962
1018
  }
963
1019
  // Annotate the CommonJS export names for ESM import in node:
964
1020
  0 && (module.exports = {
965
1021
  ValidationError,
966
1022
  clean,
1023
+ cleanAndValidate,
967
1024
  cleanKey,
968
1025
  createEnum,
969
1026
  dotGetSchema,
@@ -975,6 +1032,7 @@ function schemaWithName(name, schema) {
975
1032
  getValidationErrors,
976
1033
  isSchemaLike,
977
1034
  isSchemaOrFieldLike,
1035
+ isStrictSchemaLike,
978
1036
  isValid,
979
1037
  schemaWithName,
980
1038
  validate,