diff --git a/lib/validateOptions.ts b/lib/validateOptions.ts index 9bdb236..331dfb7 100644 --- a/lib/validateOptions.ts +++ b/lib/validateOptions.ts @@ -27,6 +27,10 @@ const validateOptions = function ({ options, optionDefinitions }: { break; } case 'number': { + if (!optionRequired && value === undefined) { + break; + } + if (typeof value !== 'number' || Number.isNaN(value)) { throw new errors.OptionInvalid(`Option '${optionDefinition.name}' must be a number.`, { data: { optionDefinition } diff --git a/test/unit/validateOptionsTests.ts b/test/unit/validateOptionsTests.ts index 7cfc5d4..2194e55 100644 --- a/test/unit/validateOptionsTests.ts +++ b/test/unit/validateOptionsTests.ts @@ -1,6 +1,6 @@ import { assert } from 'assertthat'; import { CustomError } from 'defekt'; -import { OptionDefinition } from '../../lib/elements/OptionDefinition'; +import { OptionDefinition } from '../../lib'; import { validateOptions } from 'lib/validateOptions'; suite('validateOptions', (): void => { @@ -78,6 +78,23 @@ suite('validateOptions', (): void => { }).is.not.throwing(); }); + test('accepts a missing, non-required number.', async (): Promise => { + const options = { + option: undefined + }; + const optionDefinitions: OptionDefinition[] = [ + { + name: 'option', + type: 'number', + isRequired: false + } + ]; + + assert.that((): void => { + validateOptions({ options, optionDefinitions }); + }).is.not.throwing(); + }); + test('throws an exception if a number option is NaN.', async (): Promise => { const options = { option: Number.NaN