From ce0b34dac7aaf353a19bc8cde6f3da6c175fa9cc Mon Sep 17 00:00:00 2001 From: Mathieu Bergeron Date: Wed, 14 Jul 2021 10:23:45 -0400 Subject: [PATCH 1/2] fix: implies should not fail when implied key's value is 0 --- lib/validation.ts | 4 ++-- test/validation.cjs | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/lib/validation.ts b/lib/validation.ts index c64fe9380..71dd77db0 100644 --- a/lib/validation.ts +++ b/lib/validation.ts @@ -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; } diff --git a/test/validation.cjs b/test/validation.cjs index ab5bb5bdb..239abc1ef 100644 --- a/test/validation.cjs +++ b/test/validation.cjs @@ -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; + }) + .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') From 4eb9955a8b43901724bf17b387812749058244c1 Mon Sep 17 00:00:00 2001 From: Mathieu Bergeron Date: Thu, 15 Jul 2021 16:13:22 -0400 Subject: [PATCH 2/2] addressed PR comment --- test/validation.cjs | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/test/validation.cjs b/test/validation.cjs index 239abc1ef..6e15ab4f9 100644 --- a/test/validation.cjs +++ b/test/validation.cjs @@ -102,39 +102,33 @@ describe('validation tests', () => { }); 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; + .fail(() => { + expect.fail(); }) .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; + .fail(() => { + expect.fail(); }) .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') .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); @@ -156,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);