Make zod compatible with fp-ts non-empty constructs.
npm install zod-non-empty-arrayMake zod compatible with fp-ts non-empty constructs.
 
``bash`
npm install zod-non-empty-array
#### Requirements
- Node.js v.16+
`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);
`
Zod already supports non-empty array validation. However, the generated type is incompatible with fp-ts.
Specifically, fp-ts defines NonEmptyArray as follows...
`typescript`
interface NonEmptyArray
0: T;
}
While, zod defines NonEmptyArray as follows...
`typescript`
type ZodNonEmptyArray
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)`.
- transformNonEmptyArray
- transformReadonlyNonEmptyArray
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/.
MIT