Skip to content

Errors from "allOf" do not bubble up #154

@andreaswolf

Description

@andreaswolf

I tried the following code, taken from the official JSON Schema docs (see https://json-schema.org/understanding-json-schema/reference/object#extending):

<?php

use Opis\JsonSchema\Errors\ErrorFormatter;
use Opis\JsonSchema\Validator;

include __DIR__ . '/vendor/autoload.php';

$schema = <<<JSON
{
  "allOf": [
    {
      "type": "object",
      "properties": {
        "street_address": { "type": "string" },
        "city": { "type": "string" },
        "state": { "type": "string" }
      },
      "required": ["street_address", "city", "state"]
    }
  ],

  "properties": {
    "street_address": true,
    "city": true,
    "state": true,
    "type": { "enum": [ "residential", "business" ] }
  },
  "required": ["type"],
  "additionalProperties": false
}
JSON;

$data = json_decode(<<<JSON
{
  "type": "residential",
  "foo": "bar",
  "street_address": true
}
JSON);

$validator = new Validator();
$validator->setMaxErrors(10000);
$validator->setStopAtFirstError(false);
$jsonSchema = $validator->loader()->loadObjectSchema(
    \Safe\json_decode(
        $schema
    )
);

$result = $validator->validate($data, $jsonSchema);
$errorFormatter = new ErrorFormatter();
if ($result->hasError()) {
    print_r($errorFormatter->format($result->error()));
} else {
    echo "No errors!\n";
}

I expected errors related to the additional property "foo", the wrong type for "street_address" and the missing city and state, but I only got one for "foo":

Array
(
    [/] => Array
        (
            [0] => Additional object properties are not allowed: foo
        )

)

So to me it seems that the errors from allOf are not properly bubbling up.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions