Quantcast
Channel: Active questions tagged visual-studio-code - Stack Overflow
Viewing all articles
Browse latest Browse all 98916

What is the best way to type-check the configuration for vscode extension?

$
0
0

I have a vscode extension that defines a bunch of items in "configuration" contribution point in package.json. For example:

{
    "configuration": {
        "title": "My extension config",
        "type": "object",
        "properties": {
            "my-ext.path-to-some-binary": {
                "type": ["string"],
                "default": "/default/path",
                "description": "Path to some binary, this must be a string and nothig else!"
            }
        }
    }
}

But when I read the configuration from my extension I am not guaranteed to get string type from specified configuration value at runtime.

const value: unknown = vscode.workspace.getConfiguration("my-ext").get("path-to-some-binary");
if (typeof value !== "string") {
    console.error("User you are crazy!");
}

And type checking each configuration value gets very tedious. Why vscode doesn't have an API to deal with invalid values in settings.json if it has a type schema defined in package.json? And what is the best way to match the config values at runtime to the schema defined in package.json without doing it manually or repeating the schema in code using some npm package like yup or joi?

I know that vscode-cpptools has some custom handwritten code for matching the config values to schema defined in package.json, but that seems too messy.


Viewing all articles
Browse latest Browse all 98916

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>