0.1.2 #3

Merged
Thilawyn merged 136 commits from next into master 2024-03-11 19:44:21 +01:00
2 changed files with 28 additions and 13 deletions
Showing only changes of commit c3c454eb15 - Show all commits

View File

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

View File

@@ -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. * @param args - The arguments to pass to the `safeParseAsync` method of the schema.
* @returns An Effect monad representing the parsing result. * @returns An Effect monad representing the parsing result.
*/ */
export const parseZodTypeEffect = < export const parseZodSchemaEffect = <Output, Input>(
Output,
Input,
>(
schema: z.ZodType<Output, z.ZodTypeDef, Input>, schema: z.ZodType<Output, z.ZodTypeDef, Input>,
...args: Parameters<typeof schema.safeParseAsync> ...args: Parameters<typeof schema.safeParseAsync>
) => pipe( ) => pipe(