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: setting default: undefined
causes implies to throw
#2335
fix: setting default: undefined
causes implies to throw
#2335
Conversation
I agree with your approach in principle. The author may have supplying a value they got from somewhere else and not have checked whether it is undefined. A familiar example is The I wonder about changing the routine name from |
bdbd35d
to
de7b6ee
Compare
Hey, yeah that makes sense to me. Updated |
lib/validation.ts
Outdated
val = | ||
!Object.prototype.hasOwnProperty.call(argv, val) && | ||
argv[val] !== undefined; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a negation on this one, so:
val = | |
!Object.prototype.hasOwnProperty.call(argv, val) && | |
argv[val] !== undefined; | |
val = | |
!Object.prototype.hasOwnProperty.call(argv, val) || | |
argv[val] === undefined; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, good catch.
de7b6ee
to
342d802
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
A few concerns:
In order to maintain the current behavior, could there be a configuration option to opt into the new behavior, or maybe a custom |
Thanks for comments @jly36963 . It looks like https://github.com/yargs/yargs/blame/e517318cea0087b813f5de414b3cdec7b70efe33/lib/validation.ts#L406
No, I do not think this is worth separate configuration. Any discussion about a "defined" value of |
Yeah, that might already be the case with I still think switching the |
Another simple solution might be to use a pre-validation middleware that removes keys from argv if their values are |
Looking through the the documentation, I spotted one case where Yargs sets an option to undefined when it is used on the command-line. I think this means we can't change the treatment of
I tried it out with a toy program, and sure enough this PR changes the behaviour:
|
Generally, the reason I am so hesitant with this change is related to my use of another library. With Knex (sql querybuilder), both inserts/updates ignore keys with I imagine that there are people who rely on yargs behavior remaining unchanged, in the same way that I rely on Knex behavior remaining unchanged. I recognize that Knex's behavior around keys/undefined is similar to the behavior that this PR is trying to faciliate, so it is (of sorts) the inverse scenario. In other words, changing the behavior around keys with undefined values:
Nonetheless, I anticipate people being affected by this change |
Going to close this as not-safe-enough, sorry @AgentEnder , thanks for the PR. It can be hard to fix edge cases in a big complex program without risking affecting other cases. |
When setting
.implies(key, otherKey)
, yargs will throw always throw a validation error ifkey
has a default set, even if the default isundefined
.I do concede that setting an explicit default of
undefined
is a bit weird, but this is something I came across while setting up better validations in a codebase that already had yargs configured. In my case, I did just remove the default, but it seemed like something that shouldn't have necessarily been needed to change.If you think this is unnecessary, I can close it out.