Three parts but they flow into each other, so you kind of have to design the whole thing before writing any of it.
Start by clarifying the schema definition and validation rules, then design a recursive validator that traverses nested structures and collects all errors with their paths. Discuss trade-offs between fail-fast and collect-all strategies, and how to handle extra fields (e.g., strict vs. lenient mode).
Pro tip: Mention that you would separate schema definition from validation logic to make the system extensible and testable, and that you'd use a path-based error reporting format (e.g., JSON Pointer) for clarity.
Ask questions to understand the schema representation (e.g., JSON Schema, custom DSL) and whether extra fields should be allowed or flagged. Confirm the expected output format for errors.
Outline a recursive approach that checks required keys, types, and constraints at each level, accumulating errors instead of stopping at the first. Consider using a stack or recursion to handle nested keys.
Explain how to traverse nested objects/arrays, validate each nested schema, and detect extra fields by comparing against the schema's allowed keys. Decide on strict vs. lenient behavior.
Describe how to collect errors in a list, each containing the full path to the offending key, the error type (missing, type mismatch, constraint violation, extra), and a human-readable reason.
Talk about performance (e.g., early exit vs. full validation), extensibility (adding new constraint types), and how to handle complex cases like arrays of objects or conditional schemas.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.