Populate context.options with the default values when no options are present
See original GitHub issueThe version of ESLint you are using.
7.14.0
The problem you want to solve.
When writing a plugin, I want to specify some default value for the rules. However, when the end-user don’t specify any option, nothing is passed to the rule context.
This kind of kill the point of default
values in the schema.
Your take on the correct solution to problem.
Apparently, AJV
fill the schema with the default value in place. It means that is no object is passed down, it can’t fill it.
I think, passing an empty object if no options are specified could solve the issue.
Are you willing to submit a pull request to implement this change?
Yes
I already made a minimal reproduction repo.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:3
- Comments:9 (4 by maintainers)
Top Results From Across the Web
Entity Framework not including columns with default value in ...
When I save a new entity in the database using context.SaveChanges() , I noticed that the columns with default values are not included...
Read more >Generated Values - EF Core - Microsoft Learn
This page details various patterns for configuration value generation with EF Core. Default values. On relational databases, a column can be ...
Read more >Configuring custom field contexts - Atlassian Documentation
To change the default value, select Edit default value. To change the options, select Edit options. This will not be available for custom...
Read more >Context options - SideFX
You can also create and read context option values in Python scripts. Context options are saved with the HIP file. A newly created...
Read more >Setting Default Option Values from Config Files with Click
Here is a sample Python script with a --config option that reads from a given config file (or from config.ini in the current...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
That’s how Ajv works, probably for a good reason. Is there a JSON Schema validator that works differently? I think that modifying the schema validation process to a (most likely) uncommon behavior is too complex and out of scope for ESLint.
What does seem to work with Ajv is setting defaults for objects, which then also get validated against the object’s schema in the same way, including applying defaults:
So it looks like you could add
default: {}
in the rule’s schema to enforce creating an object with default values when user doesn’t specify anything.An issue with this approach is that ESLint currently doesn’t support
default
in top-level arrays, as noticed in https://github.com/eslint/eslint/issues/11749. We’re passing a temporary array (Array.isArray(options) ? options.slice(1) : []
) to Ajv, so any changes that validation makes directly on the provided array are lost, unlike changes on deep objects. I think we can consider making this enhancement/bug fix.Unfortunately, it looks like there wasn’t enough interest from the team or community to implement this change. While we wish we’d be able to accommodate everyone’s requests, we do need to prioritize. We’ve found that issues failing to reach accepted status after 21 days tend to never be accepted, and as such, we close those issues. This doesn’t mean the idea isn’t interesting or useful, just that it’s not something the team can commit to.
Thanks for contributing to ESLint and we appreciate your understanding.