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

Defaults not applied from schema #85

Open
YodaDaCoda opened this issue Aug 29, 2019 · 4 comments

Comments

@YodaDaCoda
Copy link

commented Aug 29, 2019

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

@sindresorhus

This comment has been minimized.

Copy link
Owner

commented Sep 9, 2019

I'm not really sure what the problem is. We tell ajv to use the defaults here:

conf/index.js

Line 85 in 931ffce

useDefaults: true,

@issuehunt-app

This comment has been minimized.

Copy link

commented Sep 9, 2019

@issuehunt has funded $40.00 to this issue.


@sindresorhus sindresorhus changed the title Defaults not applied from Schema Defaults not applied from schema Sep 9, 2019

@yaodingyd

This comment has been minimized.

Copy link
Contributor

commented Sep 11, 2019

@YodaDaCoda I cannot reproduce the case with basic type(string). Can you give more details? (OS and node version)

About use object type with default, I believe this is the correct behavior according to ajv: default keywords will be ingored if not in properties or items subschemas. For foo, there is no default on its level, so it will be undefined. I use the following code and get the same result:

const Ajv = require('ajv');

const ajv = new Ajv({
		allErrors: true,
		format: 'full',
		useDefaults: true,
		errorDataPath: 'property'
 });

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


const data = {};

var validate = ajv.compile(schema);
console.log(validate(data)); // true
console.log(data); // { }

The best we can do is document this in readme.

@YodaDaCoda

This comment has been minimized.

Copy link
Author

commented 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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.