swaggest / php-json-schema

High definition PHP structures with JSON-schema based validation
MIT License
438 stars 50 forks source link

validating an array of objects #158

Closed JohnRDOrazio closed 4 months ago

JohnRDOrazio commented 4 months ago

I have been happily using this library to validate general JSON schemas, and not just Open API schemas. However I have just come across a strange issue: if I have a schema that has type array as its base, the schema fails to validate because it seems to want an object in any case. It doesn't want to let me have an array as the base.

Example data:

[
    {
        "animals": [ "cat", "dog", "bird" ],
        "cars": [ "honda", "bmw", "hyundai" ]
    },
    {
        "animals": [ "elephant", "giraffe", "cheetah" ],
        "cars": [ "mclaren", "ferrari", "alfa romeo" ]
    }
]

Schema:

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "array",
    "items": {
        "type": "object",
        "properties": {
             "animals": {
                 "type": "array",
                 "items": {
                     "type": "string"
                 }
             },
             "cars": {
                 "type": "array",
                 "items": {
                     "type": "string"
                 }
             }
        }
    }
}

This will validate with pretty much any online validator such as https://www.jsonschemavalidator.net/, but php-json-schema will throw an error saying that it was expecting an object but it received an array [{"animals": [ "cat", "dog", "bird" ],"cars": [ "honda", "bmw", "hyundai" ]},{"animals": [ "elephant", "giraffe", "cheetah" ],"cars": [ "mclaren", "ferrari", "alfa romeo" ]}].

JohnRDOrazio commented 4 months ago

Strange thing, when creating a minimal example like the one posted the test is passing. When validating real data I'm getting the "object expected" error. But since it's not always reproducible I'm going to close for now..

JohnRDOrazio commented 4 months ago

This issue can be deleted. After careful inspection of the code that was generating an error, I finally found a wrong variable that was pointing to the wrong schema. Error on my part.