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

This commit is contained in:
Julien Valverdé
2024-02-28 04:50:55 +01:00
parent 98ae430249
commit 69d47ba3cf

View File

@@ -35,22 +35,19 @@ export const ZodSchemaObject = <
static readonly schema = schema
static readonly schemaWithDefaultsValues = schemaWithDefaultValues(schema)
static instantiationSchema<
Self extends StaticMembers<ImplStaticThis<typeof ZodSchemaObject>>,
static instantiationTransform<
Instance extends Values,
>(
this: Class<Instance, [values: Values]> & Self
) {
return this.schema.transform(values => new this(values))
}
static instantiationSchemaWithDefaultValues<
Self extends StaticMembers<ImplStaticThis<typeof ZodSchemaObject>>,
Instance extends Values,
T extends z.ZodRawShape,
UnknownKeys extends z.UnknownKeysParam,
Catchall extends z.ZodTypeAny,
Output extends Values,
Input,
>(
this: Class<Instance, [values: Values]> & Self
this: Class<Instance, [values: Values]>,
schema: z.ZodObject<T, UnknownKeys, Catchall, Output, Input>,
) {
return this.schemaWithDefaultsValues.transform(values => new this(values))
return schema.transform(values => new this(values))
}
@@ -61,7 +58,9 @@ export const ZodSchemaObject = <
this: Class<Instance, [values: Values]> & Self,
...[values, params]: NewZodSchemaInstanceArgs<PartialValues>
) {
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<Instance, [values: Values]> & Self,
...[values, params]: NewZodSchemaInstanceArgs<PartialValues>
) {
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<Instance, [values: Values]> & Self,
...[values, params]: NewZodSchemaInstanceArgs<PartialValues>
) {
return parseZodSchemaEffect(this.instantiationSchemaWithDefaultValues(), values, params)
return parseZodSchemaEffect(
this.instantiationTransform(this.schemaWithDefaultsValues),
values,
params,
)
}
})
.build()