← All Exercises

Validate a Submitted Form Server Side

basicjavascript~20min

Background

Client-side validation is a convenience, not a control. Server-side rules must be authoritative and produce field-level messages the UI can attach.

Problem

Validate submitted values against required, type and length rules, returning field-level messages in a stable order.

Input

JSON object: { values: object, rules: { [field]: { required?, type?, minLength?, maxLength? } } }

Output

JSON array of "<field>:<issue>" strings, sorted

Sample tests

Sample 1

Input: {"values":{"email":"[email protected]","age":"x"},"rules":{"email":{"required":true,"type":"string","minLength":5},"age":{"type":"number"}}}
Expected: ["age:type-number"]

Sample 2

Input: {"values":{"name":""},"rules":{"name":{"required":true,"minLength":2}}}
Expected: ["name:required"]

Your solution

Learning objective

Make server-side validation the source of truth for form errors.