@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.js CHANGED
@@ -80,15 +80,6 @@ var ValidationError = class _ValidationError extends Error {
80
80
  };
81
81
  };
82
82
 
83
- // src/getSchemaFromTypedSchema.ts
84
- Symbol.metadata ?? (Symbol.metadata = Symbol("Symbol.metadata"));
85
- var getSchemaFromTypedSchema = (schema) => {
86
- const item = schema;
87
- if (!schema[Symbol.metadata]) return item;
88
- if (!schema[Symbol.metadata]._isTypedSchema) return item;
89
- return schema[Symbol.metadata]._getModel().getSchema();
90
- };
91
-
92
83
  // src/fieldType.ts
93
84
  function fieldType(opts) {
94
85
  const { name, validate: validate2, clean: clean3, ...otherFields } = opts;
@@ -387,15 +378,80 @@ function getFieldValidator(type2) {
387
378
  }
388
379
  const exists = fieldTypes_default[type2];
389
380
  if (!exists) {
390
- throw new Error("Field type does not exist");
381
+ throw new Error(`Field type does not exist. Got ${type2}`);
391
382
  }
392
383
  return type2;
393
384
  }
394
385
 
386
+ // src/models.ts
387
+ Symbol.metadata ?? (Symbol.metadata = Symbol("Symbol.metadata"));
388
+ function isSchemaLike(type2) {
389
+ var _a;
390
+ if (!type2) return false;
391
+ if (objectHasSubObjectWithKey(type2, "type")) return true;
392
+ if ((_a = type2 == null ? void 0 : type2[Symbol.metadata]) == null ? void 0 : _a._getModel) return true;
393
+ if (type2.getModel) return true;
394
+ if (type2.getSchema) return true;
395
+ if (type2.getCleanSchema) return true;
396
+ if (type2.__isModel) return true;
397
+ if (type2.__modelName) return true;
398
+ return false;
399
+ }
400
+ function isStrictSchemaLike(type2) {
401
+ if (isSchemaLike(type2)) return true;
402
+ return false;
403
+ }
404
+ function isSchemaOrFieldLike(type2) {
405
+ if (Array.isArray(type2)) {
406
+ if (type2.length !== 1) return false;
407
+ return isSchemaOrFieldLike(type2[0]);
408
+ }
409
+ if (isSchemaLike(type2)) return true;
410
+ try {
411
+ if (getFieldValidator(type2)) return true;
412
+ } catch {
413
+ return false;
414
+ }
415
+ return false;
416
+ }
417
+ function getSchemaModelName(type2) {
418
+ if (!type2) return null;
419
+ if (type2.__modelName) return type2.__modelName;
420
+ if (type2.getModel) return type2.getModel().name;
421
+ if (type2.getSchema) return type2.getSchema().__modelName;
422
+ return null;
423
+ }
424
+ function getSchemaFromAnyOrionForm(type2) {
425
+ var _a, _b;
426
+ if (!type2) return type2;
427
+ if ((_a = type2 == null ? void 0 : type2[Symbol.metadata]) == null ? void 0 : _a._getModel) {
428
+ return (_b = type2 == null ? void 0 : type2[Symbol.metadata]) == null ? void 0 : _b._getModel().getSchema();
429
+ }
430
+ if (type2 == null ? void 0 : type2.getModel) return type2.getModel().getSchema();
431
+ if (type2.getSchema) {
432
+ return type2.getSchema();
433
+ }
434
+ if (objectHasSubObjectWithKey(type2, "type")) return type2;
435
+ return type2;
436
+ }
437
+ function objectHasSubObjectWithKey(object, key) {
438
+ if (!object || typeof object !== "object") return false;
439
+ for (const key1 in object) {
440
+ const value = object[key1];
441
+ if (value && typeof value === "object" && key in value) {
442
+ return true;
443
+ }
444
+ }
445
+ return false;
446
+ }
447
+ function getSchemaWithMetadataFromAnyOrionForm(type2) {
448
+ return getSchemaFromAnyOrionForm(type2);
449
+ }
450
+
395
451
  // src/getValidationErrors/getError/index.ts
