|
| 1 | +# documenting local schema reference veriables in vscode json editor |
| 2 | + |
| 3 | +This whole process is part of an effort to figure out how to get vscode schema validation working with json bodies destined for an api endpoint with a swagger/OpenApi spec |
| 4 | + |
| 5 | +The key point about OpenApi schema, is that we can have many different endpoints with many different schemas. So, the typical schema mapping provided by vscode doesn't scale. But I can programmatically provide the schema/reference depending on how the user got the json. |
| 6 | + |
| 7 | +Also, as noted in this issue, there is no programmatic way to provide the schema and/or reference |
| 8 | + |
| 9 | +Seems someone has already opened an issue for this feature (it was pretty much rejected) |
| 10 | +[json] $schema: support predefined variables like ${workspaceFolder} #166438 |
| 11 | +<https://github.com/microsoft/vscode/issues/166438> |
| 12 | + |
| 13 | +OpenApi schema validation #162 |
| 14 | +<https://github.com/microsoft/vscode-json-languageservice/issues/162> |
| 15 | + |
| 16 | +## package.json contributes |
| 17 | + |
| 18 | +<https://code.visualstudio.com/api/references/contribution-points#contributes.jsonValidation> |
| 19 | + |
| 20 | +## current understanding of behavior |
| 21 | + |
| 22 | +Currently the "$schema" value in a vscode editor/textDocument with the language set to json, is used to provide schema validation of the json object. |
| 23 | + |
| 24 | +This schema reference in the json body can be a local file or an HTTP destination that will be fetched |
| 25 | + |
| 26 | +## Behavior change or feature request |
| 27 | + |
| 28 | +Requesting the support of variables in the "$schema" reference value, preferrably the vscode variables for debugging and task json files, so something similar. |
| 29 | + |
| 30 | +The intent is to reference many different schema files bundled with an extension. |
| 31 | + |
| 32 | +### extension schema references? |
| 33 | + |
| 34 | +What about the current ability to reference a schema provided by the extension "contributes" package.json |
| 35 | + |
| 36 | +It is my understanding that this is a static reference of a single schema to many file types |
| 37 | + |
| 38 | +### local project schema reference |
| 39 | + |
| 40 | +The following example shows local workspace reference, meaning the device_schema.json file is in the same directory as the current open project/workspace |
| 41 | + |
| 42 | +```json |
| 43 | +{ |
| 44 | + "$schema": "./device_schema.json", |
| 45 | + "address": "10.1.1.7", |
| 46 | + "port": "5443", |
| 47 | + "device_user": "admin", |
| 48 | + "device_password": "whos-clues", |
| 49 | + "management_user": "admin-cm", |
| 50 | + "management_password": "blues-clues", |
| 51 | + "something": "new" |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +## Other parts of the local file system |
| 56 | + |
| 57 | +The following shows how absolute directory traversal is possible |
| 58 | + |
| 59 | +```json |
| 60 | +{ |
| 61 | + "$schema": "/home/ted/projects/vscode-f5/schemas/nextCm/DeviceDiscoveryRequest.json", |
| 62 | + "address": "10.1.1.7", |
| 63 | + "port": "5443", |
| 64 | + "device_user": "admin", |
| 65 | + "device_password": "whos-clues", |
| 66 | + "management_user": "admin-cm", |
| 67 | + "management_password": "blues-clues", |
| 68 | + "something": "new" |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +## linux local user home system variable |
| 73 | + |
| 74 | +Attempting to use a linux current user dir var is unsuccessful |
| 75 | + |
| 76 | +```json |
| 77 | +{ |
| 78 | + "$schema": "$HOME/projects/vscode-f5/schemas/nextCm/DeviceDiscoveryRequest.json", |
| 79 | + "address": "10.1.1.7", |
| 80 | + "port": "5443", |
| 81 | + "device_user": "admin", |
| 82 | + "device_password": "whos-clues", |
| 83 | + "management_user": "admin-cm", |
| 84 | + "management_password": "blues-clues", |
| 85 | + "something": "new" |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +### error |
| 90 | + |
| 91 | +```text |
| 92 | +Unable to load schema from '/home/ted/projects/f5-fasting/NEXT/$HOME/projects/vscode-f5/schemas/nextCm/DeviceDiscoveryRequest.json': ENOENT: no such file or directory, open '/home/ted/projects/f5-fasting/NEXT/$HOME/projects/vscode-f5/schemas/nextCm/DeviceDiscoveryRequest.json'.(768) |
| 93 | +``` |
| 94 | + |
| 95 | +## vscode exec path |
| 96 | + |
| 97 | +Attempting to use built in vscode variables is unsuccessful (${execPath}) |
| 98 | + |
| 99 | +<https://code.visualstudio.com/docs/editor/variables-reference> |
| 100 | + |
| 101 | +### execPath |
| 102 | + |
| 103 | +```json |
| 104 | +{ |
| 105 | + "$schema": "${execPath}/projects/vscode-f5/schemas/nextCm/DeviceDiscoveryRequest.json", |
| 106 | + "address": "10.1.1.7", |
| 107 | + "port": "5443", |
| 108 | + "device_user": "admin", |
| 109 | + "device_password": "whos-clues", |
| 110 | + "management_user": "admin-cm", |
| 111 | + "management_password": "blues-clues", |
| 112 | + "something": "new" |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +```text |
| 117 | +Unable to load schema from '/home/ted/projects/f5-fasting/NEXT/${execPath}/projects/vscode-f5/schemas/nextCm/DeviceDiscoveryRequest.json': ENOENT: no such file or directory, open '/home/ted/projects/f5-fasting/NEXT/${execPath}/projects/vscode-f5/schemas/nextCm/DeviceDiscoveryRequest.json'.(768) |
| 118 | +``` |
| 119 | + |
| 120 | +### userHome |
| 121 | + |
| 122 | +```json |
| 123 | +{ |
| 124 | + "$schema": "${userHome}/projects/vscode-f5/schemas/nextCm/DeviceDiscoveryRequest.json", |
| 125 | + "address": "10.1.1.7", |
| 126 | + "port": "5443", |
| 127 | + "device_user": "admin", |
| 128 | + "device_password": "whos-clues", |
| 129 | + "management_user": "admin-cm", |
| 130 | + "management_password": "blues-clues", |
| 131 | + "something": "new" |
| 132 | +} |
| 133 | +``` |
| 134 | + |
| 135 | +```text |
| 136 | +Unable to load schema from '/home/ted/projects/f5-fasting/NEXT/${userHome}/projects/vscode-f5/schemas/nextCm/DeviceDiscoveryRequest.json': ENOENT: no such file or directory, open '/home/ted/projects/f5-fasting/NEXT/${userHome}/projects/vscode-f5/schemas/nextCm/DeviceDiscoveryRequest.json'.(768) |
| 137 | +``` |
| 138 | + |
| 139 | +## double bracket on vscode variable |
| 140 | + |
| 141 | +double bracket vscode variables do not work |
| 142 | + |
| 143 | +```json |
| 144 | +{ |
| 145 | + "$schema": "${{userHome}}/projects/vscode-f5/schemas/nextCm/DeviceDiscoveryRequest.json", |
| 146 | + "address": "10.1.1.7", |
| 147 | + "port": "5443", |
| 148 | + "device_user": "admin", |
| 149 | + "device_password": "whos-clues", |
| 150 | + "management_user": "admin-cm", |
| 151 | + "management_password": "blues-clues", |
| 152 | + "something": "new" |
| 153 | +} |
| 154 | +``` |
| 155 | + |
| 156 | +```text |
| 157 | +Unable to load schema from '/home/ted/projects/f5-fasting/NEXT/${{userHome}}/projects/vscode-f5/schemas/nextCm/DeviceDiscoveryRequest.json': ENOENT: no such file or directory, open '/home/ted/projects/f5-fasting/NEXT/${{userHome}}/projects/vscode-f5/schemas/nextCm/DeviceDiscoveryRequest.json'.(768) |
| 158 | +``` |
0 commit comments