Answer the question
In order to leave comments, you need to log in
How to validate jsonShema?
For validation, I use the justinrainbow/json-schema library
$pathSchema = file_get_contents('/source/test.json');
$validator = new Validator();
$validator->check(json_decode($data),
(object)$pathSchema);
if ($validator->isValid()) {
echo "valid";
} else {
echo "not valid";
}
array (size=8)
0 =>
object(stdClass)[175]
public 'id' => int 1
public 'name' => string 'Москва' (length=12)
1 =>
object(stdClass)[194]
public 'id' => int 2
public 'name' => string 'Республика Коми' (length=29)
{
"id": {
"type": "number",
},
"name": {
"type": "number",
}
}
Answer the question
In order to leave comments, you need to log in
To prohibit sending extra fields, you need to describe objects in the schema, declare their properties (properties) and specify the parameter in the schemaadditionalProperties => false
{
"type": "object",
"properties": {
"number": { "type": "number" },
"street_name": { "type": "string" }
},
"additionalProperties": false
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question