fredo-dedup / JSONSchema.jl

JSON Schema validation package for Julia
Other
31 stars 12 forks source link

validation fails every time #46

Closed mkaut closed 1 year ago

mkaut commented 1 year ago

Validation fails for any example I try, with the same result:

Validation failed:
path:         top-level
instance:     {}
schema key:   type
schema value: object

except that instance obviously varies with the tested data.

The above is from schema:

{
    "$schema": "http://json-schema.org/draft-06/schema",
    "type": "object",
    "properties": {
        "id": {
            "type": "integer"
        }
    }
}

and data = {}, but I get the same error whatever I try.

I use it as follows:

import JSONSchema
import JSON
schema = JSONSchema.Schema(JSON.parsefile("test.schema.json"))
data = read("test.json", String)  # or just data = "{}" for the test above
JSONSchema.validate(data, schema)

Julia 1.9 on Windows, JSONSchema 1.1, JSON 0.21.4.

odow commented 1 year ago

Are you passing the String "{}"? JSONSchema.jl operates on parsed JSON data, not the string representation. Perhaps we should make this clearer with an error, or use JSON.parse on string input.

odow commented 1 year ago
julia> schema = JSONSchema.Schema(raw"""
       {
           "$schema": "http://json-schema.org/draft-06/schema",
           "type": "object",
           "properties": {
               "id": {
                   "type": "integer"
               }
           }
       }
       """)
A JSONSchema

julia> data = "{}"
"{}"

julia> JSONSchema.validate(schema, data)
Validation failed:
path:         top-level
instance:     {}
schema key:   type
schema value: object

julia> JSONSchema.validate(schema, JSONSchema.JSON.parse(data))
odow commented 1 year ago

See https://github.com/fredo-dedup/JSONSchema.jl/pull/47

mkaut commented 1 year ago

Thanks a lot, that was stupid of me - should have noticed that.