diff --git a/src/core/utils.js b/src/core/utils.js index 1a835d3fbcd..ac633c34ce1 100644 --- a/src/core/utils.js +++ b/src/core/utils.js @@ -500,12 +500,25 @@ export const validateString = ( val ) => { } } +export const validateDateTime = (val) => { + if (isNaN(Date.parse(val))) { + return "Value must be a DateTime" + } +} + +export const validateGuid = (val) => { + 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" + } +} + // 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" ) { diff --git a/test/core/utils.js b/test/core/utils.js index 42103cc1035..dfe5c1ee62e 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,33 @@ describe("utils", function() { }) }) + describe("validateDateTime", function() { + let errorMessage = "Value must be a DateTime" + + it("doesn't return for valid dates", function() { + expect(validateDateTime("Mon, 25 Dec 1995 13:30:00 +0430")).toBeFalsy() + }) + + it("returns a message for invalid input'", function() { + expect(validateDateTime(null)).toEqual(errorMessage) + expect(validateDateTime("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