This repository contains experiments converting our CUE-coded schemas into jsonschema format (via openAPI spec) and modeling schema & data operations using jsonschema.
NOTE: lots of libraries to choose from: https://json-schema.org/implementations.html
If you just want to watch some errors fly by - many of which suggest that the schema is wrong, not the dashboard - run go run main.go validate -f ${dashboard json file}
I started with a fun and hacky cue -> openAPI -> jsonschema transformation (openAPI is a subset of jsonschema); the "real" dashboard schema will likely need some massaging.
- cue
- openapi-transformer-toolkit
- gojsonschema - best for go types
- jsonschema - validation
- Start with the dashboard_kind.cue from grafana/grafana
- Remove everything but the
"spec"
section & renamed to #Dashboard - openAPI spec:
cue export --out openapi dashboard_kind.cue > dashboard_kind.openapi
- jsonschema spec:
openapi-transformer-toolkit oas2json -i dashboard_kind.openapi -o ./schemas
- gotypes:
gojsonschema -p dashboard schemas/Dashboard.json -o internal/kinds/dashboard.go
I am struggling with gojsonschema the library - the most basic usage isn't working for me, and the codebase hasn't been touched in years, so I switched to "github.com/santhosh-tekuri/jsonschema" (that had been my first choice for a library; gojsonschema
was what i used to generate the go types)
- validate json dashboard input against local Dashboard.json schema
- validate by marshaling into go types
- the go types generated w/
gojsonschema
include custom unmarshallers; that pattern could be an interesting way to handle schema migrations in go code - json-to-json validation with/https://github.com/santhosh-tekuri/jsonschema
- the go types generated w/
- meta-schema validation
- validate schemas against meta-schema (draft/2020-12)
- validate schemas against meta-schema (core kind)
- validate schemas with remote references (see stretch goals below)
- json schema store webservice dealie
- individual plugin serving schema over gRPC
Generated schema weirdnesses (probably due to Cue -> openAPI -> json weirdnesses)
- DashboardCursorSync enum should have been 0, 1, 2 but ended up w/ 0.0, 0.1 etc in
enumValues_DashboardCursorSyncJson
(fixed manually for now) - exclusiveMinimum and exclusiveMaximum were originally bools in the generated schema, but they should be int.