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
Conversation
index.js
Outdated
JSON.parse(fs.readFileSync(this.path)); | ||
} catch (ex) { | ||
throw ex; | ||
} |
There was a problem hiding this comment.
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:
Line 152 in 8ce463d
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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));
Sorry about the slow reply. The option needs to be documented too. |
@sindresorhus this is ready |
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. |
@sindresorhus I set the validate option as
but the test does not pass. It gives
Any ideas? |
Hard to say without seeing your code. Can you push what you got? |
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'); |
There was a problem hiding this comment.
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}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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); | |||
} |
There was a problem hiding this comment.
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.
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, {}); |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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
.
@sindresorhus in the last commit, I throw an error with custom message to bypass the caught |
The implementation is still suboptimal and I unfortunately don't have more time to guide you through this. |
If the option
validate
is true and the configuration file is not a valid JSON, it throws an exception. References sindresorhus/electron-store#35