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

Convict doesn't persist date / other custom objects #392

Open
irgijs opened this issue May 20, 2021 · 0 comments
Open

Convict doesn't persist date / other custom objects #392

irgijs opened this issue May 20, 2021 · 0 comments

Comments

@irgijs
Copy link

irgijs commented May 20, 2021

I ran into an issue where convict doesn't persist configuration data if you attempt to set a custom-defined configuration object such as a date. Instead of returning the value that has been set, the default value is returned - even though that is a date as well.

Code to reproduce:

const convict = require('convict');
convict.addFormat('date', value => assertValidDate(value), date => new Date(date));

/**
 * Supporting function used to verify whether a string is formatted as a valid date.
 *
 * @param {string} date - The string which should be checked.
 * @throws {Error} if assertion fails.
 */
function assertValidDate(date) {
	// Date constructor does not throw an error if an invalid date is provided. valueOf() returns NaN when a date is invalid.
	if (Number.isNaN(date.valueOf())) {
		throw new Error('Provided value is not a valid date');
	}
}

// configuration schema
const configurationSchema = {
	installDate: {
		doc: 'Date on which ',
		format: 'date',
		default: new Date('8900-12-31Z'),
	},
};

// config that is set
const configFileData = {
	installDate: new Date('2021-01-01Z'),
}

const config = convict(configurationSchema).load(configFileData);


const installDate = config.get('installDate');

// output: 8900-12-31T00:00:00.000Z
// expected: 2021-01-01T00:00:00.000Z
console.log(installDate.toISOString());

It seems that this bug is caused by the function overlay

  1. The first time the if statement in this function is called it evaluates to false, so the overlay is function is called again.
  2. The second time the overlay function is called, the parameter from = new Date('2021-01-01Z') and to is new Date('8900-12-31Z')
  3. Object.keys(from) returns array[0], therefore the default parameter is never overwritten.
  4. The config still contains the default value instead of the value I attempted to set. No warning is generated.

Expected result: The config value I tried to set is persisted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant