Problem, as an interviewer would state it: "Build a form validation utility supporting both per-field rules (required, min length) AND cross-field rules (like confirming two password fields match)."
Examples:
validator.validate({ password: "abc12345", confirmPassword: "different" });
// { valid: false, errors: { confirmPassword: "Passwords do not match" } }
Clarifying questions expected:
- If a field already has its own field-level error, should a cross-field rule involving that same field still be allowed to overwrite it?
- Should field-level rules stop at the FIRST failing rule per field, or collect every failing rule's message?
- Should validation run eagerly (on every keystroke) or only on submit — and does that change this utility's own design?
Code / implementation expected: Yes — real, direct proof of field-level errors, a cross-field mismatch error, a fully valid case, and the subtle precedence case where a cross-field rule must not overwrite an existing field-level error.