Add validatePattern & UnitTest

This commit is contained in:
HelderSepu
2017-10-21 13:04:53 -04:00
parent 21178e9107
commit ac24f4376b
2 changed files with 38 additions and 8 deletions

View File

@@ -536,6 +536,13 @@ export const validateMinLength = (val, min) => {
} }
} }
export const validatePattern = (val, rxPattern) => {
var patt = new RegExp(rxPattern);
if (!patt.test(val)) {
return "Value must follow pattern " + rxPattern
}
}
// validation of parameters before execute // validation of parameters before execute
export const validateParam = (param, isXml, isOAS3 = false) => { export const validateParam = (param, isXml, isOAS3 = false) => {
let errors = [] let errors = []
@@ -549,6 +556,8 @@ export const validateParam = (param, isXml, isOAS3 = false) => {
let format = paramDetails.get("format") let format = paramDetails.get("format")
let maxLength = paramDetails.get("maxLength") let maxLength = paramDetails.get("maxLength")
let minLength = paramDetails.get("minLength") let minLength = paramDetails.get("minLength")
let pattern = paramDetails.get("pattern")
/* /*
If the parameter is required OR the parameter has a value (meaning optional, but filled in) If the parameter is required OR the parameter has a value (meaning optional, but filled in)
@@ -565,6 +574,11 @@ export const validateParam = (param, isXml, isOAS3 = false) => {
let numberCheck = type === "number" && !validateNumber(value) // validateNumber returns undefined if the value is a number 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 integerCheck = type === "integer" && !validateInteger(value) // validateInteger returns undefined if the value is an integer
if (pattern) {
let err = validatePattern(value, pattern)
if (err) errors.push(err)
}
if (maxLength || maxLength === 0) { if (maxLength || maxLength === 0) {
let err = validateMaxLength(value, maxLength) let err = validateMaxLength(value, maxLength)
if (err) errors.push(err) if (err) errors.push(err)

View File

@@ -3,6 +3,7 @@ import expect from "expect"
import { fromJS, OrderedMap } from "immutable" import { fromJS, OrderedMap } from "immutable"
import { import {
mapToList, mapToList,
validatePattern,
validateMinLength, validateMinLength,
validateMaxLength, validateMaxLength,
validateDateTime, validateDateTime,
@@ -215,9 +216,9 @@ describe("utils", function() {
expect(validateFile(1)).toEqual(errorMessage) expect(validateFile(1)).toEqual(errorMessage)
expect(validateFile("string")).toEqual(errorMessage) expect(validateFile("string")).toEqual(errorMessage)
}) })
}) })
describe("validateDateTime", function() { describe("validateDateTime", function() {
let errorMessage = "Value must be a DateTime" let errorMessage = "Value must be a DateTime"
it("doesn't return for valid dates", function() { it("doesn't return for valid dates", function() {
@@ -228,7 +229,7 @@ describe("utils", function() {
expect(validateDateTime(null)).toEqual(errorMessage) expect(validateDateTime(null)).toEqual(errorMessage)
expect(validateDateTime("string")).toEqual(errorMessage) expect(validateDateTime("string")).toEqual(errorMessage)
}) })
}) })
describe("validateGuid", function() { describe("validateGuid", function() {
let errorMessage = "Value must be a Guid" let errorMessage = "Value must be a Guid"
@@ -242,9 +243,9 @@ describe("utils", function() {
expect(validateGuid(1)).toEqual(errorMessage) expect(validateGuid(1)).toEqual(errorMessage)
expect(validateGuid("string")).toEqual(errorMessage) expect(validateGuid("string")).toEqual(errorMessage)
}) })
}) })
describe("validateMaxLength", function() { describe("validateMaxLength", function() {
let errorMessage = "Value must be less than MaxLength" let errorMessage = "Value must be less than MaxLength"
it("doesn't return for valid guid", function() { it("doesn't return for valid guid", function() {
@@ -257,9 +258,9 @@ describe("utils", function() {
expect(validateMaxLength("abc", 1)).toEqual(errorMessage) expect(validateMaxLength("abc", 1)).toEqual(errorMessage)
expect(validateMaxLength("abc", 2)).toEqual(errorMessage) expect(validateMaxLength("abc", 2)).toEqual(errorMessage)
}) })
}) })
describe("validateMinLength", function() { describe("validateMinLength", function() {
let errorMessage = "Value must be greater than MinLength" let errorMessage = "Value must be greater than MinLength"
it("doesn't return for valid guid", function() { it("doesn't return for valid guid", function() {
@@ -271,7 +272,22 @@ describe("utils", function() {
expect(validateMinLength("abc", 5)).toEqual(errorMessage) expect(validateMinLength("abc", 5)).toEqual(errorMessage)
expect(validateMinLength("abc", 8)).toEqual(errorMessage) expect(validateMinLength("abc", 8)).toEqual(errorMessage)
}) })
}) })
describe("validatePattern", function() {
let rxPattern = "^(red|blue)"
let errorMessage = "Value must follow pattern " + rxPattern
it("doesn't return for a match", function() {
expect(validatePattern("red", rxPattern)).toBeFalsy()
expect(validatePattern("blue", rxPattern)).toBeFalsy()
})
it("returns a message for invalid pattern'", function() {
expect(validatePattern("pink", rxPattern)).toEqual(errorMessage)
expect(validatePattern("123", rxPattern)).toEqual(errorMessage)
})
})
describe("validateParam", function() { describe("validateParam", function() {
let param = null let param = null