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 all commits
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
30 changes: 22 additions & 8 deletions test/validation.cjs
Expand Up @@ -101,18 +101,34 @@ describe('validation tests', () => {
failCalled.should.equal(true);
});

it("doesn't fail if implied key exists with value 0", () => {
yargs('--foo --bar 0')
.implies('foo', 'bar')
.fail(() => {
expect.fail();
})
.parse();
});

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

it('doesn\'t fail if implied key (with "no" in the name) is set', () => {
let failCalled = false;
const argv = yargs('--bar --noFoo')
.implies({
bar: 'noFoo', // --bar means --noFoo (or --no-foo with boolean-negation disabled) is required
// note that this has nothing to do with --foo
})
.fail(msg => {
failCalled = true;
.fail(() => {
expect.fail();
})
.parse();
failCalled.should.equal(false);
expect(argv.bar).to.equal(true);
expect(argv.noFoo).to.equal(true);
expect(argv.foo).to.equal(undefined);
Expand All @@ -134,17 +150,15 @@ describe('validation tests', () => {
});

it('doesn\'t fail if implied key (with "no" in the name) that should not be given is not set', () => {
let failCalled = false;
const argv = yargs('--bar')
.implies({
bar: '--no-noFoo', // --bar means --noFoo (or --no-foo with boolean-negation disabled) cannot be given
// note that this has nothing to do with --foo
})
.fail(msg => {
failCalled = true;
.fail(() => {
expect.fail();
})
.parse();
failCalled.should.equal(false);
expect(argv.bar).to.equal(true);
expect(argv.noFoo).to.equal(undefined);
expect(argv.foo).to.equal(undefined);
Expand Down