https://github.com/causaly/zod-non-empty-array
Make zod compatible with fp-ts NonEmptyArray
https://github.com/causaly/zod-non-empty-array
Last synced: 10 months ago
JSON representation
Make zod compatible with fp-ts NonEmptyArray
- Host: GitHub
- URL: https://github.com/causaly/zod-non-empty-array
- Owner: causaly
- License: mit
- Created: 2023-04-25T07:49:06.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2025-08-18T12:28:36.000Z (10 months ago)
- Last Synced: 2025-08-18T14:26:03.528Z (10 months ago)
- Language: TypeScript
- Size: 1.58 MB
- Stars: 1
- Watchers: 10
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# zod-non-empty-array
Make zod compatible with fp-ts non-empty constructs.
[](https://github.com/causaly/zod-non-empty-array/actions/workflows/ci.yml) [](https://www.npmjs.com/package/zod-non-empty-array)
## Installation
```bash
npm install zod-non-empty-array
```
#### Requirements
- Node.js v.16+
## Quick start
```typescript
import { z as zod } from 'zod';
import { identity } from 'fp-ts/lib/function';
import * as NonEmptyArray from 'fp-ts/NonEmptyArray';
import { transformNonEmptyArray } from 'zod-non-empty-array';
// create zod schema
const schema = zod.object({
id: zod.number().int().positive(),
title: zod.string(),
authors: zod
.array(zod.string())
// must have at least one author
.transform(transformNonEmptyArray),
});
type Article = zod.infer;
// parse some invalid value
const article = schema.parse({
id: 1,
title: 'A case-study of the use of fp-ts in a real-world project',
authors: ['John Doe', 'Jane Doe'],
});
// article.authors is now compatible with fp-ts
const principalAuthor = pipe(article.authors, NonEmptyArray.head);
```
## Motivation
Zod already supports [non-empty array validation](https://zod.dev/?id=nonempty). However, the generated type is incompatible with [fp-ts](https://gcanti.github.io/fp-ts/).
Specifically, fp-ts defines `NonEmptyArray` as follows...
```typescript
interface NonEmptyArray extends Array {
0: T;
}
```
While, zod defines `NonEmptyArray` as follows...
```typescript
type ZodNonEmptyArray = [T, ...T[]];
```
This incompatibility makes it difficult to integrate zod with fp-ts.
The `zod-non-empty-array` library addresses the incompatibility issue by exposing zod transformers that return fp-ts constructs. Instead of `zod.array(...).nonempty()` use `zod.array(...).transform(transformNonEmptyArray)`.
## API
- transformNonEmptyArray
- transformReadonlyNonEmptyArray
## Contribute
Source code contributions are most welcome. Please open a PR, ensure the linter is satisfied and all tests pass.
#### We are hiring
Causaly is building the world's largest biomedical knowledge platform, using technologies such as TypeScript, React and Node.js. Find out more about our openings at https://apply.workable.com/causaly/.
## License
MIT