0.1.0 #1
@@ -1,2 +1,2 @@
|
|||||||
export * from "./SchemableClass"
|
export * from "./SchemableClass"
|
||||||
export * from "./makeSchemaClass"
|
export * from "./makeSchemableClass"
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
import { Class } from "type-fest"
|
|
||||||
import { z } from "zod"
|
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,
|
SchemaWithDefaultValuesT extends z.ZodRawShape,
|
||||||
SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam,
|
SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam,
|
||||||
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
|
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
|
||||||
@@ -25,24 +25,25 @@ export function makeSchemaClass<
|
|||||||
|
|
||||||
const schema = zodObjectRemoveDefaults(schemaWithDefaultValues)
|
const schema = zodObjectRemoveDefaults(schemaWithDefaultValues)
|
||||||
|
|
||||||
const class_ = class {
|
const $schemableConfig = {
|
||||||
static readonly schema = schema
|
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
|
static readonly schemaWithDefaultValues = schemaWithDefaultValues
|
||||||
|
|
||||||
readonly schema = schema
|
readonly $schemableConfig = $schemableConfig
|
||||||
|
readonly schema = schema
|
||||||
readonly schemaWithDefaultValues = schemaWithDefaultValues
|
readonly schemaWithDefaultValues = schemaWithDefaultValues
|
||||||
|
|
||||||
constructor(data: z.output<typeof schema>) {
|
constructor(data: z.output<typeof schema>) {
|
||||||
Object.assign(this, data)
|
Object.assign(this, data)
|
||||||
}
|
}
|
||||||
}
|
} as SchemableClass<typeof $schemableConfig>
|
||||||
|
|
||||||
return class_ as (
|
|
||||||
Class<
|
|
||||||
InstanceType<typeof class_> & z.output<typeof schema>,
|
|
||||||
ConstructorParameters<typeof class_>
|
|
||||||
> &
|
|
||||||
StaticMembers<typeof class_>
|
|
||||||
)
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,3 +1,8 @@
|
|||||||
|
import { HasRequiredKeys } from "type-fest"
|
||||||
|
import { z } from "zod"
|
||||||
|
import { SchemableClass, SchemableConfig } from "."
|
||||||
|
|
||||||
|
|
||||||
type ParamsArgs = [] | [Partial<z.ParseParams>]
|
type ParamsArgs = [] | [Partial<z.ParseParams>]
|
||||||
|
|
||||||
type NewEntityArgs<Input extends object> =
|
type NewEntityArgs<Input extends object> =
|
||||||
@@ -6,12 +11,11 @@ type NewEntityArgs<Input extends object> =
|
|||||||
: [] | [Input, ...ParamsArgs]
|
: [] | [Input, ...ParamsArgs]
|
||||||
|
|
||||||
|
|
||||||
export const newEntity = <
|
export const newSchemable = <
|
||||||
$Config extends EntityConfig,
|
C extends SchemableClass<$Config>,
|
||||||
Static extends EntityClassStatic<$Config>,
|
$Config extends SchemableConfig,
|
||||||
T extends Entity<$Config>,
|
|
||||||
>(
|
>(
|
||||||
class_: EntityClass<$Config, Static, T>,
|
class_: C | SchemableClass<$Config>,
|
||||||
...[values, params]: NewEntityArgs<$Config["input"]>
|
...[values, params]: NewEntityArgs<$Config["input"]>
|
||||||
) =>
|
) =>
|
||||||
new class_(class_.schemaWithDefaultValues.parse(values || {}, params))
|
new class_(class_.schemaWithDefaultValues.parse(values || {}, params))
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { z } from "zod"
|
import { z } from "zod"
|
||||||
import { makeSchemaClass } from "./makeSchemaClass"
|
import { makeSchemableClass } from "."
|
||||||
|
import { newSchemable } from "./newSchemable"
|
||||||
|
|
||||||
|
|
||||||
const UserSchema = z.object({
|
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 {}
|
class User extends UserSchemaObject {}
|
||||||
|
|
||||||
const user = new User({ id: 1n })
|
const user1 = new User({ id: 1n })
|
||||||
user.schema
|
const user2 = newSchemable(User)
|
||||||
|
|||||||
Reference in New Issue
Block a user