This commit is contained in:
@@ -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: {
|
||||
schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>
|
||||
shape: SchemaT
|
||||
}) => z.ZodObject<JsonifySchemaT, JsonifySchemaUnknownKeys, JsonifySchemaCatchall, JsonifiedValues, Values>
|
||||
jsonifySchema: (
|
||||
schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>
|
||||
) => z.ZodObject<JsonifySchemaT, JsonifySchemaUnknownKeys, JsonifySchemaCatchall, JsonifiedValues, Values>
|
||||
|
||||
dejsonifySchema: (props: {
|
||||
schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>
|
||||
shape: SchemaT
|
||||
}) => z.ZodObject<DejsonifySchemaT, DejsonifySchemaUnknownKeys, DejsonifySchemaCatchall, Values, JsonifiedValues>
|
||||
dejsonifySchema: (
|
||||
schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>
|
||||
) => 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>
|
||||
|
||||
10
src/tests.ts
10
src/tests.ts
@@ -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({
|
||||
|
||||
Reference in New Issue
Block a user