tcomb-validation is a general-purpose JavaScript validation library built upon the tcomb type combinator library. It provides a concise yet expressive syntax for validating various data structures, including native types, refinements, objects, lists, tuples, enums, unions, dicts, and intersections, with arbitrary nesting levels. The library offers detailed information on failed validations, making it a lightweight alternative to JSON Schema for validating domain models. The current stable version is 3.4.1, with releases typically addressing bug fixes and minor features, often in conjunction with its `tcomb` dependency. A key differentiator is its direct reuse of `tcomb` type definitions for validation, streamlining development when `tcomb` is already used for runtime type checking or domain modeling. It ships with TypeScript definitions, ensuring good type safety for modern JavaScript and TypeScript projects.
npm install tcomb-validationVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to define complex types using `tcomb`, then use `tcomb-validation`'s `validate` function to check both valid and invalid data, including strict validation and error introspection.
If you relied on `undefined` being converted to `null` by `maybe`, explicitly convert `undefined` values to `null` before validation, or adapt your code to handle `undefined`.
Refactor calls to `validate` to use the `options` object. Instead of `validate(value, type, ['field'])`, use `validate(value, type, { path: ['field'] })`.Ensure you have `tcomb` installed (`npm install tcomb`) and import it (e.g., `import * as t from 'tcomb';` or `const t = require('tcomb');`) to define your validation schemas.Upgrade to `tcomb-validation@3.4.1` or newer to benefit from the TypeScript fix for recursive type definitions.
Ensure `tcomb` is installed (`npm install tcomb`) and correctly imported, typically as `t`: `import * as t from 'tcomb';` or `const t = require('tcomb');`.Use a named import for `validate`: `import { validate } from 'tcomb-validation';` or for CommonJS: `const { validate } = require('tcomb-validation');`.Adjust the data being validated to match the expected `tcomb` type, or refine the `tcomb` type definition to correctly represent the allowable data shapes.