From 0f578f44bbb046c0fc5c36dc50991a6e4b45a9e6 Mon Sep 17 00:00:00 2001 From: Wes Tyler Date: Mon, 14 Nov 2016 14:51:49 -0600 Subject: [PATCH] Code cleanup #1035 --- API.md | 7 +++++-- lib/string.js | 25 +++++++++++++++---------- test/string.js | 2 +- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/API.md b/API.md index 9fd97387b..8100841da 100755 --- a/API.md +++ b/API.md @@ -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 diff --git a/lib/string.js b/lib/string.js index 91054cce7..6c4d73f37 100755 --- a/lib/string.js +++ b/lib/string.js @@ -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) { @@ -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); }); } diff --git a/test/string.js b/test/string.js index b94247797..941713701 100755 --- a/test/string.js +++ b/test/string.js @@ -3674,7 +3674,7 @@ describe('string', () => { name: 'regex', arg: { pattern: /[a-z]/, - inverted: true + invert: true } } ]