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:
| Property | Type | Meaning |
|---|---|---|
type | "string" or "number" | The expected JS type |
required | boolean | If true and the field is missing or empty string, add an error |
min | number | For strings: minimum length. For numbers: minimum value |
max | number | For strings: maximum length. For numbers: maximum value |
Rules:
- A field that is
undefinedANDrequired: false(orrequiredomitted) has no error. - For strings,
min/maxcheckvalue.length. - For numbers,
min/maxcheck the numeric value. okistruewhenerrorshas 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
patternproperty (RegExp) that the string must match. - Support an
enumproperty (array) — the value must be one of the listed values. - Support nested schemas via a
shapeproperty 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.