Skip to content

How to use $ref relative to the root? #164

Closed Answered by santhosh-tekuri
debuggerpk asked this question in Q&A
Discussion options

You must be logged in to vote

You can use Compiler.LoadURL to handle this situation.

say you have cloned the schemas git repo into /tmp:

$ cd /tmp
$ git clone https://github.com/cdevents/spec.git

now you can compile schema as follows:

prefix := "https://cdevents.dev/0.4.0/schema/"

c := jsonschema.NewCompiler()
c.LoadURL = func(s string) (io.ReadCloser, error) {
    suffix, ok := strings.CutPrefix(s, prefix)
    if !ok {
        return nil, fmt.Errorf("%s not found", s)
    }
   return os.Open("/tmp/"+suffix)
}
sch, err := c.Compile(prefix+"artifact-deleted-event")

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@debuggerpk
Comment options

Answer selected by santhosh-tekuri
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants