Skip to content
CodeKruPro

Validate a response schema in Python

QuizMediumPython only

A test runs:

from jsonschema import validate
schema = {"type": "object", "required": ["id"],
          "properties": {"id": {"type": "integer"}}}
validate(instance=r.json(), schema=schema)

The response is {"id": "7"}. What happens?

  • validate raises jsonschema.ValidationError because "7" is a string, not an integer, so the test fails
  • validate returns False and the test passes
  • It passes, because "7" looks like a number
  • It fails because the schema must list every possible field

This question is part of Pro+

Pro+ opens every question in Interview Prep and the mock interview. Every article and the first 2 questions of each topic stay free. Hands-on Selenium and API tasks follow their course lesson's own lock.

See Pro+

Already have Pro+? Sign in