phecda-core 5.0.1 → 5.1.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
@@ -98,6 +98,7 @@ declare function Max(max: number): (target: any, property?: PropertyKey, index?:
98
98
  declare function Nested(model: Construct): (target: any, property: string, index?: any) => void;
99
99
  declare function OneOf(...validations: (Construct | ((args: RuleArgs) => boolean | Promise<boolean>))[]): (target: any, property: string, index?: any) => void;
100
100
  declare function Enum(map: Record<string, any>): (target: any, property: string, index?: any) => void;
101
+ declare function Const(value: string | number | boolean | null | undefined): (target: any, property: string, index?: any) => void;
101
102
 
102
103
  declare function getTag<M extends Construct | AbConstruct>(moduleOrInstance: M | InstanceType<M>): PropertyKey;
103
104
  /**
@@ -143,6 +144,6 @@ declare abstract class Base {
143
144
  }
144
145
 
145
146
  declare const _createErrorMessage: (type: string, { property, meta }: RuleArgs) => string;
146
- declare function validate(model: Construct, data: any, collectErrors?: boolean, createErrMsg?: (type: string, { property, meta }: RuleArgs) => string): Promise<any[]>;
147
+ declare function validate(model: Construct, data: any, collectErrors?: boolean, createErrMsg?: (type: string, { property, meta }: RuleArgs) => string, equalFn?: (a: any, b: any) => boolean): Promise<any[]>;
147
148
 
148
- export { type AbConstruct, Assign, Base, CLEAR_KEY, type ClassValue, Clear, type Construct, DataMap, Doc, Effect, Empty, Enum, Err, type Events, Expose, Global, If, Init, Inject, type InjectData, Injectable, Isolate, Max, Min, type NameSpace, Nested, OneOf, Optional, PHECDA_KEY, type Phecda, Pipeline, Provide, Required, Rule, type RuleArgs, SHARE_KEY, Storage, type StorageParam, Tag, Unique, Unmount, Watcher, type WatcherParam, _createErrorMessage, activeInstance, addDecoToClass, functionToClass, get, getInject, getMergedMeta, getMeta, getMetaKey, getMetaParams, getOwnMeta, getOwnMetaKey, getOwnMetaParams, getPhecdaFromTarget, getTag, init, invoke, invokeInit, invokeUnmount, isAsyncFunc, isPhecda, objectToClass, omit, override, partial, pick, set, setInject, setMeta, validate, wait };
149
+ export { type AbConstruct, Assign, Base, CLEAR_KEY, type ClassValue, Clear, Const, type Construct, DataMap, Doc, Effect, Empty, Enum, Err, type Events, Expose, Global, If, Init, Inject, type InjectData, Injectable, Isolate, Max, Min, type NameSpace, Nested, OneOf, Optional, PHECDA_KEY, type Phecda, Pipeline, Provide, Required, Rule, type RuleArgs, SHARE_KEY, Storage, type StorageParam, Tag, Unique, Unmount, Watcher, type WatcherParam, _createErrorMessage, activeInstance, addDecoToClass, functionToClass, get, getInject, getMergedMeta, getMeta, getMetaKey, getMetaParams, getOwnMeta, getOwnMetaKey, getOwnMetaParams, getPhecdaFromTarget, getTag, init, invoke, invokeInit, invokeUnmount, isAsyncFunc, isPhecda, objectToClass, omit, override, partial, pick, set, setInject, setMeta, validate, wait };
package/dist/index.d.ts CHANGED
@@ -98,6 +98,7 @@ declare function Max(max: number): (target: any, property?: PropertyKey, index?:
98
98
  declare function Nested(model: Construct): (target: any, property: string, index?: any) => void;
99
99
  declare function OneOf(...validations: (Construct | ((args: RuleArgs) => boolean | Promise<boolean>))[]): (target: any, property: string, index?: any) => void;
100
100
  declare function Enum(map: Record<string, any>): (target: any, property: string, index?: any) => void;
101
+ declare function Const(value: string | number | boolean | null | undefined): (target: any, property: string, index?: any) => void;
101
102
 
102
103
  declare function getTag<M extends Construct | AbConstruct>(moduleOrInstance: M | InstanceType<M>): PropertyKey;
103
104
  /**
@@ -143,6 +144,6 @@ declare abstract class Base {
143
144
  }
144
145
 
145
146
  declare const _createErrorMessage: (type: string, { property, meta }: RuleArgs) => string;
146
- declare function validate(model: Construct, data: any, collectErrors?: boolean, createErrMsg?: (type: string, { property, meta }: RuleArgs) => string): Promise<any[]>;
147
+ declare function validate(model: Construct, data: any, collectErrors?: boolean, createErrMsg?: (type: string, { property, meta }: RuleArgs) => string, equalFn?: (a: any, b: any) => boolean): Promise<any[]>;
147
148
 
148
- export { type AbConstruct, Assign, Base, CLEAR_KEY, type ClassValue, Clear, type Construct, DataMap, Doc, Effect, Empty, Enum, Err, type Events, Expose, Global, If, Init, Inject, type InjectData, Injectable, Isolate, Max, Min, type NameSpace, Nested, OneOf, Optional, PHECDA_KEY, type Phecda, Pipeline, Provide, Required, Rule, type RuleArgs, SHARE_KEY, Storage, type StorageParam, Tag, Unique, Unmount, Watcher, type WatcherParam, _createErrorMessage, activeInstance, addDecoToClass, functionToClass, get, getInject, getMergedMeta, getMeta, getMetaKey, getMetaParams, getOwnMeta, getOwnMetaKey, getOwnMetaParams, getPhecdaFromTarget, getTag, init, invoke, invokeInit, invokeUnmount, isAsyncFunc, isPhecda, objectToClass, omit, override, partial, pick, set, setInject, setMeta, validate, wait };
149
+ export { type AbConstruct, Assign, Base, CLEAR_KEY, type ClassValue, Clear, Const, type Construct, DataMap, Doc, Effect, Empty, Enum, Err, type Events, Expose, Global, If, Init, Inject, type InjectData, Injectable, Isolate, Max, Min, type NameSpace, Nested, OneOf, Optional, PHECDA_KEY, type Phecda, Pipeline, Provide, Required, Rule, type RuleArgs, SHARE_KEY, Storage, type StorageParam, Tag, Unique, Unmount, Watcher, type WatcherParam, _createErrorMessage, activeInstance, addDecoToClass, functionToClass, get, getInject, getMergedMeta, getMeta, getMetaKey, getMetaParams, getOwnMeta, getOwnMetaKey, getOwnMetaParams, getPhecdaFromTarget, getTag, init, invoke, invokeInit, invokeUnmount, isAsyncFunc, isPhecda, objectToClass, omit, override, partial, pick, set, setInject, setMeta, validate, wait };
package/dist/index.js CHANGED
@@ -26,6 +26,7 @@ __export(index_exports, {
26
26
  Base: () => Base,
27
27
  CLEAR_KEY: () => CLEAR_KEY,
28
28
  Clear: () => Clear,
29
+ Const: () => Const,
29
30
  DataMap: () => DataMap,
30
31
  Doc: () => Doc,
31
32
  Effect: () => Effect,
@@ -661,6 +662,14 @@ function Enum(map) {
661
662
  };
662
663
  }
663
664
  __name(Enum, "Enum");
665
+ function Const(value) {
666
+ return (target, property, index) => {
667
+ setMeta(target, property, index, {
668
+ const: value
669
+ });
670
+ };
671
+ }
672
+ __name(Const, "Const");
664
673
 
665
674
  // src/base.ts
666
675
  function _ts_decorate(decorators, target, key, desc) {
@@ -728,6 +737,8 @@ Base = _ts_decorate([
728
737
  // src/validate.ts
729
738
  var _createErrorMessage = /* @__PURE__ */ __name((type, { property, meta }) => {
730
739
  switch (type) {
740
+ case "const":
741
+ return `must be ${meta.const} for "${property}"`;
731
742
  case "string":
732
743
  return `must be a string for "${property}"`;
733
744
  case "number":
@@ -761,7 +772,7 @@ function isObject(value) {
761
772
  return Object.prototype.toString.call(value) === "[object Object]";
762
773
  }
763
774
  __name(isObject, "isObject");
764
- async function validate(model, data, collectErrors = false, createErrMsg = _createErrorMessage) {
775
+ async function validate(model, data, collectErrors = false, createErrMsg = _createErrorMessage, equalFn = (a, b) => a === b) {
765
776
  async function parse(model2, data2) {
766
777
  const errors = [];
767
778
  if (!isObject(data2)) {
@@ -777,6 +788,9 @@ async function validate(model, data, collectErrors = false, createErrMsg = _crea
777
788
  const allRules = [
778
789
  async (args2) => {
779
790
  const { value: value2 } = args2;
791
+ if ("const" in meta) {
792
+ if (!equalFn(value2, meta.const)) return createErrMsg("const", args2);
793
+ }
780
794
  if (required === false && value2 === void 0) return true;
781
795
  if (required !== false && value2 === void 0) return createErrMsg("required", args2);
782
796
  if (type === String && typeof value2 !== "string") return createErrMsg("string", args2);
@@ -823,8 +837,8 @@ async function validate(model, data, collectErrors = false, createErrMsg = _crea
823
837
  }
824
838
  if (oneOf) {
825
839
  let isCorrect = false;
826
- for (const modelOrRule of oneOf) {
827
- switch (modelOrRule) {
840
+ for (const item of oneOf) {
841
+ switch (item) {
828
842
  case String:
829
843
  if (typeof value2 === "string") isCorrect = true;
830
844
  break;
@@ -835,18 +849,23 @@ async function validate(model, data, collectErrors = false, createErrMsg = _crea
835
849
  if (typeof value2 === "boolean") isCorrect = true;
836
850
  break;
837
851
  default:
838
- if (isPhecda(modelOrRule)) {
839
- const errs = await validate(modelOrRule, value2);
852
+ if (isPhecda(item)) {
853
+ const errs = await validate(item, value2);
840
854
  if (!errs.length) {
841
855
  isCorrect = true;
842
856
  break;
843
857
  }
844
- } else if (typeof modelOrRule === "function") {
845
- const ret = await modelOrRule(args2);
858
+ } else if (typeof item === "function") {
859
+ const ret = await item(args2);
846
860
  if (ret) {
847
861
  isCorrect = true;
848
862
  break;
849
863
  }
864
+ } else {
865
+ if (equalFn(value2, item)) {
866
+ isCorrect = true;
867
+ break;
868
+ }
850
869
  }
851
870
  }
852
871
  }
@@ -883,6 +902,7 @@ __name(validate, "validate");
883
902
  Base,
884
903
  CLEAR_KEY,
885
904
  Clear,
905
+ Const,
886
906
  DataMap,
887
907
  Doc,
888
908
  Effect,
package/dist/index.mjs CHANGED
@@ -575,6 +575,14 @@ function Enum(map) {
575
575
  };
576
576
  }
577
577
  __name(Enum, "Enum");
578
+ function Const(value) {
579
+ return (target, property, index) => {
580
+ setMeta(target, property, index, {
581
+ const: value
582
+ });
583
+ };
584
+ }
585
+ __name(Const, "Const");
578
586
 
579
587
  // src/base.ts
580
588
  function _ts_decorate(decorators, target, key, desc) {
@@ -642,6 +650,8 @@ Base = _ts_decorate([
642
650
  // src/validate.ts
643
651
  var _createErrorMessage = /* @__PURE__ */ __name((type, { property, meta }) => {
644
652
  switch (type) {
653
+ case "const":
654
+ return `must be ${meta.const} for "${property}"`;
645
655
  case "string":
646
656
  return `must be a string for "${property}"`;
647
657
  case "number":
@@ -675,7 +685,7 @@ function isObject(value) {
675
685
  return Object.prototype.toString.call(value) === "[object Object]";
676
686
  }
677
687
  __name(isObject, "isObject");
678
- async function validate(model, data, collectErrors = false, createErrMsg = _createErrorMessage) {
688
+ async function validate(model, data, collectErrors = false, createErrMsg = _createErrorMessage, equalFn = (a, b) => a === b) {
679
689
  async function parse(model2, data2) {
680
690
  const errors = [];
681
691
  if (!isObject(data2)) {
@@ -691,6 +701,9 @@ async function validate(model, data, collectErrors = false, createErrMsg = _crea
691
701
  const allRules = [
692
702
  async (args2) => {
693
703
  const { value: value2 } = args2;
704
+ if ("const" in meta) {
705
+ if (!equalFn(value2, meta.const)) return createErrMsg("const", args2);
706
+ }
694
707
  if (required === false && value2 === void 0) return true;
695
708
  if (required !== false && value2 === void 0) return createErrMsg("required", args2);
696
709
  if (type === String && typeof value2 !== "string") return createErrMsg("string", args2);
@@ -737,8 +750,8 @@ async function validate(model, data, collectErrors = false, createErrMsg = _crea
737
750
  }
738
751
  if (oneOf) {
739
752
  let isCorrect = false;
740
- for (const modelOrRule of oneOf) {
741
- switch (modelOrRule) {
753
+ for (const item of oneOf) {
754
+ switch (item) {
742
755
  case String:
743
756
  if (typeof value2 === "string") isCorrect = true;
744
757
  break;
@@ -749,18 +762,23 @@ async function validate(model, data, collectErrors = false, createErrMsg = _crea
749
762
  if (typeof value2 === "boolean") isCorrect = true;
750
763
  break;
751
764
  default:
752
- if (isPhecda(modelOrRule)) {
753
- const errs = await validate(modelOrRule, value2);
765
+ if (isPhecda(item)) {
766
+ const errs = await validate(item, value2);
754
767
  if (!errs.length) {
755
768
  isCorrect = true;
756
769
  break;
757
770
  }
758
- } else if (typeof modelOrRule === "function") {
759
- const ret = await modelOrRule(args2);
771
+ } else if (typeof item === "function") {
772
+ const ret = await item(args2);
760
773
  if (ret) {
761
774
  isCorrect = true;
762
775
  break;
763
776
  }
777
+ } else {
778
+ if (equalFn(value2, item)) {
779
+ isCorrect = true;
780
+ break;
781
+ }
764
782
  }
765
783
  }
766
784
  }
@@ -796,6 +814,7 @@ export {
796
814
  Base,
797
815
  CLEAR_KEY,
798
816
  Clear,
817
+ Const,
799
818
  DataMap,
800
819
  Doc,
801
820
  Effect,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "phecda-core",
3
- "version": "5.0.1",
3
+ "version": "5.1.1",
4
4
  "description": "provide base function and abstract limit to other phecda module ",
5
5
  "author": "fgsreally",
6
6
  "license": "MIT",
@@ -9,14 +9,27 @@
9
9
  "phecda",
10
10
  "core"
11
11
  ],
12
+ "exports": {
13
+ ".": {
14
+ "require": "./dist/index.js",
15
+ "import": "./dist/index.mjs"
16
+ }
17
+ },
12
18
  "main": "dist/index.js",
13
19
  "module": "dist/index.mjs",
14
20
  "types": "dist/index.d.ts",
21
+ "typesVersions": {
22
+ "*": {
23
+ ".": [
24
+ "dist/index.d.ts"
25
+ ]
26
+ }
27
+ },
15
28
  "files": [
16
29
  "dist"
17
30
  ],
18
31
  "devDependencies": {
19
- "tsup": "^8.1.0"
32
+ "tsup": "^8.5.0"
20
33
  },
21
34
  "dependencies": {
22
35
  "reflect-metadata": "^0.1.14"