Skip to content

Commit

Permalink
Consolidate if statement branches for field validation
Browse files Browse the repository at this point in the history
  • Loading branch information
lukekarrys committed Aug 19, 2024
1 parent d071c63 commit fad3f72
Showing 1 changed file with 18 additions and 19 deletions.
37 changes: 18 additions & 19 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,15 +423,6 @@ const validateField = (
}
}

const validateBoolean = () => {
if (o.hint !== undefined) {
throw new TypeError('cannot provide hint for flag')
}
if (o.validOptions !== undefined) {
throw new TypeError('cannot provide validOptions for flag')
}
}

if (
o.default !== undefined &&
!isValidValue(o.default, type, multiple)
Expand All @@ -444,18 +435,26 @@ const validateField = (
})
}

if (isConfigOptionOfType(o, 'number', false)) {
validateValidOptions(o.default, o.validOptions)
} else if (isConfigOptionOfType(o, 'number', true)) {
validateValidOptions(o.default, o.validOptions)
} else if (isConfigOptionOfType(o, 'string', false)) {
if (
isConfigOptionOfType(o, 'number', false) ||
isConfigOptionOfType(o, 'number', true)
) {
validateValidOptions(o.default, o.validOptions)
} else if (isConfigOptionOfType(o, 'string', true)) {
} else if (
isConfigOptionOfType(o, 'string', false) ||
isConfigOptionOfType(o, 'string', true)
) {
validateValidOptions(o.default, o.validOptions)
} else if (isConfigOptionOfType(o, 'boolean', false)) {
validateBoolean()
} else if (isConfigOptionOfType(o, 'boolean', true)) {
validateBoolean()
} else if (
isConfigOptionOfType(o, 'boolean', false) ||
isConfigOptionOfType(o, 'boolean', true)
) {
if (o.hint !== undefined) {
throw new TypeError('cannot provide hint for flag')
}
if (o.validOptions !== undefined) {
throw new TypeError('cannot provide validOptions for flag')
}
}

return o
Expand Down

0 comments on commit fad3f72

Please sign in to comment.