express-request-checker
Advanced tools
Comparing version 0.1.0 to 0.2.0
'use strict' | ||
var defaultHeaders = { | ||
"Content-Type": "application/json" | ||
}; | ||
var defaultFormat = JSON.stringify( | ||
{ | ||
error: { | ||
reporter : "express-request-checker", | ||
scope : "`{{scope}}`", | ||
paramName : "`{{paramName}}`", | ||
errorMessage: "{{errorMessage}}", | ||
errorDetail : "{{errorDetail}}" | ||
} | ||
} | ||
); | ||
var sendErrorDetailFlag = false; | ||
var errorHttpStatusCode = 400; | ||
var errorHttpResponseHeaders = defaultHeaders; | ||
var errorHttpResponseBodyFormat = defaultFormat; | ||
var scopes = ['params', 'query', 'body']; | ||
var isSetted = function(obj) { | ||
@@ -10,2 +33,9 @@ if (typeof obj !== 'undefined' && obj !== null) { | ||
} | ||
}; | ||
var isEmptyObj = function(obj) { | ||
for (var i in obj) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
@@ -15,6 +45,6 @@ | ||
return 'Invalid value: `' + v + '`'; | ||
} | ||
}; | ||
var sendError = function(res, scope, paramName, errorMessage, errorDetail) { | ||
var response = module.exports.errorMessageFormat; | ||
var response = errorHttpResponseBodyFormat; | ||
response = response.replace('{{scope}}', scope, 'g'); | ||
@@ -24,10 +54,10 @@ response = response.replace('{{paramName}}', paramName, 'g'); | ||
if (module.exports.DEBUG_ENABLED && isSetted(errorDetail)) { | ||
if (sendErrorDetailFlag && isSetted(errorDetail)) { | ||
response = response.replace('{{errorDetail}}', errorDetail, 'g'); | ||
} | ||
else { | ||
response = response.replace('{{errorDetail}}', ''); | ||
response = response.replace('{{errorDetail}}', ''); | ||
} | ||
var httpStatus = module.exports.errorHttpStatus; | ||
var httpStatus = errorHttpStatusCode; | ||
if (!isInteger(httpStatus)) { | ||
@@ -37,2 +67,6 @@ httpStatus = 400; | ||
for (var headerKey in errorHttpResponseHeaders) { | ||
var headerValue = errorHttpResponseHeaders[headerKey]; | ||
res.set(headerKey, headerValue); | ||
} | ||
res.status(httpStatus); | ||
@@ -69,34 +103,37 @@ res.send(response); | ||
} | ||
} | ||
}; | ||
var isInteger = function(v) { | ||
return '' + parseInt(v) === '' + v; | ||
} | ||
}; | ||
var completeOptions = function(reqOptions) { | ||
if (!isSetted(reqOptions)) { | ||
throw new Error('reqOptions should not be `null`'); | ||
var completeReqOption = function(reqOption) { | ||
if (!isSetted(reqOption)) { | ||
throw new Error('reqOption should not be `null`'); | ||
} | ||
// DEFAULT request options | ||
var fullReqOptions = { | ||
scope: 'query', | ||
strict: true, | ||
params: {} | ||
// DEFAULT request option | ||
var fullReqOption = { | ||
strict: true | ||
} | ||
if (typeof reqOptions.scope === 'string') { | ||
if (reqOptions.scope === 'query' || reqOptions.scope === 'body') { | ||
fullReqOptions.scope = reqOptions.scope; | ||
} | ||
if (typeof reqOption.strict === 'boolean') { | ||
fullReqOption.strict = reqOption.strict; | ||
} | ||
if (typeof reqOptions.strict === 'boolean') { | ||
fullReqOptions.strict = reqOptions.strict; | ||
} | ||
for (var iScopeName in scopes) { | ||
var reqScopeName = scopes[iScopeName]; | ||
var reqScopeOption = reqOption[reqScopeName]; | ||
if (typeof reqOptions.params === 'object' && isSetted(reqOptions.params)) { | ||
for (var k in reqOptions.params) { | ||
// DEFAULT parameter options | ||
var fullParamOptions = { | ||
// Skip unsetted scope | ||
if (typeof reqScopeOption === 'undefined') { continue; } | ||
// Init reqOption.<scope>. | ||
fullReqOption[reqScopeName] = {}; | ||
for (var reqParamName in reqScopeOption) { | ||
var reqParamOption = reqScopeOption[reqParamName]; | ||
// DEFAULT parameter option | ||
var fullParamOption = { | ||
isOptional: false, | ||
@@ -127,82 +164,100 @@ | ||
var paramOptions = reqOptions.params[k]; | ||
if (typeof paramOptions === 'object' && isSetted(paramOptions)) { | ||
var optionArray, optionName; | ||
// Skip unsetted option | ||
if (typeof reqParamOption !== 'object' || !isSetted(reqParamOption)) { continue; } | ||
// (true|false) isOptional, allowEmpty | ||
optionArray = ['isOptional', 'allowEmpty']; | ||
for (var i in optionArray) { | ||
optionName = optionArray[i]; | ||
if (typeof paramOptions[optionName] == 'boolean') { | ||
fullParamOptions[optionName] = paramOptions[optionName]; | ||
} | ||
} | ||
var optionArray, optionName; | ||
// (true|false|null) isInteger | ||
optionArray = ['isInteger', 'isEmail']; | ||
for (var i in optionArray) { | ||
optionName = optionArray[i]; | ||
if (isSetted(paramOptions[optionName])) { | ||
fullParamOptions[optionName] = paramOptions[optionName]; | ||
} | ||
} | ||
// (true|false) isOptional, allowEmpty | ||
optionArray = ['isOptional', 'allowEmpty']; | ||
for (var iOptionName in optionArray) { | ||
optionName = optionArray[iOptionName]; | ||
// (array#function) assertTrue, assertFalse | ||
optionArray = ['assertTrue', 'assertFalse']; | ||
for (var i in optionArray) { | ||
optionName = optionArray[i]; | ||
if (isSetted(paramOptions[optionName])) { | ||
fullParamOptions[optionName] = convertToArray(paramOptions[optionName], 'function'); | ||
} | ||
} | ||
// Skip invalid option | ||
if (typeof reqParamOption[optionName] !== 'boolean') { continue; } | ||
// (array) matchRegExp, isIn, notIn | ||
optionArray = ['matchRegExp', 'isIn', 'notIn']; | ||
for (var i in optionArray) { | ||
optionName = optionArray[i]; | ||
if (isSetted(paramOptions[optionName])) { | ||
fullParamOptions[optionName] = convertToArray(paramOptions[optionName]); | ||
} | ||
} | ||
fullParamOption[optionName] = reqParamOption[optionName]; | ||
} | ||
// (number|null) equal, greaterThan, greaterEqual, lessThan, lessEqual | ||
optionArray = ['equal', 'greaterThan', 'greaterEqual', 'lessThan', 'lessEqual']; | ||
for (var i in optionArray) { | ||
optionName = optionArray[i]; | ||
if (isSetted(paramOptions[optionName]) && typeof paramOptions[optionName] === 'number') { | ||
fullParamOptions[optionName] = paramOptions[optionName]; | ||
} | ||
} | ||
// (true|false|null) isInteger, isEmail, isEmpty | ||
optionArray = ['isInteger', 'isEmail', 'isEmpty']; | ||
for (var iOptionName in optionArray) { | ||
optionName = optionArray[iOptionName]; | ||
// Skip invalid option | ||
if (!isSetted(reqParamOption[optionName])) { continue; } | ||
// (integer|null) maxLength, minLength | ||
optionArray = ['maxLength', 'minLength']; | ||
for (var i in optionArray) { | ||
optionName = optionArray[i]; | ||
if (isSetted(paramOptions[optionName]) && isInteger(paramOptions[optionName])) { | ||
fullParamOptions[optionName] = paramOptions[optionName]; | ||
} | ||
} | ||
fullParamOption[optionName] = reqParamOption[optionName]; | ||
} | ||
// isEmpty | ||
if (isSetted(paramOptions.isEmpty)) { | ||
fullParamOptions.isEmpty = paramOptions.isEmpty; | ||
} | ||
// (array#function) assertTrue, assertFalse | ||
optionArray = ['assertTrue', 'assertFalse']; | ||
for (var iOptionName in optionArray) { | ||
optionName = optionArray[iOptionName]; | ||
// Skip invalid option | ||
if (!isSetted(reqParamOption[optionName])) { continue; } | ||
fullParamOption[optionName] = convertToArray(reqParamOption[optionName], 'function'); | ||
} | ||
fullReqOptions.params[k] = fullParamOptions; | ||
// (array) matchRegExp, isIn, notIn | ||
optionArray = ['matchRegExp', 'isIn', 'notIn']; | ||
for (var iOptionName in optionArray) { | ||
optionName = optionArray[iOptionName]; | ||
// Skip invalid option | ||
if (!isSetted(reqParamOption[optionName])) { continue; } | ||
fullParamOption[optionName] = convertToArray(reqParamOption[optionName]); | ||
} | ||
// (number|null) equal, greaterThan, greaterEqual, lessThan, lessEqual | ||
optionArray = ['equal', 'greaterThan', 'greaterEqual', 'lessThan', 'lessEqual']; | ||
for (var iOptionName in optionArray) { | ||
optionName = optionArray[iOptionName]; | ||
// Skip invalid option | ||
if (!isSetted(reqParamOption[optionName]) || typeof reqParamOption[optionName] !== 'number') { continue; } | ||
fullParamOption[optionName] = reqParamOption[optionName]; | ||
} | ||
// (integer|null) maxLength, minLength | ||
optionArray = ['maxLength', 'minLength']; | ||
for (var iOptionName in optionArray) { | ||
optionName = optionArray[iOptionName]; | ||
// Skip invalid option | ||
if (!isSetted(reqParamOption[optionName]) || !isInteger(reqParamOption[optionName])) { continue; } | ||
fullParamOption[optionName] = reqParamOption[optionName]; | ||
} | ||
fullReqOption[reqScopeName][reqParamName] = fullParamOption; | ||
} | ||
} | ||
return fullReqOptions; | ||
} | ||
return fullReqOption; | ||
}; | ||
var requestChecker = function(reqOptions) { | ||
var requestChecker = function(reqOption) { | ||
var middleware = function(req, res, next) { | ||
var opt = completeOptions(reqOptions); | ||
var opt = completeReqOption(reqOption); | ||
// Replace `undefined` scope to `{}` | ||
for (var iScopeName in scopes) { | ||
var scopeName = scopes[iScopeName]; | ||
if (!isSetted(req[scopeName])) { | ||
req[scopeName] = {}; | ||
} | ||
} | ||
// Strict check | ||
if (opt.strict) { | ||
for (var k in req[opt.scope]) { | ||
if (!isSetted(opt.params[k])) { | ||
sendError(res, opt.scope, k, 'Unexpected parameter'); | ||
for (var iScopeName in scopes) { | ||
var scopeName = scopes[iScopeName]; | ||
// Skip unsetted scope | ||
if (!isSetted(opt[scopeName])) { continue; } | ||
for (var reqParamName in req[scopeName]) { | ||
// Existed. OK | ||
if (isSetted(opt[scopeName][reqParamName])) { continue; } | ||
sendError(res, scopeName, reqParamName, 'Unexpected parameter'); | ||
return false; | ||
@@ -214,129 +269,132 @@ } | ||
// General check | ||
for (var k in opt.params) { | ||
var paramOpt = opt.params[k]; | ||
var input = req[opt.scope][k]; | ||
for (var iScopeName in scopes) { | ||
var scopeName = scopes[iScopeName]; | ||
var scopeOption = opt[scopeName]; | ||
// Check existing. | ||
if(!isSetted(input)) { | ||
if (paramOpt.isOptional) { | ||
continue; | ||
} | ||
else { | ||
sendError(res, opt.scope, k, 'Missing parameter.'); | ||
for (var paramOptionName in scopeOption) { | ||
var paramOption = scopeOption[paramOptionName]; | ||
var input = req[scopeName][paramOptionName]; | ||
// Check existing. | ||
if(!isSetted(input)) { | ||
// Skip optional param | ||
if (paramOption.isOptional) { continue; } | ||
sendError(res, scopeName, paramOptionName, 'Missing parameter.'); | ||
return false; | ||
} | ||
} | ||
// Check value - assertTrue | ||
for (var i = 0; i < paramOpt.assertTrue.length; i++) { | ||
if (paramOpt.assertTrue[i](input) !== true) { | ||
sendError(res, opt.scope, k, invalidValueErrorMessage(input), 'AssertTrue: #' + i); | ||
return false; | ||
// Check value - assertTrue | ||
for (var i = 0; i < paramOption.assertTrue.length; i++) { | ||
if (paramOption.assertTrue[i](input) !== true) { | ||
sendError(res, scopeName, paramOptionName, invalidValueErrorMessage(input), 'AssertTrue: #' + i); | ||
return false; | ||
} | ||
} | ||
} | ||
// Check value - assertFalse | ||
for (var i = 0; i < paramOpt.assertFalse.length; i++) { | ||
if (paramOpt.assertFalse[i](input) !== false) { | ||
sendError(res, opt.scope, k, invalidValueErrorMessage(input), 'AssertFalse: #' + i); | ||
return false; | ||
// Check value - assertFalse | ||
for (var i = 0; i < paramOption.assertFalse.length; i++) { | ||
if (paramOption.assertFalse[i](input) !== false) { | ||
sendError(res, scopeName, paramOptionName, invalidValueErrorMessage(input), 'AssertFalse: #' + i); | ||
return false; | ||
} | ||
} | ||
} | ||
// Check value - matchRegExp | ||
for (var i = 0; i < paramOpt.matchRegExp.length; i++) { | ||
if (!paramOpt.matchRegExp[i].test(input)) { | ||
sendError(res, opt.scope, k, invalidValueErrorMessage(input), 'matchRegExp: #' + i + ', ' + paramOpt.matchRegExp[i]); | ||
return false; | ||
// Check value - matchRegExp | ||
for (var i = 0; i < paramOption.matchRegExp.length; i++) { | ||
if (!paramOption.matchRegExp[i].test(input)) { | ||
sendError(res, scopeName, paramOptionName, invalidValueErrorMessage(input), 'matchRegExp: #' + i + ', ' + paramOption.matchRegExp[i]); | ||
return false; | ||
} | ||
} | ||
} | ||
// Check value - isIn | ||
if (paramOpt.isIn.length > 0) { | ||
var isIn = false; | ||
for (var i = 0; i < paramOpt.isIn.length; i++) { | ||
if (input == paramOpt.isIn[i]) { | ||
isIn = true; | ||
// Check value - isIn | ||
if (paramOption.isIn.length > 0) { | ||
var isIn = false; | ||
for (var i = 0; i < paramOption.isIn.length; i++) { | ||
if (input == paramOption.isIn[i]) { | ||
isIn = true; | ||
} | ||
} | ||
if (!isIn) { | ||
sendError(res, scopeName, paramOptionName, invalidValueErrorMessage(input), 'isIn: ' + paramOption.isIn); | ||
return false; | ||
} | ||
} | ||
if (!isIn) { | ||
sendError(res, opt.scope, k, invalidValueErrorMessage(input), 'isIn: ' + paramOpt.isIn); | ||
// Check value - notIn | ||
for (var i = 0; i < paramOption.notIn.length; i++) { | ||
if (input == paramOption.notIn[i]) { | ||
sendError(res, scopeName, paramOptionName, invalidValueErrorMessage(input), 'notIn: ' + paramOption.notIn); | ||
return false; | ||
} | ||
} | ||
// Check value - isInteger | ||
if (isSetted(paramOption.isInteger) && isInteger(input) !== paramOption.isInteger) { | ||
sendError(res, scopeName, paramOptionName, invalidValueErrorMessage(input), 'isInteger'); | ||
return false; | ||
} | ||
} | ||
// Check value - notIn | ||
for (var i = 0; i < paramOpt.notIn.length; i++) { | ||
if (input == paramOpt.notIn[i]) { | ||
sendError(res, opt.scope, k, invalidValueErrorMessage(input), 'notIn: ' + paramOpt.notIn); | ||
// Check value - isEmail | ||
var reEmail = /^(?:[a-z\d]+[_\-\+\.]?)*[a-z\d]+@(?:([a-z\d]+\-?)*[a-z\d]+\.)+([a-z]{2,})+$/i; | ||
if (isSetted(paramOption.isEmail) && reEmail.test(input) !== paramOption.isEmail) { | ||
sendError(res, scopeName, paramOptionName, invalidValueErrorMessage(input), 'isEmail'); | ||
return false; | ||
} | ||
} | ||
// Check value - isInteger | ||
if (isSetted(paramOpt.isInteger) && isInteger(input) !== paramOpt.isInteger) { | ||
sendError(res, opt.scope, k, invalidValueErrorMessage(input), 'isInteger'); | ||
return false; | ||
} | ||
// Check value - equal | ||
if (isSetted(paramOption.equal) && !(input == paramOption.equal)) { | ||
sendError(res, scopeName, paramOptionName, invalidValueErrorMessage(input), 'equal, ' + paramOption.equal); | ||
return false; | ||
} | ||
// Check value - isEmail | ||
var reEmail = /^(?:[a-z\d]+[_\-\+\.]?)*[a-z\d]+@(?:([a-z\d]+\-?)*[a-z\d]+\.)+([a-z]{2,})+$/i; | ||
if (isSetted(paramOpt.isEmail) && reEmail.test(input) !== paramOpt.isEmail) { | ||
sendError(res, opt.scope, k, invalidValueErrorMessage(input), 'isEmail'); | ||
return false; | ||
} | ||
// Check value - greaterThan | ||
if (isSetted(paramOption.greaterThan) && !(input > paramOption.greaterThan)) { | ||
sendError(res, scopeName, paramOptionName, invalidValueErrorMessage(input), 'greaterThan, ' + paramOption.greaterThan); | ||
return false; | ||
} | ||
// Check value - equal | ||
if (isSetted(paramOpt.equal) && !(input == paramOpt.equal)) { | ||
sendError(res, opt.scope, k, invalidValueErrorMessage(input), 'equal, ' + paramOpt.equal); | ||
return false; | ||
} | ||
// Check value - greaterEqual | ||
if (isSetted(paramOption.greaterEqual) && !(input >= paramOption.greaterEqual)) { | ||
sendError(res, scopeName, paramOptionName, invalidValueErrorMessage(input), 'greaterEqual, ' + paramOption.greaterEqual); | ||
return false; | ||
} | ||
// Check value - greaterThan | ||
if (isSetted(paramOpt.greaterThan) && !(input > paramOpt.greaterThan)) { | ||
sendError(res, opt.scope, k, invalidValueErrorMessage(input), 'greaterThan, ' + paramOpt.greaterThan); | ||
return false; | ||
} | ||
// Check value - lessThan | ||
if (isSetted(paramOption.lessThan) && !(input < paramOption.lessThan)) { | ||
sendError(res, scopeName, paramOptionName, invalidValueErrorMessage(input), 'lessThan, ' + paramOption.lessThan); | ||
return false; | ||
} | ||
// Check value - greaterEqual | ||
if (isSetted(paramOpt.greaterEqual) && !(input >= paramOpt.greaterEqual)) { | ||
sendError(res, opt.scope, k, invalidValueErrorMessage(input), 'greaterEqual, ' + paramOpt.greaterEqual); | ||
return false; | ||
} | ||
// Check value - lessEqual | ||
if (isSetted(paramOption.lessEqual) && !(input <= paramOption.lessEqual)) { | ||
sendError(res, scopeName, paramOptionName, invalidValueErrorMessage(input), 'lessEqual, ' + paramOption.lessEqual); | ||
return false; | ||
} | ||
// Check value - lessThan | ||
if (isSetted(paramOpt.lessThan) && !(input < paramOpt.lessThan)) { | ||
sendError(res, opt.scope, k, invalidValueErrorMessage(input), 'lessThan, ' + paramOpt.lessThan); | ||
return false; | ||
} | ||
// Check value - allowEmpty | ||
if (isSetted(paramOption.allowEmpty) && !paramOption.allowEmpty && input === '') { | ||
sendError(res, scopeName, paramOptionName, invalidValueErrorMessage(input), 'allowEmpty, ' + paramOption.allowEmpty); | ||
return false; | ||
} | ||
// Check value - lessEqual | ||
if (isSetted(paramOpt.lessEqual) && !(input <= paramOpt.lessEqual)) { | ||
sendError(res, opt.scope, k, invalidValueErrorMessage(input), 'lessEqual, ' + paramOpt.lessEqual); | ||
return false; | ||
} | ||
// Check value - maxLength | ||
if (isSetted(paramOption.maxLength) && ('' + input).length > paramOption.maxLength) { | ||
sendError(res, scopeName, paramOptionName, invalidValueErrorMessage(input), 'maxLength, ' + paramOption.maxLength); | ||
return false; | ||
} | ||
// Check value - allowEmpty | ||
if (isSetted(paramOpt.allowEmpty) && !paramOpt.allowEmpty && input === '') { | ||
sendError(res, opt.scope, k, invalidValueErrorMessage(input), 'allowEmpty, ' + paramOpt.allowEmpty); | ||
return false; | ||
} | ||
// Check value - minLength | ||
if (isSetted(paramOption.minLength) && ('' + input).length < paramOption.minLength) { | ||
sendError(res, scopeName, paramOptionName, invalidValueErrorMessage(input), 'minLength, ' + paramOption.minLength); | ||
return false; | ||
} | ||
// Check value - maxLength | ||
if (isSetted(paramOpt.maxLength) && ('' + input).length > paramOpt.maxLength) { | ||
sendError(res, opt.scope, k, invalidValueErrorMessage(input), 'maxLength, ' + paramOpt.maxLength); | ||
return false; | ||
// Check value - isEmpty | ||
if (isSetted(paramOption.isEmpty) && (paramOption.isEmpty ^ input === '')) { | ||
sendError(res, scopeName, paramOptionName, invalidValueMessage(input), 'isEmpty, ' + paramOption.isEmpty); | ||
return false; | ||
} | ||
} | ||
// Check value - minLength | ||
if (isSetted(paramOpt.minLength) && ('' + input).length < paramOpt.minLength) { | ||
sendError(res, opt.scope, k, invalidValueErrorMessage(input), 'minLength, ' + paramOpt.minLength); | ||
return false; | ||
} | ||
// Check value - isEmpty | ||
if (isSetted(paramOpt.isEmpty) && (paramOpt.isEmpty ^ input === '')) { | ||
sendError(res, opt.scope, k, invalidValueMessage(input), 'isEmpty, ' + paramOpt.isEmpty); | ||
return false; | ||
} | ||
} | ||
@@ -346,3 +404,3 @@ | ||
next(); | ||
} | ||
}; | ||
@@ -352,5 +410,18 @@ return middleware; | ||
module.exports.DEBUG_ENABLED = false; | ||
module.exports.errorHttpStatus = 400; | ||
module.exports.errorMessageFormat = '[express-request-checker] Scope: `{{scope}}`, ParamName: `{{paramName}}`, ErrorMessage: {{errorMessage}}. {{errorDetail}}'; | ||
module.exports.SEND_ERROR_DETAIL = function(enabled) { | ||
sendErrorDetailFlag = enabled; | ||
}; | ||
module.exports.setErrorHttpStatusCode = function(code) { | ||
errorHttpStatusCode = code; | ||
}; | ||
module.exports.setErrorHttpResponseHeaders = function(headers) { | ||
errorHttpResponseHeaders = headers; | ||
}; | ||
module.exports.setErrorHttpResponseBodyFormat = function(format) { | ||
errorHttpResponseBodyFormat = format; | ||
}; | ||
module.exports.requestChecker = requestChecker; |
{ | ||
"name": "express-request-checker", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Express request checker extension.", | ||
@@ -18,8 +18,12 @@ "main": "express-request-checker.js", | ||
"keywords": [ | ||
"request", | ||
"checker", | ||
"validation", | ||
"validator", | ||
"query", | ||
"body" | ||
"middleware" | ||
], | ||
"author": "Yiling Zhou <pastgift@gmail.com>", | ||
"author": { | ||
"name": "Yiling Zhou", | ||
"email": "pastgift@gmail.com" | ||
}, | ||
"license": "MIT", | ||
@@ -26,0 +30,0 @@ "bugs": { |
# express-request-checker | ||
Create request checker middleware with options for Express. | ||
with express-request-checker, checking HTTP request's `query` or `body` will be more easy and readable. All the works is just `require` express-request-checker in `router.js` which belong to an Express project and config it. So it's no need to modify any other source file. | ||
[![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] | ||
Create request checker middleware for Express. | ||
with express-request-checker, checking HTTP request's `query`, `body` or url `params` will be more easy and readable. All the works is just `require` express-request-checker in `router.js` which belongs to an Express project and config it. So it's no need to modify any other source file. | ||
Since validation codes are written in a config-like way, `router.js` file will look like an API document, I hope that the communication cost within the development team can be reduced. | ||
- No validation codes. | ||
- `router.js` is also an API document. | ||
- Easy to combine with other validation package. | ||
### Quick Example(Javascript): | ||
@@ -17,5 +26,4 @@ ```javascript | ||
var options = { | ||
scope: 'query', // Check scope. ('query'|'body', DEFAULT: 'query') | ||
strict: false, // Allow unexpected parameter. (true|false, DEFAULT: true) | ||
params: { // Paramters. | ||
query: { // Check req.query. (params|query|body) | ||
'param1': { | ||
@@ -46,5 +54,4 @@ matchRegExp: /^[0-9]{1}$/ | ||
options = | ||
scope: 'query' # Check scope. ('query'|'body', DEFAULT: 'query') | ||
strict: false # Allow unexpected parameter. (true|false, DEFAULT: true) | ||
params: # Paramters. | ||
query: # Check req.query. (params|query|body) | ||
'param1': | ||
@@ -73,2 +80,7 @@ matchRegExp: /^[0-9]{1}$/ | ||
params: { | ||
'id': { | ||
assertTrue: validator.isInt | ||
} | ||
}, | ||
body: { | ||
'email': { | ||
@@ -82,3 +94,3 @@ assertTrue: validator.isEmail | ||
}; | ||
router.get('/path', reqChecker(options), handlerFunction); | ||
router.post('/user/:id', reqChecker(options), handlerFunction); | ||
@@ -92,3 +104,2 @@ module.exports = router; | ||
|------------|-------------| | ||
|scope |`query` | | ||
|strict |`true` | | ||
@@ -126,3 +137,3 @@ | ||
option = { | ||
params: { | ||
query: { | ||
param1: { | ||
@@ -146,3 +157,3 @@ assertTrue: [function(value) { return value > 10; }] | ||
option = { | ||
params: { | ||
query: { | ||
param1: { | ||
@@ -163,3 +174,3 @@ matchRegExp: [/^[012]{1}$/, /^[234]{1}$/] | ||
option = { | ||
params: { | ||
query: { | ||
param1: { | ||
@@ -184,3 +195,3 @@ isIn: [1, 2, 3] | ||
option = { | ||
params: { | ||
query: { | ||
param1: { | ||
@@ -202,3 +213,3 @@ isInteger: true | ||
option = { | ||
params: { | ||
query: { | ||
param1: { | ||
@@ -219,3 +230,3 @@ isEmail: true | ||
option = { | ||
params: { | ||
query: { | ||
param1: { | ||
@@ -237,3 +248,3 @@ equal: 100 | ||
option = { | ||
params: { | ||
query: { | ||
param1: { | ||
@@ -254,3 +265,3 @@ isEmpty: false | ||
option = { | ||
params: { | ||
query: { | ||
param1: { | ||
@@ -277,1 +288,6 @@ minLength: 5, | ||
[MIT](LICENSE) | ||
[downloads-image]: http://img.shields.io/npm/dm/express-request-checker.svg | ||
[npm-url]: https://npmjs.org/package/express-request-checker | ||
[npm-image]: http://img.shields.io/npm/v/express-request-checker.svg |
969
test/test.js
var assert = require('assert'); | ||
var reqCheckerModule = require('../express-request-checker'); | ||
var reqChecker = reqCheckerModule.requestChecker; | ||
var PRINT_RESULT_FLAG = false; | ||
describe('main', function() { | ||
reqCheckerModule.DEBUG_ENABLED = true; | ||
reqCheckerModule.SEND_ERROR_DETAIL(true); | ||
@@ -11,20 +14,31 @@ var options, fakeReq, fakeRes, fakeNext | ||
send: function(data) { | ||
console.log(data); | ||
if (PRINT_RESULT_FLAG) console.log(data); | ||
return false; | ||
}, | ||
status: function(status) {} | ||
status: function(status) { | ||
if (PRINT_RESULT_FLAG) console.log('status: ' + status); | ||
}, | ||
set: function(k, v) { | ||
if (PRINT_RESULT_FLAG) console.log('set header: ' + k + ', ' + v); | ||
} | ||
}; | ||
fakeNext = function() { | ||
return true; | ||
}; | ||
fakeNext = function() { return true; }; | ||
it('1-1 scope: default, strict: default -> No params required, Got 1 param', function() { | ||
options = { params: null }; | ||
fakeReq = { query: { param1: 1 } }; | ||
// ----- Basic ----- | ||
it('1-1 scope: <none>, strict: default -> No thing required, Each scope got 1 input', function() { | ||
options = { } ; | ||
fakeReq = { params: { param1: 1 }, query: { param1: 1 }, body: { param1: 1 } }; | ||
assert.notEqual(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('1-2 scope: params, strict: default -> No params required, Got 1 params input', function() { | ||
options = { params: {} }; | ||
fakeReq = { params: { param1: 1 } }; | ||
assert.equal(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('1-2 scope: query, strict: default -> No params required, Got 1 param', function() { | ||
options = { scope:'query', params: null }; | ||
it('1-3 scope: query, strict: default -> No query required, Got 1 query input', function() { | ||
options = { query: {} }; | ||
fakeReq = { query: { param1: 1 } }; | ||
@@ -34,4 +48,4 @@ assert.equal(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
it('1-3 scope: body, strict: default -> No params required, Got 1 param', function() { | ||
options = { scope:'body', params: null }; | ||
it('1-4 scope: body, strict: default -> No body required, Got 1 body input', function() { | ||
options = { body: {} }; | ||
fakeReq = { body: { param1: 1 } }; | ||
@@ -41,4 +55,4 @@ assert.equal(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
it('1-4 scope: default, strict: true -> No params required, Got 1 param', function() { | ||
options = { strict: true, params: null }; | ||
it('1-5 scope: query, strict: true -> No query required, Got 1 query input', function() { | ||
options = { strict: true, query: {} }; | ||
fakeReq = { query: { param1: 1 } }; | ||
@@ -48,4 +62,4 @@ assert.equal(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
it('1-5 scope: default, strict: false -> No params required, Got 1 param', function() { | ||
options = { strict: false, params: null }; | ||
it('1-6 scope: query, strict: false -> No query required, Got 1 query input', function() { | ||
options = { strict: false, query: {} }; | ||
fakeReq = { query: { param1: 1 } }; | ||
@@ -55,5 +69,7 @@ assert.notEqual(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
it('2-1 scope: default, strict: default -> 1 param required, Got 1 param', function() { | ||
options = { params: { | ||
param1: null | ||
it('2-1 scope: query, strict: default -> 1 query required, Got 1 query input', function() { | ||
options = { query: { | ||
param1: {} | ||
} }; | ||
@@ -66,5 +82,5 @@ fakeReq = { query: { | ||
it('2-2 scope: default, strict: default -> 1 param required, Got 2 params', function() { | ||
options = { params: { | ||
param1: null | ||
it('2-2 scope: query, strict: default -> 1 query required, Got 2 query inputs', function() { | ||
options = { query: { | ||
param1: {} | ||
} }; | ||
@@ -78,5 +94,5 @@ fakeReq = { query: { | ||
it('2-3 scope: default, strict: false -> 1 param required, Got 2 params', function() { | ||
options = { strict: false, params: { | ||
param1: null | ||
it('2-3 scope: query, strict: false -> 1 query required, Got 2 query inputs', function() { | ||
options = { strict: false, query: { | ||
param1: {} | ||
} }; | ||
@@ -90,6 +106,8 @@ fakeReq = { query: { | ||
it('3-1 scope: default, strict: default -> 2 params required, Got 1 param', function() { | ||
options = { strict: false, params: { | ||
param1: null, | ||
param2: null | ||
it('3-1 scope: query, strict: default -> 2 query required, Got 1 query input', function() { | ||
options = { query: { | ||
param1: {}, | ||
param2: {} | ||
} }; | ||
@@ -102,6 +120,6 @@ fakeReq = { query: { | ||
it('3-2 scope: default, strict: default -> 2 params required, Got 1 param, 1 unexpected param', function() { | ||
options = { strict: false, params: { | ||
param1: null, | ||
param2: null | ||
it('3-2 scope: query, strict: default -> 2 query required, Got 1 query, 1 unexpected query input', function() { | ||
options = { query: { | ||
param1: {}, | ||
param2: {} | ||
} }; | ||
@@ -115,6 +133,6 @@ fakeReq = { query: { | ||
it('3-3 scope: default, strict: false -> 2 params required, Got 2 param, 1 unexpected param', function() { | ||
options = { strict: false, params: { | ||
param1: null, | ||
param2: null | ||
it('3-3 scope: query, strict: false -> 2 query required, Got 2 query, 1 unexpected query input', function() { | ||
options = { strict: false, query: { | ||
param1: {}, | ||
param2: {} | ||
} }; | ||
@@ -129,4 +147,6 @@ fakeReq = { query: { | ||
it('4-1 scope: default, strict: default -> 1 param required(matchRegExp), Got 1 valid param', function() { | ||
options = { strict: false, params: { | ||
it('4-1 scope: query, strict: default -> 1 query required(matchRegExp), Got 1 valid query input', function() { | ||
options = { strict: false, query: { | ||
param1: { matchRegExp: /^[1]{1}$/ } | ||
@@ -140,4 +160,4 @@ } }; | ||
it('4-2 scope: default, strict: default -> 1 param required(matchRegExp), Got 1 invalid param', function() { | ||
options = { strict: false, params: { | ||
it('4-2 scope: query, strict: default -> 1 query required(matchRegExp), Got 1 invalid query input', function() { | ||
options = { strict: false, query: { | ||
param1: { matchRegExp: /^[1]{1}$/ } | ||
@@ -151,4 +171,6 @@ } }; | ||
it('5-1 scope: default, strict: default -> 1 param optional, Got 1 param', function() { | ||
options = { strict: false, params: { | ||
it('5-1 scope: query, strict: default -> 1 query optional, Got 1 query input', function() { | ||
options = { strict: false, query: { | ||
param1: { isOptional: true } | ||
@@ -162,4 +184,4 @@ } }; | ||
it('5-2 scope: default, strict: default -> 1 param optional, Got NO param', function() { | ||
options = { strict: false, params: { | ||
it('5-2 scope: query, strict: default -> 1 query optional, Got NO query input', function() { | ||
options = { strict: false, query: { | ||
param1: { isOptional: true } | ||
@@ -172,4 +194,4 @@ } }; | ||
it('5-3 scope: default, strict: default -> 1 param optional, Got invalid param', function() { | ||
options = { strict: false, params: { | ||
it('5-3 scope: query, strict: default -> 1 query optional, Got invalid query input', function() { | ||
options = { strict: false, query: { | ||
param1: { isOptional: true, matchRegExp: /^[1]{1}$/ } | ||
@@ -183,4 +205,6 @@ } }; | ||
it('6-1 scope: default, strict: default -> 1 param required, 1 param optional, Got 1 valid param', function() { | ||
options = { strict: false, params: { | ||
it('6-1 scope: query, strict: default -> 1 query required, 1 query optional, Got 1 valid query input', function() { | ||
options = { strict: false, query: { | ||
param1: { matchRegExp: /^[1]{2}$/ }, | ||
@@ -195,409 +219,526 @@ param2: { isOptional: true, matchRegExp: /^[1]{1}$/ } | ||
it('A-1 scope: default, strict: default -> 1 param required(assertTrue), Got 1 valid param', function() { | ||
options = { params: { | ||
param1: { assertTrue: function(v) { return v > 5; } } | ||
} }; | ||
fakeReq = { query: { | ||
param1: 6 | ||
} }; | ||
assert.notEqual(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('A-2 scope: default, strict: default -> 1 param required(assertTrue), Got 1 invalid param', function() { | ||
options = { params: { | ||
param1: { assertTrue: function(v) { return v > 5; } } | ||
} }; | ||
fakeReq = { query: { | ||
param1: 5 | ||
} }; | ||
assert.equal(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
// ----- Value Checking ----- | ||
it('B-1 scope: default, strict: default -> 1 param required(assertFalse), Got 1 valid param', function() { | ||
options = { params: { | ||
param1: { assertFalse: function(v) { return v > 5; } } | ||
} }; | ||
fakeReq = { query: { | ||
param1: 5 | ||
} }; | ||
assert.notEqual(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
var scopes = ['params', 'query', 'body']; | ||
for (var iScope in scopes) { | ||
scopeName = scopes[iScope]; | ||
it('B-2 scope: default, strict: default -> 1 param required(assertFalse), Got 1 invalid param', function() { | ||
options = { params: { | ||
param1: { assertFalse: function(v) { return v > 5; } } | ||
} }; | ||
fakeReq = { query: { | ||
param1: 6 | ||
} }; | ||
assert.equal(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('A-1 scope: ' + scopeName + ', strict: default -> 1 ' + scopeName + ' required(assertTrue), Got 1 valid ' + scopeName + ' input', function() { | ||
options = {}; | ||
options[scopeName] = { | ||
param1: { assertTrue: function(v) { return v > 5; } } | ||
}; | ||
fakeReq = {}; | ||
fakeReq[scopeName] = { | ||
param1: 6 | ||
}; | ||
assert.notEqual(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('C-1 scope: default, strict: default -> 1 param required(matchRegExp), Got 1 valid param', function() { | ||
options = { params: { | ||
param1: { matchRegExp: [ | ||
/^[012]{1}$/, | ||
/^[234]{1}$/ | ||
] } | ||
} }; | ||
fakeReq = { query: { | ||
param1: 2 | ||
} }; | ||
assert.notEqual(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('A-2 scope: ' + scopeName + ', strict: default -> 1 ' + scopeName + ' required(assertTrue), Got 1 invalid ' + scopeName + ' input', function() { | ||
options = {}; | ||
options[scopeName] = { | ||
param1: { assertTrue: function(v) { return v > 5; } } | ||
}; | ||
fakeReq = {}; | ||
fakeReq[scopeName] = { | ||
param1: 5 | ||
}; | ||
assert.equal(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('C-2 scope: default, strict: default -> 1 param required(matchRegExp), Got 1 invalid param', function() { | ||
options = { params: { | ||
param1: { matchRegExp: [ | ||
/^[012]{1}$/, | ||
/^[234]{1}$/ | ||
] } | ||
} }; | ||
fakeReq = { query: { | ||
param1: 1 | ||
} }; | ||
assert.equal(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('D-1 scope: default, strict: default -> 1 param required(isIn), Got 1 valid param', function() { | ||
options = { params: { | ||
param1: { isIn: [1, 2, 3] } | ||
} }; | ||
fakeReq = { query: { | ||
param1: 2 | ||
} }; | ||
assert.notEqual(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('D-2 scope: default, strict: default -> 1 param required(isIn), Got 1 invalid param', function() { | ||
options = { params: { | ||
param1: { isIn: [1, 2, 3] } | ||
} }; | ||
fakeReq = { query: { | ||
param1: 4 | ||
} }; | ||
assert.equal(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('B-1 scope: ' + scopeName + ', strict: default -> 1 ' + scopeName + ' required(assertFalse), Got 1 valid ' + scopeName + ' input', function() { | ||
options = {}; | ||
options[scopeName] = { | ||
param1: { assertFalse: function(v) { return v > 5; } } | ||
}; | ||
fakeReq = {}; | ||
fakeReq[scopeName] = { | ||
param1: 5 | ||
}; | ||
assert.notEqual(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('E-1 scope: default, strict: default -> 1 param required(notIn), Got 1 valid param', function() { | ||
options = { params: { | ||
param1: { notIn: [1, 2, 3] } | ||
} }; | ||
fakeReq = { query: { | ||
param1: 4 | ||
} }; | ||
assert.notEqual(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('B-2 scope: ' + scopeName + ', strict: default -> 1 ' + scopeName + ' required(assertFalse), Got 1 invalid ' + scopeName + ' input', function() { | ||
options = {}; | ||
options[scopeName] = { | ||
param1: { assertFalse: function(v) { return v > 5; } } | ||
}; | ||
fakeReq = {}; | ||
fakeReq[scopeName] = { | ||
param1: 6 | ||
}; | ||
assert.equal(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('E-2 scope: default, strict: default -> 1 param required(notIn), Got 1 invalid param', function() { | ||
options = { params: { | ||
param1: { notIn: [1, 2, 3] } | ||
} }; | ||
fakeReq = { query: { | ||
param1: 2 | ||
} }; | ||
assert.equal(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('F-1 scope: default, strict: default -> 1 param required(isInteger:true), Got 1 integer', function() { | ||
options = { params: { | ||
param1: { isInteger: true } | ||
} }; | ||
fakeReq = { query: { | ||
param1: 1 | ||
} }; | ||
assert.notEqual(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('F-2 scope: default, strict: default -> 1 param required(isInteger:true), Got 1 non-integer', function() { | ||
options = { params: { | ||
param1: { isInteger: true } | ||
} }; | ||
fakeReq = { query: { | ||
param1: 'hello' | ||
} }; | ||
assert.equal(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('C-1 scope: ' + scopeName + ', strict: default -> 1 ' + scopeName + ' required(matchRegExp), Got 1 valid ' + scopeName + ' input', function() { | ||
options = {}; | ||
options[scopeName] = { | ||
param1: { matchRegExp: [ | ||
/^[012]{1}$/, | ||
/^[234]{1}$/ | ||
] } | ||
}; | ||
fakeReq = {}; | ||
fakeReq[scopeName] = { | ||
param1: 2 | ||
}; | ||
assert.notEqual(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('F-3 scope: default, strict: default -> 1 param required(isInteger:false), Got 1 integer', function() { | ||
options = { params: { | ||
param1: { isInteger: false } | ||
} }; | ||
fakeReq = { query: { | ||
param1: 1 | ||
} }; | ||
assert.equal(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('C-2 scope: ' + scopeName + ', strict: default -> 1 ' + scopeName + ' required(matchRegExp), Got 1 invalid ' + scopeName + ' input', function() { | ||
options = {}; | ||
options[scopeName] = { | ||
param1: { matchRegExp: [ | ||
/^[012]{1}$/, | ||
/^[234]{1}$/ | ||
] } | ||
}; | ||
fakeReq = {}; | ||
fakeReq[scopeName] = { | ||
param1: 1 | ||
}; | ||
assert.equal(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('F-4 scope: default, strict: default -> 1 param required(isInteger:false), Got 1 non-integer', function() { | ||
options = { params: { | ||
param1: { isInteger: false } | ||
} }; | ||
fakeReq = { query: { | ||
param1: 'hello' | ||
} }; | ||
assert.notEqual(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('F-5 scope: default, strict: default -> 1 param required(isEmail:true), Got 1 email', function() { | ||
options = { params: { | ||
param1: { isEmail: true } | ||
} }; | ||
fakeReq = { query: { | ||
param1: 'abc@123.com' | ||
} }; | ||
assert.notEqual(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('D-1 scope: ' + scopeName + ', strict: default -> 1 ' + scopeName + ' required(isIn), Got 1 valid ' + scopeName + ' input', function() { | ||
options = {}; | ||
options[scopeName] = { | ||
param1: { isIn: [1, 2, 3] } | ||
}; | ||
fakeReq = {}; | ||
fakeReq[scopeName] = { | ||
param1: 2 | ||
}; | ||
assert.notEqual(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('F-6 scope: default, strict: default -> 1 param required(isEmail:true), Got 1 non-email', function() { | ||
options = { params: { | ||
param1: { isEmail: true } | ||
} }; | ||
fakeReq = { query: { | ||
param1: 'abc.123.com' | ||
} }; | ||
assert.equal(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('D-2 scope: ' + scopeName + ', strict: default -> 1 ' + scopeName + ' required(isIn), Got 1 invalid ' + scopeName + ' input', function() { | ||
options = {}; | ||
options[scopeName] = { | ||
param1: { isIn: [1, 2, 3] } | ||
}; | ||
fakeReq = {}; | ||
fakeReq[scopeName] = { | ||
param1: 4 | ||
}; | ||
assert.equal(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('F-7 scope: default, strict: default -> 1 param required(isEmail:false), Got 1 email', function() { | ||
options = { params: { | ||
param1: { isEmail: false } | ||
} }; | ||
fakeReq = { query: { | ||
param1: 'abc@123.com' | ||
} }; | ||
assert.equal(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('F-8 scope: default, strict: default -> 1 param required(isEmail:false), Got 1 non-email', function() { | ||
options = { params: { | ||
param1: { isEmail: false } | ||
} }; | ||
fakeReq = { query: { | ||
param1: 'abc.123.com' | ||
} }; | ||
assert.notEqual(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('G-1 scope: default, strict: default -> 1 param required(equal), Got 1 valid param', function() { | ||
options = { params: { | ||
param1: { equal: 5 } | ||
} }; | ||
fakeReq = { query: { | ||
param1: 5 | ||
} }; | ||
assert.notEqual(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('E-1 scope: ' + scopeName + ', strict: default -> 1 ' + scopeName + ' required(notIn), Got 1 valid ' + scopeName + ' input', function() { | ||
options = {}; | ||
options[scopeName] = { | ||
param1: { notIn: [1, 2, 3] } | ||
}; | ||
fakeReq = {}; | ||
fakeReq[scopeName] = { | ||
param1: 4 | ||
}; | ||
assert.notEqual(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('G-2 scope: default, strict: default -> 1 param required(equal), Got 1 invalid param', function() { | ||
options = { params: { | ||
param1: { equal: 5 } | ||
} }; | ||
fakeReq = { query: { | ||
param1: 6 | ||
} }; | ||
assert.equal(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('E-2 scope: ' + scopeName + ', strict: default -> 1 ' + scopeName + ' required(notIn), Got 1 invalid ' + scopeName + ' input', function() { | ||
options = {}; | ||
options[scopeName] = { | ||
param1: { notIn: [1, 2, 3] } | ||
}; | ||
fakeReq = {}; | ||
fakeReq[scopeName] = { | ||
param1: 2 | ||
}; | ||
assert.equal(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('H-1 scope: default, strict: default -> 1 param required(greaterThan), Got 1 valid param', function() { | ||
options = { params: { | ||
param1: { greaterThan: 5 } | ||
} }; | ||
fakeReq = { query: { | ||
param1: 6 | ||
} }; | ||
assert.notEqual(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('H-2 scope: default, strict: default -> 1 param required(greaterThan), Got 1 invalid param', function() { | ||
options = { params: { | ||
param1: { greaterThan: 5 } | ||
} }; | ||
fakeReq = { query: { | ||
param1: 5 | ||
} }; | ||
assert.equal(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('H-3 scope: default, strict: default -> 1 param required(greaterEqual), Got 1 valid param', function() { | ||
options = { params: { | ||
param1: { greaterEqual: 5 } | ||
} }; | ||
fakeReq = { query: { | ||
param1: 5 | ||
} }; | ||
assert.notEqual(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('F-1 scope: ' + scopeName + ', strict: default -> 1 ' + scopeName + ' required(isInteger:true), Got 1 integer ' + scopeName + ' input', function() { | ||
options = {}; | ||
options[scopeName] = { | ||
param1: { isInteger: true } | ||
}; | ||
fakeReq = {}; | ||
fakeReq[scopeName] = { | ||
param1: 1 | ||
}; | ||
assert.notEqual(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('H-4 scope: default, strict: default -> 1 param required(greaterEqual), Got 1 invalid param', function() { | ||
options = { params: { | ||
param1: { greaterEqual: 5 } | ||
} }; | ||
fakeReq = { query: { | ||
param1: 4 | ||
} }; | ||
assert.equal(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('F-2 scope: ' + scopeName + ', strict: default -> 1 ' + scopeName + ' required(isInteger:true), Got 1 non-integer ' + scopeName + ' input', function() { | ||
options = {}; | ||
options[scopeName] = { | ||
param1: { isInteger: true } | ||
}; | ||
fakeReq = {}; | ||
fakeReq[scopeName] = { | ||
param1: 'hello' | ||
}; | ||
assert.equal(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('I-1 scope: default, strict: default -> 1 param required(lessThan), Got 1 valid param', function() { | ||
options = { params: { | ||
param1: { lessThan: 5 } | ||
} }; | ||
fakeReq = { query: { | ||
param1: 4 | ||
} }; | ||
assert.notEqual(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('F-3 scope: ' + scopeName + ', strict: default -> 1 ' + scopeName + ' required(isInteger:false), Got 1 integer ' + scopeName + ' input', function() { | ||
options = {}; | ||
options[scopeName] = { | ||
param1: { isInteger: false } | ||
}; | ||
fakeReq = {}; | ||
fakeReq[scopeName] = { | ||
param1: 1 | ||
}; | ||
assert.equal(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('I-2 scope: default, strict: default -> 1 param required(lessThan), Got 1 invalid param', function() { | ||
options = { params: { | ||
param1: { lessThan: 5 } | ||
} }; | ||
fakeReq = { query: { | ||
param1: 5 | ||
} }; | ||
assert.equal(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('F-4 scope: ' + scopeName + ', strict: default -> 1 ' + scopeName + ' required(isInteger:false), Got 1 non-integer ' + scopeName + ' input', function() { | ||
options = {}; | ||
options[scopeName] = { | ||
param1: { isInteger: false } | ||
}; | ||
fakeReq = {}; | ||
fakeReq[scopeName] = { | ||
param1: 'hello' | ||
}; | ||
assert.notEqual(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('I-3 scope: default, strict: default -> 1 param required(lessEqual), Got 1 valid param', function() { | ||
options = { params: { | ||
param1: { lessEqual: 5 } | ||
} }; | ||
fakeReq = { query: { | ||
param1: 5 | ||
} }; | ||
assert.notEqual(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('F-5 scope: ' + scopeName + ', strict: default -> 1 ' + scopeName + ' required(isEmail:true), Got 1 email ' + scopeName + ' input', function() { | ||
options = {}; | ||
options[scopeName] = { | ||
param1: { isEmail: true } | ||
}; | ||
fakeReq = {}; | ||
fakeReq[scopeName] = { | ||
param1: 'abc@123.com' | ||
}; | ||
assert.notEqual(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('I-4 scope: default, strict: default -> 1 param required(lessEqual), Got 1 invalid param', function() { | ||
options = { params: { | ||
param1: { lessEqual: 5 } | ||
} }; | ||
fakeReq = { query: { | ||
param1: 6 | ||
} }; | ||
assert.equal(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('F-6 scope: ' + scopeName + ', strict: default -> 1 ' + scopeName + ' required(isEmail:true), Got 1 non-email ' + scopeName + ' input', function() { | ||
options = {}; | ||
options[scopeName] = { | ||
param1: { isEmail: true } | ||
}; | ||
fakeReq = {}; | ||
fakeReq[scopeName] = { | ||
param1: 'abc.123.com' | ||
}; | ||
assert.equal(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('J-1 scope: default, strict: default -> 1 param required(allowEmpty:true), Got 1 empty param', function() { | ||
options = { params: { | ||
param1: { allowEmpty: true} | ||
} }; | ||
fakeReq = { query: { | ||
param1: "" | ||
} }; | ||
assert.notEqual(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('F-7 scope: ' + scopeName + ', strict: default -> 1 ' + scopeName + ' required(isEmail:false), Got 1 email ' + scopeName + ' input', function() { | ||
options = {}; | ||
options[scopeName] = { | ||
param1: { isEmail: false } | ||
}; | ||
fakeReq = {}; | ||
fakeReq[scopeName] = { | ||
param1: 'abc@123.com' | ||
}; | ||
assert.equal(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('J-2 scope: default, strict: default -> 1 param required(allowEmpty:true), Got 1 non-empty param', function() { | ||
options = { params: { | ||
param1: { allowEmpty: true} | ||
it('F-8 scope: ' + scopeName + ', strict: default -> 1 ' + scopeName + ' required(isEmail:false), Got 1 non-email ' + scopeName + ' input', function() { | ||
options = {}; | ||
options[scopeName] = { | ||
param1: { isEmail: false } | ||
}; | ||
fakeReq = {}; | ||
fakeReq[scopeName] = { | ||
param1: 'abc.123.com' | ||
}; | ||
assert.notEqual(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
} }; | ||
fakeReq = { query: { | ||
param1: "hello" | ||
} }; | ||
assert.notEqual(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('J-3 scope: default, strict: default -> 1 param required(allowEmpty:false), Got 1 empty param', function() { | ||
options = { params: { | ||
param1: { allowEmpty: false} | ||
} }; | ||
fakeReq = { query: { | ||
param1: "" | ||
} }; | ||
assert.equal(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('J-4 scope: default, strict: default -> 1 param required(allowEmpty:false), Got 1 non-empty param', function() { | ||
options = { params: { | ||
param1: { allowEmpty: false} | ||
} }; | ||
fakeReq = { query: { | ||
param1: "hello" | ||
} }; | ||
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() { | ||
options = {}; | ||
options[scopeName] = { | ||
param1: { equal: 5 } | ||
}; | ||
fakeReq = {}; | ||
fakeReq[scopeName] = { | ||
param1: 5 | ||
}; | ||
assert.notEqual(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('K-1 scope: default, strict: default -> 1 param required(maxLength), Got 1 valid param', function() { | ||
options = { params: { | ||
param1: { maxLength: 5} | ||
} }; | ||
fakeReq = { query: { | ||
param1: "hello" | ||
} }; | ||
assert.notEqual(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('G-2 scope: ' + scopeName + ', strict: default -> 1 ' + scopeName + ' required(equal), Got 1 invalid ' + scopeName + ' input', function() { | ||
options = {}; | ||
options[scopeName] = { | ||
param1: { equal: 5 } | ||
}; | ||
fakeReq = {}; | ||
fakeReq[scopeName] = { | ||
param1: 6 | ||
}; | ||
assert.equal(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('K-2 scope: default, strict: default -> 1 param required(maxLength), Got 1 invalid param', function() { | ||
options = { params: { | ||
param1: { maxLength: 5} | ||
} }; | ||
fakeReq = { query: { | ||
param1: "hello " | ||
} }; | ||
assert.equal(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('K-3 scope: default, strict: default -> 1 param required(minLength), Got 1 valid param', function() { | ||
options = { params: { | ||
param1: { minLength: 5} | ||
} }; | ||
fakeReq = { query: { | ||
param1: "hello" | ||
} }; | ||
assert.notEqual(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('K-4 scope: default, strict: default -> 1 param required(minLength), Got 1 invalid param', function() { | ||
options = { params: { | ||
param1: { minLength: 5} | ||
} }; | ||
fakeReq = { query: { | ||
param1: "hell" | ||
} }; | ||
assert.equal(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('H-1 scope: ' + scopeName + ', strict: default -> 1 ' + scopeName + ' required(greaterThan), Got 1 valid ' + scopeName + ' input', function() { | ||
options = {}; | ||
options[scopeName] = { | ||
param1: { greaterThan: 5 } | ||
}; | ||
fakeReq = {}; | ||
fakeReq[scopeName] = { | ||
param1: 6 | ||
}; | ||
assert.notEqual(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
/****************************/ | ||
/* Play with other packages */ | ||
/****************************/ | ||
var validator = require('validator'); | ||
it('CO-1 scope: default, strict: default -> 1 param required(play with `validator`), Got 1 valid param', function() { | ||
options = { params: { | ||
param1: { assertTrue: validator.isEmail} | ||
} }; | ||
fakeReq = { query: { | ||
param1: "foo@bar.com" | ||
} }; | ||
assert.notEqual(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('H-2 scope: ' + scopeName + ', strict: default -> 1 ' + scopeName + ' required(greaterThan), Got 1 invalid ' + scopeName + ' input', function() { | ||
options = {}; | ||
options[scopeName] = { | ||
param1: { greaterThan: 5 } | ||
}; | ||
fakeReq = {}; | ||
fakeReq[scopeName] = { | ||
param1: 5 | ||
}; | ||
assert.equal(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('CO-2 scope: default, strict: default -> 1 param required(play with `validator`), Got 1 invalid param', function() { | ||
options = { params: { | ||
param1: { assertTrue: validator.isEmail} | ||
} }; | ||
fakeReq = { query: { | ||
param1: "foo@.com" | ||
} }; | ||
assert.equal(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('H-3 scope: ' + scopeName + ', strict: default -> 1 ' + scopeName + ' required(greaterEqual), Got 1 valid ' + scopeName + ' input', function() { | ||
options = {}; | ||
options[scopeName] = { | ||
param1: { greaterEqual: 5 } | ||
}; | ||
fakeReq = {}; | ||
fakeReq[scopeName] = { | ||
param1: 5 | ||
}; | ||
assert.notEqual(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('CO-3 scope: default, strict: default -> 1 param required(play with `validator`), Got 1 valid param', function() { | ||
options = { params: { | ||
param1: { assertTrue: validator.isJSON} | ||
} }; | ||
fakeReq = { query: { | ||
param1: '{"foo":1, "bar":2}' | ||
} }; | ||
assert.notEqual(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('H-4 scope: ' + scopeName + ', strict: default -> 1 ' + scopeName + ' required(greaterEqual), Got 1 invalid ' + scopeName + ' input', function() { | ||
options = {}; | ||
options[scopeName] = { | ||
param1: { greaterEqual: 5 } | ||
}; | ||
fakeReq = {}; | ||
fakeReq[scopeName] = { | ||
param1: 4 | ||
}; | ||
assert.equal(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('CO-4 scope: default, strict: default -> 1 param required(play with `validator`), Got 1 invalid param', function() { | ||
it('I-1 scope: ' + scopeName + ', strict: default -> 1 ' + scopeName + ' required(lessThan), Got 1 valid ' + scopeName + ' input', function() { | ||
options = {}; | ||
options[scopeName] = { | ||
param1: { lessThan: 5 } | ||
}; | ||
fakeReq = {}; | ||
fakeReq[scopeName] = { | ||
param1: 4 | ||
}; | ||
assert.notEqual(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('I-2 scope: ' + scopeName + ', strict: default -> 1 ' + scopeName + ' required(lessThan), Got 1 invalid ' + scopeName + ' input', function() { | ||
options = {}; | ||
options[scopeName] = { | ||
param1: { lessThan: 5 } | ||
}; | ||
fakeReq = {}; | ||
fakeReq[scopeName] = { | ||
param1: 5 | ||
}; | ||
assert.equal(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('I-3 scope: ' + scopeName + ', strict: default -> 1 ' + scopeName + ' required(lessEqual), Got 1 valid ' + scopeName + ' input', function() { | ||
options = {}; | ||
options[scopeName] = { | ||
param1: { lessEqual: 5 } | ||
}; | ||
fakeReq = {}; | ||
fakeReq[scopeName] = { | ||
param1: 5 | ||
}; | ||
assert.notEqual(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('I-4 scope: ' + scopeName + ', strict: default -> 1 ' + scopeName + ' required(lessEqual), Got 1 invalid ' + scopeName + ' input', function() { | ||
options = {}; | ||
options[scopeName] = { | ||
param1: { lessEqual: 5 } | ||
}; | ||
fakeReq = {}; | ||
fakeReq[scopeName] = { | ||
param1: 6 | ||
}; | ||
assert.equal(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('J-1 scope: ' + scopeName + ', strict: default -> 1 ' + scopeName + ' required(allowEmpty:true), Got 1 empty ' + scopeName + ' input', function() { | ||
options = {}; | ||
options[scopeName] = { | ||
param1: { allowEmpty: true} | ||
}; | ||
fakeReq = {}; | ||
fakeReq[scopeName] = { | ||
param1: "" | ||
}; | ||
assert.notEqual(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('J-2 scope: ' + scopeName + ', strict: default -> 1 ' + scopeName + ' required(allowEmpty:true), Got 1 non-empty ' + scopeName + ' input', function() { | ||
options = {}; | ||
options[scopeName] = { | ||
param1: { allowEmpty: true} | ||
}; | ||
fakeReq = {}; | ||
fakeReq[scopeName] = { | ||
param1: "hello" | ||
}; | ||
assert.notEqual(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('J-3 scope: ' + scopeName + ', strict: default -> 1 ' + scopeName + ' required(allowEmpty:false), Got 1 empty ' + scopeName + ' input', function() { | ||
options = {}; | ||
options[scopeName] = { | ||
param1: { allowEmpty: false} | ||
}; | ||
fakeReq = {}; | ||
fakeReq[scopeName] = { | ||
param1: "" | ||
}; | ||
assert.equal(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('J-4 scope: ' + scopeName + ', strict: default -> 1 ' + scopeName + ' required(allowEmpty:false), Got 1 non-empty ' + scopeName + ' input', function() { | ||
options = {}; | ||
options[scopeName] = { | ||
param1: { allowEmpty: false} | ||
}; | ||
fakeReq = {}; | ||
fakeReq[scopeName] = { | ||
param1: "hello" | ||
}; | ||
assert.notEqual(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('K-1 scope: ' + scopeName + ', strict: default -> 1 ' + scopeName + ' required(maxLength), Got 1 valid ' + scopeName + ' input', function() { | ||
options = {}; | ||
options[scopeName] = { | ||
param1: { maxLength: 5} | ||
}; | ||
fakeReq = {}; | ||
fakeReq[scopeName] = { | ||
param1: "hello" | ||
}; | ||
assert.notEqual(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('K-2 scope: ' + scopeName + ', strict: default -> 1 ' + scopeName + ' required(maxLength), Got 1 invalid ' + scopeName + ' input', function() { | ||
options = {}; | ||
options[scopeName] = { | ||
param1: { maxLength: 5} | ||
}; | ||
fakeReq = {}; | ||
fakeReq[scopeName] = { | ||
param1: "hello " | ||
}; | ||
assert.equal(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('K-3 scope: ' + scopeName + ', strict: default -> 1 ' + scopeName + ' required(minLength), Got 1 valid ' + scopeName + ' input', function() { | ||
options = {}; | ||
options[scopeName] = { | ||
param1: { minLength: 5} | ||
}; | ||
fakeReq = {}; | ||
fakeReq[scopeName] = { | ||
param1: "hello" | ||
}; | ||
assert.notEqual(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('K-4 scope: ' + scopeName + ', strict: default -> 1 ' + scopeName + ' required(minLength), Got 1 invalid ' + scopeName + ' input', function() { | ||
options = {}; | ||
options[scopeName] = { | ||
param1: { minLength: 5} | ||
}; | ||
fakeReq = {}; | ||
fakeReq[scopeName] = { | ||
param1: "Oops" | ||
}; | ||
assert.equal(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
/****************************/ | ||
/* Play with other packages */ | ||
/****************************/ | ||
var validator = require('validator'); | ||
it('CO-1 scope: ' + scopeName + ', strict: default -> 1 ' + scopeName + ' required(play with `validator`), Got 1 valid ' + scopeName + ' input', function() { | ||
options = {}; | ||
options[scopeName] = { | ||
param1: { assertTrue: validator.isEmail} | ||
}; | ||
fakeReq = {}; | ||
fakeReq[scopeName] = { | ||
param1: "foo@bar.com" | ||
}; | ||
assert.notEqual(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('CO-2 scope: ' + scopeName + ', strict: default -> 1 ' + scopeName + ' required(play with `validator`), Got 1 invalid ' + scopeName + ' input', function() { | ||
options = {}; | ||
options[scopeName] = { | ||
param1: { assertTrue: validator.isEmail} | ||
}; | ||
fakeReq = {}; | ||
fakeReq[scopeName] = { | ||
param1: "foo@.com" | ||
}; | ||
assert.equal(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('CO-3 scope: ' + scopeName + ', strict: default -> 1 ' + scopeName + ' required(play with `validator`), Got 1 valid ' + scopeName + ' input', function() { | ||
options = {}; | ||
options[scopeName] = { | ||
param1: { assertTrue: validator.isJSON} | ||
}; | ||
fakeReq = {}; | ||
fakeReq[scopeName] = { | ||
param1: '{"foo":1, "bar":2}' | ||
}; | ||
assert.notEqual(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
it('CO-4 scope: ' + scopeName + ', strict: default -> 1 ' + scopeName + ' required(play with `validator`), Got 1 invalid ' + scopeName + ' input', function() { | ||
options = {}; | ||
options[scopeName] = { | ||
param1: { assertTrue: validator.isJSON} | ||
}; | ||
fakeReq = {}; | ||
fakeReq[scopeName] = { | ||
param1: '{"foo":1 "bar":2}' | ||
}; | ||
assert.equal(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); | ||
}); | ||
} | ||
// ----- No Param scope ----- | ||
it('X scope: params, strict: default -> 1 params required, NO params input', function() { | ||
options = { params: { | ||
param1: { assertTrue: validator.isJSON} | ||
param1: { matchRegExp: /^[1]{2}$/ }, | ||
} }; | ||
fakeReq = { query: { | ||
param1: '{"foo":1 "bar":2}' | ||
param1: 11 | ||
} }; | ||
@@ -604,0 +745,0 @@ assert.equal(false, reqChecker(options)(fakeReq, fakeRes, fakeNext)); |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
45074
970
279
1