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

This commit is contained in:
Julien Valverdé
2024-03-15 03:36:14 +01:00
parent 272a382887
commit d0c380d19e
2 changed files with 65 additions and 61 deletions

View File

@@ -31,6 +31,6 @@ export function ZodSchemaClass<
} }
return expression return expression
.extends(ZodSchemaObjectConstructor as AbstractClass<Values, [values: Values]>) .extends(ZodSchemaObjectConstructor)
.expresses(ZodSchemaObject(schema, schemaWithDefaultValues)) .expresses(ZodSchemaObject(schema, schemaWithDefaultValues))
} }

View File

@@ -1,7 +1,7 @@
import { ImplStatic, trait } from "@thilawyn/traitify-ts" import { ImplStatic, 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 { parseZodSchemaEffect } from "../util" import { StaticMembers, parseZodSchemaEffect } from "../util"
type CreateArgs<Input extends object> = ( type CreateArgs<Input extends object> = (
@@ -23,69 +23,73 @@ export const ZodSchemaObject = <
schema: z.ZodObject<SchemaT, "strip", SchemaCatchall, Values, Values>, schema: z.ZodObject<SchemaT, "strip", SchemaCatchall, Values, Values>,
schemaWithDefaultValues: z.ZodObject<SchemaWithDefaultValuesT, "strip", SchemaWithDefaultValuesCatchall, Values, PartialValues>, schemaWithDefaultValues: z.ZodObject<SchemaWithDefaultValuesT, "strip", SchemaWithDefaultValuesCatchall, Values, PartialValues>,
) => trait ) => trait
.implement(Super => class ZodSchemaObject extends Super { .implement(Super => {
static readonly schema = schema class ZodSchemaObject extends Super {
static readonly schemaWithDefaultValues = schemaWithDefaultValues static readonly schema = schema
static readonly schemaWithDefaultValues = schemaWithDefaultValues
static transform< static transform<
Instance extends Values, Instance extends Values,
T extends z.ZodRawShape, T extends z.ZodRawShape,
UnknownKeys extends z.UnknownKeysParam, UnknownKeys extends z.UnknownKeysParam,
Catchall extends z.ZodTypeAny, Catchall extends z.ZodTypeAny,
Output extends Values, Output extends Values,
Input, Input,
>( >(
this: Class<Instance, [values: Values]>, this: Class<Instance, [values: Values]>,
schema: z.ZodObject<T, UnknownKeys, Catchall, Output, Input>, schema: z.ZodObject<T, UnknownKeys, Catchall, Output, Input>,
) { ) {
return schema.transform(values => new this(values)) return schema.transform(values => new this(values))
}
static create<
Instance extends Values
>(
this: (
Class<Instance, [values: Values]> &
ImplStatic<typeof ZodSchemaObject>
),
...[values, params]: CreateArgs<PartialValues>
) {
return this
.transform(this.schemaWithDefaultValues)
.parse(values, params)
}
static createPromise<
Instance extends Values
>(
this: (
Class<Instance, [values: Values]> &
ImplStatic<typeof ZodSchemaObject>
),
...[values, params]: CreateArgs<PartialValues>
) {
return this
.transform(this.schemaWithDefaultValues)
.parseAsync(values, params)
}
static createEffect<
Instance extends Values
>(
this: (
Class<Instance, [values: Values]> &
ImplStatic<typeof ZodSchemaObject>
),
...[values, params]: CreateArgs<PartialValues>
) {
return parseZodSchemaEffect(
this.transform(this.schemaWithDefaultValues),
values,
params,
)
}
} }
return ZodSchemaObject as Class<ZodSchemaObject & Values> & StaticMembers<typeof ZodSchemaObject>
static create<
Instance extends Values
>(
this: (
Class<Instance, [values: Values]> &
ImplStatic<typeof ZodSchemaObject>
),
...[values, params]: CreateArgs<PartialValues>
) {
return this
.transform(this.schemaWithDefaultValues)
.parse(values, params)
}
static createPromise<
Instance extends Values
>(
this: (
Class<Instance, [values: Values]> &
ImplStatic<typeof ZodSchemaObject>
),
...[values, params]: CreateArgs<PartialValues>
) {
return this
.transform(this.schemaWithDefaultValues)
.parseAsync(values, params)
}
static createEffect<
Instance extends Values
>(
this: (
Class<Instance, [values: Values]> &
ImplStatic<typeof ZodSchemaObject>
),
...[values, params]: CreateArgs<PartialValues>
) {
return parseZodSchemaEffect(
this.transform(this.schemaWithDefaultValues),
values,
params,
)
}
}) })
.build() .build()