{"id":15097551,"url":"https://github.com/survivorbat/go-scheyaml","last_synced_at":"2025-10-17T13:58:56.652Z","repository":{"id":256603234,"uuid":"855097168","full_name":"survivorbat/go-scheyaml","owner":"survivorbat","description":"Ever wanted to turn a JSON schema into an example YAML file? Probably not, but this library allows you to do just that (in a limited fashion).","archived":false,"fork":false,"pushed_at":"2025-04-02T20:05:31.000Z","size":72,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-14T23:08:45.095Z","etag":null,"topics":["example","go","jsonchema","schema","yaml"],"latest_commit_sha":null,"homepage":"https://pkg.go.dev/github.com/survivorbat/go-scheyaml","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/survivorbat.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2024-09-10T09:53:48.000Z","updated_at":"2025-04-02T20:05:35.000Z","dependencies_parsed_at":"2024-09-20T00:00:57.062Z","dependency_job_id":"2fcb1849-a362-4c53-93b9-77ddde71b39c","html_url":"https://github.com/survivorbat/go-scheyaml","commit_stats":{"total_commits":9,"total_committers":1,"mean_commits":9.0,"dds":0.0,"last_synced_commit":"7f249af7866179108df639f1414d815177012c01"},"previous_names":["survivorbat/go-scheyaml"],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/survivorbat%2Fgo-scheyaml","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/survivorbat%2Fgo-scheyaml/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/survivorbat%2Fgo-scheyaml/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/survivorbat%2Fgo-scheyaml/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/survivorbat","download_url":"https://codeload.github.com/survivorbat/go-scheyaml/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248975316,"owners_count":21192209,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["example","go","jsonchema","schema","yaml"],"created_at":"2024-09-25T16:23:31.126Z","updated_at":"2025-10-17T13:58:51.598Z","avatar_url":"https://github.com/survivorbat.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 📅 Schema YAML\n\nSchemaYAML processes a YAML file that is constrained by [JSON schema](https://json-schema.org) by filling in default values and comments found in the schema. Additionally, the user input (the `overrides`) can be validated (or not with `SkipValidate`) to be compliant with the JSON schema or return an error if not the case.\n\nThe processing is configurable to restrict returning only the required properties, which can be useful when writing\nthe user configuration to disk. This provides a minimal configuration example for the user while when processing the\nfile all remaining defaults can be automatically filled in (by `scheyaml`). See [Usage](#-usage) for an example.\n\n`ScheYAML` returns either the textual representation (configurable with `WithCommentMaxLength` and `WithIndent`) or\nthe raw `*yaml.Node` representation.\n\n`ScheYAML` uses [xeipuuv/gojsonschema](https://github.com/xeipuuv/gojsonschema)'s `jsonschema.Schema` as input.\n\n## ⬇️ Installation\n\n`go get github.com/survivorbat/go-scheyaml`\n\n## 📋 Usage\n\nA simple implementation assuming that a `json-schema.json` file is present is for example:\n\n```go\npackage main\n\nimport (\n _ \"embed\"\n \"fmt\"\n\n \"github.com/kaptinlin/jsonschema\"\n \"github.com/survivorbat/go-scheyaml\"\n)\n\n//go:embed json-schema.json\nvar file []byte\n\nfunc main() {\n // load the jsonschema\n compiler := jsonschema.NewCompiler()\n schema, err := compiler.Compile(file)\n if err != nil {\n  panic(err)\n }\n\n result, err := scheyaml.SchemaToYAML(schema, scheyaml.WithOverrideValues(map[string]any{\n  \"hello\": \"world\",\n }))\n if err != nil {\n  panic(err)\n }\n\n fmt.Println(result)\n}\n```\n\nBut using this `ScheYAML` can be especially useful when also generating the config structs based on the JSON schema using\ne.g. [omissis/go-jsonschema](https://github.com/omissis/go-jsonschema):\n\n```\ngo-jsonschema --capitalization=API --extra-imports json-schema.json --struct-name-from-title -o config.go -p config\n```\n\nGiven some simple schema:\n\n```json\n{\n  \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n  \"title\": \"Config\",\n  \"type\": \"object\",\n  \"properties\": {\n    \"name\": {\n      \"type\": \"string\",\n      \"default\": \"Hello World\"\n    }\n  }\n}\n```\n\nWill generate the following (simplified) Go struct:\n\n```go\ntype Config struct {\n // Name corresponds to the JSON schema field \"name\".\n Name string `json:\"name,omitempty\" yaml:\"name,omitempty\" mapstructure:\"name,omitempty\"`\n}\n```\n\nGiven some config file that should be valid (an empty file):\n\n```yaml\n# yaml-language-server: $schema=json-schema.json\n```\n\nNormally, the default values are \"lost\" when unmarshalling. That's where scheyaml can output a processed\nversion according to the json schema of the input that can be read, in this case as if the user would\nhave supplied:\n\n```yaml\n# yaml-language-server: $schema=json-schema.json\nname: Hello World\n```\n\nSee the example tests in `./examples_test.go` for more details.\n\n## Override- / Default Value Rules\n\nWhen override values are supplied or the json schema contains default values, the following rules apply when determining\nwhich value to use:\n\n1. if the schema is nullable (`\"type\": [\"\u003ctype\u003e\", \"null\"]`) and an override is specified for this key, use the override\n2. if the schema is not nullable and the override is not `nil`, use the override value\n3. if the schema has a default (`\"default\": \"abc\"`) use the default value of the property\n4. if 1..N pattern properties match, use the first pattern property which has a default value (if any)\n\n## ✅ Support\n\n- [x] Feature to override values in output\n- [x] Feature to override the comment on a missing default value\n- [x] Basic types (string, number, integer, null)\n- [x] Object type\n- [x] Array\n- [x] Refs\n- [x] Pattern Properties\n- [x] Add yaml server header\n- [ ] AnyOf\n- [ ] AllOf\n\n## 🔭 Plans\n\nNot much yet.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsurvivorbat%2Fgo-scheyaml","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsurvivorbat%2Fgo-scheyaml","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsurvivorbat%2Fgo-scheyaml/lists"}