https://github.com/nvie/decoders
Elegant validation library for type-safe input data for TypeScript
https://github.com/nvie/decoders
checking composition decoders elm flow input javascript type typescript validation
Last synced: 26 days ago
JSON representation
Elegant validation library for type-safe input data for TypeScript
- Host: GitHub
- URL: https://github.com/nvie/decoders
- Owner: nvie
- License: mit
- Created: 2017-10-04T19:40:11.000Z (over 8 years ago)
- Default Branch: main
- Last Pushed: 2026-03-01T21:41:16.000Z (about 1 month ago)
- Last Synced: 2026-03-01T21:48:34.650Z (about 1 month ago)
- Topics: checking, composition, decoders, elm, flow, input, javascript, type, typescript, validation
- Language: TypeScript
- Homepage: https://decoders.cc
- Size: 46.2 MB
- Stars: 395
- Watchers: 4
- Forks: 28
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
- awesome-flow - decoders - Type-safe data validation for Flow. (Packages / Helpers)
- awesome-list - decoders - safe input data (for TypeScript and Flow) | nvie | 262 | (JavaScript)
README

[](https://www.npmjs.com/package/decoders)
[](https://github.com/nvie/decoders/actions)
[](https://pkg-size.dev/decoders)
Elegant and battle-tested validation library for type-safe input data for TypeScript.
## Basic example
```typescript
import { array, isoDate, number, object, optional, string } from 'decoders';
// Incoming data at runtime, e.g. the request body
// The point is that this data is untrusted and its type unknown
const externalData = {
id: 123,
name: 'Alison Roberts',
createdAt: '2026-01-11T12:26:37.024Z',
tags: ['foo', 'bar'],
};
// Write the decoder (= what you expect the data to look like)
const userDecoder = object({
id: number,
name: string,
createdAt: optional(isoDate),
tags: array(string),
});
// Call .verify() on the incoming data
const user = userDecoder.verify(externalData);
// ^^^^
// TypeScript will infer this type as:
// {
// id: number;
// name: string;
// createdAt?: Date;
// tags: string[];
// }
```
## Installation
```bash
npm install decoders
```
## Requirements
You must set `strict: true` in your `tsconfig.json` in order for type inference to work
correctly!
```js
// tsconfig.json
{
"compilerOptions": {
"strict": true
}
}
```
## Documentation
Documentation can be found on [decoders.cc](https://decoders.cc).
There is a dedicated page that explains how to
[build your own decoders](https://decoders.cc/docs/building-your-own).