1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- 'use strict';
- /**
- * jsvalidator单元测试
- *
- * @author CaiAoLin
- * @date 2018/2/7
- * @version
- */
- const JsValidator = require('../../../app/lib/js_validator');
- const { app, assert } = require('egg-mock/bootstrap');
- describe('test/app/lib/js_validator.test.js', () => {
- it('test convert', function* () {
- // 创建 ctx
- const ctx = app.mockContext();
- const jsValidator = new JsValidator(ctx);
- const rule = {
- username: { type: 'string', required: true, allowEmpty: false, min: 4, max: 10 },
- group_id: { type: 'integer', required: true, allowEmpty: false, min: 4, max: 10 },
- password: { type: 'password', required: true, allowEmpty: false, min: 4 },
- confirm_password: { type: 'password', required: true, allowEmpty: false, min: 4, compare: 'password' },
- ip: { type: 'ip', allowEmpty: false },
- telephone: { type: 'mobile', allowEmpty: false },
- };
- // 转换为json验证
- jsValidator.convert(rule);
- const convertRule = JSON.stringify(jsValidator.rule);
- let expectRule = {
- username: { required: true, minlength: 4, maxlength: 10 },
- group_id: { required: true, min: 4, max: 10 },
- password: { required: true, minlength: 4 },
- confirm_password: { required: true, minlength: 4, equalTo: '#password' },
- ip: { ip: true, required: true },
- telephone: { mobile: true, required: true },
- };
- expectRule = JSON.stringify(expectRule);
- assert(convertRule === expectRule);
- });
- it('test js validator', function* () {
- const ctx = app.mockContext();
- const jsValidator = new JsValidator(ctx);
- const rule = {
- username: { type: 'string', required: true, allowEmpty: false, min: 4, max: 10 },
- group_id: { type: 'integer', required: true, allowEmpty: false, min: 4, max: 10 },
- password: { type: 'password', required: true, allowEmpty: false, min: 4 },
- confirm_password: { type: 'password', required: true, allowEmpty: false, min: 4, compare: 'password' },
- ip: { type: 'ip', allowEmpty: false },
- telephone: { type: 'mobile', allowEmpty: false },
- };
- const jsCode = jsValidator.convert(rule).build();
- assert(jsCode !== '');
- });
- });
|