jsonschema icon indicating copy to clipboard operation
jsonschema copied to clipboard

Items in array not validated

Open chrigrahcisco opened this issue 10 years ago • 6 comments

Mac OSX 10.10.5 Python 2.7.11 jsonschema (2.5.1)

I've been testing jsonschema to see if it will work my Python based API testing. I've hit a road block trying to figure out why this particular array is not being validated at all. It should be throwing a variety of failures, but does not. I've seen other arrays processed correctly but am not seeing why this specific one is not.

import jsonschema


schema = {
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "pushNotifDevices" : {
      "type": "array",
      "description" : "",
      "items": {
        "applicationId": {
          "type": "boolean",
          "example":"CTS",
          "description":"This application ID identifies."
        },
        "deviceType":{
          "type": "string",
          "enum":["ANDROID"],
          "description":"This enum indicates the type of device."
        },
        "deviceId": {
          "type":"string",
          "maxLength":1,
          "description":"Unqiue DeviceID."
        },
        "notifDeviceId": {
          "type":"string",
          "maxLength":1,
          "description":"This ID."
        },
        "active" : {
          "type":"boolean",
          "default":True,
          "description":"Boolean to determine if the device is active."
        },
        "failureDescription" : {
          "type":"string",
          "maxLength":1,
          "description":"This indicates the reason."
        }
      },
      "required": [
        "applicationId",
        "deviceType",
        "deviceId",
        "notifDeviceId"
      ]
    }
  }
}


data = {
    "pushNotifDevices": [
        {
            "active": True,
            "applicationId": True,
            "deviceId": "45BBBE14",
            "deviceType": "ANDROID",
            "failureDescription": "r",
            "notifDeviceId": "4"
        },
        {
            "active": 123,
            "applicationId": "CTS",
            "deviceId": "45BBBE14",
            "deviceType": "IOS",
            "failureDescription": "remote notifications are not supported",
            "notifDeviceId": "45BBBE14"
        },
        {
            "active": True,
            "applicationId": "CTS",
            "deviceId": "56A8FB8B",
            "failureDescription": "REMOTE_NOTIFICATION_SIMULATOR_NOT_SUPPORTED",
            "notifDeviceId": "56A8FB8B"
        }
    ]
}

jsonschema.Draft4Validator.check_schema(schema)
jsonschema.validate(data, schema)

chrigrahcisco avatar Apr 16 '16 18:04 chrigrahcisco

You provided items, but didn't put the properties in a propertiesin that schema, so they're all ignored (as if they're unknown validators).

Julian avatar Apr 16 '16 18:04 Julian

And I thank you kindly. :) I looked at this for hours and never noticed the properties was missing. I add that in, and now my pushNotifDevices are being validated.

Thanks for your work on this excellent tool.

chrigrahcisco avatar Apr 16 '16 18:04 chrigrahcisco

Glad to hear, and glad to hear you're enjoying :). Cheers.

Julian avatar Apr 17 '16 16:04 Julian

I should clarify, the schema is being provided to me via RAML documentation for an API I'm attempting to test.

Questions:

Is the above an issue with my schema? I'm not seeing any documentation in regards to arrays as to whether they should have properties defined.

Or is this simply something I must manually modify in the schema to support being able to use validation?

I was really hoping for my first pass at this to be able to use the schema as provided, and have the tool find type mismatches, enums that don't bind to the list, required fields that are missing, parameters that are totally missing, etc. All of the low hanging fruit with minimal additional effort on my part for right now. Unrealistic expectations?

It has found various schema issues for me, so it's working.

Many thanks.

chrigrahcisco avatar Apr 19 '16 17:04 chrigrahcisco

And I thank you kindly. :) I looked at this for hours and never noticed the properties was missing. I add that in, and now my pushNotifDevices are being validated.

Thanks for your work on this excellent tool.

@chrigrahcisco I know its long time, but can you please share the final schema that worked for you. I'm having similar issue of array objects not validated even though i've two "properties".

Assassin236 avatar Aug 27 '21 16:08 Assassin236

@Assassin236 - Hope this helps.

import jsonschema


schema = {
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "properties": {
        "pushNotifDevices": {
            "type": "array",
            "description": "",
            "items": {
                "type": "object",
                "properties": {
                    "applicationId": {
                        "type": "boolean",
                        "example": "CTS",
                        "description": "This application ID identifies."
                    },
                    "deviceType": {
                        "type": "string",
                        "enum": ["ANDROID"],
                        "description": "This enum indicates the type of device."
                    },
                    "deviceId": {
                        "type": "string",
                        "maxLength": 1,
                        "description": "Unqiue DeviceID."
                    },
                    "notifDeviceId": {
                        "type": "string",
                        "maxLength": 1,
                        "description": "This ID."
                    },
                    "active": {
                        "type": "boolean",
                        "default": True,
                        "description": "Boolean to determine if the device is active."
                    },
                    "failureDescription": {
                        "type": "string",
                        "maxLength": 1,
                        "description": "This indicates the reason."
                    }
                }
            },
            "required": [
                "applicationId",
                "deviceType",
                "deviceId",
                "notifDeviceId"
            ]
        }
    }
}


data = {
    "pushNotifDevices": [
        {
            "active": True,
            "applicationId": True,
            "deviceId": "45BBBE14",
            "deviceType": "ANDROID",
            "failureDescription": "r",
            "notifDeviceId": "4"
        },
        {
            "active": 123,
            "applicationId": "CTS",
            "deviceId": "45BBBE14",
            "deviceType": "IOS",
            "failureDescription": "remote notifications are not supported",
            "notifDeviceId": "45BBBE14"
        },
        {
            "active": True,
            "applicationId": "CTS",
            "deviceId": "56A8FB8B",
            "failureDescription": "REMOTE_NOTIFICATION_SIMULATOR_NOT_SUPPORTED",
            "notifDeviceId": "56A8FB8B"
        }
    ]
}
print("errors in schema : ")
print(jsonschema.Draft4Validator.check_schema(schema))
print("errors in data : ")
print(jsonschema.validate(data, schema))

omkarkumbhar-id avatar Jan 20 '22 03:01 omkarkumbhar-id