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

This commit is contained in:
Julien Valverdé
2024-03-16 01:48:33 +01:00
parent d7c8fd1a2d
commit a985d14bea
3 changed files with 34 additions and 62 deletions

View File

@@ -11,23 +11,14 @@ class ZodSchemaObjectConstructor<Values> {
export function ZodSchemaClass<
SchemaT extends z.ZodRawShape,
SchemaCatchall extends z.ZodTypeAny,
SchemaWithDefaultValuesT extends z.ZodRawShape,
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
Values extends object,
PartialValues extends Partial<Values>,
T extends z.ZodRawShape,
Catchall extends z.ZodTypeAny,
Values extends object,
PartialValues extends Partial<Values>,
>(
props: {
schema: z.ZodObject<SchemaT, "strip", SchemaCatchall, Values, Values>
schemaWithDefaultValues: (
schema: z.ZodObject<SchemaT, "strip", SchemaCatchall, Values, Values>
) => z.ZodObject<SchemaWithDefaultValuesT, "strip", SchemaWithDefaultValuesCatchall, Values, PartialValues>
}
schema: z.ZodObject<T, "strip", Catchall, Values, PartialValues>
) {
return expression
.extends(ZodSchemaObjectConstructor<Values>)
.expresses(ZodSchemaObject(props.schema, props.schemaWithDefaultValues(props.schema)))
.expresses(ZodSchemaObject(schema))
}

View File

@@ -31,19 +31,15 @@ import { MobXObservableZodSchemaObject } from "./traits/MobXObservableZodSchemaO
// .expresses(MobXObservableZodSchemaObject)
// .build()
const exp = ZodSchemaClass({
schema: z.object({
const exp = ZodSchemaClass(
z.object({
/** User ID */
id: z.bigint(),
id: z.bigint().default(-1n),
/** Username */
name: z.string(),
}),
schemaWithDefaultValues: s => s.extend({
id: s.shape.id.default(-1n),
}),
})
})
)
.expresses(MobXObservableZodSchemaObject)
.build()

View File

@@ -1,7 +1,7 @@
import { ImplStatic, trait } from "@thilawyn/traitify-ts"
import { Class, HasRequiredKeys } from "type-fest"
import { z } from "zod"
import { StaticMembers, parseZodSchemaEffect } from "../util"
import { StaticMembers, parseZodSchemaEffect, stripZodObjectDefaults } from "../util"
type CreateArgs<Input extends object> = (
@@ -12,33 +12,29 @@ type CreateArgs<Input extends object> = (
export const ZodSchemaObject = <
SchemaT extends z.ZodRawShape,
SchemaCatchall extends z.ZodTypeAny,
SchemaWithDefaultValuesT extends z.ZodRawShape,
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
Values extends object,
PartialValues extends Partial<Values>,
T extends z.ZodRawShape,
Catchall extends z.ZodTypeAny,
Values extends object,
PartialValues extends Partial<Values>,
>(
schema: z.ZodObject<SchemaT, "strip", SchemaCatchall, Values, Values>,
schemaWithDefaultValues: z.ZodObject<SchemaWithDefaultValuesT, "strip", SchemaWithDefaultValuesCatchall, Values, PartialValues>,
schemaWithDefaults: z.ZodObject<T, "strip", Catchall, Values, PartialValues>,
) => trait
.implement(Super => {
class ZodSchemaObject extends Super {
static readonly schema = schema
static readonly schemaWithDefaultValues = schemaWithDefaultValues
static readonly schema = stripZodObjectDefaults(schemaWithDefaults)
static readonly schemaWithDefaults = schemaWithDefaults
static transform<
Instance extends Values,
Instance extends Values,
T extends z.ZodRawShape,
UnknownKeys extends z.UnknownKeysParam,
Catchall extends z.ZodTypeAny,
Output extends Values,
Input,
TransformT extends z.ZodRawShape,
TransformUnknownKeys extends z.UnknownKeysParam,
TransformCatchall extends z.ZodTypeAny,
TransformOutput extends Values,
TransformInput,
>(
this: Class<Instance, [values: Values]>,
schema: z.ZodObject<T, UnknownKeys, Catchall, Output, Input>,
schema: z.ZodObject<TransformT, TransformUnknownKeys, TransformCatchall, TransformOutput, TransformInput>,
) {
return schema.transform(values => new this(values))
}
@@ -54,7 +50,7 @@ export const ZodSchemaObject = <
...[values, params]: CreateArgs<PartialValues>
) {
return this
.transform(this.schemaWithDefaultValues)
.transform(this.schemaWithDefaults)
.parse(values, params)
}
@@ -68,7 +64,7 @@ export const ZodSchemaObject = <
...[values, params]: CreateArgs<PartialValues>
) {
return this
.transform(this.schemaWithDefaultValues)
.transform(this.schemaWithDefaults)
.parseAsync(values, params)
}
@@ -82,7 +78,7 @@ export const ZodSchemaObject = <
...[values, params]: CreateArgs<PartialValues>
) {
return parseZodSchemaEffect(
this.transform(this.schemaWithDefaultValues),
this.transform(this.schemaWithDefaults),
values,
params,
)
@@ -95,23 +91,12 @@ export const ZodSchemaObject = <
export type ZodSchemaObjectTrait<
SchemaT extends z.ZodRawShape,
SchemaCatchall extends z.ZodTypeAny,
SchemaWithDefaultValuesT extends z.ZodRawShape,
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
Values extends object,
PartialValues extends Partial<Values>,
T extends z.ZodRawShape,
Catchall extends z.ZodTypeAny,
Values extends object,
PartialValues extends Partial<Values>,
> = (
ReturnType<
typeof ZodSchemaObject<
SchemaT,
SchemaCatchall,
SchemaWithDefaultValuesT,
SchemaWithDefaultValuesCatchall,
Values,
PartialValues
>
typeof ZodSchemaObject<T, Catchall, Values, PartialValues>
>
)