|
| 1 | +# Schema Generator |
| 2 | + |
| 3 | +Automatically generates and validates the JSON Schema for `digger.yml` configuration files. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +This tool generates [`digger-schema.json`](../../digger-schema.json) from the Go struct definitions in [`yaml.go`](../../yaml.go), ensuring the schema stays in sync with the actual configuration code. |
| 8 | + |
| 9 | +## Usage |
| 10 | + |
| 11 | +### Generate Schema |
| 12 | + |
| 13 | +```bash |
| 14 | +cd libs/digger_config |
| 15 | +make schema-generate |
| 16 | +``` |
| 17 | + |
| 18 | +This creates/updates `digger-schema.json` with: |
| 19 | +- All fields from `DiggerConfigYaml` and nested structs |
| 20 | +- Type information (string, boolean, array, object) |
| 21 | +- Enum constraints from code constants |
| 22 | +- Field descriptions and default values |
| 23 | +- YAML field name mapping (using `yaml:` struct tags) |
| 24 | + |
| 25 | +### Validate Schema |
| 26 | + |
| 27 | +```bash |
| 28 | +make schema-validate |
| 29 | +``` |
| 30 | + |
| 31 | +Validates that the existing schema matches the current Go struct definitions. Fails if: |
| 32 | +- Schema is missing properties that exist in Go structs |
| 33 | +- Critical enum values don't match code constants |
| 34 | + |
| 35 | +### Run All Checks |
| 36 | + |
| 37 | +```bash |
| 38 | +make schema-check |
| 39 | +``` |
| 40 | + |
| 41 | +Runs both validation and all schema tests. Use this in CI/CD. |
| 42 | + |
| 43 | +## How It Works |
| 44 | + |
| 45 | +1. **Reflection**: Uses [`github.com/invopop/jsonschema`](https://github.com/invopop/jsonschema) to reflect on Go structs |
| 46 | +2. **YAML Tag Mapping**: Configured with `FieldNameTag: "yaml"` to use YAML field names |
| 47 | +3. **Enhancement**: [`enhanceSchema()`](main.go:75) adds: |
| 48 | + - Enum values from code constants (e.g., `CommentRenderModeBasic`) |
| 49 | + - Field descriptions |
| 50 | + - Default values |
| 51 | +4. **Validation**: Compares generated schema against existing file |
| 52 | + |
| 53 | +## Schema ID |
| 54 | + |
| 55 | +The schema is published at: |
| 56 | +``` |
| 57 | +https://docs.opentaco.dev/schemas/digger.yml.json |
| 58 | +``` |
| 59 | + |
| 60 | +## Development |
| 61 | + |
| 62 | +### Adding New Fields |
| 63 | + |
| 64 | +When you add fields to `DiggerConfigYaml`: |
| 65 | + |
| 66 | +1. Add the field with proper `yaml:` tag: |
| 67 | + ```go |
| 68 | + NewField string `yaml:"new_field"` |
| 69 | + ``` |
| 70 | + |
| 71 | +2. Regenerate the schema: |
| 72 | + ```bash |
| 73 | + make schema-generate |
| 74 | + ``` |
| 75 | + |
| 76 | +3. If the field has enum values, add them in [`enhanceSchema()`](main.go:75): |
| 77 | + ```go |
| 78 | + if prop, ok := props.Get("new_field"); ok && prop != nil { |
| 79 | + prop.Enum = []interface{}{"value1", "value2"} |
| 80 | + prop.Description = "Description of new field" |
| 81 | + } |
| 82 | + ``` |
| 83 | + |
| 84 | +### Testing |
| 85 | + |
| 86 | +Schema tests are in [`schema_test.go`](../../schema_test.go). Add tests for: |
| 87 | +- New enum fields |
| 88 | +- Required field validation |
| 89 | +- Default value behavior |
| 90 | + |
| 91 | +## Dependencies |
| 92 | + |
| 93 | +- `github.com/invopop/jsonschema` v0.13.0 - Schema generation |
| 94 | +- `github.com/wk8/go-ordered-map/v2` v2.1.8 - Ordered property maps (transitive) |
| 95 | + |
| 96 | +## Related Files |
| 97 | + |
| 98 | +- [`../../digger-schema.json`](../../digger-schema.json) - Generated schema |
| 99 | +- [`../../yaml.go`](../../yaml.go) - Source structs |
| 100 | +- [`../../schema_test.go`](../../schema_test.go) - Schema validation tests |
| 101 | +- [`../../Makefile`](../../Makefile) - Build targets |
0 commit comments