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

EnumConstraint is to strict on number comparison

Open DannyvdSluijs opened this issue 8 months ago • 1 comments

In the core document section 3.6 equality is as follows for numerical types:

both are numbers, and have the same mathematical value; or

This also is reflected by a test in the JSON-Schema-Test in https://github.com/json-schema-org/JSON-Schema-Test-Suite/blob/main/tests/draft4/enum.json#L220-L240.

The Bowtie report confirms we are not correctly validating this case. See https://bowtie.report/#/implementations/php-justinrainbow-json-schema under Draft 4 search for enum with 0 does not match false

Reproduction

Please find code and output below.

Reproduction code
<?php

declare(strict_types=1);

use JsonSchema\Constraints\Factory;
use JsonSchema\SchemaStorage;
use JsonSchema\Validator;

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

$data = json_decode(
<<<'JSON'
0.0
JSON
);

$schema = json_decode(
<<<'JSON'
{
  "enum": [
    0
  ]
}
JSON
);

$schemaStorage = new SchemaStorage();
$schemaStorage->addSchema('internal://mySchema', $schema);
$validator = new Validator(new Factory($schemaStorage));
$validator->validate($data, $schema);

var_dump($validator->isValid(), $validator->getErrors());
Output
bool(false)
array(1) {
  [0]=>
  array(5) {
    ["property"]=>
    string(0) ""
    ["pointer"]=>
    string(0) ""
    ["message"]=>
    string(44) "Does not have a value in the enumeration [0]"
    ["constraint"]=>
    array(2) {
      ["name"]=>
      string(4) "enum"
      ["params"]=>
      array(1) {
        ["enum"]=>
        array(1) {
          [0]=>
          int(0)
        }
      }
    }
    ["context"]=>
    int(1)
  }
}

DannyvdSluijs avatar Feb 04 '25 19:02 DannyvdSluijs