Skip to content

required dosen't work if json top level is an Array #165

@renner-elio

Description

@renner-elio

If I create a Schema that requires a property and validate an JSON of an empty object {}, I get the expected ObjectException: "Required property missing ...", but if I do the same with an empty array [], the schema validates successfully despite the required property not being present.

minimal example (should throw error on last line but doesn't):

<?php
require_once __DIR__ . '/vendor/autoload.php';

use Swaggest\JsonSchema\Schema;
use Swaggest\JsonSchema\Structure\ClassStructure;

class MyClass extends ClassStructure
{
    public $name;
    public static function setUpProperties($properties, Schema $ownerSchema)
    {
        $properties->name = Schema::string();
        $ownerSchema->required = array(self::names()->name);
    }

}

$validInput = <<<JSON
{
    "name": "John"
}
JSON;

$imported = MyClass::import(json_decode($validInput));

$InvalidInput = <<<JSON
[
]
JSON;

$imported = MyClass::import(json_decode($InvalidInput));

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