Back to the blog

Developer notes

Check your JSON against a schema, locally.

Validate JSON Schema draft-07, 2019-09 and 2020-12, inspect error paths, use local references, and infer a starting schema.

By KeepSimp 4 min read

A JSON object being checked against a schema with two required fields

Valid JSON syntax only tells you that a document can be read. A schema can check what the reader expects: required fields, types, allowed values, array items and formats. KeepSimp runs those checks in a local worker and reports the paths that need attention.

Start with the contract you expect.

Open Schema, paste your JSON in Your JSON, and paste the contract in JSON Schema. Validate schema reports whether the data matches, with up to 100 issues showing instance paths, keywords and details. For example, required checks that a field exists; minimum checks a number's lower bound. The tool checks email, URI and date-time formats using Ajv's format plugin.

The default dialect is 2020-12. A $schema declaration can select draft-07 or 2019-09 instead. The validator does not coerce strings into numbers, insert defaults or remove additional properties. Your input stays unchanged when validation passes or fails.

A small schema for a project document
{
  "$schema":"https://json-schema.org/draft/2020-12/schema",
  "type":"object",
  "properties":{"project":{"type":"string"},"version":{"type":"integer","minimum":1}},
  "required":["project","version"],
  "additionalProperties":false
}

Keep reusable definitions inside the schema.

Put reusable definitions in $defs and reference them with a local $ref such as #/$defs/user. draft-07 can use definitions instead. KeepSimp does not download external schemas or resolve remote references. Copy the necessary definitions into the pasted schema when a contract normally depends on another file.

Schemas are limited to 100,000 characters. Async schemas and unknown dialects are rejected, and worker operations stop after 20 seconds. Those bounds keep the workspace responsive when a schema is unexpectedly expensive; a timeout means the check did not finish, not that the data is valid.

Infer a starting point, then review it.

Infer schema from source produces a 2020-12 schema from one sample. It marks observed object fields as required, disallows unobserved fields and uses anyOf for differing array item shapes. That describes the sample, not all valid future inputs. Review optional fields, ranges, enums, formats and additionalProperties before treating it as a contract.

Schema checks use JavaScript numbers. Documents with duplicate keys or numbers that cannot safely pass through that representation are rejected rather than silently changed. Quote large identifiers when the contract describes strings. Formatting, comparison and Pointer selection retain exact numeric text without needing schema validation.

Further reading

Topics: JSON Schema validator · validate JSON · JSON Schema online · Ajv · schema errors

A little less theory. A little more doing.

Try it on your own terms, with a free browser tool.

Try the tool