3

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.

2
  • 1
    Which version of the schema standard does your validation library use? In v4, required is now an array instead of a boolean. Commented Jul 18, 2013 at 12:42
  • Does anybody know a good schema4 validator for php. Nothing seems to work correctly. Commented Aug 31, 2017 at 9:41

1 Answer 1

1

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));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.