396
452
  async function getValidationErrors(params) {
397
- const { schema, doc, currentDoc, value, currentSchema, keys, options = {}, args = [] } = params;
398
- const info = { schema, doc, currentDoc, keys, currentSchema, options };
453
+ const { schema: schema2, doc, currentDoc, value, currentSchema, keys, options = {}, args = [] } = params;
454
+ const info = { schema: schema2, doc, currentDoc, keys, currentSchema, options };
399
455
  if (isNil(value)) {
400
456
  if (!currentSchema.optional && !options.omitRequired) {
401
457
  return Errors_default.REQUIRED;
@@ -425,6 +481,15 @@ async function getValidationErrors(params) {
425
481
  return null;
426
482
  }
427
483
 
484
+ // src/getSchemaFromTypedSchema.ts
485
+ Symbol.metadata ?? (Symbol.metadata = Symbol("Symbol.metadata"));
486
+ var getSchemaFromTypedSchema = (schema2) => {
487
+ const item = schema2;
488
+ if (!schema2[Symbol.metadata]) return item;
489
+ if (!schema2[Symbol.metadata]._isTypedSchema) return item;
490
+ return schema2[Symbol.metadata]._getModel().getSchema();
491
+ };
492
+
428
493
  // src/getValidationErrors/convertTypedSchema.ts
429
494
  var convertOnParam = (info, paramName) => {
430
495
  if (!info[paramName]) return;
@@ -451,8 +516,18 @@ function clone2(value) {
451
516
  // src/getValidationErrors/doValidation.ts
452
517
  async function doValidation(params) {
453
518
  convertTypedSchema(params);
454
- const { schema, doc, currentDoc, value, currentSchema, keys = [], addError, options, args } = params;
455
- const info = { schema, doc, currentDoc, value, currentSchema, keys, options, args, addError };
519
+ const { schema: schema2, doc, currentDoc, value, currentSchema, keys = [], addError, options, args } = params;
520
+ const info = {
521
+ schema: schema2,
522
+ doc,
523
+ currentDoc,
524
+ value,
525
+ currentSchema,
526
+ keys,
527
+ options,
528
+ args,
529
+ addError
530
+ };
456
531
  const error = await getValidationErrors(info);
457
532
  if (error) {
458
533
  addError(keys, error);
@@ -525,8 +600,8 @@ function getValidationErrorsObject(validationErrors) {
525
600
  var defaultOptions = {
526
601
  omitRequired: false
527
602
  };
528
- async function getValidationErrors2(schema, doc, passedOptions = {}, ...args) {
529
- schema = getSchemaFromTypedSchema(schema);
603
+ async function getValidationErrors2(schema2, doc, passedOptions = {}, ...args) {
604
+ schema2 = getSchemaFromAnyOrionForm(schema2);
530
605
  const options = { ...defaultOptions, ...passedOptions };
531
606
  const errors = [];
532
607
  const addError = (keys, code) => {
@@ -536,11 +611,11 @@ async function getValidationErrors2(schema, doc, passedOptions = {}, ...args) {
536
611
  });
537
612
  };
538
613
  await doValidation({
539
- schema,
614
+ schema: schema2,
540
615
  doc,
541
616
  currentDoc: doc,
542
617
  value: doc,
543
- currentSchema: { type: schema },
618
+ currentSchema: { type: schema2 },
544
619
  addError,
545
620
  options,
546
621
  args
@@ -549,16 +624,16 @@ async function getValidationErrors2(schema, doc, passedOptions = {}, ...args) {
549
624
  }
550
625
 
551
626
  // src/validate.ts
552
- async function validate(schema, doc, passedOptions = {}, ...args) {
553
- const validationErrors = await getValidationErrors2(schema, doc, passedOptions, ...args);
627
+ async function validate(schema2, doc, passedOptions = {}, ...args) {
628
+ const validationErrors = await getValidationErrors2(schema2, doc, passedOptions, ...args);
554
629
  if (validationErrors) {
555
630
  throw new ValidationError(validationErrors);
556
631
  }
557
632
  }
558
633
 
559
634
  // src/isValid.ts
560
- async function isValid(schema, doc, passedOptions = {}, ...args) {
561
- const validationErrors = await getValidationErrors2(schema, doc, passedOptions, ...args);
635
+ async function isValid(schema2, doc, passedOptions = {}, ...args) {
636
+ const validationErrors = await getValidationErrors2(schema2, doc, passedOptions, ...args);
562
637
  return !validationErrors;
563
638
  }
564
639
 
@@ -570,9 +645,9 @@ function getFieldType(type2) {
570
645
  }
571
646
 
572
647
  // src/clean/getObjectNode.ts
573
- function getObjectNode(schema, value) {
574
- if (type(schema.type) === "Object" && type(value) === "Object") {
575
- const result = schema;
648
+ function getObjectNode(schema2, value) {
649
+ if (type(schema2.type) === "Object" && type(value) === "Object") {
650
+ const result = schema2;
576
651
  return result;
577
652
  }
578
653
  return null;
@@ -616,17 +691,17 @@ async function cleanType(type2, fieldSchema, value, info, ...args) {
616
691
 
617
692
  // src/clean/recursiveClean.ts
618
693
  var cleanObjectFields = async ({
619
- schema,
694
+ schema: schema2,
620
695
  value,
621
696
  ...other
622
697
  }) => {
623
- const keys = Object.keys(schema.type).filter((key) => !key.startsWith("__"));
698
+ const keys = Object.keys(schema2.type).filter((key) => !key.startsWith("__"));
624
699
  const newDoc = {};
625
700
  for (const key of keys) {
626
701
  try {
627
702
  const cleanOptions = {
628
703
  ...other,
629
- schema: schema.type[key],
704
+ schema: schema2.type[key],
630
705
  value: value[key],
631
706
  currentDoc: value
632
707
  };
@@ -641,11 +716,11 @@ var cleanObjectFields = async ({
641
716
  return newDoc;
642
717
  };
643
718
  var cleanArrayItems = async ({
644
- schema,
719
+ schema: schema2,
645
720
  value,
646
721
  ...other
647
722
  }) => {
648
- const schemaType = schema.type[0];
723
+ const schemaType = schema2.type[0];
649
724
  const promises = value.map(async (item) => {
650
725
  const newValue = await clean({
651
726
  ...other,
@@ -660,17 +735,17 @@ var cleanArrayItems = async ({
660
735
  const result = await Promise.all(promises);
661
736
  return result.filter((value2) => value2 !== void 0);
662
737
  };
663
- function getArrayNode(schema, value) {
664
- if (Array.isArray(schema.type) && !isNil(value)) {
665
- const result = schema;
738
+ function getArrayNode(schema2, value) {
739
+ if (Array.isArray(schema2.type) && !isNil(value)) {
740
+ const result = schema2;
666
741
  return result;
667
742
  }
668
743
  return null;
669
744
  }
670
745
  var clean = async (info) => {
671
746
  convertTypedSchema(info);
672
- const { schema, args = [], value } = info;
673
- const currSchema = schema.type === void 0 ? { type: schema } : schema;
747
+ const { schema: schema2, args = [], value } = info;
748
+ const currSchema = schema2.type === void 0 ? { type: schema2 } : schema2;
674
749
  const objectSchema = getObjectNode(currSchema, value);
675
750
  if (objectSchema) {
676
751
  const newDoc = await cleanObjectFields({
@@ -707,12 +782,12 @@ var defaultOptions2 = {
707
782
  trimStrings: true,
708
783
  removeEmptyStrings: false
709
784
  };
710
- async function clean2(schema, doc, opts = {}, ...args) {
785
+ async function clean2(schema2, doc, opts = {}, ...args) {
711
786
  if (!doc) return doc;
712
- schema = getSchemaFromTypedSchema(schema);
787
+ schema2 = getSchemaFromAnyOrionForm(schema2);
713
788
  const options = { ...defaultOptions2, ...opts };
714
789
  const params = {
715
- schema: { type: schema },
790
+ schema: { type: schema2 },
716
791
  value: doc,
717
792
  doc: options.forceDoc || doc,
718
793
  currentDoc: doc,
@@ -741,11 +816,11 @@ var dotGet = function dotGet2(object, path) {
741
816
  }
742
817
  return null;
743
818
  };
744
- function dotGetSchema_default(schema, path) {
745
- if (isNil(schema)) {
819
+ function dotGetSchema_default(schema2, path) {
820
+ if (isNil(schema2)) {
746
821
  throw new Error("You need to pass a schema");
747
822
  }
748
- return dotGet({ type: schema }, path);
823
+ return dotGet({ type: schema2 }, path);
749
824
  }
750
825
 
751
826
  // src/dotGetSchema.ts
@@ -755,9 +830,9 @@ var dotGetSchema_default2 = dotGetSchema_default;
755
830
  var defaultOptions3 = {
756
831
  filter: true
757
832
  };
758
- async function cleanKey_default(schema, key, value, passedOptions = {}, ...args) {
833
+ async function cleanKey_default(schema2, key, value, passedOptions = {}, ...args) {
759
834
  const options = { ...defaultOptions3, ...passedOptions };
760
- const keySchema = dotGetSchema_default2(schema, key);
835
+ const keySchema = dotGetSchema_default2(schema2, key);
761
836
  if (!keySchema) {
762
837
  if (options.filter) {
763
838
  return;
@@ -773,9 +848,9 @@ async function cleanKey_default(schema, key, value, passedOptions = {}, ...args)
773
848
  var defaultOptions4 = {
774
849
  filter: false
775
850
  };
776
- async function validateKey_default(schema, key, value, passedOptions = {}, ...args) {
851
+ async function validateKey_default(schema2, key, value, passedOptions = {}, ...args) {
777
852
  const options = { ...defaultOptions4, ...passedOptions };
778
- const keySchema = dotGetSchema_default(schema, key);
853
+ const keySchema = dotGetSchema_default(schema2, key);
779
854
  if (!keySchema) {
780
855
  if (options.filter) {
781
856
  return Errors_default.NOT_IN_SCHEMA;
@@ -797,121 +872,101 @@ async function validateKey_default(schema, key, value, passedOptions = {}, ...ar
797
872
 
798
873
  // src/fieldTypes/enum.ts
799
874
  function createEnum(name, values) {
800
- return {
801
- type: values[0],
802
- ...fieldType({
803
- name: "enum",
804
- meta: {
805
- enumName: name,
806
- enumValues: values
807
- },
808
- toGraphQLType: (GraphQL) => {
809
- global.GraphQLEnums = global.GraphQLEnums || {};
810
- global.GraphQLEnums[name] = global.GraphQLEnums[name] || new GraphQL.GraphQLEnumType({
811
- name,
812
- values: values.reduce((result, value) => {
813
- result[value] = { value };
814
- return result;
815
- }, {})
816
- });
817
- return global.GraphQLEnums[name];
818
- },
819
- validate(value, { currentSchema }) {
820
- if (typeof value !== "string") return Errors_default.NOT_A_STRING;
821
- if (!values.includes(value)) {
822
- return Errors_default.NOT_AN_ALLOWED_VALUE;
823
- }
824
- if (value === "" && !currentSchema.optional) {
825
- return Errors_default.REQUIRED;
826
- }
827
- },
828
- clean(value, { options: { autoConvert, trimStrings, removeEmptyStrings } }) {
829
- if (autoConvert) {
830
- value = String(value);
831
- }
832
- if (trimStrings) {
833
- value = value.trim();
834
- }
835
- if (removeEmptyStrings && value === "") {
836
- return void 0;
837
- }
838
- return value;
875
+ return fieldType({
876
+ name: "enum",
877
+ meta: {
878
+ enumName: name,
879
+ enumValues: values
880
+ },
881
+ toGraphQLType: (GraphQL) => {
882
+ global.GraphQLEnums = global.GraphQLEnums || {};
883
+ global.GraphQLEnums[name] = global.GraphQLEnums[name] || new GraphQL.GraphQLEnumType({
884
+ name,
885
+ values: values.reduce((result, value) => {
886
+ result[value] = { value };
887
+ return result;
888
+ }, {})
889
+ });
890
+ return global.GraphQLEnums[name];
891
+ },
892
+ validate(value, { currentSchema }) {
893
+ if (typeof value !== "string") return Errors_default.NOT_A_STRING;
894
+ if (!values.includes(value)) {
895
+ return Errors_default.NOT_AN_ALLOWED_VALUE;
839
896
  }
840
- })
841
- };
897
+ if (value === "" && !currentSchema.optional) {
898
+ return Errors_default.REQUIRED;
899
+ }
900
+ },
901
+ clean(value, { options: { autoConvert, trimStrings, removeEmptyStrings } }) {
902
+ if (autoConvert) {
903
+ value = String(value);
904
+ }
905
+ if (trimStrings) {
906
+ value = value.trim();
907
+ }
908
+ if (removeEmptyStrings && value === "") {
909
+ return void 0;
910
+ }
911
+ return value;
912
+ }
913
+ });
842
914
  }
843
915
 
844
- // src/models.ts
845
- Symbol.metadata ?? (Symbol.metadata = Symbol("Symbol.metadata"));
846
- function isSchemaLike(type2) {
847
- var _a;
848
- if (!type2) return false;
849
- if (objectHasSubObjectWithKey(type2, "type")) return true;
850
- if ((_a = type2 == null ? void 0 : type2[Symbol.metadata]) == null ? void 0 : _a._getModel) return true;
851
- if (type2.getModel) return true;
852
- if (type2.getSchema) return true;
853
- if (type2.getCleanSchema) return true;
854
- if (type2.__isModel) return true;
855
- if (type2.__modelName) return true;
856
- return false;
857
- }
858
- function isSchemaOrFieldLike(type2) {
859
- if (Array.isArray(type2)) {
860
- if (type2.length !== 1) return false;
861
- return isSchemaOrFieldLike(type2[0]);
862
- }
863
- if (isSchemaLike(type2)) return true;
864
- try {
865
- if (getFieldValidator(type2)) return true;
866
- } catch {
867
- return false;
868
- }
869
- return false;
870
- }
871
- function getSchemaModelName(type2) {
872
- if (!type2) return null;
873
- if (type2.__modelName) return type2.__modelName;
874
- if (type2.getModel) return type2.getModel().name;
875
- if (type2.getSchema) return type2.getSchema().__modelName;
876
- return null;
877
- }
878
- function getSchemaFromAnyOrionForm(type2) {
879
- var _a, _b;
880
- if ((_a = type2 == null ? void 0 : type2[Symbol.metadata]) == null ? void 0 : _a._getModel) {
881
- return (_b = type2 == null ? void 0 : type2[Symbol.metadata]) == null ? void 0 : _b._getModel().getSchema();
916
+ // src/types/fields.ts
917
+ var subSchema = {
918
+ name: {
919
+ type: String
882
920
  }
883
- if (type2 == null ? void 0 : type2.getModel) return type2.getModel().getSchema();
884
- if (type2.getSchema) {
885
- return type2.getSchema();
886
- }
887
- if (type2.getSchema) {
888
- return type2.getSchema();
889
- }
890
- if (objectHasSubObjectWithKey(type2, "type")) return type2;
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
- }
921
+ };
922
+ var schema = {
923
+ filter: {
924
+ type: String
925
+ },
926
+ sub: {
927
+ type: subSchema
928
+ },
929
+ gender: {
930
+ type: createEnum("gender", ["male", "female"])
931
+ },
932
+ page: {
933
+ type: "integer",
934
+ defaultValue: 1,
935
+ min: 1
936
+ },
937
+ limit: {
938
+ type: "integer",
939
+ defaultValue: 0,
940
+ min: 0,
941
+ max: 200
942
+ },
943
+ sortBy: {
944
+ type: String,
945
+ optional: true
946
+ },
947
+ sortType: {
948
+ type: String,
949
+ allowedValues: ["asc", "desc"],
950
+ optional: true
900
951
  }
901
- return false;
902
- }
903
- function getSchemaWithMetadataFromAnyOrionForm(type2) {
904
- return getSchemaFromAnyOrionForm(type2);
952
+ };
953
+
954
+ // src/cleanAndValidate.ts
955
+ async function cleanAndValidate(schema2, doc) {
956
+ const cleaned = await clean2(schema2, doc);
957
+ await validate(schema2, cleaned);
958
+ return cleaned;
905
959
  }
906
960
 
907
961
  // src/schemaWithName/index.ts
908
- function schemaWithName(name, schema) {
909
- schema.__modelName = name;
910
- return schema;
962
+ function schemaWithName(name, schema2) {
963
+ schema2.__modelName = name;
964
+ return schema2;
911
965
  }
912
966
  export {
913
967
  ValidationError,
914
968
  clean2 as clean,
969
+ cleanAndValidate,
915
970
  cleanKey_default as cleanKey,
916
971
  createEnum,
917
972
  dotGetSchema_default2 as dotGetSchema,
@@ -923,6 +978,7 @@ export {
923
978
  getValidationErrors2 as getValidationErrors,
924
979
  isSchemaLike,
925
980
  isSchemaOrFieldLike,
981
+ isStrictSchemaLike,
926
982
  isValid,
927
983
  schemaWithName,
928
984
  validate,