Merge pull request #3798 from swagger-api/swagger-ui/master

Add validatePattern
This commit is contained in:
kyle
2017-11-03 17:16:38 -07:00
committed by GitHub
2 changed files with 45 additions and 8 deletions

View File

@@ -459,6 +459,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
export const validateParam = (param, isXml, isOAS3 = false) => {
let errors = []
@@ -472,6 +479,8 @@ export const validateParam = (param, isXml, isOAS3 = false) => {
let format = paramDetails.get("format")
let maxLength = paramDetails.get("maxLength")
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)
@@ -488,6 +497,11 @@ export const validateParam = (param, isXml, isOAS3 = false) => {
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
if (pattern) {
let err = validatePattern(value, pattern)
if (err) errors.push(err)
}
if (maxLength || maxLength === 0) {
let err = validateMaxLength(value, maxLength)
if (err) errors.push(err)

View File

@@ -3,6 +3,7 @@ import expect from "expect"
import { fromJS, OrderedMap } from "immutable"
import {
mapToList,
validatePattern,
validateMinLength,
validateMaxLength,
validateDateTime,
@@ -216,9 +217,9 @@ describe("utils", function() {
expect(validateFile(1)).toEqual(errorMessage)
expect(validateFile("string")).toEqual(errorMessage)
})
})
})
describe("validateDateTime", function() {
describe("validateDateTime", function() {
let errorMessage = "Value must be a DateTime"
it("doesn't return for valid dates", function() {
@@ -229,7 +230,7 @@ describe("utils", function() {
expect(validateDateTime(null)).toEqual(errorMessage)
expect(validateDateTime("string")).toEqual(errorMessage)
})
})
})
describe("validateGuid", function() {
let errorMessage = "Value must be a Guid"
@@ -243,9 +244,9 @@ describe("utils", function() {
expect(validateGuid(1)).toEqual(errorMessage)
expect(validateGuid("string")).toEqual(errorMessage)
})
})
})
describe("validateMaxLength", function() {
describe("validateMaxLength", function() {
let errorMessage = "Value must be less than MaxLength"
it("doesn't return for valid guid", function() {
@@ -258,9 +259,9 @@ describe("utils", function() {
expect(validateMaxLength("abc", 1)).toEqual(errorMessage)
expect(validateMaxLength("abc", 2)).toEqual(errorMessage)
})
})
})
describe("validateMinLength", function() {
describe("validateMinLength", function() {
let errorMessage = "Value must be greater than MinLength"
it("doesn't return for valid guid", function() {
@@ -272,7 +273,29 @@ describe("utils", function() {
expect(validateMinLength("abc", 5)).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)
})
it("fails gracefully when an invalid regex value is passed", function() {
expect(() => validatePattern("aValue", "---")).toNotThrow()
expect(() => validatePattern("aValue", 1234)).toNotThrow()
expect(() => validatePattern("aValue", null)).toNotThrow()
expect(() => validatePattern("aValue", [])).toNotThrow()
})
})
describe("validateParam", function() {
let param = null