Security News
New Python Packaging Proposal Aims to Solve Phantom Dependency Problem with SBOMs
PEP 770 proposes adding SBOM support to Python packages to improve transparency and catch hidden non-Python dependencies that security tools often miss.
This is a small utility library to accompany Zod that enables Types and Schemas to be defined in one line by creating a Class.
npm install zod-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",
});
const hello = Hello.parse(someVal)
// use method on the instance
const message = hello.getMessage();
export class World extends Hello.extend({
world: z.string()
}) {}
const world = new World({
hello: "world",
world: "hello"
});
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
It can be annoying to always have redundant declarations for types and schemas:
z.object
declarationz.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}`;
}
}
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
nullish
will not create a schema that returns an instance of the ZodClassZodClass 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
FAQs
Create classes from Zod Object schemas all in one line
The npm package zod-class receives a total of 16,653 weekly downloads. As such, zod-class popularity was classified as popular.
We found that zod-class demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
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.
Security News
PEP 770 proposes adding SBOM support to Python packages to improve transparency and catch hidden non-Python dependencies that security tools often miss.
Security News
Socket CEO Feross Aboukhadijeh discusses open source security challenges, including zero-day attacks and supply chain risks, on the Cyber Security Council podcast.
Security News
Research
Socket researchers uncover how threat actors weaponize Out-of-Band Application Security Testing (OAST) techniques across the npm, PyPI, and RubyGems ecosystems to exfiltrate sensitive data.