vadacl域和组件级别的验证
vadacl ("validation at domain and component levels")是一个基于 TypeScript 的代码库,能提高 Angular 2 形式类验证功能。它提供了一种方法让开发人员可以对数据对象设置基于域或数据库的验证规则,然后在不同的组件需要的时候增加或替换这些验证。它还可以让开发人员添加和重写那些已添加的验证错误消息,验证方法返回验证元数据中。
示例代码:
import { Validatable, PropertyValidations } from '../vadacl/interfaces' export class UserProfile implements Validatable { firstName: string = null; lastName: string = null; username: string = null; age: number = null; gender: string = null; validations: { [ index: string ] : PropertyValidations } = { firstName: { maxLength: { maxLength: 25, message: 'Your first name cannot be longer than 25 characters.'}, required: { message: 'Your first name is required.' } }, lastName: { maxLength: { maxLength: 25, message: 'Your last name cannot be longer than 25 characters.'}, required: { message: 'Your last name is required.' } }, username: { maxLength: { maxLength: 30, message: 'Your username cannot be longer than 25 characters.'}, required: { message: 'You must have a username.' } }, age: { pattern: { pattern: '[0-9]*', message: 'Enter your age as an integer.' } }, gender: { pattern: { pattern: 'M|F', message: 'Enter your gender as "M" or "F".' } } }; }
未来发展:
添加单元测试。 增加更多的验证方法。 添加在消息模块中设置域级验证错误消息的能力,以适应国际化。
评论