Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

zod-class

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

zod-class

Create classes from Zod Object schemas all in one line

  • 0.0.7
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

zod-class

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

Installation

npm install zod-class

Usage

The Z utility function is the swiss army knife in zod-class - you use it for everything.

  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 message() {
    return `hello ${name}`
  }
}

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

// use method on the instance 
const message = hello.message;
  1. Extend a class
// extend a class by first activating it with `Z(Hello)`
export class World extends Z(Hello).extend({
  world: z.string()
}) {}

const world = new World({
  hello: "world",
  world: "hello"
});

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}`;
  }
}

FAQs

Package last updated on 16 Oct 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc