This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -3,16 +3,13 @@ import { z } from "zod"
|
||||
|
||||
|
||||
/**
|
||||
* Parses a value using a ZodType schema wrapped in an Effect monad.
|
||||
* Parses a value using a Zod schema wrapped in an Effect monad.
|
||||
*
|
||||
* @param schema - The ZodType schema to use for parsing.
|
||||
* @param schema - The Zod schema to use for parsing.
|
||||
* @param args - The arguments to pass to the `safeParseAsync` method of the schema.
|
||||
* @returns An Effect monad representing the parsing result.
|
||||
*/
|
||||
export const parseZodTypeEffect = <
|
||||
Output,
|
||||
Input,
|
||||
>(
|
||||
export const parseZodSchemaEffect = <Output, Input>(
|
||||
schema: z.ZodType<Output, z.ZodTypeDef, Input>,
|
||||
...args: Parameters<typeof schema.safeParseAsync>
|
||||
) => pipe(
|
||||
|
||||
Reference in New Issue
Block a user