Schema Validation: When True Isn't an Int

MLOpsMedium+15 XP+15

Schema Validation: When True Isn't an Int

A data pipeline that trusts its input eventually ingests a row where a price arrived as the string "12.99" instead of a number, and the failure surfaces days later as a model that mysteriously got worse. Validating at the boundary turns that into an immediate, specific complaint.

Task: write validate_rows(rows, schema) returning a list of [row_index, field_name] pairs, one for every violation found.

  • rows is a list of dictionaries. schema maps a field name to a type name: "int", "float", "str" or "bool".
  • For each row, check every field in the schema, in the schema's own order. Report a violation when the field is missing from the row, or when its value is the wrong type.
  • Fields in a row that the schema doesn't mention are ignored — extra data isn't an error here.
  • Report violations in order: row by row, and within a row in schema order. A row can produce several.
  • Return an empty list when everything checks out.

One genuine Python trap to handle: bool is a subclass of int. So isinstance(True, int) is True, and a naive check lets True pass as an integer. Treat them as distinct — a True in an "int" field is a violation, and a 1 in a "bool" field is a violation too.

That asymmetry is not pedantry. A boolean flag silently accepted as the number 1 is exactly the kind of type confusion that produces a feature column of mixed meaning, and no later stage will ever notice.

Examples

rows = [{'id': 1, 'name': 'a'}, {'id': 'x', 'name': 'b'}]
schema = {'id': 'int', 'name': 'str'}
returns [[1, 'id']]
rows = [{'id': 1}]
schema = {'id': 'int', 'name': 'str'}
returns [[0, 'name']]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run