makeJsonifiableSchemableClass
This commit is contained in:
@@ -1,12 +1,14 @@
|
||||
import { Class } from "type-fest"
|
||||
import { JsonifiableObject } from "type-fest/source/jsonifiable"
|
||||
import { z } from "zod"
|
||||
import { JsonifiableSchemableConfig } from "."
|
||||
import { SchemableClass, SchemableConfig } from ".."
|
||||
import { parseZodTypeEffect } from "../util"
|
||||
import { StaticMembers, parseZodTypeEffect } from "../util"
|
||||
|
||||
|
||||
export function makeJsonifiableSchemableClass<
|
||||
C extends SchemableClass<$Config>,
|
||||
$Config extends SchemableConfig,
|
||||
C extends SchemableClass<$SchemableConfig>,
|
||||
$SchemableConfig extends SchemableConfig,
|
||||
|
||||
JsonifiedValues extends JsonifiableObject,
|
||||
|
||||
@@ -18,28 +20,28 @@ export function makeJsonifiableSchemableClass<
|
||||
DejsonifySchemaUnknownKeys extends z.UnknownKeysParam,
|
||||
DejsonifySchemaCatchall extends z.ZodTypeAny,
|
||||
>(
|
||||
class_: C | SchemableClass<$Config>,
|
||||
class_: C | SchemableClass<$SchemableConfig>,
|
||||
|
||||
props: {
|
||||
jsonifySchema: (props: {
|
||||
schema: $Config["schema"]
|
||||
s: $Config["schema"]["shape"]
|
||||
schema: $SchemableConfig["schema"]
|
||||
s: $SchemableConfig["schema"]["shape"]
|
||||
}) => z.ZodObject<
|
||||
JsonifySchemaT,
|
||||
JsonifySchemaUnknownKeys,
|
||||
JsonifySchemaCatchall,
|
||||
JsonifiedValues,
|
||||
$Config["values"]
|
||||
$SchemableConfig["values"]
|
||||
>
|
||||
|
||||
dejsonifySchema: (props: {
|
||||
schema: $Config["schema"]
|
||||
s: $Config["schema"]["shape"]
|
||||
schema: $SchemableConfig["schema"]
|
||||
s: $SchemableConfig["schema"]["shape"]
|
||||
}) => z.ZodObject<
|
||||
DejsonifySchemaT,
|
||||
DejsonifySchemaUnknownKeys,
|
||||
DejsonifySchemaCatchall,
|
||||
$Config["values"],
|
||||
$SchemableConfig["values"],
|
||||
JsonifiedValues
|
||||
>
|
||||
},
|
||||
@@ -55,13 +57,21 @@ export function makeJsonifiableSchemableClass<
|
||||
s: class_.schema.shape,
|
||||
})
|
||||
|
||||
const $jsonifiableSchemableConfig = {
|
||||
$schemableConfig: class_.$schemableConfig,
|
||||
jsonifiedValues: undefined as unknown as JsonifiedValues,
|
||||
jsonifySchema: undefined as unknown as typeof jsonifySchema,
|
||||
dejsonifySchema: undefined as unknown as typeof dejsonifySchema,
|
||||
} as const satisfies JsonifiableSchemableConfig
|
||||
|
||||
return class JsonifiableSchemableObject extends class_ {
|
||||
static readonly jsonifySchema = jsonifySchema
|
||||
static readonly dejsonifySchema = dejsonifySchema
|
||||
const jsonifiableClass = class JsonifiableSchemableObject extends class_ {
|
||||
static readonly $jsonifiableSchemableConfig = $jsonifiableSchemableConfig
|
||||
static readonly jsonifySchema = jsonifySchema
|
||||
static readonly dejsonifySchema = dejsonifySchema
|
||||
|
||||
readonly jsonifySchema = jsonifySchema
|
||||
readonly dejsonifySchema = dejsonifySchema
|
||||
readonly $jsonifiableSchemableConfig = $jsonifiableSchemableConfig
|
||||
readonly jsonifySchema = jsonifySchema
|
||||
readonly dejsonifySchema = dejsonifySchema
|
||||
|
||||
jsonify() {
|
||||
return this.jsonifySchema.parse(this)
|
||||
@@ -76,4 +86,13 @@ export function makeJsonifiableSchemableClass<
|
||||
}
|
||||
}
|
||||
|
||||
return jsonifiableClass as (
|
||||
Class<
|
||||
InstanceType<C> & InstanceType<typeof jsonifiableClass>,
|
||||
ConstructorParameters<C>
|
||||
> &
|
||||
StaticMembers<C> &
|
||||
StaticMembers<typeof jsonifiableClass>
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user