4

I get following JSON and would like to validate it.

[
    {
        "remindAt": "2015-08-23T18:53:00+02:00",
        "comment": "Postman Comment"
    },
    {
        "remindAt": "2015-08-24T18:53:00+02:00",
        "comment": "Postman Comment"
    }
]

My schema looks currently as following

{
    "type": "array",
    "required": true,
    "properties": {
        "type": "object",
        "required": false,
        "additionalProperties": false,
        "properties": {
            "remindAt": {
                "required": true,
                "type": "string",
                "format": "date-time"
            },
            "comment": {
                "required": true,
                "type": "string"
            }
        }
    }
}

This is not working. It validates to true even if I remove comment from JSON ddata. I guess structure of my schema file is wrong.

For validating I use following library https://packagist.org/packages/justinrainbow/json-schema

Can please someone explain to me what I do wrong and how I properly validate given JSON data?

Thanks in advance

3
  • 3
    with which code do you validate this ? Commented Aug 2, 2015 at 9:51
  • I use justin rainbows library to validate the schema against the json data packagist.org/packages/justinrainbow/json-schema Commented Aug 2, 2015 at 10:07
  • 2
    when you look json-schema website, the "required" is not at the same place as in your exemple : json-schema.org/examples.html Commented Aug 2, 2015 at 10:31

1 Answer 1

9

There are some errors in your schema. First, you are using properties for an array object. properties is a clause for objects, not arrays, so it will be ignored.

From json-schema v4, required is an array.

The following schema will require remindAt and comment properties for all items in the array:

{
    "type": "array",
    "items": {
        "additionalProperties": false,
        "properties": {
            "remindAt": {
                "type": "string",
                "format": "date-time"
            },
            "comment": {
                "type": "string"
            }
        },
        "required": ["remindAt", "comment"]
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

You might need to use False instead of false

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.