Skip to content

Commit

Permalink
Cleanup for #1035
Browse files Browse the repository at this point in the history
  • Loading branch information
Marsup committed Nov 14, 2016
1 parent e60376b commit 678e7a3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
6 changes: 4 additions & 2 deletions lib/language.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
11 changes: 4 additions & 7 deletions lib/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}

Expand Down

0 comments on commit 678e7a3

Please sign in to comment.