MelkorNemesis
MelkorNemesis

Reputation: 3435

JSON Validation against JSON Schema

I'm trying to validate JSON object against my given schema.

JSON data are as follows:

{
"list": {
    "places": [
        {
            "name": "Loopsiloo",
            "foursquareID": "54a6s5D4a6s5d4a6s5D4",
            "lat": 26.6546845354889,
            "lon": -99.6846873700158
        },
        {
            "name": "Loopsiloo",
            "foursquareID": "54a6s5D4a6s5d4a6s5D4",
        }
    ],
    "title": "Foo Bar",
    "dateCreated": "2013-01-29T14: 19: 30Z"
}

JSON Schema is as follows:

{
"type":"object",
"$schema": "http://json-schema.org/draft-03/schema",
"required":true,
"properties":{
    "list": {
        "type":"object",
        "id": "list",
        "required":true,
        "properties":{
            "dateCreated": {
                "type":"string",
                "id": "dateCreated",
                "required":true
            },
            "places": {
                "type":"array",
                "minitems": "1",
                "id": "places",
                "required":true,
                "items":
                {
                    "type":"object",
                    "required":true,
                    "properties":{
                        "note": {
                            "type":"string",
                            "id": "note",
                            "required":false
                        },
                        "foursquareID": {
                            "type":"string",
                            "id": "foursquareID",
                            "required":true
                        },
                        "lat": {
                            "type":"number",
                            "id": "lat",
                            "required":true
                        },
                        "lon": {
                            "type":"number",
                            "id": "lon",
                            "required":true
                        },
                        "name": {
                            "type":"string",
                            "id": "name",
                            "required":true
                        }
                    }
                }


            },
            "title": {
                "type":"string",
                "id": "title",
                "required":true
            }
        }
    }
}

}

I am validating this JSON using JsonSchema\Validator in PHP.

$validator = new JsonSchema\Validator;
$validator->check($data, file_get_contents(__DIR__ . '/../model/api-schema.json'));

My problem is that the validator validates JSON object as correct every time. In the example at the top there are properties "lat" and "lon" missing. Even if I omit whole "places", "title" or "dateCreated" property it is validated as correct.

Is there something I'm missing? I went through documentation of JSON schema, but nothing that could help me.

Upvotes: 3

Views: 3046

Answers (1)

Jindra
Jindra

Reputation: 810

This is what works for me.

$validator = new JsonSchema\Validator;
$schema = file_get_contents(__DIR__ . '/../model/api-schema.json');
$validator->check(json_decode($data), json_decode($schema));

Upvotes: 1

Related Questions