vue-ajv-validation
mixin 模块让 Vue 的 component 可以很方便的使用它。Vue 组件可在组件 option 中声明 validate 项来配置 mixin 模块的行为。在 Vue 的组件代码中,也可以直接调用验证器实例的方法来完成数据验证。另外,mxin 模块声明了 validateError 和 validateState 这两个数据对象用于保存验证错误消息和验证状态,它们可以很方便的引用以及在组件模板中绑定显示。
el: '#ObjValidationDemo' mixins: ObjValidationvueMixin validate: // schame验证规则 schame: "$schema": "http://json-schema.org/draft-04/schema#" "title": "Product" "description": "A product from Acme's catalog" "type": "object" "properties": "id": "description": "The unique identifier for a product" "type": "integer" "name": "description": "Name of the product" "type": "string" "goods": "items": "type":"string" "required": "id" "name" "price" // 要验证的对象,可以通过 vm.$get('user') 获取 target: 'user' data: user: name: '' family: "tonny""lily" computed: { let state = thisvalidateState return Object } methods: submit: this$validator )
Name {{ validateError.name }} family {{ validateError.family[$index] }} Submit