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 { 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 { JsonifiableObject } from "type-fest/source/jsonifiable"
import { z } from "zod" import { z } from "zod"
import { ZodSchemaAbstractClass } from "./shapes/ZodSchemaClass" import { ZodSchemaAbstractClass } from "./shapes/ZodSchemaClass"
@@ -7,7 +7,7 @@ import { DejsonifiableZodSchemaObject } from "./traits/DejsonifiableZodSchemaObj
import { ExtendableZodSchemaObject } from "./traits/ExtendableZodSchemaObject" import { ExtendableZodSchemaObject } from "./traits/ExtendableZodSchemaObject"
import { InstantiableZodSchemaObject } from "./traits/InstantiableZodSchemaObject" import { InstantiableZodSchemaObject } from "./traits/InstantiableZodSchemaObject"
import { JsonifiableZodSchemaObject } from "./traits/JsonifiableZodSchemaObject" import { JsonifiableZodSchemaObject } from "./traits/JsonifiableZodSchemaObject"
import { Extend, StaticMembers } from "./util" import { StaticMembers } from "./util"
export class ZodSchemaClassBuilder< export class ZodSchemaClassBuilder<
@@ -103,25 +103,17 @@ extends TraitExpressionBuilder<Superclass, Traits> {
>, >,
props: { props: {
jsonifySchema: (props: { jsonifySchema: (
schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values> 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> 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({ const jsonifySchema = props.jsonifySchema(this.expressionSuperclass.schema)
schema: this.expressionSuperclass.schema, const dejsonifySchema = props.dejsonifySchema(this.expressionSuperclass.schema)
shape: this.expressionSuperclass.schema.shape,
})
const dejsonifySchema = props.dejsonifySchema({
schema: this.expressionSuperclass.schema,
shape: this.expressionSuperclass.schema.shape,
})
class JsonifiableSchemas extends (this.expressionSuperclass as AbstractClass<object>) { class JsonifiableSchemas extends (this.expressionSuperclass as AbstractClass<object>) {
static readonly jsonifySchema = jsonifySchema static readonly jsonifySchema = jsonifySchema
@@ -134,7 +126,9 @@ extends TraitExpressionBuilder<Superclass, Traits> {
JsonifiableSchemas as unknown as ( JsonifiableSchemas as unknown as (
AbstractClass< AbstractClass<
InstanceType<Superclass> & JsonifiableSchemas, 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<Superclass> &
StaticMembers<typeof JsonifiableSchemas> StaticMembers<typeof JsonifiableSchemas>

View File

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