From 85b7b7241d6e5ae55850f02bc84f08c149073599 Mon Sep 17 00:00:00 2001 From: HelderSepu Date: Tue, 19 Sep 2017 14:31:27 -0400 Subject: [PATCH 1/5] Add validateDateTime & validateGuid Initial work to tacke some of the missing validation (Issue #993) --- src/core/utils.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/core/utils.js b/src/core/utils.js index 1a835d3fbcd..6ff31c294bb 100644 --- a/src/core/utils.js +++ b/src/core/utils.js @@ -500,12 +500,25 @@ export const validateString = ( val ) => { } } +export const validateDateTime = (val) => { + if (val && typeof val !== "string") { + return "Value must be a DateTime" + } +} + +export const validateGuid = (val) => { + if (val && typeof val !== "string") { + return "Value must be a Guid" + } +} + // validation of parameters before execute export const validateParam = (param, isXml) => { let errors = [] let value = isXml && param.get("in") === "body" ? param.get("value_xml") : param.get("value") let required = param.get("required") let type = param.get("type") + let format = param.get("format") /* If the parameter is required OR the parameter has a value (meaning optional, but filled in) @@ -528,7 +541,14 @@ export const validateParam = (param, isXml) => { } if ( type === "string" ) { - let err = validateString(value) + let err + if (format === "date-time") { + err = validateDateTime(value) + } else if (format === "uuid") { + err = validateGuid(value) + } else { + err = validateString(value) + } if (!err) return errors errors.push(err) } else if ( type === "boolean" ) { From 11768b16bfd09bf951ccd2fd9d76a3781e471527 Mon Sep 17 00:00:00 2001 From: HelderSepu Date: Fri, 22 Sep 2017 11:30:07 -0400 Subject: [PATCH 2/5] Correct IF for validateDateTime & validateGuid --- src/core/utils.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/utils.js b/src/core/utils.js index 6ff31c294bb..9495d0feba2 100644 --- a/src/core/utils.js +++ b/src/core/utils.js @@ -501,13 +501,13 @@ export const validateString = ( val ) => { } export const validateDateTime = (val) => { - if (val && typeof val !== "string") { + if (!isNaN(Date.parse(val))) { return "Value must be a DateTime" } } export const validateGuid = (val) => { - if (val && typeof val !== "string") { + if (!/^[{(]?[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}[)}]?$/.test(val)) { return "Value must be a Guid" } } From 7d9b56255c5a3a1f602482c29e5e3c03ce10076a Mon Sep 17 00:00:00 2001 From: HelderSepu Date: Fri, 22 Sep 2017 17:44:01 -0400 Subject: [PATCH 3/5] Correction to the validateDateTime --- src/core/utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/utils.js b/src/core/utils.js index 9495d0feba2..338399b333d 100644 --- a/src/core/utils.js +++ b/src/core/utils.js @@ -501,7 +501,7 @@ export const validateString = ( val ) => { } export const validateDateTime = (val) => { - if (!isNaN(Date.parse(val))) { + if (isNaN(Date.parse(val))) { return "Value must be a DateTime" } } From e08f31d6747181228a3d3a9780d5a8a76f38fdef Mon Sep 17 00:00:00 2001 From: HelderSepu Date: Fri, 22 Sep 2017 21:37:33 -0400 Subject: [PATCH 4/5] Add UnitTests for validateDateTime & validateGuid --- src/core/utils.js | 2 +- test/core/utils.js | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/core/utils.js b/src/core/utils.js index 338399b333d..ac633c34ce1 100644 --- a/src/core/utils.js +++ b/src/core/utils.js @@ -548,7 +548,7 @@ export const validateParam = (param, isXml) => { err = validateGuid(value) } else { err = validateString(value) - } + } if (!err) return errors errors.push(err) } else if ( type === "boolean" ) { diff --git a/test/core/utils.js b/test/core/utils.js index 42103cc1035..5b481e8c8d7 100644 --- a/test/core/utils.js +++ b/test/core/utils.js @@ -1,7 +1,7 @@ /* eslint-env mocha */ import expect from "expect" import { fromJS, OrderedMap } from "immutable" -import { mapToList, validateNumber, validateInteger, validateParam, validateFile, fromJSOrdered, getAcceptControllingResponse, createDeepLinkPath, escapeDeepLinkPath } from "core/utils" +import { mapToList, validateDateTime, validateGuid, validateNumber, validateInteger, validateParam, validateFile, fromJSOrdered, getAcceptControllingResponse, createDeepLinkPath, escapeDeepLinkPath } from "core/utils" import win from "core/window" describe("utils", function() { @@ -158,7 +158,7 @@ describe("utils", function() { }) }) - describe("validateFile", function() { + describe("validateFile", function() { let errorMessage = "Value must be a file" it("validates against objects which are instances of 'File'", function() { @@ -171,6 +171,34 @@ describe("utils", function() { }) }) + describe("validateDateTime", function() { + let errorMessage = "Value must be a DateTime" + + it("doesn't return for valid dates", function() { + expect(validateFile(fileObj)).toBeFalsy() + expect(validateFile(null)).toBeFalsy() + }) + + it("returns a message for invalid input'", function() { + expect(validateFile(1)).toEqual(errorMessage) + expect(validateFile("string")).toEqual(errorMessage) + }) + }) + + describe("validateGuid", function() { + let errorMessage = "Value must be a Guid" + + it("doesn't return for valid guid", function() { + expect(validateGuid("8ce4811e-cec5-4a29-891a-15d1917153c1")).toBeFalsy() + expect(validateGuid("{8ce4811e-cec5-4a29-891a-15d1917153c1}")).toBeFalsy() + }) + + it("returns a message for invalid input'", function() { + expect(validateGuid(1)).toEqual(errorMessage) + expect(validateGuid("string")).toEqual(errorMessage) + }) + }) + describe("validateParam", function() { let param = null let result = null From 6b59707af1e4b215b67fc157bed2ab16f8b096b4 Mon Sep 17 00:00:00 2001 From: Kyle Shockey Date: Fri, 22 Sep 2017 19:48:51 -0700 Subject: [PATCH 5/5] Fix tests --- test/core/utils.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/test/core/utils.js b/test/core/utils.js index 5b481e8c8d7..dfe5c1ee62e 100644 --- a/test/core/utils.js +++ b/test/core/utils.js @@ -175,13 +175,12 @@ describe("utils", function() { let errorMessage = "Value must be a DateTime" it("doesn't return for valid dates", function() { - expect(validateFile(fileObj)).toBeFalsy() - expect(validateFile(null)).toBeFalsy() + expect(validateDateTime("Mon, 25 Dec 1995 13:30:00 +0430")).toBeFalsy() }) it("returns a message for invalid input'", function() { - expect(validateFile(1)).toEqual(errorMessage) - expect(validateFile("string")).toEqual(errorMessage) + expect(validateDateTime(null)).toEqual(errorMessage) + expect(validateDateTime("string")).toEqual(errorMessage) }) })