Skip to content

Commit c3fc04f

Browse files
committedJun 29, 2020
limit regex if maxLength is set
1 parent 314a36f commit c3fc04f

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed
 

‎index.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,14 @@ var isMultipleOf = function(name, multipleOf) {
110110
return !res;
111111
}
112112

113+
var testLimitedRegex = function (r, s, maxLength) {
114+
if (maxLength > -1 && s.length > maxLength) return true
115+
return r.test(s)
116+
}
117+
113118
var compile = function(schema, cache, root, reporter, opts) {
114119
var fmts = opts ? xtend(formats, opts.formats) : formats
115-
var scope = {unique:unique, formats:fmts, isMultipleOf:isMultipleOf}
120+
var scope = {unique:unique, formats:fmts, isMultipleOf:isMultipleOf, testLimitedRegex:testLimitedRegex}
116121
var verbose = opts ? !!opts.verbose : false;
117122
var greedy = opts && opts.greedy !== undefined ?
118123
opts.greedy : false;
@@ -218,7 +223,7 @@ var compile = function(schema, cache, root, reporter, opts) {
218223
scope[n] = fmts[node.format]
219224

220225
if (typeof scope[n] === 'function') validate('if (!%s(%s)) {', n, name)
221-
else validate('if (!%s.test(%s)) {', n, name)
226+
else validate('if (!testLimitedRegex(%s, %s, %d)) {', n, name, typeof node.maxLength === 'undefined' ? -1 : node.maxLength)
222227
error('must be '+node.format+' format')
223228
validate('}')
224229
if (type !== 'string' && formats[node.format]) validate('}')
@@ -392,7 +397,7 @@ var compile = function(schema, cache, root, reporter, opts) {
392397
if (node.pattern) {
393398
var p = patterns(node.pattern)
394399
if (type !== 'string') validate('if (%s) {', types.string(name))
395-
validate('if (!(%s.test(%s))) {', p, name)
400+
validate('if (!(testLimitedRegex(%s, %s, %d))) {', p, name, typeof node.maxLength === 'undefined' ? -1 : node.maxLength)
396401
error('pattern mismatch')
397402
validate('}')
398403
if (type !== 'string') validate('}')

0 commit comments

Comments
 (0)
Please sign in to comment.