data-annotations
v1.4.1
Published
Some common object property limiters and customizable limiter injection are provided
Downloads
1
Readme
data-annotations
Some common object property limiters and customizable limiter injection are provided
Built in three common limiters
Required
Restrictions applied when required fieldsRange
It can put your numbers in a certain range, like 2-10RegularExpression
It can match regular in string type properties
Installation
npm install data-annotations --save
Usage
Use built-in limiter
The built-in limiter is particularly convenient to use, you only need to simply add a modifier to the place you need to limit to call it up, like this
export class Test{
@Required <-----
public TestStr:string;
}
Warning! If you use a modified limiter (like the one above), you need to call DataAnnotations.LimiterInit(obj)
to initialize after the object is constructed to make the limiter effective
export class Test{
constructor(){
DataAnnotations.LimiterInit(this); <-----
}
@Required
public TestStr:string;
}
When we need to check the limiter, we simply call a DataAnnotations.IsValid(obj)
, like this
function foo(){
const obj = new Test();
const result = DataAnnotations.IsValid(obj);
console.log(result.Success)
}
Custom limiter
You can use the built-in DataAnnotations.DefineLimiter()
to dynamically set a custom limiter to the properties of any object, even anonymous objects. if there is an error, it returns error content, by return Null
const obj = { test:'123' }
function foo(){
DataAnnotations.DefineLimiter("MyLimiter",obj, "test", (arg) => {
if (arg == "123") {
return "you can't input '123'";
}
return null;
})
}
The built-in limiter provides a Factory
for dynamic injection. You can use this method to dynamically inject the built-in interceptor or modify the error message
Reflection error message
You can set your ErrorMsg to bind properties to some forms to quickly get feedback information, for example
export class Test{
@Required("you need input Phone or Email") <-----
public TestStr:string;
}
The error message can also be replaced when the outer layer is instantiated
function foo(){
const obj = new Test();
RequiredFactory("Email will do")(obj,"TestStr"); <------
}
Error listener
We sometimes even want to let the error prompt appear by itself instead of manually trigger
function foo(){
const obj = new Test();
RequiredFactory("Email will do",(e)=>{
//....Do something
})(obj,"TestStr");
}
Change listener
You can use the built-in DataAnnotations.SetChangeListener()
to set a change listener to the properties of any object. Of course, anonymous is also possible, but the premise is that the target property needs at least one limiter, no matter how you set it
const obj = { test:'123' }
function foo(){
DataAnnotations.SetChangeListener(obj,'test',(s)=>{
console.log(s);
})
}