diff --git a/src/index.ts b/src/index.ts index 379ba4d..a850a00 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,2 +1,2 @@ export * from "./SchemableClass" -export * from "./makeSchemaClass" +export * from "./makeSchemableClass" diff --git a/src/makeSchemaClass.ts b/src/makeSchemableClass.ts similarity index 59% rename from src/makeSchemaClass.ts rename to src/makeSchemableClass.ts index e14a3fa..f0fd976 100644 --- a/src/makeSchemaClass.ts +++ b/src/makeSchemableClass.ts @@ -1,9 +1,9 @@ -import { Class } from "type-fest" import { z } from "zod" -import { StaticMembers, zodObjectRemoveDefaults } from "./util" +import { SchemableClass, SchemableConfig } from "." +import { zodObjectRemoveDefaults } from "./util" -export function makeSchemaClass< +export function makeSchemableClass< SchemaWithDefaultValuesT extends z.ZodRawShape, SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam, SchemaWithDefaultValuesCatchall extends z.ZodTypeAny, @@ -25,24 +25,25 @@ export function makeSchemaClass< const schema = zodObjectRemoveDefaults(schemaWithDefaultValues) - const class_ = class { - static readonly schema = schema + const $schemableConfig = { + values: {} as z.output, + input: {} as z.input, + schema, + schemaWithDefaultValues, + } as const satisfies SchemableConfig + + return class { + static readonly $schemableConfig = $schemableConfig + static readonly schema = schema static readonly schemaWithDefaultValues = schemaWithDefaultValues - readonly schema = schema + readonly $schemableConfig = $schemableConfig + readonly schema = schema readonly schemaWithDefaultValues = schemaWithDefaultValues constructor(data: z.output) { Object.assign(this, data) } - } - - return class_ as ( - Class< - InstanceType & z.output, - ConstructorParameters - > & - StaticMembers - ) + } as SchemableClass } diff --git a/src/newSchemable.ts b/src/newSchemable.ts index 26482a6..873d667 100644 --- a/src/newSchemable.ts +++ b/src/newSchemable.ts @@ -1,3 +1,8 @@ +import { HasRequiredKeys } from "type-fest" +import { z } from "zod" +import { SchemableClass, SchemableConfig } from "." + + type ParamsArgs = [] | [Partial] type NewEntityArgs = @@ -6,12 +11,11 @@ type NewEntityArgs = : [] | [Input, ...ParamsArgs] -export const newEntity = < - $Config extends EntityConfig, - Static extends EntityClassStatic<$Config>, - T extends Entity<$Config>, +export const newSchemable = < + C extends SchemableClass<$Config>, + $Config extends SchemableConfig, >( - class_: EntityClass<$Config, Static, T>, + class_: C | SchemableClass<$Config>, ...[values, params]: NewEntityArgs<$Config["input"]> ) => new class_(class_.schemaWithDefaultValues.parse(values || {}, params)) diff --git a/src/tests.ts b/src/tests.ts index 5b7ee67..29278e0 100644 --- a/src/tests.ts +++ b/src/tests.ts @@ -1,5 +1,6 @@ import { z } from "zod" -import { makeSchemaClass } from "./makeSchemaClass" +import { makeSchemableClass } from "." +import { newSchemable } from "./newSchemable" const UserSchema = z.object({ @@ -8,9 +9,9 @@ const UserSchema = z.object({ }) -const UserSchemaObject = makeSchemaClass({ schema: UserSchema }) +const UserSchemaObject = makeSchemableClass({ schema: UserSchema }) class User extends UserSchemaObject {} -const user = new User({ id: 1n }) -user.schema +const user1 = new User({ id: 1n }) +const user2 = newSchemable(User)