json-zen Playground

1 min read Original article ↗

verify(json, schema) — validates JSON against a schema string. Throws a ZenError with details if validation fails. Returns true on success.
Schema syntax: {key:type}  |  [type]  |  ?type (optional)  |  type:default  |  Aliases: s=string, n=number, b=boolean

Tip: {key:type, key?:type} · [type] · s n b · type:default

check(json, schema) — same as verify but returns true or false instead of throwing. Safe wrapper that never throws on validation failures.

Tip: {key:type, key?:type} · [type] · s n b · type:default

shape(json, schema, options?) — reshapes JSON to match the schema, filling defaults for missing/invalid fields. Returns the transformed object. Optionally pass custom type validators as options.

type:default fills missing/invalid fields

toSchema(json, options?) — infers a schema string from any JSON value. Useful for bootstrapping a schema from real data.
Options: default type to omit (e.g. "string")  |  compact strips all :type annotations from object attributes.

extendTypes(types) — registers custom type validators globally. A validator can be a function (value) => boolean, an array of allowed strings, or a RegExp. Then use the key name in any schema.

Uses custom types defined on the right →