Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
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.
npm install zod-class
import z from "zod";
import { ZodClass } from "zod-class";
// define a class using a zod schema
export class Hello extends ZodClass({
hello: z.string(),
}) {}
const hello = new Hello({
hello: "sam",
});
// extend a class
export class World extends Hello.extend({
world: z.string()
}) {}
const world = new World({
hello: "world",
world: "hello"
});
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 ZodClass({
firstName: z.string(),
lastName: z.string(),
}) {
get fullName() {
return `${this.firstName} ${this.lastName}`;
}
}
Caveat: the static HelloSchema.parse
's return type does not accurately reflect that it returns an instance of the created class,
const unknownValue: unknown;
// we wish it was `HelloSchema`, not `{ key: string }`.
const hello2: {
key: string;
} = HelloSchema.parse(unknownValue);
Workaround: just cast it
// option 1
const hello: HelloSchema = HelloSchema.parse(unknownValue);
// option 2
const hello = HelloSchema.parse<HelloSchema>(unknownValue);
FAQs
Create classes from Zod Object schemas all in one line
The npm package zod-class receives a total of 25,321 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
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.