Skip to content

Commit

Permalink
增加param的验证配置
Browse files Browse the repository at this point in the history
  • Loading branch information
wei.ma1 committed Jun 14, 2021
1 parent 4ad14a0 commit 68a6084
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "es6-mock",
"version": "2.1.1",
"version": "2.1.2",
"description": "es6-mock",
"main": "src/index.js",
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions src/mini-require.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function miniRequire(moduleName) {
global.__parentPath = filePath.replace(/\/[^/]+$/, '');

const { content, filename } = getContent(filePath) || {};
const params = [
const param = [
'exports', 'module', 'require',
'__dirname', '__filename',
'$request', '$response',
Expand All @@ -38,7 +38,7 @@ function miniRequire(moduleName) {
return `${$1} ${name}`;
});

const fn = new Function(...params, `${data}\n ${exportConsts.join('\n')} \n return module.exports`);
const fn = new Function(...param, `${data}\n ${exportConsts.join('\n')} \n return module.exports`);

const module = {
exports: {}
Expand Down
8 changes: 4 additions & 4 deletions src/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ const Methods = {
module.exports = function (validates) {
__request.validated = true;
if(!validateMethod(validates.method)){
return validateParams(__request.method === Methods.Get ? __request.query : __request.body, validates.params);
return validateParam(__request.method === Methods.Get ? __request.query : __request.body, validates.param || validates.params);
}
return Promise.resolve(false);
}

/**
* 校验参数
* @param body
* @param params
* @param param
*/
function validateParams(body, params) {
const validator = new Validator(body, params);
function validateParam(body, param) {
const validator = new Validator(body, param);
return validator.check().then((matched) => {
if (!matched) {
__request.validateFailed = true;
Expand Down
2 changes: 1 addition & 1 deletion test/test5.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// 使用validate校验method及参数 test5.js
// 参数校验使用库:node-input-validator
$validate({
params: {
param: {
offset: 'required|integer',
limit: 'required|integer'
},
Expand Down
4 changes: 2 additions & 2 deletions test/test8.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ sleep(500);

// 校验数据
validate({
params: {
param: {
name: 'required',
id: 'required'
},
Expand All @@ -15,5 +15,5 @@ validate({
export default {
'result_code|1-10': '0',
data: '1000',
params: getRequest().query
param: getRequest().query
};

0 comments on commit 68a6084

Please sign in to comment.