@thaumaturgy/zod
TypeScript icon, indicating that this package has built-in type declarations

0.15.0 • Public • Published

@thaumaturgy/zod

npm

Thaumaturgy is a fixtures and seeding library for TypeScript.

This package contains bindings for zod.

Installation

Install @thaumaturgy/zod as a development dependency:

npm install -D @thaumaturgy/zod

Note: @thaumaturgy/zod has a peer dependency on zod.

Usage

import { z } from 'zod';
import { Realm } from '@thaumaturgy/zod';

const Movie = z
  .object({
    title: z.string(),
    year: z.number(),
  })
  .strict()
  .describe('Movie');

const realm = new Realm();

realm.define(Movie, {
  sequences: {
    titles: new Sequence(n => `Movie ${n}` as const),
    years: new Sequence(n => 2022 - n),
  },
  manifest: ({ sequences }) => ({
    title: sequences.titles.next(),
    year: sequences.years.next(),
  }),
});

const movie = realm.manifest(Movie);

console.log(movie);
// > { title: 'Movie 1', year: 2021 }

Entity Hierarchies

import { z } from 'zod';
import { Realm, Ref } from '@thaumaturgy/zod';

const Author = z
  .object({
    id: z.string(),
    name: z.string(),
  })
  .describe('Author');

const Book = z
  .object({
    id: z.string(),
    authorId: z.string(),
    title: z.string(),
  })
  .describe('Book');

const realm = new Realm();

realm.define(Author, {
  manifest: ({ uuid }) => ({
    id: uuid(),
    name: 'J. R. R. Tolkien',
  }),
});

realm.define(Book, {
  manifest: ({ uuid }) => ({
    id: uuid(),
    authorId: Ref.to(Author).through(author => author.id),
    title: 'The Lord of the Rings',
  }),
});

const book = realm.manifest(Book);

console.log(book);
// > {
//     id: '1ab4790a-9911-4e20-9006-b12e6b60dfe6',
//     authorId: 'c6a1f6e0-6845-4675-b570-87024446a371',
//     title: 'The Lord of the Rings'
//   }

Package Sidebar

Install

npm i @thaumaturgy/zod

Weekly Downloads

1

Version

0.15.0

License

MIT

Unpacked Size

12.2 kB

Total Files

15

Last publish

Collaborators

  • maxdeviant