ZodSchemaClassBuilder work
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import { Trait, TraitExpressionBuilder, expression } from "@thilawyn/traitify-ts"
|
||||
import { Trait, TraitExpressionBuilder } from "@thilawyn/traitify-ts"
|
||||
import { AbstractClass, Simplify } from "type-fest"
|
||||
import { JsonifiableObject } from "type-fest/source/jsonifiable"
|
||||
import { z } from "zod"
|
||||
import { ZodSchemaClass } from "./shapes/ZodSchemaClass"
|
||||
import { ExtendableZodSchemaObject } from "./traits/ExtendableZodSchemaObject"
|
||||
import { InstantiableZodSchemaObject } from "./traits/InstantiableZodSchemaObject"
|
||||
import { Extend, StaticMembers } from "./util"
|
||||
@@ -81,7 +82,15 @@ extends TraitExpressionBuilder<Superclass, Traits> {
|
||||
}
|
||||
|
||||
jsonifiable<
|
||||
Superclass extends AbstractClass<object>,
|
||||
Superclass extends ZodSchemaClass<Instance, SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, DefaultValues>,
|
||||
Instance extends Values,
|
||||
|
||||
SchemaT extends z.ZodRawShape,
|
||||
SchemaUnknownKeys extends z.UnknownKeysParam,
|
||||
SchemaCatchall extends z.ZodTypeAny,
|
||||
|
||||
Values extends object,
|
||||
DefaultValues extends Partial<Values>,
|
||||
|
||||
JsonifiedValues extends JsonifiableObject,
|
||||
|
||||
@@ -93,42 +102,65 @@ extends TraitExpressionBuilder<Superclass, Traits> {
|
||||
DejsonifySchemaUnknownKeys extends z.UnknownKeysParam,
|
||||
DejsonifySchemaCatchall extends z.ZodTypeAny,
|
||||
>(
|
||||
this: ZodSchemaClassBuilder<Superclass, Traits>,
|
||||
) {
|
||||
this: ZodSchemaClassBuilder<
|
||||
Superclass | ZodSchemaClass<Instance, SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, DefaultValues>,
|
||||
Traits
|
||||
>,
|
||||
|
||||
}
|
||||
}
|
||||
props: {
|
||||
jsonifySchema: (props: {
|
||||
schema: z.ZodObject<
|
||||
SchemaT,
|
||||
SchemaUnknownKeys,
|
||||
SchemaCatchall,
|
||||
Values,
|
||||
Values
|
||||
>
|
||||
|
||||
shape: SchemaT
|
||||
}) => z.ZodObject<
|
||||
JsonifySchemaT,
|
||||
JsonifySchemaUnknownKeys,
|
||||
JsonifySchemaCatchall,
|
||||
JsonifiedValues,
|
||||
Values
|
||||
>
|
||||
|
||||
export function ZodSchemaClassOf<
|
||||
Superclass extends AbstractClass<object, []>,
|
||||
dejsonifySchema: (props: {
|
||||
schema: z.ZodObject<
|
||||
SchemaT,
|
||||
SchemaUnknownKeys,
|
||||
SchemaCatchall,
|
||||
Values,
|
||||
Values
|
||||
>
|
||||
|
||||
SchemaT extends z.ZodRawShape,
|
||||
SchemaUnknownKeys extends z.UnknownKeysParam,
|
||||
SchemaCatchall extends z.ZodTypeAny,
|
||||
|
||||
Values extends object,
|
||||
DefaultValues extends Partial<Values>,
|
||||
>(
|
||||
of: Superclass,
|
||||
|
||||
{ schema, defaultValues }: {
|
||||
schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>
|
||||
defaultValues: DefaultValues
|
||||
shape: SchemaT
|
||||
}) => z.ZodObject<
|
||||
DejsonifySchemaT,
|
||||
DejsonifySchemaUnknownKeys,
|
||||
DejsonifySchemaCatchall,
|
||||
Values,
|
||||
JsonifiedValues
|
||||
>
|
||||
},
|
||||
) {
|
||||
class Schemas extends (of as AbstractClass<object, []>) {
|
||||
static readonly schema = schema
|
||||
static readonly defaultValues = defaultValues
|
||||
const jsonifySchema = props.jsonifySchema({
|
||||
schema: this.expressionSuperclass.schema,
|
||||
shape: this.expressionSuperclass.schema.shape,
|
||||
})
|
||||
const dejsonifySchema = props.dejsonifySchema({
|
||||
schema: this.expressionSuperclass.schema,
|
||||
shape: this.expressionSuperclass.schema.shape,
|
||||
})
|
||||
|
||||
constructor(values: Values) {
|
||||
super()
|
||||
Object.assign(this, values)
|
||||
}
|
||||
class Schemas extends this.expressionSuperclass {
|
||||
static readonly jsonifySchema = jsonifySchema
|
||||
static readonly dejsonifySchema = dejsonifySchema
|
||||
}
|
||||
|
||||
return expression
|
||||
.extends(Schemas as unknown as (
|
||||
return new ZodSchemaClassBuilder(
|
||||
Schemas as unknown as (
|
||||
AbstractClass<
|
||||
InstanceType<Superclass> &
|
||||
Simplify<
|
||||
@@ -143,28 +175,11 @@ export function ZodSchemaClassOf<
|
||||
StaticMembers<typeof Schemas>,
|
||||
]>
|
||||
>
|
||||
))
|
||||
.expresses(
|
||||
InstantiableZodSchemaObject,
|
||||
ExtendableZodSchemaObject,
|
||||
),
|
||||
|
||||
[
|
||||
...this.expressionTraits,
|
||||
],
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
class DefaultRoot {}
|
||||
|
||||
export function ZodSchemaClass<
|
||||
SchemaT extends z.ZodRawShape,
|
||||
SchemaUnknownKeys extends z.UnknownKeysParam,
|
||||
SchemaCatchall extends z.ZodTypeAny,
|
||||
|
||||
Values extends object,
|
||||
DefaultValues extends Partial<Values>,
|
||||
>(
|
||||
props: {
|
||||
schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>
|
||||
defaultValues: DefaultValues
|
||||
},
|
||||
) {
|
||||
return ZodSchemaClassOf(DefaultRoot, props)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user