json-schema icon indicating copy to clipboard operation
json-schema copied to clipboard

String validation with maxLength:0 accepts strings of any length

Open tapsavo opened this issue 1 year ago • 0 comments

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.

tapsavo avatar Jul 13 '23 12:07 tapsavo