Code Monkey home page Code Monkey logo

Comments (6)

santhosh-tekuri avatar santhosh-tekuri commented on July 30, 2024 1

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.

santhosh-tekuri avatar santhosh-tekuri commented on July 30, 2024 1

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.

santhosh-tekuri avatar santhosh-tekuri commented on July 30, 2024

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.

santhosh-tekuri avatar santhosh-tekuri commented on July 30, 2024

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.

davidjb avatar davidjb commented on July 30, 2024

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 panicking.

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.

liangcan1995 avatar liangcan1995 commented on July 30, 2024

How do I update Resouse ?

from jsonschema.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.