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

JsonSchema\Exception\InvalidArgumentException | is an invalid type for string|null #692

Closed
Aribros opened this issue Aug 16, 2022 · 5 comments
Labels
issue-isn't-ours The issue isn't in this project; please contact the project that actually contains the problem.

Comments

@Aribros
Copy link

Aribros commented Aug 16, 2022

This exception happens when I create a schema with a properties that has a contain the same words separated by underscore. Please see the example schema below:

        $item_schema = [
                'title' => 'Invoice Items',
                'type' => 'array',
                'items' => [      
                    'type' => 'object',
                    'properties' => [
                        'quantity' => [
                            'title' => 'Quantity',
                            'type' => 'number',
                            'min' => 0,                    
                        ],
                        'quantity_unit' => [
                            'title' => 'Quantity Unit',
                            'type' => 'string',
                            "format" =>  "text",
                        ],    
                        'title' => [
                            'title' => 'title',
                            'type' => 'string',
                            "format" =>  "text",
                        ],                                            
                    ],            
                ],
            ];`

`

The above schema gives an exception as below during validation. The "unit(s)" is a value of the property quantity_unit.

`
JsonSchema\Exception\InvalidArgumentException

unit(s) is an invalid type for string|null
1. in /.../vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/TypeConstraint.php at line 214

 
        if ('email' === $type) {
            return is_string($value);
        }
 
        if ('null' === $type) {
            return is_null($value);
        }
 
        throw new InvalidArgumentException((is_object($value) ? 'object' : $value) . ' is an invalid type for ' . $type);
    }
 
    /**
     * Converts a value to boolean. For example, "true" becomes true.
     *
     * @param $value The value to convert to boolean
     *
     * @return bool|mixed
     */

`

If I rename the property quantity_unit to qty_unit or something else, it works but does not work if any other properties contain the name "quantity" separated by an underscore, it throws the above exception.

It seems because there is an existing property called quantity, no other property can have a name containing quantity.

@erayd
Copy link
Contributor

erayd commented Aug 16, 2022

Thanks for your report. Could you please provide a minimal, complete example that reproduces this issue?

Which version of the library are you encountering this with?

@Aribros
Copy link
Author

Aribros commented Aug 16, 2022

The issue was encountered first on version 5.2.10 and I upgrade to version 5.2.12, but the issue persisted.

To reproduce the issue, you can use the schema above to create a sample json data and try to validate

$data = [ 'title' => 'Hp Laptop', 'quantity' => 10, 'quantity_unit' => 'unit(s)', ];

This throws the exception below

 `JsonSchema\Exception\InvalidArgumentException
unit(s) is an invalid type for string|null
 1. in /.../vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/TypeConstraint.php at line 214


    if ('email' === $type) {
        return is_string($value);
    }

    if ('null' === $type) {
        return is_null($value);
    }

    throw new InvalidArgumentException((is_object($value) ? 'object' : $value) . ' is an invalid type for ' . $type);
}

/**
 * Converts a value to boolean. For example, "true" becomes true.
 *
 * @param $value The value to convert to boolean
 *
 * @return bool|mixed
 */

`

@erayd
Copy link
Contributor

erayd commented Aug 16, 2022

I needed to properly encapsulate your data in order for it to match the schema - however, it does not replicate the issue, and seems to validate just fine (see below). Could you please post a modified version of this example that will reproduce it?

This test was run with PHP v7.4 and justinrainbow/json-schema v5.2.12. I have also tested it with PHP v5.6 and v8.1, and got the same results.

Example:

<?php

require('vendor/autoload.php');
use JsonSchema\Validator;

$item_schema = [
    'title' => 'Invoice Items',
    'type' => 'array',
    'items' => [
        'type' => 'object',
        'properties' => [
            'quantity' => [
                'title' => 'Quantity',
                'type' => 'number',
                'min' => 0,
            ],
            'quantity_unit' => [
                'title' => 'Quantity Unit',
                'type' => 'string',
                "format" =>  "text",
            ],
            'title' => [
                'title' => 'title',
                'type' => 'string',
                "format" =>  "text",
            ],
        ],
    ],
];

$data = [ (object)[ 'title' => 'Hp Laptop', 'quantity' => 10, 'quantity_unit' => 'unit(s)', ]];

$validator = new Validator();
var_dump($validator->validate($data, $item_schema));
var_dump($validator->isValid());
var_dump($validator->getErrors());

Output:

[root@heryshaf test]# php74 test.php 
int(0)
bool(true)
array(0) {
}

@Aribros
Copy link
Author

Aribros commented Aug 16, 2022

The issue was encounter while using the Yii2 wrapper for this library. I also reported it here because the exception was thrown by the library.

@erayd
Copy link
Contributor

erayd commented Aug 16, 2022

@Aribros Noting that I cannot reproduce it using the information you have supplied, and the data / schema you have provided appear to validate perfectly, I am going to assume that the problem is with that wrapper (and not with this library). As such, I will close this issue - it doesn't look like this is our bug.

With that said, if you can provide a minimal example (or modify the one I have posted above that uses your example data) that reproduces the issue using just this library, then I am happy to investigate further.

@erayd erayd closed this as completed Aug 16, 2022
@erayd erayd added the issue-isn't-ours The issue isn't in this project; please contact the project that actually contains the problem. label Aug 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
issue-isn't-ours The issue isn't in this project; please contact the project that actually contains the problem.
Projects
None yet
Development

No branches or pull requests

2 participants