Implement a nested object validator
Problem: Implement a Nested Object Validator
Design and implement a helper function, validate(object, required_style), that verifies whether a given object conforms to a specified schema. The input object can be arbitrarily nested, containing sub-objects (e.g., under keys like "OBJ") and various key-value pairs.
The required_style parameter is a dictionary that defines the schema. It maps field names to either required primitive types (such as string or integer) or to other nested schemas (which are themselves dictionaries).
The function should return a boolean indicating validity. If the object is invalid, it must also return a detailed explanation that includes the offending path within the object and the specific reason for the failure (e.g., a missing key, an unexpected type).
As part of your solution, you must explicitly define and justify your approach for the following:
- How to treat extra fields present in the
objectbut not inrequired_style(should they be ignored or flagged as errors?). - How to handle
nullvalues within theobject. - How to validate
arraytypes within theobject.
Finally, demonstrate your approach with a concrete example. Provide an example object containing fields like name, age, school, and nested attributes (e.g., color, height), and show how its corresponding required_style schema would be expressed.
AI interview prep
Core dilemma
Balancing the flexibility and extensibility of a schema validation system for arbitrary nested objects with the need for clear, actionable error reporting and efficient performance, especially for deep structures.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Implement a nested object validator
Problem: Implement a Nested Object Validator
Design and implement a helper function, validate(object, required_style), that verifies whether a given object conforms to a specified schema. The input object can be arbitrarily nested, containing sub-objects (e.g., under keys like "OBJ") and various key-value pairs.
The required_style parameter is a dictionary that defines the schema. It maps field names to either required primitive types (such as string or integer) or to other nested schemas (which are themselves dictionaries).
The function should return a boolean indicating validity. If the object is invalid, it must also return a detailed explanation that includes the offending path within the object and the specific reason for the failure (e.g., a missing key, an unexpected type).
As part of your solution, you must explicitly define and justify your approach for the following:
- How to treat extra fields present in the
objectbut not inrequired_style(should they be ignored or flagged as errors?). - How to handle
nullvalues within theobject. - How to validate
arraytypes within theobject.
Finally, demonstrate your approach with a concrete example. Provide an example object containing fields like name, age, school, and nested attributes (e.g., color, height), and show how its corresponding required_style schema would be expressed.