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

This commit is contained in:
Julien Valverdé
2024-02-28 02:57:30 +01:00
parent d204b71a05
commit c3c454eb15
2 changed files with 28 additions and 13 deletions

View File

@@ -1,7 +1,7 @@
import { ImplStaticThis, trait } from "@thilawyn/traitify-ts"
import { Class, HasRequiredKeys } from "type-fest"
import { z } from "zod"
import { StaticMembers } from "../util"
import { StaticMembers, parseZodSchemaEffect } from "../util"
type ParseParamsArgs = [] | [params: Partial<z.ParseParams>]
@@ -50,20 +50,38 @@ export const ZodSchemaObject = <
>(
this: Class<Instance, [values: Values]> & Self
) {
return this.schema.transform(values => new this(values))
return this.schemaWithDefaultsValues.transform(values => new this(values))
}
static create<
Instance extends Values
Self extends StaticMembers<ImplStaticThis<typeof ZodSchemaObject>>,
Instance extends Values,
>(
this: (
Class<Instance, [values: Values]> &
StaticMembers<ImplStaticThis<typeof ZodSchemaObject>>
),
this: Class<Instance, [values: Values]> & Self,
...[values, params]: NewZodSchemaInstanceArgs<PartialValues>
) {
return this.instantiationSchemaWithDefaultValues().parse(values, params)
}
static createPromise<
Self extends StaticMembers<ImplStaticThis<typeof ZodSchemaObject>>,
Instance extends Values,
>(
this: Class<Instance, [values: Values]> & Self,
...[values, params]: NewZodSchemaInstanceArgs<PartialValues>
) {
return this.instantiationSchemaWithDefaultValues().parseAsync(values, params)
}
static createEffect<
Self extends StaticMembers<ImplStaticThis<typeof ZodSchemaObject>>,
Instance extends Values,
>(
this: Class<Instance, [values: Values]> & Self,
...[values, params]: NewZodSchemaInstanceArgs<PartialValues>
) {
return parseZodSchemaEffect(this.instantiationSchemaWithDefaultValues(), values, params)
}
})
.build()