makeSchemableClass refactoring
This commit is contained in:
@@ -1,2 +1,2 @@
|
||||
export * from "./SchemableClass"
|
||||
export * from "./makeSchemaClass"
|
||||
export * from "./makeSchemableClass"
|
||||
|
||||
@@ -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 {
|
||||
const $schemableConfig = {
|
||||
values: {} as z.output<typeof schemaWithDefaultValues>,
|
||||
input: {} as z.input<typeof schemaWithDefaultValues>,
|
||||
schema,
|
||||
schemaWithDefaultValues,
|
||||
} as const satisfies SchemableConfig
|
||||
|
||||
return class {
|
||||
static readonly $schemableConfig = $schemableConfig
|
||||
static readonly schema = schema
|
||||
static readonly schemaWithDefaultValues = schemaWithDefaultValues
|
||||
|
||||
readonly $schemableConfig = $schemableConfig
|
||||
readonly schema = schema
|
||||
readonly schemaWithDefaultValues = schemaWithDefaultValues
|
||||
|
||||
constructor(data: z.output<typeof schema>) {
|
||||
Object.assign(this, data)
|
||||
}
|
||||
}
|
||||
|
||||
return class_ as (
|
||||
Class<
|
||||
InstanceType<typeof class_> & z.output<typeof schema>,
|
||||
ConstructorParameters<typeof class_>
|
||||
> &
|
||||
StaticMembers<typeof class_>
|
||||
)
|
||||
} as SchemableClass<typeof $schemableConfig>
|
||||
|
||||
}
|
||||
@@ -1,3 +1,8 @@
|
||||
import { HasRequiredKeys } from "type-fest"
|
||||
import { z } from "zod"
|
||||
import { SchemableClass, SchemableConfig } from "."
|
||||
|
||||
|
||||
type ParamsArgs = [] | [Partial<z.ParseParams>]
|
||||
|
||||
type NewEntityArgs<Input extends object> =
|
||||
@@ -6,12 +11,11 @@ type NewEntityArgs<Input extends object> =
|
||||
: [] | [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))
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user