Skip to content

Commit

Permalink
fix: allow flags to have false value in when
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Goberling authored and Michael Goberling committed Nov 16, 2022
1 parent 3584a1e commit 96e38f1
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/parser/validate.ts
Expand Up @@ -82,7 +82,7 @@ export async function validate(parse: {

function getPresentFlags(flags: Record<string, unknown>): string[] {
return Object.keys(flags).reduce((acc, key) => {
if (flags[key]) acc.push(key)
if (flags[key] !== undefined) acc.push(key)
return acc
}, [] as string[])
}
Expand Down Expand Up @@ -113,7 +113,7 @@ export async function validate(parse: {
continue
if (parse.output.metadata.flags && parse.output.metadata.flags[name]?.setFromDefault)
continue
if (parse.output.flags[flag]) {
if (parse.output.flags[flag] !== undefined) {
return {...base, status: 'failed', reason: `--${flag}=${parse.output.flags[flag]} cannot also be provided when using --${name}`}
}
}
Expand All @@ -126,7 +126,7 @@ export async function validate(parse: {
const resolved = await resolveFlags(flags)
const keys = getPresentFlags(resolved)
for (const flag of keys) {
if (flag !== name && parse.output.flags[flag]) {
if (flag !== name && parse.output.flags[flag] !== undefined) {
return {...base, status: 'failed', reason: `--${flag} cannot also be provided when using --${name}`}
}
}
Expand All @@ -137,7 +137,7 @@ export async function validate(parse: {
async function validateDependsOn(name: string, flags: FlagRelationship[]): Promise<Validation> {
const base = {name, validationFn: 'validateDependsOn'}
const resolved = await resolveFlags(flags)
const foundAll = Object.values(resolved).every(Boolean)
const foundAll = Object.values(resolved).every(val => val !== undefined)
if (!foundAll) {
const formattedFlags = Object.keys(resolved).map(f => `--${f}`).join(', ')
return {...base, status: 'failed', reason: `All of the following must be provided when using --${name}: ${formattedFlags}`}
Expand Down
42 changes: 42 additions & 0 deletions test/parser/validate.test.ts
Expand Up @@ -748,6 +748,48 @@ describe('validate', () => {
})
})

it('should pass if the specified flags whose when property resolves to true, flag has a false value', async () => {
const input = {
argv: [],
flags: {
cookies: {input: [], name: 'cookies'},
sprinkles: {input: [], name: 'sprinkles'},
dessert: {
input: [],
name: 'dessert',
relationships: [
{
type: 'all',
flags: [
'sprinkles',
{name: 'cookies', when: async () => Promise.resolve(true)},
],
},
],
},
},
args: [],
strict: true,
context: {},
'--': true,
}

const output = {
args: {},
argv: [],
flags: {sprinkles: true, dessert: 'ice-cream', cookies: false},
raw: [
{type: 'flag', flag: 'sprinkles', input: true},
{type: 'flag', flag: 'dessert', input: 'ice-cream'},
{type: 'flag', flag: 'cookies', input: false},
],
metadata: {},
}

// @ts-expect-error
await validate({input, output})
})

describe('mixed', () => {
const input = {
argv: [],
Expand Down

0 comments on commit 96e38f1

Please sign in to comment.