ZodSchemaClass work
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Julien Valverdé
2024-02-24 00:11:06 +01:00
parent 53bd30aa7a
commit ec2b940f5c
2 changed files with 18 additions and 24 deletions

View File

@@ -1,5 +1,5 @@
import { Trait, TraitExpressionBuilder } from "@thilawyn/traitify-ts"
import { AbstractClass, Simplify } from "type-fest"
import { AbstractClass } from "type-fest"
import { JsonifiableObject } from "type-fest/source/jsonifiable"
import { z } from "zod"
import { ZodSchemaAbstractClass } from "./shapes/ZodSchemaClass"
@@ -7,7 +7,7 @@ import { DejsonifiableZodSchemaObject } from "./traits/DejsonifiableZodSchemaObj
import { ExtendableZodSchemaObject } from "./traits/ExtendableZodSchemaObject"
import { InstantiableZodSchemaObject } from "./traits/InstantiableZodSchemaObject"
import { JsonifiableZodSchemaObject } from "./traits/JsonifiableZodSchemaObject"
import { Extend, StaticMembers } from "./util"
import { StaticMembers } from "./util"
export class ZodSchemaClassBuilder<
@@ -103,25 +103,17 @@ extends TraitExpressionBuilder<Superclass, Traits> {
>,
props: {
jsonifySchema: (props: {
jsonifySchema: (
schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>
shape: SchemaT
}) => z.ZodObject<JsonifySchemaT, JsonifySchemaUnknownKeys, JsonifySchemaCatchall, JsonifiedValues, Values>
) => z.ZodObject<JsonifySchemaT, JsonifySchemaUnknownKeys, JsonifySchemaCatchall, JsonifiedValues, Values>
dejsonifySchema: (props: {
dejsonifySchema: (
schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>
shape: SchemaT
}) => z.ZodObject<DejsonifySchemaT, DejsonifySchemaUnknownKeys, DejsonifySchemaCatchall, Values, JsonifiedValues>
) => z.ZodObject<DejsonifySchemaT, DejsonifySchemaUnknownKeys, DejsonifySchemaCatchall, Values, JsonifiedValues>
},
) {
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,
})
const jsonifySchema = props.jsonifySchema(this.expressionSuperclass.schema)
const dejsonifySchema = props.dejsonifySchema(this.expressionSuperclass.schema)
class JsonifiableSchemas extends (this.expressionSuperclass as AbstractClass<object>) {
static readonly jsonifySchema = jsonifySchema
@@ -134,7 +126,9 @@ extends TraitExpressionBuilder<Superclass, Traits> {
JsonifiableSchemas as unknown as (
AbstractClass<
InstanceType<Superclass> & JsonifiableSchemas,
ConstructorParameters<Superclass>
// TODO: for some reason, ConstructorParameters<Superclass> does not work here. Maybe try to find a fix?
ConstructorParameters<ZodSchemaAbstractClass<Instance, SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, DefaultValues>>
> &
StaticMembers<Superclass> &
StaticMembers<typeof JsonifiableSchemas>

View File

@@ -18,12 +18,12 @@ const exp = new ZodSchemaClassBuilder(TraitExpression.NullSuperclass, [])
defaultValues: { id: -1n },
})
.jsonifiable({
jsonifySchema: ({ schema, shape }) => schema.extend({
id: jsonify.bigint(shape.id)
jsonifySchema: s => s.extend({
id: jsonify.bigint(s.shape.id)
}),
dejsonifySchema: ({ schema, shape }) => schema.extend({
id: dejsonify.bigint(shape.id)
dejsonifySchema: s => s.extend({
id: dejsonify.bigint(s.shape.id)
}),
})
.expresses(ObservableZodSchemaObject)
@@ -35,7 +35,7 @@ class User extends exp.extends implements Implements<typeof exp> {}
User.defaultValues
const inst = User.create({ id: 1n, name: "" })
console.log(inst)
inst.jsonify()
const jsonifiedUser = inst.jsonify()
class SubTest extends User.extend({