jsonschema icon indicating copy to clipboard operation
jsonschema copied to clipboard

References not working?

Open rbren opened this issue 3 years ago • 5 comments

Seems like refs are not get resolved as they were in the previous version.

Environment

What is your OS and version?

What version of qri are you using (qri version)?

0.2.0

What version of Qri Desktop are you using?

What browser(s) did you use? What version?

Issue

What did you do?

package main

import (
    "context"
    "encoding/json"
    "fmt"

    "github.com/qri-io/jsonschema"
)

func main() {
    ctx := context.Background()
    var schemaData = []byte(`{
    "$id": "https://qri.io/schema/",
    "$comment" : "sample comment",
    "title": "Person",
    "type": "object",
    "definitions": {
        "age": {
            "type": "integer",
            "minimum": 0
        }
    },
    "properties": {
        "age": {"$ref": "#/definitions/age"}
    },
    "required": ["age"]
  }`)

    rs := &jsonschema.Schema{}
    if err := json.Unmarshal(schemaData, rs); err != nil {
        panic("unmarshal schema: " + err.Error())
    }
   var valid = []byte(`{
        "age": 10
    }`)
    errs, err := rs.ValidateBytes(ctx, valid)
    if err != nil {
        panic(err)
    }

    if len(errs) > 0 {
        fmt.Println(errs[0].Error())
    }
}

What happened?

/age: 10 failed to resolve schema for ref #/definitions/age

What did you expect to happen?

The same schemas work at https://www.jsonschemavalidator.net/

Please link any related issues.

Do you have a suggested fix?

rbren avatar Mar 19 '21 17:03 rbren

As of draft2019_09 it should be $defs instead of definitions. Think definitions should be supported for backwards compatibility, but unsure if that works currently.

Let me know if this helps, I'll keep the issue open so I can come back to it sometime.

Arqu avatar Mar 19 '21 17:03 Arqu

Wouldn't it be possible to just register definitions as a keyword using the jsonschemas.NewDefs handler for it to work in a compatible mode?

jsonschema.RegisterKeyword("definitions", jsonschema.NewDefs)
jsonschema.LoadDraft2019_09()

bmfs avatar Apr 06 '21 10:04 bmfs

I don't think so, as far as I remember, there was a clash if you included both hence it also being disabled in tests. Happy to be wrong about it though if that's not the case.

Arqu avatar Apr 18 '21 21:04 Arqu

I've used this approach and haven't detected any problem so far.

@rbren does it work for you?

bmfs avatar Jul 14 '21 09:07 bmfs

I haven't been able to verify yet - still on the old version

rbren avatar Jul 14 '21 12:07 rbren