qri-io / jsonschema

golang implementation of https://json-schema.org drafts 7 & 2019-09
MIT License
461 stars 54 forks source link

Make json.Marshal normalize refs #74

Open amansx opened 4 years ago

amansx commented 4 years ago

How can I make the "remote" refs here, be normalized as one consolidated schema?


        var schemaData = []byte(`{
        "$id": "schema://a",
        "type": "object",
        "properties": {
            "a": {"type": "integer"},
            "friend": {
                "name" : {"type": "bool"}
            }
        },
        "required": ["a"]       
    }`)

    var schemaData2 = []byte(`{
        "$id": "schema://b",
        "type": "object",
        "properties": {
            "allfriends": {
                "type" : "array",
                "items" : { "$ref" : "schema://a" }
            }
        }
    }`)

    r := jsonschema.GetSchemaRegistry()

    g1 := &jsonschema.Schema{}
    g2 := &jsonschema.Schema{}

    if err := json.Unmarshal(schemaData, &g1); err != nil {
        panic("unmarshal schema: " + err.Error())
    }
    g1.Register("schema", r)

    if err := json.Unmarshal(schemaData2, &g2); err != nil {
        panic("unmarshal schema: " + err.Error())
    }
    g2.Register("", r)
Kemosabert commented 4 years ago

Hey @amansx, I was looking for the same thing and managed to resolve it by first reading all the schemas in the global registry and then performing the validations.

Arqu commented 4 years ago

I don't think this is supported right now as during the Marshal process we only serialize the reference hence it doesn't print out the resolved schema if you try to print it out. For that to happen we would need to extend the marshalling to serialize the r.resolved schema instead of it's reference. If we do add this, I'd like to have this under a flag or env var of some sort.