eggjs&sequelize使用教程一(表单自动验证)
来源:互联网 发布:linux sip服务器 编辑:程序博客网 时间:2024/05/21 08:44
egg form 表单验证
共三个文件
1.validate.config.js 主要是配置 formateError: 是定制错误时返回的结构, default设置对应的错误代码
module.exports = { formateError: null, defaultMsg: { required: 'missing filed', isReg: 'not match', notReg: 'match', isEmail: 'not email', isUrl: 'not url', isIP: 'not IP', isAlpha: 'not all letter', isAlphanumeric: ' not letter or number', isNumeric: 'not a number', isInt: 'not int number', isFloat: 'not float number', isLowercase: 'letters are not small letter', isUppercase: 'letters are not big letter', notNull: 'not allow null', isNull: 'must be null', notEmpty: 'string not allow empty', contains: 'not contain require letters', notIn: 'is in required array', isIn: 'not in required array', len: 'the length is out of bounds', max: 'the number bigger than max value', min: 'the number samller than min value', } }
2.validate.js 验证的对象,所有的验证方法都在里面,可以自行扩充
3.测试文件 validate.test.js
示例
const validate = require('./validate.js');const assert = require('assert');require('should');function getInstance(){ return new validate({ // formateError:function(err){ // return err; // } });}function check(rule,key,ruleName,data,ResultBoolean){ if(ResultBoolean===true){ let instance = getInstance(); let result = instance.validate(rule,data); assert.equal(result, true); assert.equal(instance.errors.length,0); }else{ let instanceFalse = getInstance(); let resultFalse = instanceFalse.validate(rule,data); assert.equal(resultFalse, false); assert.equal(instanceFalse.errors.length,1); instanceFalse.errors[0].should.have.property('msg',rule[key][ruleName].msg); instanceFalse.errors[0].should.have.property('filed').equal(key); } // validate false // instance.should.have.propertyByPath('errors',0,'filed').equal('name'); }it('required',function(){ let rule = { name: { required:{args:true,msg:'required'}} }; let myCheck= check.bind(null,rule,'name','required'); myCheck({name:'ac'},true); myCheck({names:'ac2'},false); });
运行测试
自行安装相应的包
# mocha validate.test.js
在egg中的使用方法
在app/middelware下创建 form-validate.js
module.exports = app => { return async function formValidate(rule,ctx,next) { const validate = new ctx.app.validate(); const requireFiled = Object.getOwnPropertyNames(rule); let form = {}; if(ctx.method=='POST'){ let queryFiled = Object.getOwnPropertyNames(ctx.request.body); for(let queryKey of queryFiled){ if(requireFiled.indexOf(queryKey)==-1){ delete ctx.request.body[queryKey]; } } form = ctx.request.body; }else{ let queryFiled = Object.getOwnPropertyNames(ctx.request.query); for(let queryKey of queryFiled){ if(requireFiled.indexOf(queryKey)==-1){ delete ctx.request.query[queryKey]; } } form = ctx.request.query; } if(validate.validate(rule,form) === false){ return ctx.setReturn(app.code.formError,validate.errors); } await next(); }}
validate.validate(rule,form)遮就是验证表单,前面只是区分get和post,和去除掉多余的表单项
rule这个变量就是从路由中传过来的
路由的写法
module.exports = app => { const { router } = app; let formValidate = app.middlewares.formValidate(app); let { testFormRule,loginForm } = require('./form/form-rule')(app); router.get('/', formValidate.bind(null,testFormRule), 'home.index'); router.post('/api/login',formValidate.bind(null,loginForm),'login.login');};
loginForm
module.exports = app => { const loginForm = { username: { required: { args: true, msg: app.code.usernameNotNull }, notEmpty: { args: true, msg: app.code.usernameNotNull } }, password: { required: { args: true, msg: app.code.passwordNotAllowNull }, notEmpty: { args: true, msg: app.code.passwordNotAllowNull } } }; return loginForm;}
当错误是返回的错误结构是[{filed:’username’,msg:501}]
具体代码位置
阅读全文
0 0
- eggjs&sequelize使用教程一(表单自动验证)
- eggjs&sequelize使用教程一(环境搭建)
- sequelize(一)
- 使用thinkphp框架完成表单自动验证
- jQuery EasyUI使用教程之表单验证
- LEADTOOLS使用教程:表单自动对齐
- HTML5表单教程之4种自动验证属性
- HTML5表单教程之4种自动验证属性
- HTML5表单自动验证
- 表单自动验证 js
- HTML5表单自动验证
- html5表单自动验证
- 表单自动验证
- 表单验证(使用Vtype)
- 表单验证(使用AJAX)
- sequelize的使用
- 使用VB实现邮箱自动注册(一):表单自动提交
- Struts2 官方教程:使用XML进行表单验证(Form Validation Using XML)
- jsp、http响应
- 所有路径的最短距离(c语言)
- Kaggle Titanic 机器学习实践笔记(二)
- java中写入文件时换行符是用"/r/n"还是"/n"?
- Qt之图形(绘制文本)
- eggjs&sequelize使用教程一(表单自动验证)
- SDUT-2138 数据结构实验之图论三:判断可达性(DFS)
- USB摄像头驱动框架分析
- EOJ 2983 蛇行图案
- 用IDEA打开现有的项目
- (CNN)卷积神经网络(四)dropout
- centos7安装jdk
- 个人总结55
- 构造器