Typanion is a lean, type-safe runtime TypeScript validator library with zero external runtime dependencies. It excels at validating complex, nested data structures and provides strong type inference, which allows TypeScript to refine types based on successful validation. Unlike some alternatives, Typanion emphasizes a functional and tree-shakeable API, making it efficient for bundlers. It provides detailed error reports and supports coercions, enabling data transformation during validation. While it may not have the extensive ecosystem of libraries like Zod or Yup, its core differentiators lie in its minimal footprint, functional design, and robust TypeScript inference. Currently, in version 3.14.0, its release cadence appears less frequent, suggesting a focus on stability over rapid iteration, with the last major activity around two years ago.
npm install typanionVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to define a schema for a user object, validate both valid and invalid data, collect detailed error messages, leverage TypeScript's type inference upon successful validation, and briefly showcases a coercion example.
To allow extraneous properties, pass `extra: t.isUnknown()` or a specific schema for extra properties to `t.isObject` (e.g., `t.isObject({ /* ... */ }, { extra: t.isUnknown() })` or `t.isObject({ /* ... */ }, { extra: t.isDict(t.isUnknown()) })`).For stricter date validation, consider using `t.cascade` with a custom regex predicate (e.g., `t.isString()` combined with `t.matches()` for ISO8601) before `t.isDate()`, or implement custom date parsing logic if milliseconds are critical. (Refer to GitHub issue #39 and #36).
Instead of `isHexColor`, which might be missing or unsupported in `cascade`, use `t.isString()` combined with `t.matches(/^#[0-9a-fA-F]{3,6}$/)` for basic hex color string validation. (Refer to GitHub issue #41).Carefully review the inferred types when using deeply nested `isEnum`. If type widening occurs, consider breaking down complex schemas or using explicit type assertions after validation to enforce the desired type. (Refer to GitHub issue #14).
Inspect the `errors` array returned by the validation function for detailed messages. Adjust the input data to conform to the schema or refine the schema to accurately reflect the expected data shape. For example, if 'Expected number, received string' ensure the field is parsed as a number before validation.
Ensure you are using `import * as t from 'typanion'` and accessing predicates as `t.isObject` or explicitly named imports `import { isObject } from 'typanion'`. Avoid `const { isObject } = require('typanion')` if the package is primarily ESM.Wrap the code that uses the validated value within the `if (validator(value))` block. `typanion` uses type predicates, so TypeScript's type narrowing only applies inside the conditional block where validation is successful. If coercion is used, ensure `coercions` are flushed.
No dependency data recorded yet.