12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- '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);
- });
- });
|