ExtendableZodSchemaObject
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Julien Valverdé
2024-02-25 04:45:58 +01:00
parent e7c65c0a07
commit bf429fc63b
3 changed files with 25 additions and 53 deletions

View File

@@ -56,4 +56,9 @@ export class ZodSchemaClassExtender<Superclass extends AbstractClass<object>> {
]> ]>
) )
} }
build() {
return this.superclass
}
} }

View File

@@ -38,13 +38,22 @@ const inst = User.create({ id: 1n, name: "" })
const jsonifiedUser = await inst.jsonifyPromise() const jsonifiedUser = await inst.jsonifyPromise()
class SubTest extends User.extend({ const extended = User.extend()
schema: ({ schema }) => schema.extend({ .schema({
prout: z.string() schema: s => s.extend({
}), name: z.literal("Admin")
}),
defaultValues: v => v,
})
defaultValues: v => v,
}) {} class SubTest extends User.extend()
.schema({
schema: s => s.extend({}),
defaultValues: v => v,
})
.build()
{}
const subInst = await SubTest.createPromise({ name: "", prout: "" }) const subInst = await SubTest.createPromise({ name: "", prout: "" })
// console.log(subInst) // console.log(subInst)

View File

@@ -1,58 +1,16 @@
import { trait } from "@thilawyn/traitify-ts" import { trait } from "@thilawyn/traitify-ts"
import { AbstractClass } from "type-fest" import { ZodSchemaClassExtender } from "../builders/ZodSchemaClassExtender"
import { z } from "zod" import { ZodSchemaClass } from "../shapes/ZodSchemaClass"
import { ZodSchemaAbstractClass } from "../shapes/ZodSchemaClass"
import { Extend, StaticMembers } from "../util"
export const ExtendableZodSchemaObject = trait export const ExtendableZodSchemaObject = trait
.implement(Super => class ExtendableZodSchemaObject extends Super { .implement(Super => class ExtendableZodSchemaObject extends Super {
static extend< static extend<
Super extends ZodSchemaAbstractClass<SuperInstance, SuperSchemaT, SuperSchemaUnknownKeys, SuperSchemaCatchall, SuperValues, SuperDefaultValues>, Self extends ZodSchemaClass<any, any, any, any, any, any>,
SuperInstance extends SuperValues,
SuperSchemaT extends z.ZodRawShape,
SuperSchemaUnknownKeys extends z.UnknownKeysParam,
SuperSchemaCatchall extends z.ZodTypeAny,
SuperValues extends object,
SuperDefaultValues extends Partial<SuperValues>,
SchemaT extends z.ZodRawShape,
SchemaUnknownKeys extends z.UnknownKeysParam,
SchemaCatchall extends z.ZodTypeAny,
Values extends SuperValues,
DefaultValues extends Partial<Values>,
>( >(
this: Super | ZodSchemaAbstractClass<SuperInstance, SuperSchemaT, SuperSchemaUnknownKeys, SuperSchemaCatchall, SuperValues, SuperDefaultValues>, this: Self
props: {
schema: (props: {
schema: Super["schema"]
shape: Super["schema"]["shape"]
}) => z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>
defaultValues: (defaultValues: SuperDefaultValues) => DefaultValues
},
): (
AbstractClass<
Extend<[SuperInstance, Values]>,
[values: Values]
> &
Extend<[
StaticMembers<Super>,
{
readonly schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>,
readonly defaultValues: DefaultValues,
},
]>
) { ) {
const schema = props.schema({ schema: this.schema, shape: this.schema.shape }) return new ZodSchemaClassExtender(this)
const defaultValues = props.defaultValues(this.defaultValues)
return class extends this {
static readonly schema = schema
static readonly defaultValues = defaultValues
} as any
} }
}) })
.build() .build()