Comparing version 0.0.4 to 0.0.5
import { ZodObject, ZodRawShape, ZodType, ParseParams, SafeParseReturnType } from "zod"; | ||
type UnionToIntersection<T> = (T extends any ? (x: T) => any : never) extends (x: infer R) => any ? R : never; | ||
type ZodValue<T extends ZodType> = T extends ZodType<infer Output> ? UnionToIntersection<Output> : never; | ||
declare const IS_ZOD_CLASS: unique symbol; | ||
type Ctor<Shape extends ZodRawShape = ZodRawShape, Self = any> = { | ||
[IS_ZOD_CLASS]: true; | ||
shape: Shape; | ||
schema: ZodObject<Shape>; | ||
parse(value: unknown): Self; | ||
new (input: any): Self; | ||
}; | ||
export declare function $<Super extends Ctor>(Super: Super): { | ||
export declare function isZodClass(a: any): a is Ctor; | ||
export declare function Z<Shape extends ZodRawShape = ZodRawShape>(shape: Shape): ZodClass<Shape>; | ||
export declare function Z<Super extends Ctor>(Super: Super): { | ||
parse(value: unknown): InstanceType<Super>; | ||
extend<Shape extends ZodRawShape>(shape: Shape): ZodClass<Omit<Super["shape"], keyof Shape> & Shape, InstanceType<Super>>; | ||
}; | ||
export interface ZodClass<T extends ZodRawShape, Self = {}> extends Omit<ZodObject<T>, "parse" | "parseAsync" | "safeParse" | "safeParseAsync"> { | ||
[IS_ZOD_CLASS]: true; | ||
shape: T; | ||
@@ -35,5 +40,8 @@ schema: ZodObject<T>; | ||
* @returns | ||
* @deprecated - use {@link Z}({ shape }) instead | ||
*/ | ||
export declare function ZodClass<T extends ZodRawShape>(shape: T): ZodClass<T>; | ||
type UnionToIntersection<T> = (T extends any ? (x: T) => any : never) extends (x: infer R) => any ? R : never; | ||
type ZodValue<T extends ZodType> = T extends ZodType<infer Output> ? UnionToIntersection<Output> : never; | ||
export {}; | ||
//# sourceMappingURL=index.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.ZodClass = exports.$ = void 0; | ||
exports.ZodClass = exports.Z = exports.isZodClass = void 0; | ||
const zod_1 = require("zod"); | ||
function $(Super) { | ||
return { | ||
extend(augmentation) { | ||
var _a; | ||
const augmented = Super.schema.extend(augmentation); | ||
// @ts-ignore | ||
return _a = class extends Super { | ||
constructor(value) { | ||
super(value); | ||
Object.assign(this, augmented.parse(value)); | ||
} | ||
}, | ||
_a.schema = augmented, | ||
_a; | ||
}, | ||
}; | ||
const IS_ZOD_CLASS = Symbol.for("zod-class"); | ||
function isZodClass(a) { | ||
return typeof a === "function" && a[IS_ZOD_CLASS]; | ||
} | ||
exports.$ = $; | ||
exports.isZodClass = isZodClass; | ||
function Z(shapeOrSuper) { | ||
if (isZodClass(shapeOrSuper)) { | ||
const Super = shapeOrSuper; | ||
return { | ||
parse(value) { | ||
return Super.parse(value); | ||
}, | ||
extend(augmentation) { | ||
var _a; | ||
const augmented = Super.schema.extend(augmentation); | ||
// @ts-ignore | ||
return _a = class extends Super { | ||
constructor(value) { | ||
super(value); | ||
Object.assign(this, augmented.parse(value)); | ||
} | ||
}, | ||
_a.schema = augmented, | ||
_a; | ||
}, | ||
}; | ||
} | ||
return ZodClass(shapeOrSuper); | ||
} | ||
exports.Z = Z; | ||
/** | ||
@@ -37,7 +49,8 @@ * Creates a class and a Zod schema in one line. | ||
* @returns | ||
* @deprecated - use {@link Z}({ shape }) instead | ||
*/ | ||
function ZodClass(shape) { | ||
var _a; | ||
var _a, _b; | ||
const _schema = (0, zod_1.object)(shape); | ||
return _a = class { | ||
return _b = class { | ||
static parse(value, params) { | ||
@@ -63,4 +76,6 @@ return new this(this.schema.parse(value, params)); | ||
}, | ||
_a.schema = _schema, | ||
_a; | ||
_a = IS_ZOD_CLASS, | ||
_b[_a] = true, | ||
_b.schema = _schema, | ||
_b; | ||
} | ||
@@ -67,0 +82,0 @@ exports.ZodClass = ZodClass; |
import { ZodObject, ZodRawShape, ZodType, ParseParams, SafeParseReturnType } from "zod"; | ||
type UnionToIntersection<T> = (T extends any ? (x: T) => any : never) extends (x: infer R) => any ? R : never; | ||
type ZodValue<T extends ZodType> = T extends ZodType<infer Output> ? UnionToIntersection<Output> : never; | ||
declare const IS_ZOD_CLASS: unique symbol; | ||
type Ctor<Shape extends ZodRawShape = ZodRawShape, Self = any> = { | ||
[IS_ZOD_CLASS]: true; | ||
shape: Shape; | ||
schema: ZodObject<Shape>; | ||
parse(value: unknown): Self; | ||
new (input: any): Self; | ||
}; | ||
export declare function $<Super extends Ctor>(Super: Super): { | ||
export declare function isZodClass(a: any): a is Ctor; | ||
export declare function Z<Shape extends ZodRawShape = ZodRawShape>(shape: Shape): ZodClass<Shape>; | ||
export declare function Z<Super extends Ctor>(Super: Super): { | ||
parse(value: unknown): InstanceType<Super>; | ||
extend<Shape extends ZodRawShape>(shape: Shape): ZodClass<Omit<Super["shape"], keyof Shape> & Shape, InstanceType<Super>>; | ||
}; | ||
export interface ZodClass<T extends ZodRawShape, Self = {}> extends Omit<ZodObject<T>, "parse" | "parseAsync" | "safeParse" | "safeParseAsync"> { | ||
[IS_ZOD_CLASS]: true; | ||
shape: T; | ||
@@ -35,5 +40,8 @@ schema: ZodObject<T>; | ||
* @returns | ||
* @deprecated - use {@link Z}({ shape }) instead | ||
*/ | ||
export declare function ZodClass<T extends ZodRawShape>(shape: T): ZodClass<T>; | ||
type UnionToIntersection<T> = (T extends any ? (x: T) => any : never) extends (x: infer R) => any ? R : never; | ||
type ZodValue<T extends ZodType> = T extends ZodType<infer Output> ? UnionToIntersection<Output> : never; | ||
export {}; | ||
//# sourceMappingURL=index.d.ts.map |
import { object, } from "zod"; | ||
export function $(Super) { | ||
return { | ||
extend(augmentation) { | ||
var _a; | ||
const augmented = Super.schema.extend(augmentation); | ||
// @ts-ignore | ||
return _a = class extends Super { | ||
constructor(value) { | ||
super(value); | ||
Object.assign(this, augmented.parse(value)); | ||
} | ||
}, | ||
_a.schema = augmented, | ||
_a; | ||
}, | ||
}; | ||
const IS_ZOD_CLASS = Symbol.for("zod-class"); | ||
export function isZodClass(a) { | ||
return typeof a === "function" && a[IS_ZOD_CLASS]; | ||
} | ||
export function Z(shapeOrSuper) { | ||
if (isZodClass(shapeOrSuper)) { | ||
const Super = shapeOrSuper; | ||
return { | ||
parse(value) { | ||
return Super.parse(value); | ||
}, | ||
extend(augmentation) { | ||
var _a; | ||
const augmented = Super.schema.extend(augmentation); | ||
// @ts-ignore | ||
return _a = class extends Super { | ||
constructor(value) { | ||
super(value); | ||
Object.assign(this, augmented.parse(value)); | ||
} | ||
}, | ||
_a.schema = augmented, | ||
_a; | ||
}, | ||
}; | ||
} | ||
return ZodClass(shapeOrSuper); | ||
} | ||
/** | ||
@@ -33,7 +44,8 @@ * Creates a class and a Zod schema in one line. | ||
* @returns | ||
* @deprecated - use {@link Z}({ shape }) instead | ||
*/ | ||
export function ZodClass(shape) { | ||
var _a; | ||
var _a, _b; | ||
const _schema = object(shape); | ||
return _a = class { | ||
return _b = class { | ||
static parse(value, params) { | ||
@@ -59,4 +71,6 @@ return new this(this.schema.parse(value, params)); | ||
}, | ||
_a.schema = _schema, | ||
_a; | ||
_a = IS_ZOD_CLASS, | ||
_b[_a] = true, | ||
_b.schema = _schema, | ||
_b; | ||
} | ||
@@ -63,0 +77,0 @@ function coerceSafeParse(clazz, result) { |
{ | ||
"name": "zod-class", | ||
"description": "Create classes from Zod Object schemas all in one line", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"repository": { | ||
"url": "https://github.com/sam-goodwin/zod-class" | ||
}, | ||
"exports": { | ||
@@ -6,0 +9,0 @@ ".": { |
@@ -13,11 +13,18 @@ # zod-class | ||
The `$` utility function is the swiss army knife in `zod-class` - you use it for everything. | ||
1. Define a new class | ||
```ts | ||
import z from "zod"; | ||
import { ZodClass } from "zod-class"; | ||
import { Z } from "zod-class"; | ||
// define a class using a zod schema | ||
export class Hello extends ZodClass({ | ||
hello: z.string(), | ||
}) {} | ||
export class Hello extends Z({ | ||
name: z.string(), | ||
}) { | ||
get message() { | ||
return `hello ${name}` | ||
} | ||
} | ||
@@ -27,5 +34,17 @@ const hello = new Hello({ | ||
}); | ||
``` | ||
// extend a class | ||
export class World extends Hello.extend({ | ||
2. Parse a value to an instance of a ZodClass | ||
```ts | ||
const hello = Z(Hello).parse(someVal) | ||
// use method on the instance | ||
const message = hello.message; | ||
``` | ||
3. Extend a class | ||
```ts | ||
// extend a class by first activating it with `Z(Hello)` | ||
export class World extends Z(Hello).extend({ | ||
world: z.string() | ||
@@ -69,23 +88,1 @@ }) {} | ||
## Caveats | ||
Caveat: the static `HelloSchema.parse`'s return type does not accurately reflect that it returns an instance of the created class, | ||
```ts | ||
const unknownValue: unknown; | ||
// we wish it was `HelloSchema`, not `{ key: string }`. | ||
const hello2: { | ||
key: string; | ||
} = HelloSchema.parse(unknownValue); | ||
``` | ||
Workaround: just cast it | ||
```ts | ||
// option 1 | ||
const hello: HelloSchema = HelloSchema.parse(unknownValue); | ||
// option 2 | ||
const hello = HelloSchema.parse<HelloSchema>(unknownValue); | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
75385
266
86