I’m trying to write a JSON schema for a config file, so the config file object has a tasks array containing several properties, one of them being an enum "restart".
I want my configuration to have a "restartAttempts" property only if "restart" is set to "on-failure", so I thought I could use "if", "then" blocks in my schema, but it turns out that it’s not working as expected, even if I provide "restart": "on-failure", I still get a syntax error in my lsp telling me property restartAttempts is not allowed
. Here is the schema:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "TMConfig",
"description": "TaskMaster configuration file",
"type": "object",
"properties": {
"tasks": {
"type": "array",
"description": "The tasks to be executed",
"items": {
"type": "object",
"properties": {
"command": {
"type": "string",
"description": "The executable to run"
},
"arguments": {
"type": "array",
"default": [],
"description": "The arguments to pass to the executablenDefault is []",
"items": {
"type": "string"
}
},
"startAtLaunch": {
"type": "boolean",
"default": true,
"description": "Wether or not to start the process(es) at launchnDefault is true"
},
"instances": {
"type": "number",
"default": 1,
"description": "The number of instances of the process to runnDefault is 1",
"minimum": 1
},
"restart": {
"enum": ["always", "never", "on-failure", "unless-stopped"],
"default": "on-failure",
"description": "'always': always restart the process if it exitsn'never': never restart the processn'on-failure': restart if the process exits with an error coden'unless-stopped': restart the process except if the user stops it manually through the consolenDefault is 'on-failure'"
},
"expectedExitStatus": {
"type": "number",
"default": 0,
"description": "The expected success exit status code for the process(es)nDefault is 0"
},
"startTime": {
"type": "number",
"default": 0,
"description": "The time to wait (in milliseconds) before considering that a process is sucessfuly startednDefault is 0",
"minimum": 0
},
"stopTime": {
"type": "number",
"default": 5000,
"description": "The time to wait (in milliseconds) after a graceful stop before killing a processnDefault is 5000",
"minimum": 0
},
"stopSignal": {
"enum": [
"SIGINT",
"SIGQUIT",
"SIGTERM",
"SIGUSR1",
"SIGUSR2",
"SIGSTOP",
"SIGTSTP"
],
"default": "SIGSTOP",
"description": "The signal used to quit a process gracefullynDefault is 'SIGSTOP'"
},
"stdout": {
"enum": ["ignore", "inherit", "redirect"],
"default": "redirect",
"description": "'ignore': ignore stdoutn'inherit': show stdout on taskmaster consolen'redirect': redirect stdout to a log filenDefault is 'redirect'"
},
"stderr": {
"enum": ["ignore", "inherit", "redirect"],
"default": "redirect",
"description": "'ignore': ignore stderrn'inherit': show stderr on taskmaster consolen'redirect': redirect stderr to a log filenDefault is 'redirect'"
},
"logDir": {
"type": "string",
"default": "/var/log/taskmaster",
"description": "The path in which to save output log filesnDefault is /var/log/taskmaster"
},
"environment": {
"type": "object",
"default": {},
"description": "Variables to pass as environment to the process(es)nDefault is {}",
"required": [],
"additionalProperties": {
"type": "string"
}
},
"workingDirectory": {
"type": "string",
"default": ".",
"description": "The working directorynDefault is '.'"
},
"permissions": {
"type": ["number", "null"],
"default": null,
"description": "The permissions umask to set before launching the program, or null to leave the permissions as they arenDefault is null",
"maximum": 777
}
},
"if": {
"properties": {
"restart": { "const": "on-failure" }
}
},
"then": {
"properties": {
"restartAttempts": {
"type": "number",
"default": 5,
"description": "The number of restart attempts (or 0 for infinite attemps) if 'restart' is set to 'on-failure'nDefault is 5",
"minimum": 0
}
}
},
"required": ["command"],
"additionalProperties": false
},
"additionalItems": false
}
}
}
I’m using neovim with Mason and the jsonls lsp (vscode-json-language-server).
I checked if there was an available update in Mason / Lazy, but it seems to be up to date, but I’m doing the same as the JSON Schemas official example
2
Answers
Solved using
"unevaluatedProperties": false
instead of"additionalProperties": false
(see Json schema add property on if condition)I recommend setting
required
keyword in thethen
statement. Otherwise, your schema can haverestart
andrestartAttempts
but it will always evaluated to true ifrestartAttempts
is missing or is misspelled.without required, both of these are still passing validation because
restartAttempts
will only be evaluated if it’s present, when not required.