KatInterview

Collect Path-Tagged Errors from a Nested Payload Schema Check

Trees & GraphsAuto-graded · 25 test cases
MediumAuto-gradedTrees & Graphsrecursiondfsjson

Last updated

Recursively walk a JSON-like value alongside a simplified schema and report type and missing-key errors with exact paths, in deterministic order.

Nested Payload Checker

A service receives JSON-like data and must confirm it matches an expected shape before processing it. Your job is to build a small checker that walks a nested value together with a simplified schema and lists every problem it finds.

The value being checked is built only from Python dict, list, str, int, float, bool and None.

The schema

A schema is a dict that may use these keys:

  • "type": one of "object", "array", "string", "number", "boolean", "null".
  • "properties": a dict mapping key names to sub-schemas. It is meaningful for objects.
  • "required": a list of key names that must be present. It is meaningful for objects.
  • "items": a single sub-schema that every element of an array must satisfy. It is meaningful for arrays.

How values map to type names

Python valueType name
None"null"
bool"boolean"
dict"object"
list"array"
str"string"
int or float (not bool)"number"
anything else"unknown"

A bool is never a "number", even though Python treats True as an int. An "unknown" value never matches any declared type.

Task

Implement check_payload(data, schema), which returns a list of error strings. If data conforms to schema, return an empty list.

Each error has the form "<path>: <message>".

Paths

  • The root value has path $.
  • A member name of an object at path P has path P.name.
  • Element number i (0-based) of an array at path P has path P[i].

For example: $.users[0].name.

Messages

  • Type mismatch: expected <declared type>, got <actual type>.
  • Missing key: missing required property '<key>', where <key> is the name from the required list.

Rules for visiting a node

For a value and its schema at some path:

  1. If the schema has a "type" and the value's type name differs, record one type-mismatch error for this path and stop. Nothing below this node is examined.
  2. If the schema has no "type", nothing is checked at this node and nothing below it is examined.
  3. If the type is "object" and matches:
    • First, go through required in list order and record a missing-key error for each key not present in the value.
    • Then go through properties in the schema's own key order. For each declared key that is present in the value, visit the member with its sub-schema. Declared keys that are absent are skipped silently (apart from any missing-key error already recorded). Keys in the value that the schema does not declare are ignored.
  4. If the type is "array" and matches, and the schema has "items", visit each element in index order starting at 0 using the items schema. Without "items", elements are not checked.
  5. For other matching types, there is nothing more to do.

Errors appear in the result in the order they are discovered: depth-first, with a node's own missing-key errors first, then each property's entire subtree before the next property, and array elements by ascending index.

Constraints

  • The value and schema are finite and nested to a modest depth (recursion is acceptable).
  • Schema keys are exactly those listed above; any other keys can be ignored.
  • Keys in required and properties are strings.
  • Object keys in the value are strings.
  • A required key need not appear in properties; it is then only checked for presence.
  • An empty array with an items schema produces no errors.
  • A mismatch at the root yields exactly one error with path $.

Examples

Example 1

Input: check_payload('101', {'type': 'object', 'required': ['id', 'tags'], 'properties': {'id': {'type': 'number'}, 'tags': {'type': 'array', 'items': {'type': 'string'}}}})

Output: ['$: expected object, got string']

Example 2

Input: check_payload({'id': '101', 'tags': ['pinned', 42]}, {'type': 'object', 'required': ['id', 'tags'], 'properties': {'id': {'type': 'number'}, 'tags': {'type': 'array', 'items': {'type': 'string'}}}})

Output: ['$.id: expected number, got string', '$.tags[1]: expected string, got number']

Example 3

Input: check_payload({'user': {}}, {'type': 'object', 'required': ['user'], 'properties': {'user': {'type': 'object', 'required': ['email']}}})

Output: ['$.user: missing required property \'email\'']

Hints

Hint 1

Write a helper that turns a Python value into one of the six type names, and check for bool before int, since bool is a subclass of int.

Hint 2

Use one recursive function taking the value, its schema and the current path string. Append to a shared list and return immediately after recording a type mismatch.

Hint 3

For objects, emit all missing-key errors first, then loop over the schema's properties in order and recurse only into keys present in the value. For arrays, recurse over indices in ascending order using the path suffix [i].