opis / json-schema

JSON Schema validator for PHP
https://opis.io/json-schema
Apache License 2.0
568 stars 58 forks source link

String validation with maxLength:0 accepts strings of any length #133

Open tapsavo opened 1 year ago

tapsavo commented 1 year ago

I have data:

{
    "empty_string": "asdf"
}

and schema:

{
    "type": "object",
    "properties": {
        "empty_string": {
            "type": "string",
            "maxLength": 0
        }
    }
}

and php code:

$data   = [
    'empty_string' => "asdf",
];
$schema = [
    'type'       => 'object',
    'properties' => [
        "empty_string" => [
            "type"      => "string",
            "maxLength" => 0
        ]
    ]
];

$validator = new \Opis\JsonSchema\Validator();
$result    = $validator->validate(\json_decode(\json_encode($data)), \json_encode($schema));

var_dump($result->isValid());

$result->isValid() returns true regardless of what "empty_string" is set to. "", "foo", "asdfasdfasdfs"... any length of string passes the validation.

Setting "maxLength" to any value greater than zero works fine.

Is this intended behavior? This used to work in json-schema 1.1.0, we recently upgraded to 2.3.0 where we found out this issue. I guess technically matching empty strings with pattern: "^$" works.