0.1.0 #1

Merged
Thilawyn merged 24 commits from next into master 2024-01-05 00:39:33 +01:00
4 changed files with 31 additions and 25 deletions
Showing only changes of commit fcacd8566d - Show all commits

View File

@@ -1,2 +1,2 @@
export * from "./SchemableClass"
export * from "./makeSchemaClass"
export * from "./makeSchemableClass"

View File

@@ -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>
}

View File

@@ -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))

View File

@@ -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)