Comparing version 0.0.2 to 0.0.3
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; | ||
export interface ZodClass<T extends ZodRawShape> extends Omit<ZodObject<T>, "parse" | "parseAsync" | "safeParse" | "safeParseAsync"> { | ||
export interface ZodClass<T extends ZodRawShape> extends Omit<ZodObject<T>, "parse" | "parseAsync" | "safeParse" | "safeParseAsync" | "extend"> { | ||
extend<U extends ZodRawShape>(augmentation: U): ZodClass<Omit<T, keyof U> & U>; | ||
parse<T extends InstanceType<this> = InstanceType<this>>(value: unknown): T; | ||
@@ -6,0 +7,0 @@ parseAsync<Output extends InstanceType<this> = InstanceType<this>>(value: unknown): Promise<Output>; |
@@ -21,24 +21,40 @@ "use strict"; | ||
function ZodClass(shape) { | ||
const schema = (0, zod_1.object)(shape); | ||
return class { | ||
static parse(value, params) { | ||
return new this(schema.parse(value, params)); | ||
} | ||
static parseAsync(value, params) { | ||
return schema | ||
.parseAsync(value, params) | ||
.then((value) => new this(value)); | ||
} | ||
static safeParse(value, params) { | ||
return coerceSafeParse(this, schema.safeParse(value, params)); | ||
} | ||
static safeParseAsync(value, params) { | ||
return schema | ||
.safeParseAsync(value, params) | ||
.then((result) => coerceSafeParse(this, result)); | ||
} | ||
constructor(value) { | ||
Object.assign(this, schema.parse(value)); | ||
} | ||
}; | ||
var _a; | ||
const _schema = (0, zod_1.object)(shape); | ||
return _a = class { | ||
static extend(augmentation) { | ||
var _b; | ||
const augmented = this.schema.extend(augmentation); | ||
// @ts-ignore | ||
return _b = class extends this { | ||
constructor(value) { | ||
super(value); | ||
Object.assign(this, augmented.parse(value)); | ||
} | ||
}, | ||
_b.schema = augmented, | ||
_b; | ||
} | ||
static parse(value, params) { | ||
return new this(this.schema.parse(value, params)); | ||
} | ||
static parseAsync(value, params) { | ||
return this.schema | ||
.parseAsync(value, params) | ||
.then((value) => new this(value)); | ||
} | ||
static safeParse(value, params) { | ||
return coerceSafeParse(this, this.schema.safeParse(value, params)); | ||
} | ||
static safeParseAsync(value, params) { | ||
return this.schema | ||
.safeParseAsync(value, params) | ||
.then((result) => coerceSafeParse(this, result)); | ||
} | ||
constructor(value) { | ||
Object.assign(this, _schema.parse(value)); | ||
} | ||
}, | ||
_a.schema = _schema, | ||
_a; | ||
} | ||
@@ -45,0 +61,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; | ||
export interface ZodClass<T extends ZodRawShape> extends Omit<ZodObject<T>, "parse" | "parseAsync" | "safeParse" | "safeParseAsync"> { | ||
export interface ZodClass<T extends ZodRawShape> extends Omit<ZodObject<T>, "parse" | "parseAsync" | "safeParse" | "safeParseAsync" | "extend"> { | ||
extend<U extends ZodRawShape>(augmentation: U): ZodClass<Omit<T, keyof U> & U>; | ||
parse<T extends InstanceType<this> = InstanceType<this>>(value: unknown): T; | ||
@@ -6,0 +7,0 @@ parseAsync<Output extends InstanceType<this> = InstanceType<this>>(value: unknown): Promise<Output>; |
@@ -18,24 +18,40 @@ import { object, } from "zod"; | ||
export function ZodClass(shape) { | ||
const schema = object(shape); | ||
return class { | ||
static parse(value, params) { | ||
return new this(schema.parse(value, params)); | ||
} | ||
static parseAsync(value, params) { | ||
return schema | ||
.parseAsync(value, params) | ||
.then((value) => new this(value)); | ||
} | ||
static safeParse(value, params) { | ||
return coerceSafeParse(this, schema.safeParse(value, params)); | ||
} | ||
static safeParseAsync(value, params) { | ||
return schema | ||
.safeParseAsync(value, params) | ||
.then((result) => coerceSafeParse(this, result)); | ||
} | ||
constructor(value) { | ||
Object.assign(this, schema.parse(value)); | ||
} | ||
}; | ||
var _a; | ||
const _schema = object(shape); | ||
return _a = class { | ||
static extend(augmentation) { | ||
var _b; | ||
const augmented = this.schema.extend(augmentation); | ||
// @ts-ignore | ||
return _b = class extends this { | ||
constructor(value) { | ||
super(value); | ||
Object.assign(this, augmented.parse(value)); | ||
} | ||
}, | ||
_b.schema = augmented, | ||
_b; | ||
} | ||
static parse(value, params) { | ||
return new this(this.schema.parse(value, params)); | ||
} | ||
static parseAsync(value, params) { | ||
return this.schema | ||
.parseAsync(value, params) | ||
.then((value) => new this(value)); | ||
} | ||
static safeParse(value, params) { | ||
return coerceSafeParse(this, this.schema.safeParse(value, params)); | ||
} | ||
static safeParseAsync(value, params) { | ||
return this.schema | ||
.safeParseAsync(value, params) | ||
.then((result) => coerceSafeParse(this, result)); | ||
} | ||
constructor(value) { | ||
Object.assign(this, _schema.parse(value)); | ||
} | ||
}, | ||
_a.schema = _schema, | ||
_a; | ||
} | ||
@@ -42,0 +58,0 @@ function coerceSafeParse(clazz, result) { |
{ | ||
"name": "zod-class", | ||
"description": "Create classes from Zod Object schemas all in one line", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"exports": { | ||
@@ -18,2 +18,3 @@ ".": { | ||
"build": "tsc -b", | ||
"test": "NODE_OPTIONS=--experimental-vm-modules jest", | ||
"watch": "tsc -b -w" | ||
@@ -25,6 +26,12 @@ }, | ||
"devDependencies": { | ||
"@tsconfig/node16": "^1.0.3", | ||
"@types/jest": "^29.5.4", | ||
"@types/node": "^16", | ||
"zod": "3.20.2", | ||
"@tsconfig/node16": "^1.0.3" | ||
"jest": "^29.7.0", | ||
"prettier": "^3.0.3", | ||
"ts-jest": "^29.1.1", | ||
"ts-node": "^10.9.1", | ||
"typescript": "^5.2.2", | ||
"zod": "3.20.2" | ||
} | ||
} |
@@ -13,2 +13,3 @@ # zod-class | ||
```ts | ||
@@ -18,9 +19,20 @@ import z from "zod"; | ||
export class HelloSchema extends ZodClass({ | ||
key: z.string(), | ||
// define a class using a zod schema | ||
export class Hello extends ZodClass({ | ||
hello: z.string(), | ||
}) {} | ||
const hello = new HelloSchema({ | ||
key: "key", | ||
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" | ||
}); | ||
``` | ||
@@ -27,0 +39,0 @@ |
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
70580
194
89
9