conf icon indicating copy to clipboard operation
conf copied to clipboard

Constructor throws exception if underlying Config is invalid

Open BittnerBarnabas opened this issue 1 year ago • 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 .

BittnerBarnabas avatar Oct 21 '23 19:10 BittnerBarnabas

Same issue

ParticleG avatar Feb 05 '24 04:02 ParticleG

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

simondw avatar Feb 13 '24 10:02 simondw

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

tim661811 avatar Mar 04 '24 06:03 tim661811

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

dopry avatar Jul 10 '24 16:07 dopry