phecda-core 5.0.0 → 5.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.d.mts CHANGED
@@ -85,8 +85,8 @@ interface RuleArgs {
85
85
  property: string;
86
86
  value: any;
87
87
  /**
88
- * paramIndex work for params' @Rule
89
- */
88
+ * paramIndex work for params' @Rule
89
+ */
90
90
  index?: number;
91
91
  meta: any;
92
92
  }
@@ -95,9 +95,9 @@ declare function Required(target: any, property: PropertyKey, index?: any): void
95
95
  declare function Optional(target: any, property: PropertyKey, index?: any): void;
96
96
  declare function Min(min: number): (target: any, property?: PropertyKey, index?: any) => void;
97
97
  declare function Max(max: number): (target: any, property?: PropertyKey, index?: any) => void;
98
- declare function Nested(model: Construct): (target: any, property: string) => void;
99
- declare function OneOf(...models: Construct[]): (target: any, property: string) => void;
100
- declare function Enum(map: Record<string, any>): (target: any, property: string) => void;
98
+ declare function Nested(model: Construct): (target: any, property: string, index?: any) => void;
99
+ declare function OneOf(...validations: (Construct | ((args: RuleArgs) => boolean | Promise<boolean>))[]): (target: any, property: string, index?: any) => void;
100
+ declare function Enum(map: Record<string, any>): (target: any, property: string, index?: any) => void;
101
101
 
102
102
  declare function getTag<M extends Construct | AbConstruct>(moduleOrInstance: M | InstanceType<M>): PropertyKey;
103
103
  /**
package/dist/index.d.ts CHANGED
@@ -85,8 +85,8 @@ interface RuleArgs {
85
85
  property: string;
86
86
  value: any;
87
87
  /**
88
- * paramIndex work for params' @Rule
89
- */
88
+ * paramIndex work for params' @Rule
89
+ */
90
90
  index?: number;
91
91
  meta: any;
92
92
  }
@@ -95,9 +95,9 @@ declare function Required(target: any, property: PropertyKey, index?: any): void
95
95
  declare function Optional(target: any, property: PropertyKey, index?: any): void;
96
96
  declare function Min(min: number): (target: any, property?: PropertyKey, index?: any) => void;
97
97
  declare function Max(max: number): (target: any, property?: PropertyKey, index?: any) => void;
98
- declare function Nested(model: Construct): (target: any, property: string) => void;
99
- declare function OneOf(...models: Construct[]): (target: any, property: string) => void;
100
- declare function Enum(map: Record<string, any>): (target: any, property: string) => void;
98
+ declare function Nested(model: Construct): (target: any, property: string, index?: any) => void;
99
+ declare function OneOf(...validations: (Construct | ((args: RuleArgs) => boolean | Promise<boolean>))[]): (target: any, property: string, index?: any) => void;
100
+ declare function Enum(map: Record<string, any>): (target: any, property: string, index?: any) => void;
101
101
 
102
102
  declare function getTag<M extends Construct | AbConstruct>(moduleOrInstance: M | InstanceType<M>): PropertyKey;
