fredo-dedup / JSONSchema.jl

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

Spurious validation failure with `additionalProperties` #30

Closed helgee closed 3 years ago

helgee commented 3 years ago

The schema and data at the following link produce a spurious validation failure: https://www.jsonschemavalidator.net/s/kGlYqDn1

julia> validate(data, schema)
Validation failed:
path:         top-level
instance:     {
    "billing_address": {
                          "street_address": "1600 Pennsylvania Avenue NW",
                                    "city": "Washington",
                                   "state": "DC"
                       },
   "shipping_address": {
                          "street_address": "1st Street SE",
                                    "city": "Washington",
                                   "state": "DC"
                       }
}
schema key:   additionalProperties
schema value: false

BTW: I am working on JSONSchemaGenerator.jl which automatically generates JSON Schema from Julia structs with StructTypes.jl annotations.

odow commented 3 years ago

Provide a minimal working example. I can't reproduce:

julia> schema = JSONSchema.Schema(raw"""{
           "$schema": "http://json-schema.org/draft-07/schema#",
           "definitions": {
               "address": {
                   "type": "object",
                   "properties": {
                       "street_address": {
                           "type": "string"
                       },
                       "city": {
                           "type": "string"
                       },
                       "state": {
                           "type": "string"
                       }
                   },
                   "required": [
                       "street_address",
                       "city",
                       "state"
                   ],
                   "additionalProperties": false
               }
           },
           "type": "object",
           "properties": {
               "billing_address": {
                   "$ref": "#/definitions/address"
               },
               "shipping_address": {
                   "$ref": "#/definitions/address"
               }
           },
           "required": [
               "billing_address",
               "shipping_address"
           ],
           "additionalProperties": false
       }""")
A JSONSchema

julia> data = JSONSchema.JSON.parse("""{
           "billing_address": {
               "street_address": "1600 Pennsylvania Avenue NW",
               "city": "Washington",
               "state": "DC"
           },
           "shipping_address": {
               "street_address": "1st Street SE",
               "city": "Washington",
               "state": "DC"
           }
       }""")
Dict{String, Any} with 2 entries:
  "shipping_address" => Dict{String, Any}("city"=>"Washington", "street_address"=>"1st Street SE", "state"=>"DC")
  "billing_address"  => Dict{String, Any}("city"=>"Washington", "street_address"=>"1600 Pennsylvania Avenue NW", "state"=>"DC")

julia> validate(data, schema)
helgee commented 3 years ago

Okay, the problem is that I was using JSON3.jl. The workaround here is to always use JSON3.read(data, Dict) or JSON3.read(data, Array) when using JSONSchema.jl in conjunction with JSON3.jl.

Sorry for the noise 😬