Skip to content

Document how to use SchemaRegistry #116

@ieure

Description

@ieure

I have a schema broken into a few components, common.json, input.json, and stored.json; input and stored both reference types from common.

I'm loading all the schemas in my package's init():

var (
	SchemaCommon *jsonschema.Schema
	SchemaInput  *jsonschema.Schema
	SchemaStored *jsonschema.Schema
)

func init() {
	SchemaCommon = jsonschema.Must(schemaCommon)
	SchemaInput = jsonschema.Must(schemaInput)
	SchemaStored = jsonschema.Must(schemaStored)
}

When I try to validate a document against the input or stored schemas, it gives me a "failed to resolve schema for ref" error.

So, okay, it looks like I have to register the schemas in the schema registry for jsonschema to know about them. That's fine, but the obvious Register() / Get() mechanism doesn't work:

func init() {
	SchemaCommon = jsonschema.Must(schemaCommon)
	SchemaInput = jsonschema.Must(schemaInput)
	SchemaStored = jsonschema.Must(schemaStored)

	reg := jsonschema.GetSchemaRegistry()
	reg.Register(SchemaCommon)
	s := reg.Get(context.Background(), "https://example.com/schema/1.0/common.json")
	if s == nil {
		panic("Schema wasn't registered")
	}
}
panic: Schema wasn't registered

The ID I'm supplying is the value of the $id property at the top level of common.json.

Similarly, if I use RegisterLocal() / GetLocal(), I get a nil schema back as well.

How is this supposed to work?

Specific things it would be good to document:

  • What's the difference between the "local" and "top level" contexts?
  • What's the difference between Get() and GetKnown()? What makes a schema "known?"
  • What's the difference between Schema.Register() and SchemaRegistry.Register()? What cases should each be used in?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions