0.1.2 #3
@@ -2,7 +2,7 @@ import { Effect } from "effect"
|
|||||||
import { AbstractClass, Class as ConcreteClass, HasRequiredKeys, Opaque } from "type-fest"
|
import { AbstractClass, Class as ConcreteClass, HasRequiredKeys, Opaque } from "type-fest"
|
||||||
import { z } from "zod"
|
import { z } from "zod"
|
||||||
import { DefinedDefaultValuesTag } from "."
|
import { DefinedDefaultValuesTag } from "."
|
||||||
import { Class, ClassType, StaticMembers } from "./util"
|
import { Class, ClassType, GetClassType, StaticMembers } from "./util"
|
||||||
|
|
||||||
|
|
||||||
export type NewZodSchemaInstanceInput<
|
export type NewZodSchemaInstanceInput<
|
||||||
@@ -28,7 +28,9 @@ type ExtendZodSchemaClass<
|
|||||||
Parent extends AbstractClass<any>,
|
Parent extends AbstractClass<any>,
|
||||||
Self extends AbstractClass<any>,
|
Self extends AbstractClass<any>,
|
||||||
> = (
|
> = (
|
||||||
AbstractClass<
|
Class<
|
||||||
|
GetClassType<Parent>,
|
||||||
|
|
||||||
Omit<InstanceType<Parent>, keyof InstanceType<DefaultZodSchemaClass>> &
|
Omit<InstanceType<Parent>, keyof InstanceType<DefaultZodSchemaClass>> &
|
||||||
InstanceType<Self>,
|
InstanceType<Self>,
|
||||||
|
|
||||||
|
|||||||
@@ -128,3 +128,20 @@ export function ZodSchemaClassOf<
|
|||||||
StaticMembers<TZodSchemaClassImpl>
|
StaticMembers<TZodSchemaClassImpl>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export function ZodSchemaClass<
|
||||||
|
SchemaT extends z.ZodRawShape,
|
||||||
|
SchemaUnknownKeys extends z.UnknownKeysParam,
|
||||||
|
SchemaCatchall extends z.ZodTypeAny,
|
||||||
|
|
||||||
|
Values extends {},
|
||||||
|
DefaultValues extends Partial<Values>,
|
||||||
|
>(
|
||||||
|
props: {
|
||||||
|
schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>
|
||||||
|
defaultValues: Opaque<DefaultValues, DefinedDefaultValuesTag>
|
||||||
|
},
|
||||||
|
) {
|
||||||
|
return ZodSchemaClassOf(Object, props)
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,12 +1,8 @@
|
|||||||
import { z } from "zod"
|
import { z } from "zod"
|
||||||
import { ZodSchemaClassOf, defineDefaultValues } from "."
|
import { ZodSchemaClass, defineDefaultValues } from "."
|
||||||
|
|
||||||
|
|
||||||
class Root {
|
class Test extends ZodSchemaClass({
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
class Test extends ZodSchemaClassOf(Root, {
|
|
||||||
schema: z.object({
|
schema: z.object({
|
||||||
id: z.bigint(),
|
id: z.bigint(),
|
||||||
name: z.string(),
|
name: z.string(),
|
||||||
@@ -18,6 +14,7 @@ class Test extends ZodSchemaClassOf(Root, {
|
|||||||
Test.defaultValues
|
Test.defaultValues
|
||||||
const inst = await Test.instantiatePromise({ id: 1n, name: "" })
|
const inst = await Test.instantiatePromise({ id: 1n, name: "" })
|
||||||
|
|
||||||
|
|
||||||
class SubTest extends Test.extend({
|
class SubTest extends Test.extend({
|
||||||
schema: ({ schema }) => schema.extend({
|
schema: ({ schema }) => schema.extend({
|
||||||
prout: z.string()
|
prout: z.string()
|
||||||
|
|||||||
@@ -22,6 +22,14 @@ export type Class<
|
|||||||
: never
|
: never
|
||||||
)
|
)
|
||||||
|
|
||||||
|
export type GetClassType<C> = (
|
||||||
|
C extends ConcreteClass<any>
|
||||||
|
? "Class"
|
||||||
|
: C extends AbstractClass<any>
|
||||||
|
? "AbstractClass"
|
||||||
|
: never
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents the static members of a class.
|
* Represents the static members of a class.
|
||||||
|
|||||||
Reference in New Issue
Block a user