diff --git a/src/traits/ZodSchemaObject.ts b/src/traits/ZodSchemaObject.ts index be4daf9..48b2f44 100644 --- a/src/traits/ZodSchemaObject.ts +++ b/src/traits/ZodSchemaObject.ts @@ -35,22 +35,19 @@ export const ZodSchemaObject = < static readonly schema = schema static readonly schemaWithDefaultsValues = schemaWithDefaultValues(schema) - static instantiationSchema< - Self extends StaticMembers>, - Instance extends Values, - >( - this: Class & Self - ) { - return this.schema.transform(values => new this(values)) - } + static instantiationTransform< + Instance extends Values, - static instantiationSchemaWithDefaultValues< - Self extends StaticMembers>, - Instance extends Values, + T extends z.ZodRawShape, + UnknownKeys extends z.UnknownKeysParam, + Catchall extends z.ZodTypeAny, + Output extends Values, + Input, >( - this: Class & Self + this: Class, + schema: z.ZodObject, ) { - return this.schemaWithDefaultsValues.transform(values => new this(values)) + return schema.transform(values => new this(values)) } @@ -61,7 +58,9 @@ export const ZodSchemaObject = < this: Class & Self, ...[values, params]: NewZodSchemaInstanceArgs ) { - return this.instantiationSchemaWithDefaultValues().parse(values, params) + return this + .instantiationTransform(this.schemaWithDefaultsValues) + .parse(values, params) } static createPromise< @@ -71,7 +70,9 @@ export const ZodSchemaObject = < this: Class & Self, ...[values, params]: NewZodSchemaInstanceArgs ) { - return this.instantiationSchemaWithDefaultValues().parseAsync(values, params) + return this + .instantiationTransform(this.schemaWithDefaultsValues) + .parseAsync(values, params) } static createEffect< @@ -81,7 +82,11 @@ export const ZodSchemaObject = < this: Class & Self, ...[values, params]: NewZodSchemaInstanceArgs ) { - return parseZodSchemaEffect(this.instantiationSchemaWithDefaultValues(), values, params) + return parseZodSchemaEffect( + this.instantiationTransform(this.schemaWithDefaultsValues), + values, + params, + ) } }) .build()