jtd-ts is a typescript first jtd parser, validator, and fuzzer
The root of the API is exposed through a CompiledSchema, which represents a
compiled JTD schema and has methods for guarding if an
object complies, fuzzing random objects, or getting the
underlying schema.
There are two primary ways to compile a schema, using native typescript
functions, or compiling a raw schema.
The first is the typescript native way, using functions that have similar
names to their JTD counterparts, e.g. boolean. The advantage of this
aproach is that it's a little less verbose, and it skips some checks compared
to compiling a raw schema. The disadvantage is that there are some concncepts
in JTD that this can not express, primarily, circular refs. This interface
doesn't allow declaring an infinite type.
Every function except for compile corresponds to its appropriate
schema definition. In addition, there are three functions that behave
slightly differently.
nullable - modifies an existing schema to make it nullable
metadata - modifies an existing schema to include metadata, the
type of the metadata is preserved.
definitions - creates a builder to accurately type a directed
acyclic graph of definitions and references, before needing to call
build to create the actual
CompiledSchema.
If you've defined your schema elsewhere, you can similarly just compile it in
typescript. The advantage of this is portability, and typescript will infer
the appropriate type for the guards. It can also handle circular refs. The
downside is it's more verbose, and the type checking doesn't regonize all
errors, so if you're not targeting portability, the other interface is
better, and can still export the corresponding schema.
In addition to the compile function, this API also exposes interfaces
for the various schemas, like BooleanSchema.
jtd-ts
is a typescript first jtd parser, validator, and fuzzerThe root of the API is exposed through a CompiledSchema, which represents a compiled JTD schema and has methods for
guard
ing if an object complies,fuzz
ing random objects, or getting the underlyingschema
.There are two primary ways to compile a schema, using native typescript functions, or compiling a raw schema.
Native Typescript Functions
The first is the typescript native way, using functions that have similar names to their JTD counterparts, e.g. boolean. The advantage of this aproach is that it's a little less verbose, and it skips some checks compared to compiling a raw schema. The disadvantage is that there are some concncepts in JTD that this can not express, primarily, circular refs. This interface doesn't allow declaring an infinite type.
Every function except for compile corresponds to its appropriate schema definition. In addition, there are three functions that behave slightly differently.
build
to create the actual CompiledSchema.Usage
Schema Compilation
If you've defined your schema elsewhere, you can similarly just compile it in typescript. The advantage of this is portability, and typescript will infer the appropriate type for the guards. It can also handle circular refs. The downside is it's more verbose, and the type checking doesn't regonize all errors, so if you're not targeting portability, the other interface is better, and can still export the corresponding schema.
In addition to the compile function, this API also exposes interfaces for the various schemas, like BooleanSchema.
Usage