103
103
  /**
package/dist/index.js CHANGED
@@ -638,24 +638,24 @@ function Max(max) {
638
638
  }
639
639
  __name(Max, "Max");
640
640
  function Nested(model) {
641
- return (target, property) => {
642
- setMeta(target, property, void 0, {
641
+ return (target, property, index) => {
642
+ setMeta(target, property, index, {
643
643
  nested: model
644
644
  });
645
645
  };
646
646
  }
647
647
  __name(Nested, "Nested");
648
- function OneOf(...models) {
649
- return (target, property) => {
650
- setMeta(target, property, void 0, {
651
- oneOf: models
648
+ function OneOf(...validations) {
649
+ return (target, property, index) => {
650
+ setMeta(target, property, index, {
651
+ oneOf: validations
652
652
  });
653
653
  };
654
654
  }
655
655
  __name(OneOf, "OneOf");
656
656
  function Enum(map) {
657
- return (target, property) => {
658
- setMeta(target, property, void 0, {
657
+ return (target, property, index) => {
658
+ setMeta(target, property, index, {
659
659
  enum: map
660
660
  });
661
661
  };
@@ -735,7 +735,7 @@ var _createErrorMessage = /* @__PURE__ */ __name((type, { property, meta }) => {
735
735
  case "boolean":
736
736
  return `must be a boolean for "${property}"`;
737
737
  case "oneOf":
738
- return `must be one of models(${meta.oneof.map((m) => getTag(m)).join(", ")}) for "${property}"`;
738
+ return `must pass one of these validations for "${property}"`;
739
739
  case "min":
740
740
  return `must be greater than ${meta.min} for "${property}"`;
741
741
  case "max":
@@ -764,6 +764,10 @@ __name(isObject, "isObject");
764
764
  async function validate(model, data, collectErrors = false, createErrMsg = _createErrorMessage) {
765
765
  async function parse(model2, data2) {
766
766
  const errors = [];
767
+ if (!isObject(data2)) {
768
+ errors.push("data must be an object");
769
+ return errors;
770
+ }
767
771
  for (const key of getMetaKey(model2)) {
768
772
  const meta = getMergedMeta(model2, key);
769
773
  const property = key === SHARE_KEY ? "" : key;
@@ -820,18 +824,30 @@ async function validate(model, data, collectErrors = false, createErrMsg = _crea
820
824
  if (oneOf) {
821
825
  let isCorrect = false;
822
826
  for (const modelOrRule of oneOf) {
823
- if (isPhecda(modelOrRule)) {
824
- const errs = await validate(modelOrRule, value2);
825
- if (!errs.length) {
826
- isCorrect = true;
827
+ switch (modelOrRule) {
828
+ case String:
829
+ if (typeof value2 === "string") isCorrect = true;
827
830
  break;
828
- }
829
- } else if (typeof modelOrRule === "function") {
830
- const errs = await modelOrRule(args2);
831
- if (!errs.length) {
832
- isCorrect = true;
831
+ case Number:
832
+ if (typeof value2 === "number") isCorrect = true;
833
833
  break;
834
- }
834
+ case Boolean:
835
+ if (typeof value2 === "boolean") isCorrect = true;
836
+ break;
837
+ default:
838
+ if (isPhecda(modelOrRule)) {
839
+ const errs = await validate(modelOrRule, value2);
840
+ if (!errs.length) {
841
+ isCorrect = true;
842
+ break;
843
+ }
844
+ } else if (typeof modelOrRule === "function") {
845
+ const ret = await modelOrRule(args2);
846
+ if (ret) {
847
+ isCorrect = true;
848
+ break;
849
+ }
850
+ }
835
851
  }
836
852
  }
837
853
  if (!isCorrect) return createErrMsg("oneOf", args2);
package/dist/index.mjs CHANGED
@@ -552,24 +552,24 @@ function Max(max) {
552
552
  }
553
553
  __name(Max, "Max");
554
554
  function Nested(model) {
555
- return (target, property) => {
556
- setMeta(target, property, void 0, {
555
+ return (target, property, index) => {
556
+ setMeta(target, property, index, {
557
557
  nested: model
558
558
  });
559
559
  };
560
560
  }
561
561
  __name(Nested, "Nested");
562
- function OneOf(...models) {
563
- return (target, property) => {
564
- setMeta(target, property, void 0, {
565
- oneOf: models
562
+ function OneOf(...validations) {
563
+ return (target, property, index) => {
564
+ setMeta(target, property, index, {
565
+ oneOf: validations
566
566
  });
567
567
  };
568
568
  }
569
569
  __name(OneOf, "OneOf");
570
570
  function Enum(map) {
571
- return (target, property) => {
572
- setMeta(target, property, void 0, {
571
+ return (target, property, index) => {
572
+ setMeta(target, property, index, {
573
573
  enum: map
574
574
  });
575
575
  };
@@ -649,7 +649,7 @@ var _createErrorMessage = /* @__PURE__ */ __name((type, { property, meta }) => {
649
649
  case "boolean":
650
650
  return `must be a boolean for "${property}"`;
651
651
  case "oneOf":
652
- return `must be one of models(${meta.oneof.map((m) => getTag(m)).join(", ")}) for "${property}"`;
652
+ return `must pass one of these validations for "${property}"`;
653
653
  case "min":
654
654
  return `must be greater than ${meta.min} for "${property}"`;
655
655
  case "max":
@@ -678,6 +678,10 @@ __name(isObject, "isObject");
678
678
  async function validate(model, data, collectErrors = false, createErrMsg = _createErrorMessage) {
679
679
  async function parse(model2, data2) {
680
680
  const errors = [];
681
+ if (!isObject(data2)) {
682
+ errors.push("data must be an object");
683
+ return errors;
684
+ }
681
685
  for (const key of getMetaKey(model2)) {
682
686
  const meta = getMergedMeta(model2, key);
683
687
  const property = key === SHARE_KEY ? "" : key;
@@ -734,18 +738,30 @@ async function validate(model, data, collectErrors = false, createErrMsg = _crea
734
738
  if (oneOf) {
735
739
  let isCorrect = false;
736
740
  for (const modelOrRule of oneOf) {
737
- if (isPhecda(modelOrRule)) {
738
- const errs = await validate(modelOrRule, value2);
739
- if (!errs.length) {
740
- isCorrect = true;
741
+ switch (modelOrRule) {
742
+ case String:
743
+ if (typeof value2 === "string") isCorrect = true;
741
744
  break;
742
- }
743
- } else if (typeof modelOrRule === "function") {
744
- const errs = await modelOrRule(args2);
745
- if (!errs.length) {
746
- isCorrect = true;
745
+ case Number:
746
+ if (typeof value2 === "number") isCorrect = true;
747
747
  break;
748
- }
748
+ case Boolean:
749
+ if (typeof value2 === "boolean") isCorrect = true;
750
+ break;
751
+ default:
752
+ if (isPhecda(modelOrRule)) {
753
+ const errs = await validate(modelOrRule, value2);
754
+ if (!errs.length) {
755
+ isCorrect = true;
756
+ break;
757
+ }
758
+ } else if (typeof modelOrRule === "function") {
759
+ const ret = await modelOrRule(args2);
760
+ if (ret) {
761
+ isCorrect = true;
762
+ break;
763
+ }
764
+ }
749
765
  }
750
766
  }
751
767
  if (!isCorrect) return createErrMsg("oneOf", args2);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "phecda-core",
3
- "version": "5.0.0",
3
+ "version": "5.0.1",
4
4
  "description": "provide base function and abstract limit to other phecda module ",
5
5
  "author": "fgsreally",
6
6
  "license": "MIT",