Skip to content

Commit b3e86b3

Browse files
authored
Merge pull request #3798 from swagger-api/swagger-ui/master
Add validatePattern
2 parents 53ca59e + b584837 commit b3e86b3

File tree

2 files changed

+45
-8
lines changed

2 files changed

+45
-8
lines changed

src/core/utils.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,13 @@ export const validateMinLength = (val, min) => {
459459
}
460460
}
461461

462+
export const validatePattern = (val, rxPattern) => {
463+
var patt = new RegExp(rxPattern)
464+
if (!patt.test(val)) {
465+
return "Value must follow pattern " + rxPattern
466+
}
467+
}
468+
462469
// validation of parameters before execute
463470
export const validateParam = (param, isXml, isOAS3 = false) => {
464471
let errors = []
@@ -472,6 +479,8 @@ export const validateParam = (param, isXml, isOAS3 = false) => {
472479
let format = paramDetails.get("format")
473480
let maxLength = paramDetails.get("maxLength")
474481
let minLength = paramDetails.get("minLength")
482+
let pattern = paramDetails.get("pattern")
483+
475484

476485
/*
477486
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) => {
488497
let numberCheck = type === "number" && !validateNumber(value) // validateNumber returns undefined if the value is a number
489498
let integerCheck = type === "integer" && !validateInteger(value) // validateInteger returns undefined if the value is an integer
490499

500+
if (pattern) {
501+
let err = validatePattern(value, pattern)
502+
if (err) errors.push(err)
503+
}
504+
491505
if (maxLength || maxLength === 0) {
492506
let err = validateMaxLength(value, maxLength)
493507
if (err) errors.push(err)

test/core/utils.js

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import expect from "expect"
33
import { fromJS, OrderedMap } from "immutable"
44
import {
55
mapToList,
6+
validatePattern,
67
validateMinLength,
78
validateMaxLength,
89
validateDateTime,
@@ -216,9 +217,9 @@ describe("utils", function() {
216217
expect(validateFile(1)).toEqual(errorMessage)
217218
expect(validateFile("string")).toEqual(errorMessage)
218219
})
219-
})
220+
})
220221

221-
describe("validateDateTime", function() {
222+
describe("validateDateTime", function() {
222223
let errorMessage = "Value must be a DateTime"
223224

224225
it("doesn't return for valid dates", function() {
@@ -229,7 +230,7 @@ describe("utils", function() {
229230
expect(validateDateTime(null)).toEqual(errorMessage)
230231
expect(validateDateTime("string")).toEqual(errorMessage)
231232
})
232-
})
233+
})
233234

234235
describe("validateGuid", function() {
235236
let errorMessage = "Value must be a Guid"
@@ -243,9 +244,9 @@ describe("utils", function() {
243244
expect(validateGuid(1)).toEqual(errorMessage)
244245
expect(validateGuid("string")).toEqual(errorMessage)
245246
})
246-
})
247+
})
247248

248-
describe("validateMaxLength", function() {
249+
describe("validateMaxLength", function() {
249250
let errorMessage = "Value must be less than MaxLength"
250251

251252
it("doesn't return for valid guid", function() {
@@ -258,9 +259,9 @@ describe("utils", function() {
258259
expect(validateMaxLength("abc", 1)).toEqual(errorMessage)
259260
expect(validateMaxLength("abc", 2)).toEqual(errorMessage)
260261
})
261-
})
262+
})
262263

263-
describe("validateMinLength", function() {
264+
describe("validateMinLength", function() {
264265
let errorMessage = "Value must be greater than MinLength"
265266

266267
it("doesn't return for valid guid", function() {
@@ -272,7 +273,29 @@ describe("utils", function() {
272273
expect(validateMinLength("abc", 5)).toEqual(errorMessage)
273274
expect(validateMinLength("abc", 8)).toEqual(errorMessage)
274275
})
275-
})
276+
})
277+
278+
describe("validatePattern", function() {
279+
let rxPattern = "^(red|blue)"
280+
let errorMessage = "Value must follow pattern " + rxPattern
281+
282+
it("doesn't return for a match", function() {
283+
expect(validatePattern("red", rxPattern)).toBeFalsy()
284+
expect(validatePattern("blue", rxPattern)).toBeFalsy()
285+
})
286+
287+
it("returns a message for invalid pattern", function() {
288+
expect(validatePattern("pink", rxPattern)).toEqual(errorMessage)
289+
expect(validatePattern("123", rxPattern)).toEqual(errorMessage)
290+
})
291+
292+
it("fails gracefully when an invalid regex value is passed", function() {
293+
expect(() => validatePattern("aValue", "---")).toNotThrow()
294+
expect(() => validatePattern("aValue", 1234)).toNotThrow()
295+
expect(() => validatePattern("aValue", null)).toNotThrow()
296+
expect(() => validatePattern("aValue", [])).toNotThrow()
297+
})
298+
})
276299

277300
describe("validateParam", function() {
278301
let param = null

0 commit comments

Comments
 (0)