Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix: implies should not fail when implied key's value is 0, false or …
…empty string (#1985)
  • Loading branch information
mathieubergeron committed Jul 28, 2021
1 parent 14bd6be commit 8010472
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
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

0 comments on commit 8010472

Please sign in to comment.