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);
})
}
这篇博客介绍了如何使用数据注解来减少模型验证的工作量。博客提供了一些常见的对象属性限制器,并且可以自定义限制器。博客中详细介绍了如何安装和使用这些限制器。
博客的闪光点是清晰地介绍了如何使用内置限制器和自定义限制器。内置限制器提供了方便的使用方式,只需在需要限制的地方添加修饰符即可调用。自定义限制器可以动态设置任何对象的属性的自定义限制器,甚至是匿名对象。
博客提到了一些改进的空间。首先,在使用修改后的限制器时,需要在对象构造后调用
DataAnnotations.LimiterInit(obj)
来初始化才能使限制器生效。这一点可以在博客中更早地提到,以免读者在使用时遇到困惑。其次,博客中提到了使用
DataAnnotations.DefineLimiter()
来动态设置自定义限制器,但没有给出具体的示例代码。在博客中提供一个示例代码将更有帮助。最后,博客中提到了设置错误消息和设置错误监听器的功能,但没有给出具体的示例代码。在博客中提供一些示例代码将更加完整。
总的来说,这篇博客提供了有用的信息和示例代码,但可以在一些地方进行改进和完善。希望作者能够进一步扩展和完善这篇博客的内容。