garycourt / JSV

A JavaScript implementation of an extendable, fully compliant JSON Schema validator.
618 stars 84 forks source link

Union type validation only checks against first definition #45

Closed nikos closed 12 years ago

nikos commented 12 years ago

It seems to me like JSV takes only the very first union constraint for validation, but not any subsequent. My JSON schema definition looks like ...

{
    "type":"object",
    "additionalProperties":false,
    "properties":{
        "imgs":{
            "type":"array",
            "title":"Images",
            "items":[
                {
                    "type":"object",
                    "additionalProperties":false,
                    "properties":{
                        "image_url":{
                            "title":"Image (intern)",
                            "type":"string",
                            "format":"uri"
                        }
                    }
                },
                {
                    "type":"object",
                    "additionalProperties":false,
                    "properties":{
                        "flickr_url":{
                            "title":"Image (flickr)",
                            "type":"string",
                            "format":"uri"
                        }
                    }
                }
            ]
        }
    }
}

It is fine for

{
"imgs":[
  {"image_url":"http://example.org"}
]
}

but not

{
"imgs":[
  {"flickr_url":"http://example.org"}
]
}

Any help would on this would be very much appreciated, thanks for your time, Niko

garycourt commented 12 years ago

The array version of "items" is tuple typing. You need to use "additionalItems" along with union types.