Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate JSON Schema for External Validation #363

Open
oxr463 opened this issue Jun 26, 2022 · 2 comments
Open

Generate JSON Schema for External Validation #363

oxr463 opened this issue Jun 26, 2022 · 2 comments
Labels
enhancement New feature or request

Comments

@oxr463
Copy link

oxr463 commented Jun 26, 2022

I would like to take this file https://github.com/coreos/butane/blob/main/config/flatcar/v1_1_exp/schema.go and generate a JSON schema for validating generated Ignition configs,

@jbirch-atlassian
Copy link

jbirch-atlassian commented Aug 20, 2022

I'd like to throw my vote behind this feature also, but for slightly different outcomes.

A machine-readable schema of all specifications in https://coreos.github.io/butane/specs/ will allow me to use my IDE to tell me what I can and can't write, as well as what I must write, instead of me as a human continually referencing the documentation with my fallible eyes and fallible brain. The existing documentation is fantastic for purpose and semantic intent, but machine-readable documentation would be fantastic for speeding up my development — That is, preventing me entirely from writing something invalid, rather than finding out at ignition generation time.

For other readers, in the interim I am generating the schema I need myself, using github.com/invopop/jsonschema. You may find the following snippet helpful if you choose to go down this path also:

package main

import (
	"encoding/json"
	"fmt"
	"os"

	"github.com/coreos/butane/config/fcos/v1_4"
	"github.com/invopop/jsonschema"
)


func main() {
	s := jsonschema.Reflect(v1_4.Config{})
	data, err := json.MarshalIndent(s, "", "  ")
	if err != nil {
		panic(err.Error())
	}

	file, err := os.OpenFile("fcos.v1_4.schema.json", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
	if err != nil {
		panic(err.Error())
	}
	defer file.Close()

	_, err = fmt.Fprintf(file, string(data))
	if err != nil {
		panic(err.Error())
	}
}

Note that this isn't quite the same, as default struct values in Golang are doing a lot of heavy lifting. For example, using jsonschema will generate a document that vaguely suggests that something like KernelArguments is required because it's a KernelArguments not *KernelArguments in the Config struct. If you add this into any schema validation you have in your local development environment, you'll still get stuff yelling at you.

@SRv6d
Copy link

SRv6d commented Aug 30, 2024

Could schema generation be added to internal/doc/main.go so this can be automated? On first glance it doesn't look to be too much work when using invopop/jsonschema as @jbirch-atlassian suggested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants