From 678e7a3094e1de25c3af7d81e60473a59aca2bf5 Mon Sep 17 00:00:00 2001 From: Nicolas Morel Date: Mon, 14 Nov 2016 22:21:29 +0100 Subject: [PATCH] Cleanup for #1035 --- lib/language.js | 6 ++++-- lib/string.js | 11 ++++------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/lib/language.js b/lib/language.js index df667d087..9aae41566 100755 --- a/lib/language.js +++ b/lib/language.js @@ -122,9 +122,11 @@ exports.errors = { token: 'must only contain alpha-numeric and underscore characters', regex: { base: 'with value "{{!value}}" fails to match the required pattern: {{pattern}}', - inverted: 'with value "{{!value}}" matches the inverted pattern: {{pattern}}', name: 'with value "{{!value}}" fails to match the {{name}} pattern', - invertedName: 'with value "{{!value}}" matches the inverted {{name}} pattern' + invert: { + base: 'with value "{{!value}}" matches the inverted pattern: {{pattern}}', + name: 'with value "{{!value}}" matches the inverted {{name}} pattern' + } }, email: 'must be a valid email', uri: 'must be a valid uri', diff --git a/lib/string.js b/lib/string.js index 6c4d73f37..b7a526478 100755 --- a/lib/string.js +++ b/lib/string.js @@ -98,32 +98,29 @@ internals.String = class extends Any { const patternObject = { pattern: new RegExp(pattern.source, pattern.ignoreCase ? 'i' : undefined) // Future version should break this and forbid unsupported regex flags }; - let patternIsInverted = false; if (typeof patternOptions === 'string') { patternObject.name = patternOptions; } else if (typeof patternOptions === 'object') { - patternIsInverted = !!patternOptions.invert; - patternObject.invert = patternIsInverted; + patternObject.invert = !!patternOptions.invert; if (patternOptions.name) { patternObject.name = patternOptions.name; } } - const baseRegex = patternIsInverted ? 'string.regex.inverted' : 'string.regex.base'; - const nameRegex = patternIsInverted ? 'string.regex.invertedName' : 'string.regex.name'; + const errorCode = ['string.regex', patternObject.invert ? '.invert' : '', patternObject.name ? '.name' : '.base'].join(''); return this._test('regex', patternObject, function (value, state, options) { const patternMatch = patternObject.pattern.test(value); - if (patternMatch ^ patternIsInverted) { + if (patternMatch ^ patternObject.invert) { return value; } - return this.createError((patternObject.name ? nameRegex : baseRegex), { name: patternObject.name, pattern, value }, state, options); + return this.createError(errorCode, { name: patternObject.name, pattern: patternObject.pattern, value }, state, options); }); }