Defaults not applied from schema
See original GitHub issueI’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)
issuehunt ($40.00)
Become a backer now!
Or submit a pull request to get the deposits!
Tips
- Checkout the Issuehunt explorer to discover more funded issues.
- Need some help from other developers? Add your repositories on IssueHunt to raise funds.
IssueHunt has been backed by the following sponsors. Become a sponsor
Issue Analytics
- State:
- Created 4 years ago
- Comments:10 (3 by maintainers)
Top 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 >
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 Free
Top 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

ajvmaintainers has decided thatdefaultwill not work for nested properties, if top level property has nodefaultvalueThis issue should be closed, as this library is using
useDefaultsfromajvfor this feature.@yaodingyd thanks, this has made me review my issue.
This is interesting. Adding
default: {}tofooin 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
ajvissue. Can anyone with a better understanding than myself weigh in?