Static trait refactoring
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Julien Valverdé
2024-03-22 22:12:23 +01:00
parent 94bb187219
commit 1910c9ff9f
3 changed files with 36 additions and 48 deletions

View File

@@ -1,4 +1,4 @@
import { ImplStatic, expression } from "@thilawyn/traitify-ts"
import { ImplStatic, expression, implStaticThis } from "@thilawyn/traitify-ts"
import { AbstractClass } from "type-fest"
import { z } from "zod"
import { Extend, StaticMembers } from "../util"
@@ -18,8 +18,7 @@ export const ExtendableZodSchemaObject = <
.subtrait()
.implement(Super => class ExtendableZodSchemaObjectImpl extends Super {
static extend<
Self extends AbstractClass<ExtendableZodSchemaObjectImpl & Values, [values: Values]>
& ImplStatic<typeof ExtendableZodSchemaObjectImpl>,
Self extends AbstractClass<Values, [values: Values]>,
ExtendedT extends z.ZodRawShape,
ExtendedCatchall extends z.ZodTypeAny,
@@ -29,12 +28,13 @@ export const ExtendableZodSchemaObject = <
this: Self,
schemaWithDefaults: (
schemaWithDefaults: typeof this.schemaWithDefaults
schemaWithDefaults: z.ZodObject<T, "strip", Catchall, Values, PartialValues>
) => z.ZodObject<ExtendedT, "strip", ExtendedCatchall, ExtendedValues, ExtendedPartialValues>,
) {
const t = implStaticThis(ExtendableZodSchemaObjectImpl, this)
return expression
.extends(
this as unknown as (
t as unknown as (
AbstractClass<
Omit<
Extend<[InstanceType<Self>, ExtendedValues]>,
@@ -45,7 +45,7 @@ export const ExtendableZodSchemaObject = <
Omit<StaticMembers<Self>, keyof ImplStatic<typeof ExtendableZodSchemaObjectImpl>>
)
)
.expresses(ExtendableZodSchemaObject(schemaWithDefaults(this.schemaWithDefaults)))
.expresses(ExtendableZodSchemaObject(schemaWithDefaults(t.schemaWithDefaults)))
}
})
.build()

View File

@@ -1,4 +1,4 @@
import { ImplStatic, TraitInstance, TraitStaticMembers, trait } from "@thilawyn/traitify-ts"
import { TraitInstance, TraitStaticMembers, implStaticInstantiableThis, trait } from "@thilawyn/traitify-ts"
import { Class, Jsonifiable } from "type-fest"
import { z } from "zod"
import { parseZodSchemaEffect } from "../util"
@@ -80,45 +80,39 @@ export const JsonifiedZodSchemaObject = <
static jsonify<
Instance extends JsonifiedValues
>(
this: (
Class<Instance, [values: JsonifiedValues]> &
ImplStatic<typeof JsonifiedZodSchemaObjectImpl>
),
values: Values,
params?: Partial<z.ParseParams>,
this: Class<Instance, [values: JsonifiedValues]>,
values: Values,
params?: Partial<z.ParseParams>,
) {
return this
.pipeSchemaIntoInstance(this.jsonifySchema)
const t = implStaticInstantiableThis(JsonifiedZodSchemaObjectImpl, this)
return t
.pipeSchemaIntoInstance(t.jsonifySchema)
.parse(values, params)
}
static jsonifyPromise<
Instance extends JsonifiedValues
>(
this: (
Class<Instance, [values: JsonifiedValues]> &
ImplStatic<typeof JsonifiedZodSchemaObjectImpl>
),
values: Values,
params?: Partial<z.ParseParams>,
this: Class<Instance, [values: JsonifiedValues]>,
values: Values,
params?: Partial<z.ParseParams>,
) {
return this
.pipeSchemaIntoInstance(this.jsonifySchema)
const t = implStaticInstantiableThis(JsonifiedZodSchemaObjectImpl, this)
return t
.pipeSchemaIntoInstance(t.jsonifySchema)
.parseAsync(values, params)
}
static jsonifyEffect<
Instance extends JsonifiedValues
>(
this: (
Class<Instance, [values: JsonifiedValues]> &
ImplStatic<typeof JsonifiedZodSchemaObjectImpl>
),
values: Values,
params?: Partial<z.ParseParams>,
this: Class<Instance, [values: JsonifiedValues]>,
values: Values,
params?: Partial<z.ParseParams>,
) {
const t = implStaticInstantiableThis(JsonifiedZodSchemaObjectImpl, this)
return parseZodSchemaEffect(
this.pipeSchemaIntoInstance(this.jsonifySchema),
t.pipeSchemaIntoInstance(t.jsonifySchema),
values,
params,
)

View File

@@ -1,4 +1,4 @@
import { ImplStatic, trait } from "@thilawyn/traitify-ts"
import { implStaticInstantiableThis, trait } from "@thilawyn/traitify-ts"
import { Class, HasRequiredKeys } from "type-fest"
import { z } from "zod"
import { parseZodSchemaEffect, stripZodObjectDefaults } from "../util"
@@ -59,42 +59,36 @@ export const ZodSchemaObject = <
static create<
Instance extends Values
>(
this: (
Class<Instance, [values: Values]> &
ImplStatic<typeof ZodSchemaObjectImpl>
),
this: Class<Instance, [values: Values]>,
...[values, params]: CreateArgs<PartialValues>
) {
return this
.pipeSchemaIntoInstance(this.schemaWithDefaults)
const t = implStaticInstantiableThis(ZodSchemaObjectImpl, this)
return t
.pipeSchemaIntoInstance(t.schemaWithDefaults)
.parse(values, params)
}
static createPromise<
Instance extends Values
>(
this: (
Class<Instance, [values: Values]> &
ImplStatic<typeof ZodSchemaObjectImpl>
),
this: Class<Instance, [values: Values]>,
...[values, params]: CreateArgs<PartialValues>
) {
return this
.pipeSchemaIntoInstance(this.schemaWithDefaults)
const t = implStaticInstantiableThis(ZodSchemaObjectImpl, this)
return t
.pipeSchemaIntoInstance(t.schemaWithDefaults)
.parseAsync(values, params)
}
static createEffect<
Instance extends Values
>(
this: (
Class<Instance, [values: Values]> &
ImplStatic<typeof ZodSchemaObjectImpl>
),
this: Class<Instance, [values: Values]>,
...[values, params]: CreateArgs<PartialValues>
) {
const t = implStaticInstantiableThis(ZodSchemaObjectImpl, this)
return parseZodSchemaEffect(
this.pipeSchemaIntoInstance(this.schemaWithDefaults),
t.pipeSchemaIntoInstance(t.schemaWithDefaults),
values,
params,
)