0

I am getting none when I try to validate my Json schema with my Json response using Validate from Jsonschema.validate, while it shows matched on https://www.jsonschemavalidator.net/

Json Schema

 {
        "KPI": [{
            "KPIDefinition": {
                "id": {
                    "type": "string"
                },
                "name": {
                    "type": "string"
                },
                "version": {
                    "type": "number"
                },
                "description": {
                    "type": "string"
                },
                "datatype": {
                    "type": "string"
                },
                "units": {
                    "type": "string"
                }
            },
            "KPIGroups": [{
                "id": {
                    "type": "number"
                },
                "name": {
                    "type": "string"
                }
            }]
        }],
        "response": [{
            "Description": {
                "type": "string"
            }
        }]
    }

JSON Response JSON Response

{
  "KPI": [
    {
      "KPIDefinition": {
        "id": "2",
        "name": "KPI 2",
        "version": 1,
        "description": "This is KPI 2",
        "datatype": "1",
        "units": "perHour"
      },
      "KPIGroups": [
        {
          "id": 7,
          "name": "Group 7"
        }
      ]
    },
    {
      "KPIDefinition": {
        "id": "3",
        "name": "Parameter 3",
        "version": 1,
        "description": "This is KPI 3",
        "datatype": "1",
        "units": "per Hour"
      },
      "KPIGroups": [
        {
          "id": 7,
          "name": "Group 7"
        }
      ]
    }
  ],
  "response": [
    {
      "Description": "RECORD Found"
    }
  ]
}

Code

json_schema2 = {"KPI":[{"KPIDefinition":{"id_new":{"type":"number"},"name":{"type":"string"},"version":{"type":"number"},"description":{"type":"string"},"datatype":{"type":"string"},"units":{"type":"string"}},"KPIGroups":[{"id":{"type":"number"},"name":{"type":"string"}}]}],"response":[{"Description":{"type":"string"}}]}

json_resp = {"KPI":[{"KPIDefinition":{"id":"2","name":"Parameter 2","version":1,"description":"This is parameter 2 definition version 1","datatype":"1","units":"kN"},"KPIGroups":[{"id":7,"name":"Group 7"}]},{"KPIDefinition":{"id":"3","name":"Parameter 3","version":1,"description":"This is parameter 3 definition version 1","datatype":"1","units":"kN"},"KPIGroups":[{"id":7,"name":"Group 7"}]}],"response":[{"Description":"RECORD FETCHED"}]}

print(jsonschema.validate(instance=json_resp, schema=json_schema2))

Validation is not being done correctly, I changed the dataType and key name in my response but still, it is not raising an exception or error.

1
  • What result did you expect, exactly? (Did you try reading the documentation?) Commented Jun 25, 2020 at 6:49

1 Answer 1

2

jsonschema.validate(..) is not supposed to return anything.

Your schema object and the JSON object are both okay and validation has passed if it didn't raise any exceptions -- which seems to be the case here.

That being said, you should wrap your call within a try-except block so as to be able to catch validation errors.

Something like:

try:
    jsonschema.validate(...)
    print("Validation passed!")
except ValidationError:
    print("Validation failed")
# similarly catch SchemaError too if needed.

Update: Your schema is invalid. As it stands, it will validate almost all inputs. A schema JSON should be an object (dict) that should have fields like "type" and based on the type, it might have other required fields like "items" or "properties". Please read up on how to write JSONSchema.

Here's a schema I wrote for your JSON:

{
  "type": "object",
  "required": [
    "KPI",
    "response"
  ],
  "properties": {
    "KPI": {
        "type": "array",
        "items": {
          "type": "object",
            "required": ["KPIDefinition","KPIGroups"],
            "properties": {
              "KPIDefinition": {
                "type": "object",
                "required": ["id","name"],
                "properties": {
                  "id": {"type": "string"},
                  "name": {"type": "string"},
                  "version": {"type": "integer"},
                  "description": {"type": "string"},
                  "datatype": {"type": "string"},
                  "units": {"type": "string"},
                },
                "KPIGroups": {
                  "type": "array",
                  "items": {
                    "type": "object",
                    "required": ["id", "name"],
                    "properties": {
                      "id": {"type": "integer"},
                      "name": {"type": "string"}
                    }
                  }
                }
              }
            }
        }
    },
    "response": {
        "type": "array",
        "items": {
            "type": "object",
            "required": ["Description"],
            "properties": {
              "Description": {"type": "string"}
            }
        }
    }
  }
}
Sign up to request clarification or add additional context in comments.

4 Comments

I have deliberately changed a key and validated, still it didn't raise an exception. I am not sure why.
Please update your question with a super compact code that reproduces the problem.
Thanks for the suggestion, just did that.
Your schema was wrong. Without relevant fields your schema object is the same as passing {} (empty schema) which implies it will validate against all inputs -- which is what is happening with you.

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.