-
Notifications
You must be signed in to change notification settings - Fork 358
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
patterProperties with int key #780
Comments
@sakarikl thank for the report. Could you also add some reproduction steps e.g. a schema and json value? |
$json = '{
"10": 123,
"101": 5
}
';
$schema = [
'type' => 'object',
'patternProperties' => [
'^[0-9]+$' => ['type' => 'integer']
],
];
$data = json_decode($json, true);
$validator = new JsonSchema\Validator();
$validator->validate(
$data,
$schema,
Constraint::CHECK_MODE_COERCE_TYPES | Constraint::CHECK_MODE_APPLY_DEFAULTS | Constraint::CHECK_MODE_EXCEPTIONS | Constraint::CHECK_MODE_TYPE_CAST,
);
$validator->isValid(); This reproduces error. Sorry meant to put this to original post :) |
Based on your code I'm getting the same error. What I noticed however is the line This causes issues down the line like with the My suggested code would be: $data = json_decode(
<<<'JSON'
{
"10": 123,
"101": 5
}
JSON
);
$schema = json_decode(<<<'JSON'
{
"type": "object",
"patternProperties": {
"^[0-9]+$": {
"type": "integer"
}
}
}
JSON
);
$validator = new Validator();
$validator->validate(
$data,
$schema,
Constraint::CHECK_MODE_COERCE_TYPES | Constraint::CHECK_MODE_APPLY_DEFAULTS | Constraint::CHECK_MODE_EXCEPTIONS | Constraint::CHECK_MODE_TYPE_CAST,
);
$validator->isValid();
if ($validator->isValid()) {
echo "The supplied JSON validates against the schema.\n";
} else {
echo "JSON does not validate. Violations:\n";
foreach ($validator->getErrors() as $error) {
printf("[%s] %s\n", $error['property'], $error['message']);
}
} |
Seeing the thumbs up as a answer to your problem. Closing the issue for now, feel free to reopen if needed, |
I can't use that workaround but I can live with that by making a patch for myself. |
preg_match(): Argument #2 ($subject) must be of type string, int given
string cast seems to be missing from src/JsonSchema/Constraints/ObjectConstraint.php@69
'patternProperties' => [ '^[0-9]+$' => [ 'type' => 'object', 'additionalProperties' => false,
The text was updated successfully, but these errors were encountered: