Closed mark-sp closed 7 months ago
the elements within the inner object are not right
Please post a minimal (but complete) code sample demonstrating the problem you're having, including the input.
Edited my last post due to new findings. I wrote the code based on the example, just changing the source of both the input JSON and the schema. I believe the problem is related to the way I reference the schema. Is it possible to import it from a string? My code is inside a bigger application that should store the schema in a DB.
I've corrected the schema file and I validated it using an online tool, so it should be ok now.
<?php
$data = json_decode($jsonInputString);
$schema = json_decode($jsonSchemaString);
require_once 'vendor/autoload.php';
$validator = new JsonSchema\Validator;
$validator->check($data, $schema);
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) {
echo sprintf("[%s] %s\n", $error['property'], $error['message']);
}
}
Hi, unless you provide a complete code sample there's not much we can do for you. We need to see both the input being validated as well as the schema. If you can reduce your problem to a minimal code sample that illustrates the issue you're seeing there's a very good chance we can tell you what's happening.
Find below sample of the JSON I am trying to validate as well as the schema. The main PHP is as I posted before. I believe the problem is on the way I load the schema:
$schema = json_decode(file_get_contents('patient_solti_schema.json'));
$data = json_decode(file_get_contents('test.json'));
$validator->check($data, $schema);
if instead I use
$data = json_decode(file_get_contents('test.json'));
$validator->check($data, (object)['$ref' => 'file://' . realpath('patient_solti_schema.json')]);
Then it works. This is fine for this example, but it won't work in my application, where I have to load the schema from a DB. Is there any alternative way to load the schema from, for instance, a variable?
Schema:
{
"$schema" : "http://json-schema.org/draft-04/schema#",
"definitions" : {
"phisical" : {
"type" : "object",
"properties" : {
"weight" : { "type" : "integer" },
"height" : { "type" : "integer" }
},
"required" : ["weight"]
}
},
"title" : "Patient SOLTI schema",
"type" : "object",
"properties" : {
"current patient status": {
"type": "string",
"enum": [ "Con ensayos", "Sin ensayos" ]
},
"collection site": {
"type": "string"
},
"patient code" : {
"type": "string"
},
"birthdate" : {
"type" : "string",
"format" : "date-time"
},
"gender" : {
"type" : "string",
"enum" : ["M", "F"]
},
"region" : {
"type" : "string"
},
"province": {
"type" : "string"
},
"ECOG" : {
"type" : "integer",
"minimum" : 0
},
"treatments" : { "type": "array", "id" : "treatments",
"items" : [{
"type":"object",
"properties":{
"name":{
"type":"string"
},
"dose":{
"type":"integer"
}
},
"required" : ["dose","name"]
}]
},
"phisical1" : { "$ref": "#/definitions/phisical" },
"recruitment date" : {
"type" : "string",
"format" : "date-time"
}
}
}
Input:
{
"current patient status" : "Con ensayos",
"collection site": "ICO Hospitalet",
"patient code" : "53",
"gender" : "F",
"weight" : 56,
"height" : 162,
"recruitment date" : "2015-07-18T12:34:54Z",
"phisical1": {
"ola" : 44,
"weight" : 56
},
"treatments" : [{"name":"je", "dose" : 33}]
}
Hey there, sorry for the long delay. I finally had a chance to try out your code. You never really told us what was going wrong, but one thing I noticed is that you don't have a call to addStorage
(to resolve your internal $ref):
$schemaStorage->addSchema('file://mySchema', $schema); // I took this line from the sample in the readme
Once I did that things seemed to work smoothly (although I didn't get any validation errors; are we expecting some?).
@mark-sp 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, and can be closed? Feel free to close it yourself with some comments if helpful.
Hi,
I just found out about json-schema specification and its PHP library and I am wondering whether the library supports nested JSON structures.
If so, I'd like to know how I have to set up my schema file to do the right validation. I've been following guidelines from json-schema docs with no problem so far, but I couldn't manage to make it work when one of the properties is itself an object: the validation is always OK although the elements within the inner object are not right.
Any idea of what I could be doing wrong? Is that a limitation of the library?