DEV Community

stereobooster
stereobooster

Posted on • Originally published at stereobooster.com on

Runtime type validators

Where can be used

  • validate http request from client (at server side)
  • validate http response from server (at client side)
  • validate data from untyped storage, for example localStorage, JSON columns in DB, etc.
  • validate env variables
  • validate CLI arguments
  • validate configuration files
  • validate data from URL (query params)

Features

  • Basic validation - can validate static types at runtime (can be restricted to JSON type)
  • Serialisation / deserialisation - for example, TS/JS supports Date, but JSON doesn’t. So you need to do conversion in order to support it
  • Compatibility with other schemas - there are tools which can convert from- or to- another schemas, for example JSONSchema, OpenAPI etc
  • Strict or permissive mode - would object be accepted if it contains extra fields
  • Helpful error messages - if in case of failed validation it points what exactly went wrong or simply dismisses the input
  • AOT (ahead of time) - can it use type information in order to produce validation libraries or faster serialiser, deserialiser
  • Custom types - can user create custom types, for example type for email, instead of string

Libraries

Not a full list, but rather a high level overview

Fast

  • typia
    • Doesn’t need extra schema definition. Uses AOT to generate validators, serialisers and deserialisers directly from TypeScript types
    • Supports protocol buffers
  • typebox
    • Supports all JSON, JS, JSONSchema, but for TS discriminated unions you need to use typebox-validators
    • Can use JIT (and AOT?)
  • ts-runtime-checks
    • Uses AOT

See: Runtype Benchmarks

Popular

Other

Related

Top comments (0)