sindresorhus/conf

Constructor throws exception if underlying Config is invalid

BittnerBarnabas opened this issue · 4 comments

Trying to use the component in an electron app, with specifying the JSON schema.

What I expect is that I can create the store with a potentially invalid underlying config, but when I call store.get(key) an exception should be thrown that the config is not valid.

What happens currently is that an exception is thrown from the constructor, as it seems there's no way to disable validation when the object is created: https://github.com/sindresorhus/conf/blob/main/source/index.ts#L133 . Currently, this makes it impossible to gracefully fix the config, the only option is to nuke the whole thing with clearInvalidConfig which will make users really mad that they lose their whole config. <- just tested this, doesn't work 😞

Example:
config.json contents:

{ "myKey": "myValue" }

Code:

store = new ElectronStore({
        schema: {
            myKey: {
                type: 'boolean',
            },
        },
    })

This throws an exception right away Error: Config schema violation: 'myKey' must be boolean .

Same issue

Same issue, would really like to be able to log / handle errors but still be able to create a Store and get values from other collections / properties within a collection which do pass validation

This issue sounds similar to #173. I'm really hoping we can get this sorted, and see this meaningful improvement included in this library. Unfortunately, that issue has already been inactive since the beginning of 2023

@BittnerBarnabas with #194 merged you should be able to write a migration to update your schema to a valid schema before validation is ran. Can you test with 13.0.1 to see if implementing a migration resolves your issue?