Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/marcello3d/io-ts-path
Generate type-safe paths from io-ts models.
https://github.com/marcello3d/io-ts-path
Last synced: 15 days ago
JSON representation
Generate type-safe paths from io-ts models.
- Host: GitHub
- URL: https://github.com/marcello3d/io-ts-path
- Owner: marcello3d
- Created: 2019-01-18T05:53:24.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2019-02-01T05:04:02.000Z (almost 6 years ago)
- Last Synced: 2024-10-18T15:06:03.360Z (29 days ago)
- Language: TypeScript
- Homepage:
- Size: 53.7 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# io-ts-path
[![CircleCI](https://circleci.com/gh/marcello3d/io-ts-path.svg?style=svg)](https://circleci.com/gh/marcello3d/io-ts-path)
[![npm version](https://badge.fury.io/js/io-ts-path.svg)](https://badge.fury.io/js/io-ts-path)
[![codecov](https://codecov.io/gh/marcello3d/io-ts-path/branch/master/graph/badge.svg)](https://codecov.io/gh/marcello3d/io-ts-path)Generate type-safe paths from [io-ts](https://github.com/gcanti/io-ts) models.
## Basic usage
### Type-safe path from io-ts type
```typescript
import * as t from 'io-ts';
import { query, path } from 'io-ts-path';const Person = t.type({
name: t.string,
age: t.number,
fullName: t.type({
first: t.string,
last: t.string
})
});path(query(Person).name)
// => ['name']path(query(Person).fullName.first)
// => ['fullName', 'first']
```Usage with dictionaries and arrays:
```typescript
import * as t from 'io-ts';
import { query, path, WildCard } from 'io-ts-path';const Model = t.type({
users: t.dictionary(
t.string,
t.type({
name: t.string,
age: t.number,
books: t.array(t.string),
}),
),
});path(query(Model).users._.books)
// => ['users', WildCard, 'books']
```### io-ts type from path
```typescript
import * as t from 'io-ts';
import { type } from 'io-ts-path';const Person = t.type({
name: t.string,
age: t.number,
fullName: t.type({
first: t.string,
last: t.string
})
});type(Person, ['age'])
// => t.numbertype(Person, ['fullName', 'first'])
// => t.stringtype(Person, ['foo'])
// => throws PathError
``````typescript
import * as t from 'io-ts';
import { type, WildCard } from 'io-ts-path';const Model = t.type({
users: t.dictionary(
t.string,
t.type({
name: t.string,
age: t.number,
books: t.array(t.string),
}),
),
});type(Model, ['users', WildCard, 'books'])
// => t.array(t.string)
```