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

Incorrect default type should not be allowed #143

Open
sindresorhus opened this issue Nov 5, 2018 · 8 comments
Open

Incorrect default type should not be allowed #143

sindresorhus opened this issue Nov 5, 2018 · 8 comments

Comments

@sindresorhus
Copy link
Member

The parser should not allow setting the incorrect type for default.

yargs([], {number: ['foo'], default: {foo: 'x'}});
//=> { _: [], foo: 'x' }

The above should not be allowed as we defined foo as a number, but the default is set to a string. This should throw an error to prevent mistakes.

@bcoe
Copy link
Member

bcoe commented Jan 28, 2019

@sindresorhus all of the validation currently happens inside of yargs (https://github.com/yargs/yargs/blob/master/lib/validation.js). Challenges to implementing this feature in yargs-parser, from yargs' point of view are:

  1. would need to figure out a way to handle translations, even though all the language files are in yargs.
  2. would probably need to refactor the validation.js in yargs, moving some of the logic towards `yargs-parser.

I think this could be a really interesting task for someone.

@bcoe
Copy link
Member

bcoe commented Jan 28, 2019

refreshing myself with how we handle other strings, we do already have support for translations in yargs-parser, which is good:

  var __ = opts.__ || function (str) {
    return util.format.apply(util, Array.prototype.slice.call(arguments))
  }

@sindresorhus
Copy link
Member Author

would need to figure out a way to handle translations, even though all the language files are in yargs.

Give the errors a unique .code property code and then detect that in Yargs. yargs-parser should not do translations.

@bcoe
Copy link
Member

bcoe commented Jan 28, 2019

@sindresorhus I was about to reply; since this is a development time error, not a user-land error, I wonder fi we could get away with a throw in this one case, even though we're not throwing for any other errors currently in the parser.

@sindresorhus
Copy link
Member Author

Yes, throwing would be the correct thing to do here. It's a developer-error, not a end-user error. Throw early.

@bcoe
Copy link
Member

bcoe commented Nov 18, 2019

@sindresorhus, @juergba has been starting to add some more early validation checks in yargs-parser, this seems like an ideal candidate for one.

@bcoe
Copy link
Member

bcoe commented Sep 20, 2020

see: #301

@Eomm
Copy link
Contributor

Eomm commented Sep 21, 2020

If it doesn't cost too much effort, would you mind to keep an option to coerce the values as it was in previous versions of the module?

An example use case is to save the user's default in the env and then use process.env to define the yargs-parser defaults.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants