Gathering detailed insights and metrics for zod-non-empty-array
Gathering detailed insights and metrics for zod-non-empty-array
Gathering detailed insights and metrics for zod-non-empty-array
Gathering detailed insights and metrics for zod-non-empty-array
npm install zod-non-empty-array
Typescript
Module System
Min. Node Version
Node Version
NPM Version
TypeScript (92.59%)
JavaScript (4.1%)
Shell (3.3%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
1 Stars
15 Commits
1 Forks
10 Watchers
5 Branches
25 Contributors
Updated on Jul 31, 2024
Latest Version
0.2.0
Package Id
zod-non-empty-array@0.2.0
Unpacked Size
12.62 kB
Size
4.16 kB
File Count
15
NPM Version
8.19.4
Node Version
16.20.2
Published on
Jul 16, 2024
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
1
1
21
Make zod compatible with fp-ts non-empty constructs.
1npm install zod-non-empty-array
1import { z as zod } from 'zod'; 2import { identity } from 'fp-ts/lib/function'; 3import * as NonEmptyArray from 'fp-ts/NonEmptyArray'; 4import { transformNonEmptyArray } from 'zod-non-empty-array'; 5 6// create zod schema 7const schema = zod.object({ 8 id: zod.number().int().positive(), 9 title: zod.string(), 10 authors: zod 11 .array(zod.string()) 12 // must have at least one author 13 .transform(transformNonEmptyArray), 14}); 15 16type Article = zod.infer<typeof schema>; 17 18// parse some invalid value 19const article = schema.parse({ 20 id: 1, 21 title: 'A case-study of the use of fp-ts in a real-world project', 22 authors: ['John Doe', 'Jane Doe'], 23}); 24 25// article.authors is now compatible with fp-ts 26const 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...
1interface NonEmptyArray<T> extends Array<T> { 2 0: T; 3}
While, zod defines NonEmptyArray
as follows...
1type ZodNonEmptyArray<T> = [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)
.
Source code contributions are most welcome. Please open a PR, ensure the linter is satisfied and all tests pass.
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
No vulnerabilities found.
No security vulnerabilities found.