Skip to main content
✅ Validators & Form Logic

What you're building

A SchemaValidator that accepts a schema definition and validates plain objects against it — the core concept behind Zod, Yup, Joi, and JSON Schema. You'll implement type checking, required/optional fields, min/max constraints, and collect structured errors.

Requirements

Implement SchemaValidator with a constructor that takes a schema object, and a validate(data) method that returns { ok: boolean, errors: { [field]: string } }.

Schema field descriptors:

PropertyTypeMeaning
type"string" or "number"The expected JS type
requiredbooleanIf true and the field is missing or empty string, add an error
minnumberFor strings: minimum length. For numbers: minimum value
maxnumberFor strings: maximum length. For numbers: maximum value

Rules:

  • A field that is undefined AND required: false (or required omitted) has no error.
  • For strings, min/max check value.length.
  • For numbers, min/max check the numeric value.
  • ok is true when errors has zero keys.

Example schema:

const schema = {
  email:  { type: "string", required: true, min: 5 },
  age:    { type: "number", required: true, min: 18, max: 120 },
  bio:    { type: "string", required: false, max: 200 },
};

Stretch goals

  • Support a pattern property (RegExp) that the string must match.
  • Support an enum property (array) — the value must be one of the listed values.
  • Support nested schemas via a shape property for object fields.

What this proves

Algorithmic thinking, working from a spec, and building composable abstractions — the exact skills tested in full-stack and backend interviews. A custom schema validator is also a standout portfolio piece.

⚡ 60 XP
solution.jsRuns in your browser
Loading editor…
Tests4 to pass
valid data passes with ok: true
required field missing produces error
number out of range produces error
optional absent field is allowed

Run your code to check these.