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

It don't validate minimum constratint with oneOf definition for integer or number types #503

Closed
luigimoyano opened this issue Apr 10, 2018 · 2 comments

Comments

@luigimoyano
Copy link

luigimoyano commented Apr 10, 2018

Using the library we have find this scenario. It allows negative numbers with this definition. It works well for the other constraints but no for check the minimum constraint.

{
    "$schema": "http://json-schema.org/draft-06/schema#",
    "type": "object",
    "properties": {
        "attributes": {
            "type": "object",
            "properties": {
                "spaces": {
                    "description": "Number of spaces",
                    "type": ["string", "integer"],
                    "oneOf": [
                        {
                            "type": "integer",
                            "minimum": 0
                        },
                        {
                            "type": "string",
                            "enum": [
                                "Ample"
                            ]
                        }
                    ]
                }
            }
        }
    }
}
@luigimoyano luigimoyano changed the title It don't validate negative numbers with oneOf definition It don't validate minimum constratint with oneOf definition for integer or number types Apr 10, 2018
@shmax
Copy link
Collaborator

shmax commented Apr 10, 2018

Please provide a PR with a failing unit test, and we'll go from there.

@DannyvdSluijs
Copy link
Collaborator

@luigimoyano the current version of the master branch doesn't have this issue. I've used your schema as defined above and when trying to validate using the code snippet below you can see in the output there is an error with regards to the minimum value.
In an attempt to cleanup this repo we are trying to filter the issues and see which ones might be closed. Is it safe to assume this is a rather old issue, which sadly was left unanswered, but can be closed? Feel free to close it yourself with some comments if helpful.

Code

<?php

use JsonSchema\Constraints\Constraint;
use JsonSchema\Uri\UriRetriever;
use JsonSchema\Validator;

require_once '../../vendor/autoload.php';

$retriever = new UriRetriever();
$schema = $retriever->retrieve('file://' . realpath('./schema.json'));
$obj = json_decode('{"attributes": {"spaces":  -1 }}');
$validator = new Validator();
$result = $validator->validate($obj, $schema, Constraint::CHECK_MODE_APPLY_DEFAULTS);

var_dump(array_map(fn(array $error) => $error['message'],$validator->getErrors()));

Output

array(4) {
  [0]=>
  string(45) "Integer value found, but a string is required"
  [1]=>
  string(50) "Does not have a value in the enumeration ["Ample"]"
  [2]=>
  string(52) "Must have a minimum value greater than or equal to 0"
  [3]=>
  string(34) "Failed to match exactly one schema"
}

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

No branches or pull requests

4 participants