Deviopath › TypeScript

Mini Schema Validator

Hard · Modelling

Write validate(value, schema) that checks value against a Schema (see the starter) and returns a list of error strings (empty when valid).

Paths start at $. An object property adds .key, an array item adds [index].

- "string", "number", "boolean": if typeof value differs, add "<path>: expected <type>".

- "array": if the value is not an array, add "<path>: expected array". Otherwise validate every item with items.

- "object": if the value is not a plain object (null and arrays do not count), add "<path>: expected object". Otherwise go through the keys of properties in their declared order: if the property value is undefined, add "<path>.<key>: required" only when the key is listed in required; otherwise validate it recursively. Extra keys are ignored.

Errors appear in the order they are found (depth first).

Next: CSV Parser with Quoted Fields