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.

Defaults not applied from schema

See original GitHub issue

Issuehunt badges

I’m using a schema to create my settings, and I’ve been unable to have conf or electron-config use the defaults provided in the schema.

I’ve included a reduced test case here:

const Conf = require('conf');

const schema = {
  fizz: {
    type: 'string',
    default: 'buzz',
  },
  foo: {
    type: 'object',
    properties: {
      bar: {
        type: 'string',
        default: 'baz',
      },
    },
  },
};

const config = new Conf({schema});

console.log(config.get('fizz'));    // undefined - should be 'buzz'
console.log(config.get('foo'));     // undefined - should be { bar: 'baz' }
console.log(config.get('foo.bar')); // undefined - should be 'baz'

console.assert(config.get('fizz') === 'buzz');   // Assertion failed
console.assert(config.get('foo.bar') === 'baz'); // Assertion failed

An rough solution to populate defaults from schema is below, but I wouldn’t expect to have to do this when using a schema that includes defaults.

function setDefaults(schema, config, key_chain) {
  for (let [key, val] of Object.entries(schema)) {
    let this_key = (key_chain ? key_chain + '.' : '') + key;
    if (val.type === 'object') {
      return setDefaults(val.properties, config, this_key);
    }
    if (!config.has(key) && Object.hasOwnProperty.call(val, 'default')) {
      config.set(this_key, val.default);
    }
  }
}

IssueHunt Summary

Backers (Total: $40.00)

Become a backer now!

Or submit a pull request to get the deposits!

Tips


IssueHunt has been backed by the following sponsors. Become a sponsor

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
sushantdhimancommented, Nov 12, 2021

ajv maintainers has decided that default will not work for nested properties, if top level property has no default value

This issue should be closed, as this library is using useDefaults from ajv for this feature.

2reactions
YodaDaCodacommented, Sep 16, 2019

@yaodingyd thanks, this has made me review my issue.

This is interesting. Adding default: {} to foo in my test above produces the desired results.

From what I can tell, draft 07 of the spec doesn’t define the behaviour of a validator in this regard. Draft 08 adds some examples that happen to include handling of defaults, and seems to imply that the results I expected are correct (i.e. able to specify defaults on a sub-property without specifying that the property is default: {}).

This sounds like an ajv issue. Can anyone with a better understanding than myself weigh in?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Default Schema not being applied even after setting ...
After doing some soul searching I ran the following command that led me to the path that something was amiss (possible with the...
Read more >
Default value not applied when it's defined through $ref #473
Looking at your schema, it appears you have most of it defined under #/test, which is not a valid location for a subschema....
Read more >
default schema not working? - MSDN - Microsoft
I connect to the database, and the default schema seems to be set to ... NOT that you cannot set it for a...
Read more >
Documentation: 15: ALTER DEFAULT PRIVILEGES
ALTER DEFAULT PRIVILEGES allows you to set the privileges that will be applied to objects created in the future. (It does not affect...
Read more >
Default value | Opis JSON Schema
When a data doesn't have a corresponding value, the value of this keyword will be used instead to do the validation checks. This...
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