Skip to content

Commit d8d8434

Browse files
ben-waltersMarsup
authored andcommitted
Applied suggestions
1 parent dec12ec commit d8d8434

File tree

3 files changed

+23
-19
lines changed

3 files changed

+23
-19
lines changed

API.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2803,7 +2803,7 @@ Requires the string value to be a valid GUID.
28032803

28042804
- `options` - optional settings:
28052805
- `version` - specifies one or more acceptable versions. Can be an Array or String with the following values:
2806-
`uuidv1`, `uuidv2`, `uuidv3`, `uuidv4`, `uuidv5`. If no `version` is specified then it is assumed to be a generic `guid`
2806+
`uuidv1`, `uuidv2`, `uuidv3`, `uuidv4`, `uuidv5`, `uuidv6`, `uuidv7` or `uuidv8`. If no `version` is specified then it is assumed to be a generic `guid`
28072807
which will not validate the version or variant of the guid and just check for general structure format.
28082808
- `separator` - defines the allowed or required GUID separator where:
28092809
- `true` - a separator is required, can be either `:` or `-`.

lib/types/string.js

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -318,21 +318,12 @@ module.exports = Any.extend({
318318

319319
Common.assertOptions(options, ['version', 'separator', 'wrapper']);
320320

321-
if (
322-
options.wrapper !== undefined &&
323-
options.wrapper !== true &&
324-
options.wrapper !== false
325-
) {
326-
const validWrappers = Object.keys(internals.guidBrackets).filter(
327-
(key) => !!key
328-
);
329-
assert(
330-
validWrappers.includes(options.wrapper),
331-
`"wrapper" must be true, false, or one of ${validWrappers.join(
332-
', '
333-
)}`
334-
);
335-
}
321+
assert(
322+
options.wrapper === undefined ||
323+
typeof options.wrapper === 'boolean' ||
324+
(typeof options.wrapper === 'string' && typeof internals.guidBrackets[options.wrapper] === 'string'),
325+
`"wrapper" must be true, false, or one of "${Object.keys(internals.guidBrackets).filter(Boolean).join('", "')}"`
326+
);
336327

337328
let versionNumbers = '';
338329

@@ -359,8 +350,8 @@ module.exports = Any.extend({
359350
options.separator === true ? '[:-]' :
360351
options.separator === false ? '[]?' : `\\${options.separator}`;
361352

362-
let wrapperStart = '';
363-
let wrapperEnd = '';
353+
let wrapperStart;
354+
let wrapperEnd;
364355

365356
if (options.wrapper === undefined) {
366357
wrapperStart = '[\\[{\\(]?';
@@ -402,7 +393,7 @@ module.exports = Any.extend({
402393
const open = results[1];
403394
const close = results[results.length - 1];
404395

405-
if (internals.guidBrackets[open || ''] !== (close || '')) {
396+
if ((open || close) && internals.guidBrackets[open] !== close) {
406397
return helpers.error('string.guid');
407398
}
408399

test/types/string.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3248,6 +3248,19 @@ describe('string', () => {
32483248

32493249
});
32503250

3251+
it('errors on invalid wrapper', () => {
3252+
3253+
expect(() => {
3254+
3255+
Joi.string().guid({ wrapper: 1 });
3256+
}).to.throw('"wrapper" must be true, false, or one of "{", "[", "("');
3257+
3258+
expect(() => {
3259+
3260+
Joi.string().guid({ wrapper: '|' });
3261+
}).to.throw('"wrapper" must be true, false, or one of "{", "[", "("');
3262+
});
3263+
32513264
it('validates combination of guid and min', () => {
32523265

32533266
const rule = Joi.string().guid().min(36);

0 commit comments

Comments
 (0)