Skip to content

Commit 53eb0ea

Browse files
committed
feat: [#229] add JSON schema file and IDE integration
- Create schemas/ directory with environment-config.json - Add VS Code settings for automatic schema validation - Disable progress output when schema command outputs to stdout - Add comprehensive IDE setup guide - Add schemas/README.md explaining schema usage - Update project-words.txt with technical terms This enables IDE autocomplete and validation for environment config files.
1 parent 1fa3ca9 commit 53eb0ea

File tree

6 files changed

+464
-12
lines changed

6 files changed

+464
-12
lines changed

.vscode/settings.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,13 @@
22
"evenBetterToml.formatter.allowedBlankLines": 1,
33
"cSpell.words": [
44
"millis"
5+
],
6+
"json.schemas": [
7+
{
8+
"fileMatch": [
9+
"envs/*.json"
10+
],
11+
"url": "./schemas/environment-config.json"
12+
}
513
]
614
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# JSON Schema IDE Setup Guide
2+
3+
This guide shows you how to get autocomplete, validation, and documentation tooltips in your IDE when editing environment configuration files.
4+
5+
## Quick Start
6+
7+
### 1. Generate the Schema
8+
9+
```bash
10+
cargo run --bin torrust-tracker-deployer -- create schema > envs/environment-schema.json
11+
```
12+
13+
This creates a JSON Schema file that describes the structure and validation rules for environment configurations.
14+
15+
### 2. VS Code Setup (Already Configured)
16+
17+
The repository includes `.vscode/settings.json` with JSON schema mappings. VS Code will automatically provide:
18+
19+
- **Autocomplete**: Press `Ctrl+Space` to see available fields
20+
- **Validation**: Red underlines for invalid values or missing required fields
21+
- **Documentation**: Hover over fields to see descriptions and examples
22+
- **Enum values**: Dropdown suggestions for fields with limited options
23+
24+
### 3. Test It Out
25+
26+
Open any environment file in the `envs/` directory:
27+
28+
```bash
29+
code envs/example.json
30+
```
31+
32+
Try these actions:
33+
34+
- **Start typing** a field name - you'll see autocomplete suggestions
35+
- **Hover** over a field - you'll see documentation from the schema
36+
- **Remove** a required field - you'll see a validation error
37+
- **Type** an invalid value - you'll get instant feedback
38+
39+
## File Patterns Covered
40+
41+
The schema automatically applies to:
42+
43+
- `envs/*.json` - User-provided environment configuration files
44+
45+
**Important**: The schema does NOT apply to `data/*/environment.json` files. Those are internal application state files with a different structure containing additional runtime information beyond the user-provided configuration.
46+
47+
## Manual Schema Association
48+
49+
If you want to use the schema in a file outside the `envs/` directory, add this at the top of your JSON file:
50+
51+
```json
52+
{
53+
"$schema": "../envs/environment-schema.json",
54+
"environment": {
55+
...
56+
}
57+
}
58+
```
59+
60+
## Other IDEs
61+
62+
### IntelliJ IDEA / CLion / RustRover
63+
64+
1. Open **Settings****Languages & Frameworks****Schemas and DTDs****JSON Schema Mappings**
65+
2. Click **+** to add a new mapping
66+
3. Set **Schema file or URL**: `envs/environment-schema.json`
67+
4. Add file pattern: `envs/*.json`
68+
69+
### Neovim with LSP
70+
71+
Add to your LSP configuration:
72+
73+
```lua
74+
require('lspconfig').jsonls.setup({
75+
settings = {
76+
json = {
77+
schemas = {
78+
{
79+
fileMatch = { "envs/*.json" },
80+
url = "file:///absolute/path/to/envs/environment-schema.json"
81+
}
82+
}
83+
}
84+
}
85+
})
86+
```
87+
88+
## Keeping Schema Updated
89+
90+
Regenerate the schema whenever you:
91+
92+
- Add new configuration fields
93+
- Change validation rules
94+
- Update enum values
95+
- Modify field types
96+
97+
```bash
98+
# Quick regeneration
99+
cargo run --bin torrust-tracker-deployer -- create schema > envs/environment-schema.json
100+
```
101+
102+
## Benefits
103+
104+
**Fewer errors**: Catch configuration mistakes before running commands
105+
**Faster editing**: Autocomplete reduces typing and lookups
106+
**Self-documenting**: Descriptions and examples right in your editor
107+
**Type safety**: Validation ensures correct types for all fields
108+
**Discoverability**: See all available options without reading docs
109+
110+
## Troubleshooting
111+
112+
### Schema not working in VS Code
113+
114+
1. Reload the window: `Ctrl+Shift+P` → "Developer: Reload Window"
115+
2. Check `.vscode/settings.json` exists with correct schema mapping
116+
3. Verify the schema file exists at `envs/environment-schema.json`
117+
118+
### Autocomplete not showing
119+
120+
1. Make sure you're editing a `.json` file
121+
2. Check the file is in the `envs/` directory and matches the pattern `envs/*.json`
122+
3. Try `Ctrl+Space` to manually trigger autocomplete
123+
124+
### Validation errors seem wrong
125+
126+
The schema might be outdated. Regenerate it:
127+
128+
```bash
129+
cargo run --bin torrust-tracker-deployer -- create schema > envs/environment-schema.json
130+
```

project-words.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ impls
108108
isreg
109109
journalctl
110110
jsonlint
111+
jsonls
111112
keepalive
112113
keygen
113114
keypair
@@ -122,6 +123,7 @@ logfile
122123
logfiles
123124
logicaldisk
124125
loglevel
126+
lspconfig
125127
lxdbr
126128
maxbytes
127129
mgmt

schemas/README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# JSON Schemas
2+
3+
This directory contains JSON Schema files for validating configuration files used in this project.
4+
5+
## Environment Configuration Schema
6+
7+
**File**: `environment-config.json`
8+
9+
This schema validates user-provided environment configuration files (stored in the `envs/` directory). It ensures that configuration files have:
10+
11+
- Correct structure and required fields
12+
- Valid provider configurations (LXD, Hetzner)
13+
- Proper SSH credentials format
14+
- Valid tracker configuration
15+
16+
## Regenerating the Schema
17+
18+
To regenerate the schema after code changes:
19+
20+
```bash
21+
cargo run --bin torrust-tracker-deployer -- create schema > schemas/environment-config.json
22+
```
23+
24+
**When to regenerate:**
25+
26+
- After adding new configuration fields
27+
- After changing validation rules or types
28+
- After modifying enums or provider options
29+
30+
## IDE Setup
31+
32+
For instructions on configuring your IDE to use this schema for autocomplete and validation, see:
33+
34+
📖 **[JSON Schema IDE Setup Guide](../docs/user-guide/json-schema-ide-setup.md)**
35+
36+
## What This Schema Validates
37+
38+
The schema applies to files matching the pattern `envs/*.json` and validates:
39+
40+
- **Environment settings**: Name, instance name
41+
- **SSH credentials**: Key paths, username, port
42+
- **Provider configuration**: LXD profiles or Hetzner server settings
43+
- **Tracker configuration**: Database, UDP/HTTP trackers, API settings
44+
45+
**Note**: This schema does NOT apply to internal application state files (`data/*/environment.json`), which have a different structure.

0 commit comments

Comments
 (0)