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.