Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EnumConstraint is to strict on number comparison #778

Open
DannyvdSluijs opened this issue Feb 4, 2025 · 1 comment
Open

EnumConstraint is to strict on number comparison #778

DannyvdSluijs opened this issue Feb 4, 2025 · 1 comment

Comments

@DannyvdSluijs
Copy link
Collaborator

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
Copy link
Collaborator Author

Also good to note the feedback on the bowtie.report site is not correctly displaying float values causing for some mis-interpretation. Issue was reported in bowtie-json-schema/bowtie#1813

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant