Skip to the solution
MediumLow-Level DesignAsked in 20246 min read

JSON schema validator

2.5k views

The problem

Problem, as an interviewer would state it: "Implement a minimal validate(schema, data) supporting a real subset of JSON Schema: type, required, properties, items, and minimum — returning every validation error found, with the exact path to each one."

Examples:

Code
validate({ type: "object", required: ["name"], properties: { name: { type: "string" } } }, { });
// { valid: false, errors: ["$: missing required property \"name\""] }

Clarifying questions expected:

  • Should validation stop at the FIRST error, or collect every error across the whole structure?
  • Does each error need to report the exact PATH to the offending field, for real usability?
  • Which real JSON Schema keywords are actually in scope — the full real spec, or a deliberately small subset?

Code / implementation expected: Yes — real, direct proof across 5 scenarios: fully valid data, a missing required field, a wrong type, a below-minimum number, and an invalid nested array item type.

  • js-coding
  • utility
  • algorithm

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read · 9 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal, direct proof: the validator correctly reports the exact error and path across 5 real scenarios, including a nested array item type mismatch

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 64 of 154 in JavaScript coding

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextMedium

Trie autocomplete suffix search

Continue