diff --git a/source/options.js b/source/options.js index 2706d71..05169ef 100644 --- a/source/options.js +++ b/source/options.js @@ -27,7 +27,7 @@ const validateOptions = options => { }, }, defaultNotInChoices: { - filter: ([, flag]) => flag.default && Array.isArray(flag.choices) && [flag.default].flat().every(value => flag.choices.includes(value)), + filter: ([, flag]) => flag.default && Array.isArray(flag.choices) && ![flag.default].flat().every(value => flag.choices.includes(value)), message: flagKeys => `Each value of the option \`default\` must exist within the option \`choices\`. Invalid flags: ${joinFlagKeys(flagKeys)}`, }, }, diff --git a/test/choices.js b/test/choices.js index c526c52..e786d8b 100644 --- a/test/choices.js +++ b/test/choices.js @@ -177,8 +177,8 @@ test('choices - choices must be of the same type', t => { }, {message: 'Each value of the option `choices` must be of the same type as its flag. Invalid flags: (`--number`, type: \'number\'), (`--boolean`, type: \'boolean\')'}); }); -test('choices - default must only include valid choices', t => { - t.throws(() => { +test('choices - success when each value of default exist within the option choices', t => { + t.notThrows(() => { meow({ importMeta, flags: { @@ -187,7 +187,44 @@ test('choices - default must only include valid choices', t => { choices: [1, 2, 3], default: 1, }, + string: { + type: 'string', + choices: ['dog', 'cat', 'unicorn'], + default: 'dog', + }, + multiString: { + type: 'string', + choices: ['dog', 'cat', 'unicorn'], + default: ['dog', 'cat'], + isMultiple: true, + }, + }, + }); + }); +}); + +test('choices - throws when default does not only include valid choices', t => { + t.throws(() => { + meow({ + importMeta, + flags: { + number: { + type: 'number', + choices: [1, 2, 3], + default: 8, + }, + string: { + type: 'string', + choices: ['dog', 'cat'], + default: 'unicorn', + }, + multiString: { + type: 'string', + choices: ['dog', 'cat'], + default: ['dog', 'unicorn'], + isMultiple: true, + }, }, }); - }, {message: 'Each value of the option `default` must exist within the option `choices`. Invalid flags: `--number`'}); + }, {message: 'Each value of the option `default` must exist within the option `choices`. Invalid flags: `--number`, `--string`, `--multiString`'}); });