From ec2179f019661148a0e38fb6c6392ca65226d07d Mon Sep 17 00:00:00 2001 From: HelderSepu Date: Sat, 28 Oct 2017 16:21:40 -0400 Subject: [PATCH] Fix issue with the error messages Many of the errors where incorrectly showing "Required field is not provided" when the field was provided but not valid. This was raised by @ron on PR #3825 --- src/core/utils.js | 20 ++++++++++---------- test/core/utils.js | 6 +++--- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/core/utils.js b/src/core/utils.js index 0f247b52..f0b5cf9e 100644 --- a/src/core/utils.js +++ b/src/core/utils.js @@ -556,14 +556,19 @@ export const validateParam = (param, isXml, isOAS3 = false) => { Only bother validating the parameter if the type was specified. */ if ( type && (required || value) ) { - // These checks should evaluate to true if the parameter's value is valid - let stringCheck = type === "string" && value && !validateString(value) + // These checks should evaluate to true if there is a parameter + let stringCheck = type === "string" && value let arrayCheck = type === "array" && Array.isArray(value) && value.length let listCheck = type === "array" && Im.List.isList(value) && value.count() let fileCheck = type === "file" && value instanceof win.File - let booleanCheck = type === "boolean" && !validateBoolean(value) - let numberCheck = type === "number" && !validateNumber(value) // validateNumber returns undefined if the value is a number - let integerCheck = type === "integer" && !validateInteger(value) // validateInteger returns undefined if the value is an integer + let booleanCheck = type === "boolean" && (value || value === false) + let numberCheck = type === "number" && value + let integerCheck = type === "integer" && value + + if ( required && !(stringCheck || arrayCheck || listCheck || fileCheck || booleanCheck || numberCheck || integerCheck) ) { + errors.push("Required field is not provided") + return errors + } if (maxLength || maxLength === 0) { let err = validateMaxLength(value, maxLength) @@ -575,11 +580,6 @@ export const validateParam = (param, isXml, isOAS3 = false) => { if (err) errors.push(err) } - if ( required && !(stringCheck || arrayCheck || listCheck || fileCheck || booleanCheck || numberCheck || integerCheck) ) { - errors.push("Required field is not provided") - return errors - } - if (maximum || maximum === 0) { let err = validateMaximum(value, maximum) if (err) errors.push(err) diff --git a/test/core/utils.js b/test/core/utils.js index dd37a518..b90e263c 100644 --- a/test/core/utils.js +++ b/test/core/utils.js @@ -524,7 +524,7 @@ describe("utils", function() { type: "boolean", value: "test string" } - assertValidateParam(param, ["Required field is not provided"]) + assertValidateParam(param, ["Value must be a boolean"]) // valid boolean value param = { @@ -584,7 +584,7 @@ describe("utils", function() { type: "number", value: "test" } - assertValidateParam(param, ["Required field is not provided"]) + assertValidateParam(param, ["Value must be a number"]) // invalid number, undefined value param = { @@ -666,7 +666,7 @@ describe("utils", function() { type: "integer", value: "test" } - assertValidateParam(param, ["Required field is not provided"]) + assertValidateParam(param, ["Value must be an integer"]) // invalid integer, undefined value param = {