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

Add option to validate JSON file #42

Closed
wants to merge 8 commits into from
Closed

Add option to validate JSON file #42

wants to merge 8 commits into from

Conversation

bampakoa
Copy link

If the option validate is true and the configuration file is not a valid JSON, it throws an exception. References sindresorhus/electron-store#35

index.js Outdated
JSON.parse(fs.readFileSync(this.path));
} catch (ex) {
throw ex;
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why you're wrapping this in try/catch? It has no purpose.

And I think this check would be better done here instead:

conf/index.js

Line 152 in 8ce463d

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the try/catch in order to throw the exception to the consumer of the library so that he can handle it appropriately.

I added the check in the constructor because options object is available there and I thought that it would be a good choice to validate early on.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But you're catching and throwing again. These are identical:

try {
	JSON.parse(fs.readFileSync(this.path));
} catch (ex) {
	throw ex;
}
JSON.parse(fs.readFileSync(this.path));

@sindresorhus
Copy link
Owner

Sorry about the slow reply. The option needs to be documented too.

@bampakoa
Copy link
Author

@sindresorhus this is ready

@sindresorhus
Copy link
Owner

Thinking about this a bit more, I think it would be better to handle this in https://github.com/sindresorhus/conf/pull/42/files#diff-168726dbe96b3ce427e7fedce31bb0bcR148 Then you also don't have to read the JSON twice.

@bampakoa
Copy link
Author

bampakoa commented Oct 18, 2018

@sindresorhus I set the validate option as this.validate = options.validate; inside the ctor and then inside the setter of the store I check it as:

if (this.validate) {
   JSON.parse(data);
}

but the test does not pass. It gives this.validate as undefined


test('`validate` option', t => {
	fs.writeFileSync(t.context.conf.path, 'Some text');
	const conf = new Conf({cwd: tempy.directory(), validate: true});
	t.throws(() => conf.store);
});

Any ideas?

@sindresorhus
Copy link
Owner

Hard to say without seeing your code. Can you push what you got?

@bampakoa
Copy link
Author

sure, here it is

test.js Outdated
@@ -331,3 +331,9 @@ test('doesn\'t write to disk upon instanciation if and only if the store didn\'t
exists = fs.existsSync(conf.path);
t.is(exists, true);
});

test('`validate` option', t => {
fs.writeFileSync(t.context.conf.path, 'Some text');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You cannot use the auto-generated instance here. The .path is different for each instance.


test('`validate` option', t => {
fs.writeFileSync(t.context.conf.path, 'Some text');
const conf = new Conf({cwd: tempy.directory(), validate: true});
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const conf = new Conf({cwd: tempy.directory(), validate: true});
const conf = new Conf({cwd: tempy.directory(), validate: true});
fs.writeFileSync(conf.path, 'Non-JSON data');

index.js Outdated
this.path = path.resolve(options.cwd, `${options.configName}${fileExtension}`);
this.path = path.resolve(options.cwd, `${options.configName}.${options.fileExtension}`);

this.validate = options.validate;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
this.validate = options.validate;
this._validate = options.validate;

It's internal and should be marked as such.

@@ -138,6 +140,10 @@ class Conf {
} catch (_) {}
}

if (this.validate) {
JSON.parse(data);
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is incorrect. It will never pass as it's just the same as the parse on line 147.

@bampakoa
Copy link
Author

here they are @sindresorhus. Thanks for your suggestions! I also added a second test for valid data.

test.js Outdated
test('`validate` option with invalid data', t => {
const conf = new Conf({cwd: tempy.directory(), validate: true});
fs.writeFileSync(conf.path, 'Non-JSON data');
t.deepEqual(conf.store, {});
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a t.throws assertion. The whole point of this PR is to throw when invalid config and validate: true.

Copy link
Author

@bampakoa bampakoa Nov 13, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sindresorhus It will never throw because of this check in line 155:

if (error.name === 'SyntaxError') {
  return plainObject();
}

JSON.parse throws SyntaxError which is already caught. We can either try/catch the JSON.parse in line 143-146 and throw error there, which you have suggested not to do, or remove the check for SyntaxError.

@bampakoa
Copy link
Author

bampakoa commented Jan 25, 2019

@sindresorhus in the last commit, I throw an error with custom message to bypass the caught SyntaxError error later on.

@sindresorhus
Copy link
Owner

The implementation is still suboptimal and I unfortunately don't have more time to guide you through this.

@bampakoa bampakoa deleted the validation branch February 11, 2019 20:06
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

Successfully merging this pull request may close these issues.

None yet

2 participants