npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

zod-class

v0.0.15

Published

Create classes from Zod Object schemas all in one line

Downloads

67,002

Readme

zod-class

This is a small utility library to accompany Zod that enables Types and Schemas to be defined in one line by creating a Class.

Installation

npm install zod-class

Usage

  1. Define a new class
import z from "zod";
import { Z } from "zod-class";

// define a class using a zod schema
export class Hello extends Z.class({
  name: z.string(),
}) {
  get getMessage() {
    return `hello ${name}`
  }
}

const hello = new Hello({
  hello: "sam",
});
  1. Parse a value to an instance of a ZodClass
const hello = Hello.parse(someVal)

// use method on the instance 
const message = hello.getMessage();
  1. Extend a class
export class World extends Hello.extend({
  world: z.string()
}) {}

const world = new World({
  hello: "world",
  world: "hello"
});
  1. Access A ZodClass's property to re-use in other schemas
import { z } from "zod";
import { Z } from "zod-class";

export class Product extends Z.class({
  id: z.string().brand<"ProductId">,
  price: z.number().min(1)
}) {}

export class Order extends Z.class({
  id: z.string().brand<"OrderId">,
  productId: Product.shape.id // 👈 Re-using the branded type `id` from `Product` class 
}) {}


Product.Id // 👈 Properties are also available in friendly pascal case directly on the class constructor

Why?

It can be annoying to always have redundant declarations for types and schemas:

  1. the z.object declaration
  2. the derived type using z.infer
interface HelloSchema extends z.infer<typeof HelloSchema> {}
const HelloSchema = z.object({
  key: z.string(),
});

zod-class enables this to be achieved in a single line.

It also provides a class that can be instantiated and methods added to.

export class Person extends Z.class({
  firstName: z.string(),
  lastName: z.string(),
}) {
  get fullName() {
    return `${this.firstName} ${this.lastName}`;
  }
}

Workarounds

Creating a class that adequately sub-types a Zod Schema is difficult because of how Zod is implemented. zod-class covers the most common use-cases but there are holes.

If you encounter a problem with type errors, you can always workaround it with the schema() method.

For example, if you have a function that expects a ZodType<T>:

function createDTO<T>(schema: ZodType<T>): DTO<T>;

And a class, User, constructed with Z.class:

class User extends Z.class({
  username: z.string()
}) {}

You should be able to just pass User in

const UserDTO = createDTO(User);

In some cases, this can error. To workaround, call User.schema() instead:

const UserDTO = createDTO(User.schema());

See relevant issue: #17

  1. nullish will not create a schema that returns an instance of the ZodClass

ZodClass does not provide a type-safe implementation of schema.nullish().

User.nullish().parse(value) 

This will not return an instance of User:

{ username: string } | null | undefined

Workaround with User.schema()

User.schema().nullish().parse(value) // User | null | undefined