Skip to content

Commit

Permalink
Supports describe API for string.regex invalidate config
Browse files Browse the repository at this point in the history
  • Loading branch information
WesTyler committed Nov 14, 2016
1 parent fe35fcc commit 83b1aa7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,12 @@ internals.String = class extends Any {

pattern = new RegExp(pattern.source, pattern.ignoreCase ? 'i' : undefined); // Future version should break this and forbid unsupported regex flags

return this._test('regex', pattern, function (value, state, options) {
const patternIsInvalidated = (typeof config === 'object' && Hoek.reach(config, 'invalidate'));
const testName = patternIsInvalidated ? 'invalidatedRegex' : 'regex';

return this._test(testName, pattern, function (value, state, options) {

const patternMatch = pattern.test(value);
const patternIsInvalidated = (typeof config === 'object' && Hoek.reach(config, 'invalidate'));

if ((patternMatch && !patternIsInvalidated) || (!patternMatch && patternIsInvalidated)) {
return value;
Expand Down
19 changes: 19 additions & 0 deletions test/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -3659,5 +3659,24 @@ describe('string', () => {
});
done();
});

it('describes invalidate regex pattern', (done) => {

const schema = Joi.string().regex(/[a-z]/, {
invalidate: true
});
const description = schema.describe();
expect(description).to.equal({
type: 'string',
invalids: [''],
rules: [
{
name: 'invalidatedRegex',
arg: /[a-z]/
}
]
});
done();
});
});
});

0 comments on commit 83b1aa7

Please sign in to comment.