Comments (6)
it is not that straight forward for what you are asking. let us say:
schema1.json is:
{
"$def": {
"a": ...,
"b": ...,
}
}
here when you compile schema1.json
then only schema1.json
is compiled.
schema1.json#/$def/a
and schema1.json#/$def/a
are not compiled
so if you add schema2.json:
{
"$ref": "schema1.json#/$def/a"
}
now compiling schema2.json works correctly if you have used same Compiler
as you suggested, if you create new Compiler
and somehow add schema1 compiled schema to it, still it won't resolve the $ref
in schema2.json
. I hope you got my point.
from jsonschema.
Thanks @santhosh-tekuri, I really appreciate it - the first is effectively what I've implemented at the moment but the second is an interesting idea - I didn't realise the internals of "re-compiling" the same schema.
it does not recompile, before compiling we check if it is already precompiled, if so we return the precompiled schema again.
see https://github.com/santhosh-tekuri/jsonschema/blob/master/compiler.go#L284-L289
from jsonschema.
you can not do that. But you can do the following.
add all your resources into single compiler and then compile each one sequentially; for example:
resources = map[string]string { ....}
c := jsonschema.NewCompiler()
for url, content := range resources {
if err:=c.AddResource(url, strings.NewReader(content)); err!=nil {
panic(err)
}
}
schemas := map[string]*jsonschema.Schema{}
for url := range resources {
sch, err := c.Compile(url)
if err!=nil {
panic(err)
}
schemas[url] = sch
}
there is no restrictions that all resources should be added before compile if they are independent. you can compile and then add new resources which has $ref
to previously compiled and then compile the newly added resources.
from jsonschema.
as long as you use same Compiler
instance, any new resources added can reference to previously compiled schemas
c := jsonschema.NewCompiler()
if err := c.AddResource("schema1.json", strings.newReader("{}"); err!=nil {
panic(err)
}
sch1, err := c.Compile("schema1.json")
if err!=nil {
panic(err)
}
sch1_again, err := c.Compile("schema1.json") // compiling same schema simply returns previously compiled schema
if err!=nil {
panic(err)
}
// now sch1 and sch1_again both point to same schema struct
// now you can add another resource which has $ref to schema1.json
if err := c.AddResource("schema2.json", strings.newReader(`{"$ref": "schema1.json"}`); err!=nil {
panic(err)
}
sch2, err := c.Compile("schema2.json")
if err!=nil {
panic(err)
}
from jsonschema.
Thanks @santhosh-tekuri, I really appreciate it - the first is effectively what I've implemented at the moment but the second is an interesting idea - I didn't realise the internals of "re-compiling" the same schema.
In my case, I was endeavouring to have separation between compilation of schemas where possible so each is a clean environment, and then referencing the the pre-compiled schemas in separate code. Also, I'd aimed to do as much as possible at the top level in a module, avoiding the need for init()
if possible and the boilerplate for checking err
and panic
king.
Is something like AddResourceSchema
feasible? As I say, I've only tested the compilation process with the example above but the $ref
entries do appear to resolve.
from jsonschema.
How do I update Resouse ?
from jsonschema.
Related Issues (20)
- Link in README for YAML playground example is broken HOT 1
- Issue with custom Regex Library HOT 10
- Feature: Adding Context to Validate Method HOT 2
- implementing json.Marshaler interface HOT 9
- Create JSONSchema from Compile()'s result HOT 2
- Wrong unevaluatedProperties validation errors HOT 6
- compiler.go bug in 171
- Invalid JSON schema does not error out HOT 2
- [bug] Not reading from stdin HOT 2
- Missing document file path in validation output when validating multiple documents HOT 5
- Error messages are revealing absolute file paths HOT 1
- YouValidateMe JSON Schema library server project built on this library
- ECMA-262 as the default regex in this library? HOT 1
- jsonschema.Compiler has no field or method CompileRegex HOT 7
- Question: parsing Schema to string HOT 1
- error is missing instanceLocation HOT 1
- Cannot install older version than `@latest` HOT 3
- The latest release doesn't build HOT 2
- Extended the implementation of a new custom validation traversal logic(I'm not sure if there are any other side effects of this)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jsonschema.