express-request-checker
Advanced tools
Comparing version 0.2.1 to 0.2.2
@@ -116,2 +116,19 @@ 'use strict' | ||
var _isArrayOrArrayString = function(v) { | ||
var obj; | ||
if (typeof v === 'string') { | ||
try { | ||
obj = JSON.parse(v); | ||
} | ||
catch (err) { | ||
return false; | ||
} | ||
} | ||
else { | ||
obj = v | ||
} | ||
return Array.isArray(obj); | ||
}; | ||
var checkerList = { | ||
@@ -187,2 +204,21 @@ assertTrue : function(v, func) { | ||
isArray : function(v, flg) { | ||
if (typeof flg !== 'boolean') { return true; } | ||
return !(flg ^ _isArrayOrArrayString(v)); | ||
}, | ||
isIntegerArray : function(v, flg) { | ||
if (typeof flg !== 'boolean') { return true; } | ||
if (!_isArrayOrArrayString(v)) { return !(flg ^ false); } | ||
var arr = JSON.parse(v); | ||
for (var i = 0; i < arr.length; i++) { | ||
if (!isInteger(arr[i])) { return !(flg ^ false); } | ||
} | ||
return !(flg ^ true); | ||
}, | ||
equal : function(v, optionValue) { | ||
@@ -189,0 +225,0 @@ return _compare(v, optionValue, '='); |
{ | ||
"name": "express-request-checker", | ||
"version": "0.2.1", | ||
"version": "0.2.2", | ||
"description": "Express request checker extension.", | ||
@@ -5,0 +5,0 @@ "main": "express-request-checker.js", |
@@ -104,20 +104,22 @@ # express-request-checker | ||
|Option |Default Value| | ||
|------------|-------------| | ||
|isOptional |`false` | | ||
|assertTrue |`[]` | | ||
|assertFalse |`[]` | | ||
|matchRegExp |`[]` | | ||
|isIn |`[]` | | ||
|notIn |`[]` | | ||
|isInteger |`null` | | ||
|isEmail |`null` | | ||
|equal |`null` | | ||
|greaterThan |`null` | | ||
|greaterEqual|`null` | | ||
|lessThan |`null` | | ||
|lessEqual |`null` | | ||
|allowEmpty |`false` | | ||
|minLength |`null` | | ||
|maxLangth |`null` | | ||
|Option |Default Value| | ||
|--------------|-------------| | ||
|isOptional |`false` | | ||
|assertTrue |`[]` | | ||
|assertFalse |`[]` | | ||
|matchRegExp |`[]` | | ||
|isIn |`[]` | | ||
|notIn |`[]` | | ||
|isInteger |`null` | | ||
|isEmail |`null` | | ||
|isArray |`null` | | ||
|isIntegerArray|`null` | | ||
|equal |`null` | | ||
|greaterThan |`null` | | ||
|greaterEqual |`null` | | ||
|lessThan |`null` | | ||
|lessEqual |`null` | | ||
|allowEmpty |`false` | | ||
|minLength |`null` | | ||
|maxLangth |`null` | | ||
@@ -213,2 +215,36 @@ ### Parameter Options | ||
#### isArray | ||
`true` or `false`. (DEFALT:`null` - Don't care) | ||
when `true`, The value of parameter in request must be an Array or stringified Array. | ||
when `false `, The value of parameter in request must NOT be an Array or stringified Array. | ||
Example: | ||
```javascript | ||
option = { | ||
query: { | ||
param1: { | ||
isArray: true | ||
} | ||
} | ||
} | ||
``` | ||
#### isIntegerArray | ||
`true` or `false`. (DEFALT:`null` - Don't care) | ||
when `true`, The value of parameter in request must be an Array or stringified Array whose elements are all integers. | ||
when `false `, The value of parameter in request must NOT be an Array or stringified Array whose elements are all integers. | ||
Example: | ||
```javascript | ||
option = { | ||
query: { | ||
param1: { | ||
isIntegerArray: true | ||
} | ||
} | ||
} | ||
``` | ||
#### equal / greaterThan / greaterEqual / lessThan / lessEqual | ||
@@ -281,2 +317,2 @@ `integer` or `null`. (DEFALT:`null` - Don't care) | ||
[npm-url]: https://npmjs.org/package/express-request-checker | ||
[npm-image]: http://img.shields.io/npm/v/express-request-checker.svg | ||
[npm-image]: http://img.shields.io/npm/v/express-request-checker.svg |
115
test/test.js
@@ -443,2 +443,117 @@ var assert = require('assert'); | ||
it('F-9 scope: ' + scopeName + ', strict: default -> 1 ' + scopeName + ' required(isArray:true), Got 1 email ' + scopeName + ' input', function() { | ||
options = {}; | ||
options[scopeName] = { | ||
param1: { isArray: true } | ||
}; | ||
fakeReq = {}; | ||
fakeReq[scopeName] = { | ||
param1: '["a", "b"]' | ||
}; | ||
assert.notEqual(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('F-10 scope: ' + scopeName + ', strict: default -> 1 ' + scopeName + ' required(isArray:true), Got 1 non-email ' + scopeName + ' input', function() { | ||
options = {}; | ||
options[scopeName] = { | ||
param1: { isArray: true } | ||
}; | ||
fakeReq = {}; | ||
fakeReq[scopeName] = { | ||
param1: '{"a": "b"}' | ||
}; | ||
assert.equal(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('F-11 scope: ' + scopeName + ', strict: default -> 1 ' + scopeName + ' required(isArray:false), Got 1 email ' + scopeName + ' input', function() { | ||
options = {}; | ||
options[scopeName] = { | ||
param1: { isArray: false } | ||
}; | ||
fakeReq = {}; | ||
fakeReq[scopeName] = { | ||
param1: '["a", "b"]' | ||
}; | ||
assert.equal(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('F-12 scope: ' + scopeName + ', strict: default -> 1 ' + scopeName + ' required(isArray:false), Got 1 non-email ' + scopeName + ' input', function() { | ||
options = {}; | ||
options[scopeName] = { | ||
param1: { isArray: false } | ||
}; | ||
fakeReq = {}; | ||
fakeReq[scopeName] = { | ||
param1: '{"a": "b"}' | ||
}; | ||
assert.notEqual(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('F-13 scope: ' + scopeName + ', strict: default -> 1 ' + scopeName + ' required(isIntegerArray:true), Got 1 email ' + scopeName + ' input', function() { | ||
options = {}; | ||
options[scopeName] = { | ||
param1: { isIntegerArray: true } | ||
}; | ||
fakeReq = {}; | ||
fakeReq[scopeName] = { | ||
param1: '[1, 2]' | ||
}; | ||
assert.notEqual(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('F-14 scope: ' + scopeName + ', strict: default -> 1 ' + scopeName + ' required(isIntegerArray:true), Got 1 non-email ' + scopeName + ' input', function() { | ||
options = {}; | ||
options[scopeName] = { | ||
param1: { isIntegerArray: true } | ||
}; | ||
fakeReq = {}; | ||
fakeReq[scopeName] = { | ||
param1: '["a", "b"]' | ||
}; | ||
assert.equal(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('F-15 scope: ' + scopeName + ', strict: default -> 1 ' + scopeName + ' required(isIntegerArray:false), Got 1 email ' + scopeName + ' input', function() { | ||
options = {}; | ||
options[scopeName] = { | ||
param1: { isIntegerArray: false } | ||
}; | ||
fakeReq = {}; | ||
fakeReq[scopeName] = { | ||
param1: '[1, 2]' | ||
}; | ||
assert.equal(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('F-16 scope: ' + scopeName + ', strict: default -> 1 ' + scopeName + ' required(isIntegerArray:false), Got 1 non-email ' + scopeName + ' input', function() { | ||
options = {}; | ||
options[scopeName] = { | ||
param1: { isIntegerArray: false } | ||
}; | ||
fakeReq = {}; | ||
fakeReq[scopeName] = { | ||
param1: '["a", "b"]' | ||
}; | ||
assert.notEqual(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('G-1 scope: ' + scopeName + ', strict: default -> 1 ' + scopeName + ' required(equal), Got 1 valid ' + scopeName + ' input', function() { | ||
@@ -445,0 +560,0 @@ options = {}; |
59006
1381
316