3

I have this schema for a json response

{
    "title": "Products",
    "description": "schema for products",
    "type": "array",
    "properties": {
        "id": {
            "description": "id of a product",
            "type": "integer"
        },
        "name": {
            "description": "name of the product",
            "type": "string"
        },
        "created_at": {
            "description": "record created_at",
            "type": "string",
            "format": "date-time"
        },
        "updated_at": {
            "description": "record updated_at",
            "type": "string",
            "format": "date-time"
        }
    },
    "required": ["id", "name"]
}

and I want to match this schema with this json

[{
    "id": 1,
    "name": "Cricket Ball"
}, {
    "id": 2,
    "name": "Soccer Ball"
}, {
    "id": 3,
    "name": "football ball"
}, {
    "id": 4,
    "name": "Basketball ball"
}, {
    "id": 5,
    "name": "Table Tennis ball"
}, {
    "id": 6,
    "name": "Tennis ball"
}]

This schema matches the response but it also matches the schema in which the required field is this

"required": ["ids", "names"]

I think the schema is validated against the array and the objects in the array are not validated.

2
  • 2
    Its very unclear what you are actually doing. Include the controller and the view (if you are using jbuilder) or the serializer if you are using AMS. Commented Sep 22, 2016 at 8:11
  • this isn't related to rails. removed the rails tag in the recent edit Commented Sep 22, 2016 at 8:16

2 Answers 2

6

The way you have it set up now, your properties key refers to the array itself, not to each item, and is being ignored (because arrays don't have properties, they just have items). You need to use the items key to validate each item in the array, like so:

{
    "title": "Products",
    "description": "schema for products",
    "type": "array",
    "items": {
      "type": "object",
      "properties": {
        "id": {
            "description": "id of a product",
            "type": "integer"
        },
        "name": {
            "description": "name of the product",
            "type": "string"
        },
        "created_at": {
            "description": "record created_at",
            "type": "string",
            "format": "date-time"
        },
        "updated_at": {
            "description": "record updated_at",
            "type": "string",
            "format": "date-time"
        }
      },
      "required": ["id", "name"]
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

PS The json scheme validator is a great way to test schemas out live.
that did it. awesome
I tried the schema validator but purposefully change the second object in the sample JSON data array, changing "id" to "id2", but the page says "no errors found". How do we make the validator validates the 2nd, 3rd, 4th, etc. object as well?
Why is the following json not giving validation errors against your schema? [{ "id": "1", "name": 2 }, { "id": "3" }]
0

try map

new_array = response.map{ |k| { 'id': k['properties']['id']['description'], 'name': k['properties']['name']['description'] } }

1 Comment

Sorry, but this is not exactly what I asked. I don't want the first one be converted to the second one. I want to match json-schema

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.