diff --git a/src/SchemaClass.ts b/src/SchemaClass.ts new file mode 100644 index 0000000..478adf4 --- /dev/null +++ b/src/SchemaClass.ts @@ -0,0 +1,86 @@ +import { Class } from "type-fest" +import { z } from "zod" + + +export type SchemaClass< + Values extends {} = {}, + Input extends {} = {}, + + SchemaT extends z.ZodRawShape = z.ZodRawShape, + SchemaUnknownKeys extends z.UnknownKeysParam = z.UnknownKeysParam, + SchemaCatchall extends z.ZodTypeAny = z.ZodTypeAny, + + SchemaWithDefaultValuesT extends z.ZodRawShape = z.ZodRawShape, + SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam = z.UnknownKeysParam, + SchemaWithDefaultValuesCatchall extends z.ZodTypeAny = z.ZodTypeAny, +> = ( + Class< + SchemaObject< + Values, + Input, + + SchemaT, + SchemaUnknownKeys, + SchemaCatchall, + + SchemaWithDefaultValuesT, + SchemaWithDefaultValuesUnknownKeys, + SchemaWithDefaultValuesCatchall + >, + + SchemaClassConstructorParams + > & { + readonly schema: z.ZodObject< + SchemaT, + SchemaUnknownKeys, + SchemaCatchall, + Values, + Values + > + + readonly schemaWithDefaultValues: z.ZodObject< + SchemaWithDefaultValuesT, + SchemaWithDefaultValuesUnknownKeys, + SchemaWithDefaultValuesCatchall, + Values, + Input + > + } +) + +export type SchemaClassConstructorParams< + Values extends {} = {} +> = ( + Parameters< + (data: Values) => void + > +) + +export type SchemaObject< + Values extends {} = {}, + Input extends {} = {}, + + SchemaT extends z.ZodRawShape = z.ZodRawShape, + SchemaUnknownKeys extends z.UnknownKeysParam = z.UnknownKeysParam, + SchemaCatchall extends z.ZodTypeAny = z.ZodTypeAny, + + SchemaWithDefaultValuesT extends z.ZodRawShape = z.ZodRawShape, + SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam = z.UnknownKeysParam, + SchemaWithDefaultValuesCatchall extends z.ZodTypeAny = z.ZodTypeAny, +> = { + readonly schema: z.ZodObject< + SchemaT, + SchemaUnknownKeys, + SchemaCatchall, + Values, + Values + > + + readonly schemaWithDefaultValues: z.ZodObject< + SchemaWithDefaultValuesT, + SchemaWithDefaultValuesUnknownKeys, + SchemaWithDefaultValuesCatchall, + Values, + Input + > +} & Values diff --git a/src/index.ts b/src/index.ts index 8038132..9877e90 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1 +1,2 @@ +export * from "./SchemaClass" export * from "./makeSchemaClass" diff --git a/src/makeSchemaClass.ts b/src/makeSchemaClass.ts index caf104a..e14a3fa 100644 --- a/src/makeSchemaClass.ts +++ b/src/makeSchemaClass.ts @@ -25,7 +25,7 @@ export function makeSchemaClass< const schema = zodObjectRemoveDefaults(schemaWithDefaultValues) - class SchemaObject { + const class_ = class { static readonly schema = schema static readonly schemaWithDefaultValues = schemaWithDefaultValues @@ -37,12 +37,12 @@ export function makeSchemaClass< } } - return SchemaObject as ( + return class_ as ( Class< - SchemaObject & z.output, - ConstructorParameters + InstanceType & z.output, + ConstructorParameters > & - StaticMembers + StaticMembers ) } diff --git a/src/tests.ts b/src/tests.ts index 1c2b569..5b7ee67 100644 --- a/src/tests.ts +++ b/src/tests.ts @@ -10,5 +10,7 @@ const UserSchema = z.object({ const UserSchemaObject = makeSchemaClass({ schema: UserSchema }) -const user = new UserSchemaObject({ id: 1n }) -user.id +class User extends UserSchemaObject {} + +const user = new User({ id: 1n }) +user.schema