Skip to content

Commit 0f578f4

Browse files
committed
Code cleanup #1035
1 parent ea40559 commit 0f578f4

File tree

3 files changed

+21
-13
lines changed

3 files changed

+21
-13
lines changed

API.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -1542,18 +1542,21 @@ const schema = Joi.object({
15421542
});
15431543
```
15441544
1545-
#### `string.regex(pattern, [name | config])`
1545+
#### `string.regex(pattern, [name | options])`
15461546
15471547
Defines a regular expression rule where:
15481548
- `pattern` - a regular expression object the string value must match against.
15491549
- `name` - optional name for patterns (useful with multiple patterns).
1550-
- `config` - an optional configuration object with the following supported properties:
1550+
- `options` - an optional configuration object with the following supported properties:
15511551
- `name` - optional pattern name.
15521552
- `invert` - optional boolean flag. Defaults to `false` behavior. If specified as `true`, the provided pattern will be disallowed instead of required.
15531553
15541554
```js
15551555
const schema = Joi.string().regex(/^[abc]+$/);
15561556

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

lib/string.js

+15-10
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,22 @@ 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;
101102

102-
const patternIsInverted = (typeof patternOptions === 'object' && patternOptions.invert);
103-
if (patternIsInverted) {
104-
patternObject.inverted = true;
103+
if (typeof patternOptions === 'string') {
104+
patternObject.name = patternOptions;
105105
}
106+
else if (typeof patternOptions === 'object') {
107+
patternIsInverted = !!patternOptions.invert;
108+
patternObject.invert = patternIsInverted;
109+
110+
if (patternOptions.name) {
111+
patternObject.name = patternOptions.name;
112+
}
113+
}
114+
115+
const baseRegex = patternIsInverted ? 'string.regex.inverted' : 'string.regex.base';
116+
const nameRegex = patternIsInverted ? 'string.regex.invertedName' : 'string.regex.name';
106117

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

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

115-
const name = typeof patternOptions === 'string' ?
116-
patternOptions :
117-
Hoek.reach(patternOptions, 'name');
118-
const baseRegex = patternIsInverted ? 'string.regex.inverted' : 'string.regex.base';
119-
const nameRegex = patternIsInverted ? 'string.regex.invertedName' : 'string.regex.name';
120-
121-
return this.createError((name ? nameRegex : baseRegex), { name, pattern, value }, state, options);
126+
return this.createError((patternObject.name ? nameRegex : baseRegex), { name: patternObject.name, pattern, value }, state, options);
122127
});
123128
}
124129

test/string.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3674,7 +3674,7 @@ describe('string', () => {
36743674
name: 'regex',
36753675
arg: {
36763676
pattern: /[a-z]/,
3677-
inverted: true
3677+
invert: true
36783678
}
36793679
}
36803680
]

0 commit comments

Comments
 (0)