zod-non-empty-array
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

zod-non-empty-array

Make zod compatible with fp-ts non-empty constructs.

Build Status npm version

Installation

npm install zod-non-empty-array

Requirements

  • Node.js v.16+

Quick start

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<typeof schema>;

// 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. However, the generated type is incompatible with fp-ts.

Specifically, fp-ts defines NonEmptyArray as follows...

interface NonEmptyArray<T> extends Array<T> {
  0: T;
}

While, zod defines NonEmptyArray as follows...

type 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).

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

/zod-non-empty-array/

    Package Sidebar

    Install

    npm i zod-non-empty-array

    Weekly Downloads

    184

    Version

    0.1.0

    License

    MIT

    Unpacked Size

    12.9 kB

    Total Files

    15

    Last publish

    Collaborators

    • jmike