tezvyn:

How would you implement specific error messages for failed validation rules?

Curated by the Tezvyn teamSource: nngroup.combeginner
How would you implement specific error messages for failed validation rules?
WHAT IT TESTS

Architecting validation as structured data instead of booleans.

A GOOD ANSWER COVERS

error codes from validators, a mapping layer separating logic from copy, and accessible inline rendering.

WHAT THIS TESTS: This question probes whether you treat validation as a data structure problem or a UI afterthought. Senior engineers are expected to separate business rules from presentation, produce machine-readable failures that support internationalization, and respect UX principles like proximity, specificity, and timing. The interviewer wants to see a pipeline mindset: input enters, rules execute, structured errors emit, and the view layer maps those errors to human text without leaking copy into domain logic.

A GOOD ANSWER COVERS: First, validators should return rich error descriptors instead of booleans. For example, a password validator returns an object containing a code such as MIN_LENGTH_VIOLATION and metadata like current length and required length. Second, introduce a catalog or dictionary layer that maps each code to a template string or i18n key, keeping user-facing copy out of the validation engine. Third, the UI layer should consume these descriptors and render them adjacent to the offending field using accessible markup such as aria-describedby or role alert, following the visibility guideline that errors must be noticeable and placed close to their source. Fourth, mention timing strategy: do not show errors prematurely on blur for empty optional fields, but do use inline real-time feedback for error-prone interactions like password creation. Fifth, note that the backend should emit the same structured codes so the frontend and API remain consistent.

COMMON WRONG ANSWERS: A major red flag is returning generic true or false from validation functions, which forces the UI to guess what went wrong. Another is hardcoding English sentences inside validator classes or API responses, which blocks translation and couples product copy to deployment cycles. Some candidates suggest putting all validation logic exclusively on the frontend, ignoring that server-side guards are still required for security. Finally, proposing a single global toast for every field error ignores the proximity principle and increases cognitive load.

LIKELY FOLLOW-UPS: The interviewer may ask how you would handle multiple simultaneous failures on one field, such as a password failing length, uppercase, and symbol rules. They might also probe how you would support right-to-left languages or pluralization in error templates, or how to prevent error fatigue when a form has many invalid fields. Another angle is accessibility: how would you ensure screen readers announce errors in the correct order without overwhelming the user.

ONE CONCRETE EXAMPLE: Imagine an email validator. Instead of returning false when input is bad, it returns an array containing one object with code INVALID_EMAIL_FORMAT. The UI layer looks up that code in a messages file and receives the string Email format is incorrect. If the user has not yet touched the field, no error displays. Once they blur with invalid input, the input border turns red, an icon appears, and aria-describedby points to the message node placed directly beneath the field. If the backend later rejects the email because it is already registered, the API returns a different code, EMAIL_ALREADY_EXISTS, and the same rendering pipeline handles it without any frontend validation code changing.

Source: nngroup.com

Read the original → nngroup.com

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.

How would you implement specific error messages for failed validation rules? · Tezvyn