jsonschema validation with golang #1780
Replies: 3 comments
-
|
Hi! I just started learning CUE and I'm also curious about this. The current library is marked as experimental, but I want to use it. Having example code for this would be appreciated. I use mage to run tasks, so I prefer doing import jsonschema / validate generated jsonschema against written CUE files with go library than invoking |
Beta Was this translation helpful? Give feedback.
-
|
You want to use You might also need to look at the Orphaned files, like this: |
Beta Was this translation helpful? Give feedback.
-
|
Thanks for the suggestion, but I don't think the problem OP mentioned is solved yet.
This would handle the loading of CUE/JSON files into Go program. Great! But, how do I transform this loaded data (in CUE format) into proper CUE schema? It definitely needs to be For example, this github-workflow.json is the source JSON input. The task is to output a CUE schema file like this github-workflow.cue. The minimal code (please ignore the dumb fmt.Println and no error check): inputBytes, _ := os.ReadFile("./github-workflow.json")
ctx := cuecontext.New()
inputCue := ctx.CompileBytes(inputBytes)
A, _ := jsonschema.Extract(inputCue, &jsonschema.Config{
ID: "https://json.schemastore.org/github-workflow.json",
PkgName: "json",
})
outputCue := ctx.BuildFile(A)
fmt.Println(outputCue)It outputs this {
on: "branch_protection_rule" | "check_run" | "check_suite" | "create" | "delete" | "deployment" | "deployment_status" | "discussion" | "discussion_comment" | "fork" | "gollum" | "issue_comment" | "issues" | "label" | "member" | "milestone" | "page_build" | "project" | "project_card" | "project_column" | "public" | "pull_request" | "pull_request_review" | "pull_request_review_comment" | "pull_request_target" | "push" | "registry_package" | "release" | "status" | "watch" | "workflow_call" | "workflow_dispatch" | "workflow_run" | "repository_dispatch" | ["branch_protection_rule" | "check_run" | "check_suite" | "create" | "delete" | "deployment" | "deployment_status" | "discussion" | "discussion_comment" | "fork" | "gollum" | "issue_comment" | "issues" | "label" | "member" | "milestone" | "page_build" | "project" | "project_card" | "project_column" | "public" | "pull_request" | "pull_request_review" | "pull_request_review_comment" | "pull_request_target" | "push" | "registry_package" | "release" | "status" | "watch" | "workflow_call" | "workflow_dispatch" | "workflow_run" | "repository_dispatch", ..."branch_protection_rule" | "check_run" | "check_suite" | "create" | "delete" | "deployment" | "deployment_status" | "discussion" | "discussion_comment" | "fork" | "gollum" | "issue_comment" | "issues" | "label" | "member" | "milestone" | "page_build" | "project" | "project_card" | "project_column" | "public" | "pull_request" | "pull_request_review" | "pull_request_review_comment" | "pull_request_target" | "push" | "registry_package" | "release" | "status" | "watch" | "workflow_call" | "workflow_dispatch" | "workflow_run" | "repository_dispatch"] | {}
jobs: {}
}instead of the proper CUE schema file I got from running |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I want to parse a jsonschema into a cue file. When I use the command-line client to do it, it works as expected. But I could not find the right way to do it with golang. I did something like this
but it returns some errors. Am I doing it the right way ?
Beta Was this translation helpful? Give feedback.
All reactions