@ssense/restify-request-validator
Advanced tools
Comparing version 1.0.7 to 1.0.8
@@ -82,3 +82,3 @@ "use strict"; | ||
} | ||
errorMessages = errorMessages.concat(this.validateField(key, input[key], type, paramValidation)); | ||
errorMessages = errorMessages.concat(this.validateField(input, key, type, paramValidation)); | ||
if (this.failOnFirstError && errorMessages.length) { | ||
@@ -94,29 +94,29 @@ break; | ||
}; | ||
RequestValidator.prototype.validateField = function (key, value, type, paramValidation) { | ||
RequestValidator.prototype.validateField = function (input, key, type, paramValidation) { | ||
var errorMessages = []; | ||
var typeValidation = { value: value, type: paramValidation.type }; | ||
var typeValidation = { value: input[key], type: paramValidation.type }; | ||
if (RequestValidator.checkType(typeValidation) !== true) { | ||
errorMessages.push("Param " + key + " has invalid type (" + paramValidation.type + ")"); | ||
} | ||
value = typeValidation.value; | ||
input[key] = typeValidation.value; | ||
if (type !== 'undefined' && paramValidation.type === 'numeric') { | ||
value = parseInt(value, 10); | ||
input[key] = parseInt(input[key], 10); | ||
} | ||
if (value instanceof Array | ||
&& RequestValidator.checkArrayType(value, paramValidation.arrayType) !== true) { | ||
if (input[key] instanceof Array | ||
&& RequestValidator.checkArrayType(input[key], paramValidation.arrayType) !== true) { | ||
errorMessages.push("Param " + key + " has invalid content type (" + paramValidation.arrayType + "[])"); | ||
} | ||
if (RequestValidator.checkLength(value, paramValidation.length) !== true) { | ||
if (RequestValidator.checkLength(input[key], paramValidation.length) !== true) { | ||
errorMessages.push("Param " + key + " must have a length of " + paramValidation.length); | ||
} | ||
if (RequestValidator.checkMin(value, paramValidation.min) !== true) { | ||
if (RequestValidator.checkMin(input[key], paramValidation.min) !== true) { | ||
errorMessages.push("Param " + key + " must have a minimum length of " + paramValidation.min); | ||
} | ||
if (RequestValidator.checkMax(value, paramValidation.max) !== true) { | ||
if (RequestValidator.checkMax(input[key], paramValidation.max) !== true) { | ||
errorMessages.push("Param " + key + " must have a maximum length of " + paramValidation.max); | ||
} | ||
if (RequestValidator.checkValues(value, paramValidation.values) !== true) { | ||
if (RequestValidator.checkValues(input[key], paramValidation.values) !== true) { | ||
errorMessages.push("Param " + key + " must belong to [" + paramValidation.values.toString() + "]"); | ||
} | ||
if (paramValidation.regex && !paramValidation.regex.test(value)) { | ||
if (paramValidation.regex && !paramValidation.regex.test(input[key])) { | ||
errorMessages.push("Param " + key + " must match regex " + paramValidation.regex); | ||
@@ -123,0 +123,0 @@ } |
{ | ||
"name": "@ssense/restify-request-validator", | ||
"version": "1.0.7", | ||
"version": "1.0.8", | ||
"description": "Restify requests validator", | ||
@@ -5,0 +5,0 @@ "main": "js/index.js", |
@@ -120,3 +120,3 @@ import {ParamValidation} from './ParamValidation'; | ||
errorMessages = errorMessages.concat(this.validateField(key, input[key], type, paramValidation)); | ||
errorMessages = errorMessages.concat(this.validateField(input, key, type, paramValidation)); | ||
if (this.failOnFirstError && errorMessages.length) { | ||
@@ -135,20 +135,20 @@ break; | ||
private validateField(key: any, value: any, type: any, paramValidation: any): string[] { | ||
private validateField(input: any, key: any, type: any, paramValidation: any): string[] { | ||
const errorMessages: string[] = []; | ||
// Check type | ||
const typeValidation = {value, type: paramValidation.type}; | ||
const typeValidation = {value: input[key], type: paramValidation.type}; | ||
if (RequestValidator.checkType(typeValidation) !== true) { | ||
errorMessages.push(`Param ${key} has invalid type (${paramValidation.type})`); | ||
} | ||
value = typeValidation.value; | ||
input[key] = typeValidation.value; | ||
// Parse "numeric" values to numbers in order to pass next validations | ||
if (type !== 'undefined' && paramValidation.type === 'numeric') { | ||
value = parseInt(value, 10); | ||
input[key] = parseInt(input[key], 10); | ||
} | ||
// Check array content if needed | ||
if (value instanceof Array | ||
&& RequestValidator.checkArrayType(value, paramValidation.arrayType) !== true) { | ||
if (input[key] instanceof Array | ||
&& RequestValidator.checkArrayType(input[key], paramValidation.arrayType) !== true) { | ||
errorMessages.push(`Param ${key} has invalid content type (${paramValidation.arrayType}[])`); | ||
@@ -158,3 +158,3 @@ } | ||
// Check length | ||
if (RequestValidator.checkLength(value, paramValidation.length) !== true) { | ||
if (RequestValidator.checkLength(input[key], paramValidation.length) !== true) { | ||
errorMessages.push(`Param ${key} must have a length of ${paramValidation.length}`); | ||
@@ -164,3 +164,3 @@ } | ||
// Check min | ||
if (RequestValidator.checkMin(value, paramValidation.min) !== true) { | ||
if (RequestValidator.checkMin(input[key], paramValidation.min) !== true) { | ||
errorMessages.push(`Param ${key} must have a minimum length of ${paramValidation.min}`); | ||
@@ -170,3 +170,3 @@ } | ||
// Check max | ||
if (RequestValidator.checkMax(value, paramValidation.max) !== true) { | ||
if (RequestValidator.checkMax(input[key], paramValidation.max) !== true) { | ||
errorMessages.push(`Param ${key} must have a maximum length of ${paramValidation.max}`); | ||
@@ -176,3 +176,3 @@ } | ||
// Check values | ||
if (RequestValidator.checkValues(value, paramValidation.values) !== true) { | ||
if (RequestValidator.checkValues(input[key], paramValidation.values) !== true) { | ||
errorMessages.push(`Param ${key} must belong to [${paramValidation.values.toString()}]`); | ||
@@ -182,3 +182,3 @@ } | ||
// Check regex | ||
if (paramValidation.regex && !paramValidation.regex.test(value)) { | ||
if (paramValidation.regex && !paramValidation.regex.test(input[key])) { | ||
errorMessages.push(`Param ${key} must match regex ${paramValidation.regex}`); | ||
@@ -185,0 +185,0 @@ } |
Sorry, the diff of this file is not supported yet
51089