Comparing version 0.0.1 to 0.0.2
@@ -1,6 +0,9 @@ | ||
import { ZodObject, ZodRawShape, ZodType } from "zod"; | ||
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"> { | ||
export interface ZodClass<T extends ZodRawShape> extends Omit<ZodObject<T>, "parse" | "parseAsync" | "safeParse" | "safeParseAsync"> { | ||
parse<T extends InstanceType<this> = InstanceType<this>>(value: unknown): T; | ||
parseAsync<Output extends InstanceType<this> = InstanceType<this>>(value: unknown): Promise<Output>; | ||
safeParse<Output extends InstanceType<this> = InstanceType<this>>(data: unknown, params?: Partial<ParseParams>): SafeParseReturnType<ZodValue<ZodObject<T>>, Output>; | ||
safeParseAsync<Output extends InstanceType<this> = InstanceType<this>>(value: unknown): Promise<SafeParseReturnType<ZodValue<ZodObject<T>>, Output>>; | ||
new (data: ZodValue<ZodObject<T>>): ZodValue<ZodObject<T>>; | ||
@@ -7,0 +10,0 @@ } |
@@ -23,2 +23,18 @@ "use strict"; | ||
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) { | ||
@@ -30,2 +46,13 @@ Object.assign(this, schema.parse(value)); | ||
exports.ZodClass = ZodClass; | ||
function coerceSafeParse(clazz, result) { | ||
if (result.success) { | ||
return { | ||
success: true, | ||
data: new clazz(result.data), | ||
}; | ||
} | ||
else { | ||
return result; | ||
} | ||
} | ||
//# sourceMappingURL=index.js.map |
@@ -1,6 +0,9 @@ | ||
import { ZodObject, ZodRawShape, ZodType } from "zod"; | ||
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"> { | ||
export interface ZodClass<T extends ZodRawShape> extends Omit<ZodObject<T>, "parse" | "parseAsync" | "safeParse" | "safeParseAsync"> { | ||
parse<T extends InstanceType<this> = InstanceType<this>>(value: unknown): T; | ||
parseAsync<Output extends InstanceType<this> = InstanceType<this>>(value: unknown): Promise<Output>; | ||
safeParse<Output extends InstanceType<this> = InstanceType<this>>(data: unknown, params?: Partial<ParseParams>): SafeParseReturnType<ZodValue<ZodObject<T>>, Output>; | ||
safeParseAsync<Output extends InstanceType<this> = InstanceType<this>>(value: unknown): Promise<SafeParseReturnType<ZodValue<ZodObject<T>>, Output>>; | ||
new (data: ZodValue<ZodObject<T>>): ZodValue<ZodObject<T>>; | ||
@@ -7,0 +10,0 @@ } |
@@ -1,2 +0,2 @@ | ||
import { object } from "zod"; | ||
import { object, } from "zod"; | ||
/** | ||
@@ -20,2 +20,18 @@ * Creates a class and a Zod schema in one line. | ||
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) { | ||
@@ -26,2 +42,13 @@ Object.assign(this, schema.parse(value)); | ||
} | ||
function coerceSafeParse(clazz, result) { | ||
if (result.success) { | ||
return { | ||
success: true, | ||
data: new clazz(result.data), | ||
}; | ||
} | ||
else { | ||
return result; | ||
} | ||
} | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "zod-class", | ||
"description": "Create classes from Zod Object schemas all in one line", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"exports": { | ||
@@ -6,0 +6,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
62777
160