Skip to content

Commit 678e7a3

Browse files
committed
Cleanup for #1035
1 parent e60376b commit 678e7a3

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

lib/language.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,11 @@ exports.errors = {
122122
token: 'must only contain alpha-numeric and underscore characters',
123123
regex: {
124124
base: 'with value "{{!value}}" fails to match the required pattern: {{pattern}}',
125-
inverted: 'with value "{{!value}}" matches the inverted pattern: {{pattern}}',
126125
name: 'with value "{{!value}}" fails to match the {{name}} pattern',
127-
invertedName: 'with value "{{!value}}" matches the inverted {{name}} pattern'
126+
invert: {
127+
base: 'with value "{{!value}}" matches the inverted pattern: {{pattern}}',
128+
name: 'with value "{{!value}}" matches the inverted {{name}} pattern'
129+
}
128130
},
129131
email: 'must be a valid email',
130132
uri: 'must be a valid uri',

lib/string.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,32 +98,29 @@ internals.String = class extends Any {
9898
const patternObject = {
9999
pattern: new RegExp(pattern.source, pattern.ignoreCase ? 'i' : undefined) // Future version should break this and forbid unsupported regex flags
100100
};
101-
let patternIsInverted = false;
102101

103102
if (typeof patternOptions === 'string') {
104103
patternObject.name = patternOptions;
105104
}
106105
else if (typeof patternOptions === 'object') {
107-
patternIsInverted = !!patternOptions.invert;
108-
patternObject.invert = patternIsInverted;
106+
patternObject.invert = !!patternOptions.invert;
109107

110108
if (patternOptions.name) {
111109
patternObject.name = patternOptions.name;
112110
}
113111
}
114112

115-
const baseRegex = patternIsInverted ? 'string.regex.inverted' : 'string.regex.base';
116-
const nameRegex = patternIsInverted ? 'string.regex.invertedName' : 'string.regex.name';
113+
const errorCode = ['string.regex', patternObject.invert ? '.invert' : '', patternObject.name ? '.name' : '.base'].join('');
117114

118115
return this._test('regex', patternObject, function (value, state, options) {
119116

120117
const patternMatch = patternObject.pattern.test(value);
121118

122-
if (patternMatch ^ patternIsInverted) {
119+
if (patternMatch ^ patternObject.invert) {
123120
return value;
124121
}
125122

126-
return this.createError((patternObject.name ? nameRegex : baseRegex), { name: patternObject.name, pattern, value }, state, options);
123+
return this.createError(errorCode, { name: patternObject.name, pattern: patternObject.pattern, value }, state, options);
127124
});
128125
}
129126

0 commit comments

Comments
 (0)