phecda-core 5.0.0 → 5.1.0
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 +8 -7
- package/dist/index.d.ts +8 -7
- package/dist/index.js +57 -21
- package/dist/index.mjs +56 -21
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -85,8 +85,8 @@ interface RuleArgs {
|
|
|
85
85
|
property: string;
|
|
86
86
|
value: any;
|
|
87
87
|
/**
|
|
88
|
-
|
|
89
|
-
|
|
88
|
+
* paramIndex work for params' @Rule
|
|
89
|
+
*/
|
|
90
90
|
index?: number;
|
|
91
91
|
meta: any;
|
|
92
92
|
}
|
|
@@ -95,9 +95,10 @@ 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(...
|
|
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
|
+
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
|
@@ -85,8 +85,8 @@ interface RuleArgs {
|
|
|
85
85
|
property: string;
|
|
86
86
|
value: any;
|
|
87
87
|
/**
|
|
88
|
-
|
|
89
|
-
|
|
88
|
+
* paramIndex work for params' @Rule
|
|
89
|
+
*/
|
|
90
90
|
index?: number;
|
|
91
91
|
meta: any;
|
|
92
92
|
}
|
|
@@ -95,9 +95,10 @@ 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(...
|
|
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
|
+
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,
|
|
@@ -638,29 +639,37 @@ function Max(max) {
|
|
|
638
639
|
}
|
|
639
640
|
__name(Max, "Max");
|
|
640
641
|
function Nested(model) {
|
|
641
|
-
return (target, property) => {
|
|
642
|
-
setMeta(target, property,
|
|
642
|
+
return (target, property, index) => {
|
|
643
|
+
setMeta(target, property, index, {
|
|
643
644
|
nested: model
|
|
644
645
|
});
|
|
645
646
|
};
|
|
646
647
|
}
|
|
647
648
|
__name(Nested, "Nested");
|
|
648
|
-
function OneOf(...
|
|
649
|
-
return (target, property) => {
|
|
650
|
-
setMeta(target, property,
|
|
651
|
-
oneOf:
|
|
649
|
+
function OneOf(...validations) {
|
|
650
|
+
return (target, property, index) => {
|
|
651
|
+
setMeta(target, property, index, {
|
|
652
|
+
oneOf: validations
|
|
652
653
|
});
|
|
653
654
|
};
|
|
654
655
|
}
|
|
655
656
|
__name(OneOf, "OneOf");
|
|
656
657
|
function Enum(map) {
|
|
657
|
-
return (target, property) => {
|
|
658
|
-
setMeta(target, property,
|
|
658
|
+
return (target, property, index) => {
|
|
659
|
+
setMeta(target, property, index, {
|
|
659
660
|
enum: map
|
|
660
661
|
});
|
|
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":
|
|
@@ -735,7 +746,7 @@ var _createErrorMessage = /* @__PURE__ */ __name((type, { property, meta }) => {
|
|
|
735
746
|
case "boolean":
|
|
736
747
|
return `must be a boolean for "${property}"`;
|
|
737
748
|
case "oneOf":
|
|
738
|
-
return `must
|
|
749
|
+
return `must pass one of these validations for "${property}"`;
|
|
739
750
|
case "min":
|
|
740
751
|
return `must be greater than ${meta.min} for "${property}"`;
|
|
741
752
|
case "max":
|
|
@@ -761,9 +772,13 @@ 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 = [];
|
|
778
|
+
if (!isObject(data2)) {
|
|
779
|
+
errors.push("data must be an object");
|
|
780
|
+
return errors;
|
|
781
|
+
}
|
|
767
782
|
for (const key of getMetaKey(model2)) {
|
|
768
783
|
const meta = getMergedMeta(model2, key);
|
|
769
784
|
const property = key === SHARE_KEY ? "" : key;
|
|
@@ -773,6 +788,9 @@ async function validate(model, data, collectErrors = false, createErrMsg = _crea
|
|
|
773
788
|
const allRules = [
|
|
774
789
|
async (args2) => {
|
|
775
790
|
const { value: value2 } = args2;
|
|
791
|
+
if ("const" in meta) {
|
|
792
|
+
if (!equalFn(value2, meta.const)) return createErrMsg("const", args2);
|
|
793
|
+
}
|
|
776
794
|
if (required === false && value2 === void 0) return true;
|
|
777
795
|
if (required !== false && value2 === void 0) return createErrMsg("required", args2);
|
|
778
796
|
if (type === String && typeof value2 !== "string") return createErrMsg("string", args2);
|
|
@@ -819,19 +837,36 @@ async function validate(model, data, collectErrors = false, createErrMsg = _crea
|
|
|
819
837
|
}
|
|
820
838
|
if (oneOf) {
|
|
821
839
|
let isCorrect = false;
|
|
822
|
-
for (const
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
isCorrect = true;
|
|
840
|
+
for (const item of oneOf) {
|
|
841
|
+
switch (item) {
|
|
842
|
+
case String:
|
|
843
|
+
if (typeof value2 === "string") isCorrect = true;
|
|
827
844
|
break;
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
const errs = await modelOrRule(args2);
|
|
831
|
-
if (!errs.length) {
|
|
832
|
-
isCorrect = true;
|
|
845
|
+
case Number:
|
|
846
|
+
if (typeof value2 === "number") isCorrect = true;
|
|
833
847
|
break;
|
|
834
|
-
|
|
848
|
+
case Boolean:
|
|
849
|
+
if (typeof value2 === "boolean") isCorrect = true;
|
|
850
|
+
break;
|
|
851
|
+
default:
|
|
852
|
+
if (isPhecda(item)) {
|
|
853
|
+
const errs = await validate(item, value2);
|
|
854
|
+
if (!errs.length) {
|
|
855
|
+
isCorrect = true;
|
|
856
|
+
break;
|
|
857
|
+
}
|
|
858
|
+
} else if (typeof item === "function") {
|
|
859
|
+
const ret = await item(args2);
|
|
860
|
+
if (ret) {
|
|
861
|
+
isCorrect = true;
|
|
862
|
+
break;
|
|
863
|
+
}
|
|
864
|
+
} else {
|
|
865
|
+
if (equalFn(value2, item)) {
|
|
866
|
+
isCorrect = true;
|
|
867
|
+
break;
|
|
868
|
+
}
|
|
869
|
+
}
|
|
835
870
|
}
|
|
836
871
|
}
|
|
837
872
|
if (!isCorrect) return createErrMsg("oneOf", args2);
|
|
@@ -867,6 +902,7 @@ __name(validate, "validate");
|
|
|
867
902
|
Base,
|
|
868
903
|
CLEAR_KEY,
|
|
869
904
|
Clear,
|
|
905
|
+
Const,
|
|
870
906
|
DataMap,
|
|
871
907
|
Doc,
|
|
872
908
|
Effect,
|
package/dist/index.mjs
CHANGED
|
@@ -552,29 +552,37 @@ function Max(max) {
|
|
|
552
552
|
}
|
|
553
553
|
__name(Max, "Max");
|
|
554
554
|
function Nested(model) {
|
|
555
|
-
return (target, property) => {
|
|
556
|
-
setMeta(target, property,
|
|
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(...
|
|
563
|
-
return (target, property) => {
|
|
564
|
-
setMeta(target, property,
|
|
565
|
-
oneOf:
|
|
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,
|
|
571
|
+
return (target, property, index) => {
|
|
572
|
+
setMeta(target, property, index, {
|
|
573
573
|
enum: map
|
|
574
574
|
});
|
|
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":
|
|
@@ -649,7 +659,7 @@ var _createErrorMessage = /* @__PURE__ */ __name((type, { property, meta }) => {
|
|
|
649
659
|
case "boolean":
|
|
650
660
|
return `must be a boolean for "${property}"`;
|
|
651
661
|
case "oneOf":
|
|
652
|
-
return `must
|
|
662
|
+
return `must pass one of these validations for "${property}"`;
|
|
653
663
|
case "min":
|
|
654
664
|
return `must be greater than ${meta.min} for "${property}"`;
|
|
655
665
|
case "max":
|
|
@@ -675,9 +685,13 @@ 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 = [];
|
|
691
|
+
if (!isObject(data2)) {
|
|
692
|
+
errors.push("data must be an object");
|
|
693
|
+
return errors;
|
|
694
|
+
}
|
|
681
695
|
for (const key of getMetaKey(model2)) {
|
|
682
696
|
const meta = getMergedMeta(model2, key);
|
|
683
697
|
const property = key === SHARE_KEY ? "" : key;
|
|
@@ -687,6 +701,9 @@ async function validate(model, data, collectErrors = false, createErrMsg = _crea
|
|
|
687
701
|
const allRules = [
|
|
688
702
|
async (args2) => {
|
|
689
703
|
const { value: value2 } = args2;
|
|
704
|
+
if ("const" in meta) {
|
|
705
|
+
if (!equalFn(value2, meta.const)) return createErrMsg("const", args2);
|
|
706
|
+
}
|
|
690
707
|
if (required === false && value2 === void 0) return true;
|
|
691
708
|
if (required !== false && value2 === void 0) return createErrMsg("required", args2);
|
|
692
709
|
if (type === String && typeof value2 !== "string") return createErrMsg("string", args2);
|
|
@@ -733,19 +750,36 @@ async function validate(model, data, collectErrors = false, createErrMsg = _crea
|
|
|
733
750
|
}
|
|
734
751
|
if (oneOf) {
|
|
735
752
|
let isCorrect = false;
|
|
736
|
-
for (const
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
isCorrect = true;
|
|
753
|
+
for (const item of oneOf) {
|
|
754
|
+
switch (item) {
|
|
755
|
+
case String:
|
|
756
|
+
if (typeof value2 === "string") isCorrect = true;
|
|
741
757
|
break;
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
const errs = await modelOrRule(args2);
|
|
745
|
-
if (!errs.length) {
|
|
746
|
-
isCorrect = true;
|
|
758
|
+
case Number:
|
|
759
|
+
if (typeof value2 === "number") isCorrect = true;
|
|
747
760
|
break;
|
|
748
|
-
|
|
761
|
+
case Boolean:
|
|
762
|
+
if (typeof value2 === "boolean") isCorrect = true;
|
|
763
|
+
break;
|
|
764
|
+
default:
|
|
765
|
+
if (isPhecda(item)) {
|
|
766
|
+
const errs = await validate(item, value2);
|
|
767
|
+
if (!errs.length) {
|
|
768
|
+
isCorrect = true;
|
|
769
|
+
break;
|
|
770
|
+
}
|
|
771
|
+
} else if (typeof item === "function") {
|
|
772
|
+
const ret = await item(args2);
|
|
773
|
+
if (ret) {
|
|
774
|
+
isCorrect = true;
|
|
775
|
+
break;
|
|
776
|
+
}
|
|
777
|
+
} else {
|
|
778
|
+
if (equalFn(value2, item)) {
|
|
779
|
+
isCorrect = true;
|
|
780
|
+
break;
|
|
781
|
+
}
|
|
782
|
+
}
|
|
749
783
|
}
|
|
750
784
|
}
|
|
751
785
|
if (!isCorrect) return createErrMsg("oneOf", args2);
|
|
@@ -780,6 +814,7 @@ export {
|
|
|
780
814
|
Base,
|
|
781
815
|
CLEAR_KEY,
|
|
782
816
|
Clear,
|
|
817
|
+
Const,
|
|
783
818
|
DataMap,
|
|
784
819
|
Doc,
|
|
785
820
|
Effect,
|