Comparing version 0.0.10 to 0.0.11
@@ -1,9 +0,11 @@ | ||
import { ZodObject, ZodRawShape, ZodType, SafeParseReturnType, ZodArray, ZodTuple, ZodRecord, ZodMap, ZodSet, ZodFunction, ZodLazy, z, ZodPromise, ZodOptional, ZodNullable } from "zod"; | ||
import tf, { PascalCase } from "type-fest"; | ||
import { SafeParseReturnType, ZodArray, ZodFunction, ZodLazy, ZodMap, ZodNullable, ZodObject, ZodOptional, ZodPromise, ZodRawShape, ZodRecord, ZodSet, ZodTuple, ZodType, z } from "zod"; | ||
type Ctor<T = any> = { | ||
new (input: any): T; | ||
}; | ||
export interface ZodClass<Properties, Instance> extends ZodType<Instance> { | ||
extend<Super extends Ctor, Shape extends ZodRawShape>(this: Super, shape: Shape): { | ||
export interface ZodClass<Members, Instance, Shape extends ZodRawShape> extends ZodType<Instance> { | ||
shape: Shape; | ||
extend<Super extends Ctor, ChildShape extends ZodRawShape>(this: Super, shape: ChildShape): StaticProperties<ChildShape> & { | ||
[k in keyof Super]: Super[k]; | ||
} & ZodClass<Z.infer<ZodObject<Shape>> & ConstructorParameters<Super>[0], Z.infer<ZodObject<Shape>> & InstanceType<Super>>; | ||
} & ZodClass<Z.infer<ZodObject<ChildShape>> & ConstructorParameters<Super>[0], Z.infer<ZodObject<ChildShape>> & InstanceType<Super>, Omit<Shape, keyof ChildShape> & ChildShape>; | ||
parse<T>(this: Ctor<T>, value: unknown): T; | ||
@@ -15,3 +17,3 @@ parseAsync<T>(this: Ctor<T>, value: unknown): Promise<T>; | ||
nullable<Self extends ZodType>(this: Self): ZodNullable<Self>; | ||
new (data: Properties): Instance; | ||
new (data: Members): Instance; | ||
} | ||
@@ -32,9 +34,12 @@ type OptionalKeys<Shape> = { | ||
} | ||
type StaticProperties<Shape extends ZodRawShape> = { | ||
[property in keyof Shape as PascalCase<property>]: Shape[property]; | ||
}; | ||
export interface Z { | ||
class<T extends ZodRawShape>(shape: T): ZodClass<Z.infer<ZodObject<T>>, Z.infer<ZodObject<T>>>; | ||
class<Shape extends ZodRawShape>(shape: Shape): StaticProperties<Shape> & ZodClass<Z.infer<ZodObject<Shape>>, Z.infer<ZodObject<Shape>>, Shape>; | ||
} | ||
export declare const Z: { | ||
class<T extends ZodRawShape>(shape: T): ZodClass<{ [k in keyof T]: Z.infer<T[k]>; }, { [k_1 in keyof Pick<T, Exclude<keyof T, OptionalKeys<T>>>]: Z.infer<T[k_1]>; } & { [k_2 in OptionalKeys<T>]+?: Z.infer<T[k_2]>; }>; | ||
class<T extends ZodRawShape>(shape: T): { [property in keyof T as tf.PascalCase<property>]: T[property]; } & ZodClass<{ [k in keyof T]: Z.infer<T[k]>; }, { [k_1 in keyof Pick<T, Exclude<keyof T, OptionalKeys<T>>>]: Z.infer<T[k_1]>; } & { [k_2 in OptionalKeys<T>]+?: Z.infer<T[k_2]>; }, T>; | ||
}; | ||
export {}; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -9,2 +9,3 @@ "use strict"; | ||
const zod_1 = require("zod"); | ||
const to_pascal_case_js_1 = require("./to-pascal-case.js"); | ||
const IS_ZOD_CLASS = Symbol.for("zod-class"); | ||
@@ -23,3 +24,3 @@ exports.Z = { | ||
// @ts-ignore | ||
return _c = class extends this { | ||
const clazz = (_c = class extends this { | ||
constructor(value) { | ||
@@ -30,4 +31,7 @@ super(value); | ||
}, | ||
__setFunctionName(_c, "clazz"), | ||
_c.schema = augmented, | ||
_c; | ||
_c); | ||
Object.assign(clazz, getStaticMembers(augmentation)); | ||
return clazz; | ||
} | ||
@@ -65,5 +69,9 @@ static optional() { | ||
_a); | ||
Object.assign(clazz, getStaticMembers(shape)); | ||
return clazz; | ||
}, | ||
}; | ||
function getStaticMembers(shape) { | ||
return Object.fromEntries(Object.entries(shape).map(([key, value]) => [(0, to_pascal_case_js_1.toPascalCase)(key), value])); | ||
} | ||
function coerceSafeParse(clazz, result) { | ||
@@ -70,0 +78,0 @@ if (result.success) { |
@@ -1,9 +0,11 @@ | ||
import { ZodObject, ZodRawShape, ZodType, SafeParseReturnType, ZodArray, ZodTuple, ZodRecord, ZodMap, ZodSet, ZodFunction, ZodLazy, z, ZodPromise, ZodOptional, ZodNullable } from "zod"; | ||
import tf, { PascalCase } from "type-fest"; | ||
import { SafeParseReturnType, ZodArray, ZodFunction, ZodLazy, ZodMap, ZodNullable, ZodObject, ZodOptional, ZodPromise, ZodRawShape, ZodRecord, ZodSet, ZodTuple, ZodType, z } from "zod"; | ||
type Ctor<T = any> = { | ||
new (input: any): T; | ||
}; | ||
export interface ZodClass<Properties, Instance> extends ZodType<Instance> { | ||
extend<Super extends Ctor, Shape extends ZodRawShape>(this: Super, shape: Shape): { | ||
export interface ZodClass<Members, Instance, Shape extends ZodRawShape> extends ZodType<Instance> { | ||
shape: Shape; | ||
extend<Super extends Ctor, ChildShape extends ZodRawShape>(this: Super, shape: ChildShape): StaticProperties<ChildShape> & { | ||
[k in keyof Super]: Super[k]; | ||
} & ZodClass<Z.infer<ZodObject<Shape>> & ConstructorParameters<Super>[0], Z.infer<ZodObject<Shape>> & InstanceType<Super>>; | ||
} & ZodClass<Z.infer<ZodObject<ChildShape>> & ConstructorParameters<Super>[0], Z.infer<ZodObject<ChildShape>> & InstanceType<Super>, Omit<Shape, keyof ChildShape> & ChildShape>; | ||
parse<T>(this: Ctor<T>, value: unknown): T; | ||
@@ -15,3 +17,3 @@ parseAsync<T>(this: Ctor<T>, value: unknown): Promise<T>; | ||
nullable<Self extends ZodType>(this: Self): ZodNullable<Self>; | ||
new (data: Properties): Instance; | ||
new (data: Members): Instance; | ||
} | ||
@@ -32,9 +34,12 @@ type OptionalKeys<Shape> = { | ||
} | ||
type StaticProperties<Shape extends ZodRawShape> = { | ||
[property in keyof Shape as PascalCase<property>]: Shape[property]; | ||
}; | ||
export interface Z { | ||
class<T extends ZodRawShape>(shape: T): ZodClass<Z.infer<ZodObject<T>>, Z.infer<ZodObject<T>>>; | ||
class<Shape extends ZodRawShape>(shape: Shape): StaticProperties<Shape> & ZodClass<Z.infer<ZodObject<Shape>>, Z.infer<ZodObject<Shape>>, Shape>; | ||
} | ||
export declare const Z: { | ||
class<T extends ZodRawShape>(shape: T): ZodClass<{ [k in keyof T]: Z.infer<T[k]>; }, { [k_1 in keyof Pick<T, Exclude<keyof T, OptionalKeys<T>>>]: Z.infer<T[k_1]>; } & { [k_2 in OptionalKeys<T>]+?: Z.infer<T[k_2]>; }>; | ||
class<T extends ZodRawShape>(shape: T): { [property in keyof T as tf.PascalCase<property>]: T[property]; } & ZodClass<{ [k in keyof T]: Z.infer<T[k]>; }, { [k_1 in keyof Pick<T, Exclude<keyof T, OptionalKeys<T>>>]: Z.infer<T[k_1]>; } & { [k_2 in OptionalKeys<T>]+?: Z.infer<T[k_2]>; }, T>; | ||
}; | ||
export {}; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -5,3 +5,4 @@ var __setFunctionName = (this && this.__setFunctionName) || function (f, name, prefix) { | ||
}; | ||
import { object, ZodOptional, ZodNullable, } from "zod"; | ||
import { ZodNullable, ZodOptional, object, } from "zod"; | ||
import { toPascalCase } from "./to-pascal-case.js"; | ||
const IS_ZOD_CLASS = Symbol.for("zod-class"); | ||
@@ -20,3 +21,3 @@ export const Z = { | ||
// @ts-ignore | ||
return _c = class extends this { | ||
const clazz = (_c = class extends this { | ||
constructor(value) { | ||
@@ -27,4 +28,7 @@ super(value); | ||
}, | ||
__setFunctionName(_c, "clazz"), | ||
_c.schema = augmented, | ||
_c; | ||
_c); | ||
Object.assign(clazz, getStaticMembers(augmentation)); | ||
return clazz; | ||
} | ||
@@ -62,5 +66,9 @@ static optional() { | ||
_a); | ||
Object.assign(clazz, getStaticMembers(shape)); | ||
return clazz; | ||
}, | ||
}; | ||
function getStaticMembers(shape) { | ||
return Object.fromEntries(Object.entries(shape).map(([key, value]) => [toPascalCase(key), value])); | ||
} | ||
function coerceSafeParse(clazz, result) { | ||
@@ -67,0 +75,0 @@ if (result.success) { |
{ | ||
"name": "zod-class", | ||
"description": "Create classes from Zod Object schemas all in one line", | ||
"version": "0.0.10", | ||
"version": "0.0.11", | ||
"repository": { | ||
@@ -37,3 +37,6 @@ "url": "https://github.com/sam-goodwin/zod-class" | ||
"zod": "3.20.2" | ||
}, | ||
"dependencies": { | ||
"type-fest": "^4.6.0" | ||
} | ||
} |
# zod-class | ||
This is a small utility library to accompany [Zod](https://github.com/colinhacks/zod) to enable for Types and Schemas to be defined in one line by creating a Class. | ||
This is a small utility library to accompany [Zod](https://github.com/colinhacks/zod) that enables Types and Schemas to be defined in one line by creating a Class. | ||
@@ -54,2 +54,22 @@ ## Installation | ||
4. Access A ZodClass's property to re-use in other schemas | ||
```ts | ||
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 | ||
``` | ||
## Why? | ||
@@ -56,0 +76,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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
132104
21
271
103
0
2
+ Addedtype-fest@^4.6.0
+ Addedtype-fest@4.31.0(transitive)