diff --git a/src/ZodSchemaClass.ts b/src/ZodSchemaClass.ts index 2791794..94b84e5 100644 --- a/src/ZodSchemaClass.ts +++ b/src/ZodSchemaClass.ts @@ -11,23 +11,14 @@ class ZodSchemaObjectConstructor { 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, + T extends z.ZodRawShape, + Catchall extends z.ZodTypeAny, + Values extends object, + PartialValues extends Partial, >( - props: { - schema: z.ZodObject - - schemaWithDefaultValues: ( - schema: z.ZodObject - ) => z.ZodObject - } + schema: z.ZodObject ) { return expression .extends(ZodSchemaObjectConstructor) - .expresses(ZodSchemaObject(props.schema, props.schemaWithDefaultValues(props.schema))) + .expresses(ZodSchemaObject(schema)) } diff --git a/src/tests.ts b/src/tests.ts index 2caa7d2..42f4ca7 100644 --- a/src/tests.ts +++ b/src/tests.ts @@ -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() diff --git a/src/traits/ZodSchemaObject.ts b/src/traits/ZodSchemaObject.ts index 4ccb7d7..e5ddb0c 100644 --- a/src/traits/ZodSchemaObject.ts +++ b/src/traits/ZodSchemaObject.ts @@ -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 = ( @@ -12,33 +12,29 @@ type CreateArgs = ( 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, + T extends z.ZodRawShape, + Catchall extends z.ZodTypeAny, + Values extends object, + PartialValues extends Partial, >( - schema: z.ZodObject, - schemaWithDefaultValues: z.ZodObject, + schemaWithDefaults: z.ZodObject, ) => 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, - schema: z.ZodObject, + schema: z.ZodObject, ) { return schema.transform(values => new this(values)) } @@ -54,7 +50,7 @@ export const ZodSchemaObject = < ...[values, params]: CreateArgs ) { return this - .transform(this.schemaWithDefaultValues) + .transform(this.schemaWithDefaults) .parse(values, params) } @@ -68,7 +64,7 @@ export const ZodSchemaObject = < ...[values, params]: CreateArgs ) { return this - .transform(this.schemaWithDefaultValues) + .transform(this.schemaWithDefaults) .parseAsync(values, params) } @@ -82,7 +78,7 @@ export const ZodSchemaObject = < ...[values, params]: CreateArgs ) { 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, + T extends z.ZodRawShape, + Catchall extends z.ZodTypeAny, + Values extends object, + PartialValues extends Partial, > = ( ReturnType< - typeof ZodSchemaObject< - SchemaT, - SchemaCatchall, - SchemaWithDefaultValuesT, - SchemaWithDefaultValuesCatchall, - - Values, - PartialValues - > + typeof ZodSchemaObject > )