Typescript validators

2 min read Original article ↗

TypeScript validator libraries feature tests

This app is a feature test for Typescript validator (or, as they also called, "parser" or "codec") libraries.

The app is not a performance benchmark: for performance comparison, see this awesome app.

The test case is a generic data structure in combination with a set of "ways to break its invariants".

The test case checks such features as:

  • Any basic features you’d expect from a validator: “This string is a string, this object has fields I expect…”
  • Products and sums (algebraic data types)
  • Nominal types: telling apart two primitives (e.g. strings) of different meaning (e.g. UserId and PostId)
  • "Codecs" semantics support: not only "validate an unknown value" but also "encode the validated value back [to JSON]"
  • Transformation support: interpretation of network-serializable types as environment-specific types, e.g. string[] as Set<string>
  • Representation of recursive types, e.g. tree
  • Importantly, any of those features in combination with each other

The table below shows the results of each test against each library.

For each library-specific code and details, click on the library name.

ajvarktypedecoderseffect-schemarescript-schemaruntypesschemata-tssuperstructtypeboxvalibotvalitayupzod

addFavouriteRed

Adds a duplicated valid colour to the favouriteColours field

addFavouriteTiger

Adds an invalid colour to the favouriteColours field

addFileSystemDupeFile

Adds a duplicated value to the tree. My tree has the “unique list” semantics, so that shouldn’t be possible

addFileSystemUFOType

An enum test not unlike the Tiger test, but in composition with recursive data structures

addTwoAtsToEmail

Renders the email invalid by adding two @s

clearName

Clears the name field

prefixCustomerId

Adds an invalid prefix to the stripeId field

setCreatedAtCyborgWar

Sets invalid createdAt date

setHalfVisits

Renders the visits field to be a float instead of an integer

setProfileArtist

Sets the valid profile field to an invalid structure

setSubscriptionTypeBanana

Sets the subscription field to banana

switchDates

Switches the createdAt and updatedAt fields

encodedEqualsInput

decode then encode doesnt break the input

transformationsPossible

transformations are possible

acceptsTypedInput

The library accepts not only unknown/any types as validation input, but more refined "intermediate" types as well

branded

Branded types are supported

canGenerateJsonSchema

Whether the schema itself can be serialized to cross-system communication. Became more relevant with OpenAI introducing structured outputs

emailFormatAmbiguityIsAccountedFor

Email format ambiguity is accounted for either in API or in Docs. The library doesn't promise not being able to deliver

templateLiterals

Template literals are supported

typedErrors

Typed errors are supported