jsonschema
jsonschema copied to clipboard
Using package with embedded references in go 1.16?
A question asked in our discord
I have a question, not sure if this is the place to ask. But how do you do references using the embed directive recently introduced in Go 16? Example, I have a JSON schema that references another definition in another file, how do we do that with the package?
Veeeery interesting question. Just looked into the embed functionality, and while technically there are not restrictions to pull in the schema from anywhere you like (it consumes just a []byte anyways) having external references does break the flow a bit as it doesn't know how to resolve those. Typically we would handle those by fetching with the appropriate schema ie file:// and http:// however for embed it's up to the user to figure out the resolution mechanics for their embedded files.
The best entry point I currently see to kind of hack this together is to manually register external schemas (and adjust the references accordingly) by manually adding schemas to the SchemaRegistry. If this approach sounds remotely reasonable, I'd be happy to expand the Validate path on Schema to work off of a user supplied state instead of self generating one.
Also posting back on discord just to continue the feedback loop here.
@Arqu so I guess essentially we can use this by loading all the reference schemas and then the referencing schema. Is that what you're saying?
In theory yes, but as said needs some work to be usable. However, a PR got recently merged which would allow you to implement your own resolver, which should open a way for you to actually do this: https://github.com/qri-io/jsonschema/commit/36b007137385468972baae3654ba74320a372047