Require properties on an optional object type

1,084 views
Skip to first unread message

Naqeeb Memon

unread,
Jul 29, 2014, 11:34:12 AM7/29/14
to json-...@googlegroups.com
I have defined a customer object type in my json schema:

    "customer": {
        "type": "object",
        "properties": {
          "id": { "type": "string" },
          "first_name": { "type": "string" },
          "last_name": { "type": "string"},
          "email": { "type": "string" },
          "billing_address": { "$ref": "#\/definitions\/street_address" },
          "shipping_address": { "$ref": "#\/definitions\/street_address" },
        },
        "required": [ "id", "first_name", "last_name", "email", "billing_address"]
      },


I would like to validate shipping_address (optional object) if it is sent and reject it if it is missing the required fields.  Here is the street_address object definition:  

    "street_address": {
        "type": "object",
        "properties": {
          "first_name": {"type": "string" },
          "last_name": { "type": "string" },
          "address": { "type": "string" },
          "address2": { "type": "string" },
          "city": { "type": "string" },
          "state": { "type": "string" },
          "zip_code": { "type": "string"},
          "country_code": { "type": "string"},
          "phone": { "type": "string"},
          "fax": {"type": "string"}
        },
        "required": [
          "first_name",
          "last_name",
          "address",
          "city",
          "state",
          "zip_code",
          "country_code"
        ]
      },

How do I configure my JSON schema to accomplish this? When I send a shipping address now, the fields inside the object do not get validated.

Joe McIntyre

unread,
Jul 29, 2014, 1:34:51 PM7/29/14
to json-...@googlegroups.com
Naqeeb,

You are using the correct schema approach.
 - when shipping_address is not present, the document is valid since shipping_address is not in the required properties list.
 - when shipping_address is present, it must satisfy the definition provided, as specified in the 'street_address' definition.
I suspect that the reason you are not getting the validation error is because the "$ref" definitions contain extraneous backslashes and the validator you are using treats unresolved references as an empty schema (thus everything is accepted as valid).

Below is a complete schema (with the corrected "$ref") and example that shows an invalid shipping address (address is an integer instead of a string) that is flagged as invalid.
{
  "title":"",
  "description":"",

  "type": "object",
  "properties": {
    "customer": {
      "type": "object",
      "properties": {
        "id": { "type": "string" },
        "first_name": { "type": "string" },
        "last_name": { "type": "string"},
        "email": { "type": "string" },
        "billing_address": { "$ref": "#/definitions/street_address" },
        "shipping_address": { "$ref": "#/definitions/street_address" }
      },
      "required": ["id", "first_name", "last_name", "email", "billing_address"]
    }
  },
  "definitions": {
    "street_address": {
      "type": "object",
      "properties": {
        "first_name": {"type": "string" },
        "last_name": { "type": "string" },
        "address": { "type": "string" },
        "address2": { "type": "string" },
        "city": { "type": "string" },
        "state": { "type": "string" },
        "zip_code": { "type": "string"},
        "country_code": { "type": "string"},
        "phone": { "type": "string"},
        "fax": {"type": "string"}
      },
      "required": ["first_name", "last_name", "address", "city", "state", "zip_code", "country_code"]
    }
  }
}

Example document that fails on the shipping_address,
{
  "customer": {
    "id":"1",
    "first_name":"John",
    "last_name":"Doe",
    "email":"joh...@example.com",
    "billing_address": {
      "first_name":"John",
      "last_name":"Doe",
      "address":"123 Elm St",
      "city":"Anytown",
      "state":"NY",
      "zip_code":"10001",
      "country_code":"USA"
    },
    "shipping_address": {
      "first_name":"John",
      "last_name":"Doe",
      "address":123,
      "city":"Anytown",
      "state":"NY",
      "zip_code":"10001",
      "country_code":"USA"
    }
  }
}
Reply all
Reply to author
Forward
0 new messages