Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: implies should not fail when implied key's value is 0, false or empty string #1985

Merged
merged 3 commits into from Jul 28, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/validation.ts
Expand Up @@ -339,10 +339,10 @@ export function validation(
} else if (val.match(/^--no-.+/)) {
// check if key/value doesn't exist
val = val.match(/^--no-(.+)/)[1];
val = !argv[val];
val = !Object.prototype.hasOwnProperty.call(argv, val);
} else {
// check if key/value exists
val = argv[val];
val = Object.prototype.hasOwnProperty.call(argv, val);
}
return val;
}
Expand Down
22 changes: 22 additions & 0 deletions test/validation.cjs
Expand Up @@ -101,6 +101,28 @@ describe('validation tests', () => {
failCalled.should.equal(true);
});

it("doesn't fail if implied key exists with value 0", () => {
let failCalled = false;
yargs('--foo --bar 0')
.implies('foo', 'bar')
.fail(msg => {
failCalled = true;
mathieubergeron marked this conversation as resolved.
Show resolved Hide resolved
})
.parse();
failCalled.should.equal(false);
});

it("doesn't fail if implied key exists with value false", () => {
let failCalled = false;
yargs('--foo --bar false')
.implies('foo', 'bar')
.fail(msg => {
failCalled = true;
})
.parse();
failCalled.should.equal(false);
});

it('doesn\'t fail if implied key (with "no" in the name) is set', () => {
let failCalled = false;
const argv = yargs('--bar --noFoo')
Expand Down