|
| 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 | +``` |
0 commit comments