question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Populate context.options with the default values when no options are present

See original GitHub issue

The 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:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
mdjermanoviccommented, Dec 5, 2020

why there’s a difference between “no options” and “empty option”

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:

const schema = {
  type: "array",
  items: [
      {
          type: "object",
          properties: {
              first: {
                  type: "string",
                  default: "Here I am !"
              },
              second: {
                  type: "string",
                  default: "Me too"
              }
          },
          additionalProperties: false,
          default: {} // <--
      }
  ],
  minItems: 0,
  maxItems: 1
};

const Ajv = require("ajv");

const ajv = new Ajv({ useDefaults: true });

const validate = ajv.compile(schema);

const data = [ /* nothing */ ];

validate(data);

console.log(data); // [ { first: 'Here I am !', second: 'Me too' } ]

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.

0reactions
eslint-deprecated[bot]commented, Jan 9, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found