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 #30

Open
Aribros opened this issue Aug 16, 2022 · 2 comments
Assignees

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.

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
 */

@schmunk42
Copy link
Member

Isn't this an issue of https://github.com/justinrainbow/json-schema ?

@Aribros
Copy link
Author

Aribros commented Aug 18, 2022

I also filed a similar issue on the said library here, but after investigation by the maintainers, they claimed they could not reproduce the issue from their own end.

Therefore, I suggest we investigate further as this issue was noticed through the Yii2 library and not when using the validation library directly.

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