Skip to content

Commit

Permalink
Code cleanup #1035
Browse files Browse the repository at this point in the history
  • Loading branch information
WesTyler committed Nov 14, 2016
1 parent ea40559 commit 0f578f4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
7 changes: 5 additions & 2 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -1542,18 +1542,21 @@ const schema = Joi.object({
});
```
#### `string.regex(pattern, [name | config])`
#### `string.regex(pattern, [name | options])`
Defines a regular expression rule where:
- `pattern` - a regular expression object the string value must match against.
- `name` - optional name for patterns (useful with multiple patterns).
- `config` - an optional configuration object with the following supported properties:
- `options` - an optional configuration object with the following supported properties:
- `name` - optional pattern name.
- `invert` - optional boolean flag. Defaults to `false` behavior. If specified as `true`, the provided pattern will be disallowed instead of required.
```js
const schema = Joi.string().regex(/^[abc]+$/);

const inlineNamedSchema = Joi.string().regex(/[0-9]/, 'numbers');
inlineNamedSchema.validate('alpha'); // ValidationError: "value" with value "alpha" fails to match the numbers pattern

const namedSchema = Joi.string().regex(/[0-9]/, { name: 'numbers'});
namedSchema.validate('alpha'); // ValidationError: "value" with value "alpha" fails to match the numbers pattern

Expand Down
25 changes: 15 additions & 10 deletions lib/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,22 @@ 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;

const patternIsInverted = (typeof patternOptions === 'object' && patternOptions.invert);
if (patternIsInverted) {
patternObject.inverted = true;
if (typeof patternOptions === 'string') {
patternObject.name = patternOptions;
}
else if (typeof patternOptions === 'object') {
patternIsInverted = !!patternOptions.invert;
patternObject.invert = patternIsInverted;

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';

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

Expand All @@ -112,13 +123,7 @@ internals.String = class extends Any {
return value;
}

const name = typeof patternOptions === 'string' ?
patternOptions :
Hoek.reach(patternOptions, 'name');
const baseRegex = patternIsInverted ? 'string.regex.inverted' : 'string.regex.base';
const nameRegex = patternIsInverted ? 'string.regex.invertedName' : 'string.regex.name';

return this.createError((name ? nameRegex : baseRegex), { name, pattern, value }, state, options);
return this.createError((patternObject.name ? nameRegex : baseRegex), { name: patternObject.name, pattern, value }, state, options);
});
}

Expand Down
2 changes: 1 addition & 1 deletion test/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -3674,7 +3674,7 @@ describe('string', () => {
name: 'regex',
arg: {
pattern: /[a-z]/,
inverted: true
invert: true
}
}
]
Expand Down

0 comments on commit 0f578f4

Please sign in to comment.