diff --git a/src/builders/ZodSchemaClassBuilder.ts b/src/builders/ZodSchemaClassBuilder.ts index 1ad8caa..0445816 100644 --- a/src/builders/ZodSchemaClassBuilder.ts +++ b/src/builders/ZodSchemaClassBuilder.ts @@ -4,10 +4,8 @@ import { JsonifiableObject } from "type-fest/source/jsonifiable" import { z } from "zod" import { ZodSchemaAbstractClass } from "../shapes/ZodSchemaClass" import { DejsonifiableZodSchemaObject } from "../traits/DejsonifiableZodSchemaObject" -import { ExtendableZodSchemaObject } from "../traits/ExtendableZodSchemaObject" -import { InstantiableZodSchemaObject } from "../traits/InstantiableZodSchemaObject" import { JsonifiableZodSchemaObject } from "../traits/JsonifiableZodSchemaObject" -import { ZodSchemaObjectInstantiationSchemas } from "../traits/ZodSchemaObjectInstantiationSchemas" +import { ZodSchemaObject } from "../traits/ZodSchemaObject" import { StaticMembers } from "../util" @@ -34,7 +32,7 @@ extends TraitExpressionBuilder { >( this: ZodSchemaClassBuilder, - props: { + { schema, schemaWithDefaultValues }: { schema: z.ZodObject schemaWithDefaultValues: ( @@ -42,10 +40,7 @@ extends TraitExpressionBuilder { ) => z.ZodObject }, ) { - class Schemas extends (this.expressionSuperclass as AbstractClass) { - static readonly schema = props.schema - static readonly schemaWithDefaultValues = props.schemaWithDefaultValues(props.schema) - + class ZodSchemaObjectConstructor extends (this.expressionSuperclass as AbstractClass) { constructor(values: Values) { super() Object.assign(this, values) @@ -53,21 +48,15 @@ extends TraitExpressionBuilder { } return new this.constructor( - Schemas as unknown as ( + ZodSchemaObjectConstructor as unknown as ( AbstractClass< InstanceType & Values, - ConstructorParameters + [values: Values] > & - StaticMembers & - StaticMembers + StaticMembers ), - [ - ...this.expressionTraits, - ZodSchemaObjectInstantiationSchemas, - InstantiableZodSchemaObject, - ExtendableZodSchemaObject, - ], + [...this.expressionTraits, ZodSchemaObject(schema, schemaWithDefaultValues)], ) } diff --git a/src/tests.ts b/src/tests.ts index 302199c..fa638e9 100644 --- a/src/tests.ts +++ b/src/tests.ts @@ -1,69 +1,24 @@ -import { Implements, expression } from "@thilawyn/traitify-ts" -import { Class } from "type-fest" +import { Implements } from "@thilawyn/traitify-ts" import { z } from "zod" -import { ZodSchemaObject } from "./traits/ZodSchemaObject" -import { StaticMembers } from "./util" +import { zodSchemaClass } from "./builders/ZodSchemaClassBuilder" +import { MobXObservableZodSchemaObject } from "./traits/MobXObservableZodSchemaObject" -// const exp = zodSchemaClass -// .schema({ -// schema: z.object({ -// /** User ID */ -// id: z.bigint(), +const exp = zodSchemaClass + .schema({ + schema: z.object({ + /** User ID */ + id: z.bigint(), -// /** Username */ -// name: z.string(), -// }), + /** Username */ + name: z.string(), + }), -// schemaWithDefaultValues: s => s.extend({ -// id: s.shape.id.default(-1n), -// }), -// }) -// .jsonifiable({ -// jsonifySchema: s => s.extend({ -// id: jsonify.bigint(s.shape.id) -// }), - -// dejsonifySchema: s => s.extend({ -// id: dejsonify.bigint(s.shape.id) -// }), -// }) -// .expresses(MobXObservableZodSchemaObject) -// .build() - -const schema = z.object({ - /** User ID */ - id: z.bigint(), - - /** Username */ - name: z.string(), -}) -const schemaWithDefaultsValues = schema.extend({ - id: schema.shape.id.default(-1n), -}) -class UserSchemas { - static readonly schema = schema - static readonly schemaWithDefaultValues = schemaWithDefaultsValues - - constructor(values: z.output) { - Object.assign(this, values) - } -} - -const exp = expression - .extends(UserSchemas as ( - Class> & - StaticMembers - )) - .expresses( - ZodSchemaObject( - schema, - - s => s.extend({ - id: s.shape.id.default(-1n), - }), - ) - ) + schemaWithDefaultValues: s => s.extend({ + id: s.shape.id.default(-1n), + }), + }) + .expresses(MobXObservableZodSchemaObject) .build() diff --git a/src/traits/InstantiableZodSchemaObject.ts b/src/traits/InstantiableZodSchemaObject.ts deleted file mode 100644 index 1817ab6..0000000 --- a/src/traits/InstantiableZodSchemaObject.ts +++ /dev/null @@ -1,90 +0,0 @@ -import { TraitStaticMembers, trait } from "@thilawyn/traitify-ts" -import { HasRequiredKeys } from "type-fest" -import { z } from "zod" -import { ZodSchemaClass } from "../shapes/ZodSchemaClass" -import { parseZodTypeEffect } from "../util" -import { ZodSchemaObjectInstantiationSchemas } from "./ZodSchemaObjectInstantiationSchemas" - - -type ZodSchemaObjectInstantiationSchemasStaticMembers = TraitStaticMembers - -type NewZodSchemaInstanceInput< - Values extends object, - DefaultValues extends Partial, -> = { - [Key in Exclude]: Values[Key] -} & { - [Key in keyof DefaultValues]?: Key extends keyof Values - ? Values[Key] - : never -} - -type ParseParamsArgs = [] | [params: Partial] - -type NewZodSchemaInstanceArgs = ( - HasRequiredKeys extends true - ? [values: Input, ...args: ParseParamsArgs] - : [] | [values: Input, ...args: ParseParamsArgs] -) - - -export const InstantiableZodSchemaObject = trait - .implement(Super => class InstantiableZodSchemaObject extends Super { - static create< - Instance extends Values, - - SchemaWithDefaultValuesT extends z.ZodRawShape, - SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam, - SchemaWithDefaultValuesCatchall extends z.ZodTypeAny, - - Values extends object, - PartialValues extends Partial, - >( - this: ( - ZodSchemaClass & - ZodSchemaObjectInstantiationSchemasStaticMembers - ), - ...[values, params]: NewZodSchemaInstanceArgs - ) { - return this.instantiationSchemaWithDefaultValues().parse(values, params) - } - - static async createPromise< - Instance extends Values, - - SchemaWithDefaultValuesT extends z.ZodRawShape, - SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam, - SchemaWithDefaultValuesCatchall extends z.ZodTypeAny, - - Values extends object, - PartialValues extends Partial, - >( - this: ( - ZodSchemaClass & - ZodSchemaObjectInstantiationSchemasStaticMembers - ), - ...[values, params]: NewZodSchemaInstanceArgs - ) { - return this.instantiationSchemaWithDefaultValues().parseAsync(values, params) - } - - static createEffect< - Instance extends Values, - - SchemaWithDefaultValuesT extends z.ZodRawShape, - SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam, - SchemaWithDefaultValuesCatchall extends z.ZodTypeAny, - - Values extends object, - PartialValues extends Partial, - >( - this: ( - ZodSchemaClass & - ZodSchemaObjectInstantiationSchemasStaticMembers - ), - ...[values, params]: NewZodSchemaInstanceArgs - ) { - return parseZodTypeEffect(this.instantiationSchemaWithDefaultValues(), values, params) - } - }) - .build() diff --git a/src/traits/ZodSchemaObjectInstantiationSchemas.ts b/src/traits/ZodSchemaObjectInstantiationSchemas.ts deleted file mode 100644 index 4af87cb..0000000 --- a/src/traits/ZodSchemaObjectInstantiationSchemas.ts +++ /dev/null @@ -1,37 +0,0 @@ -import { trait } from "@thilawyn/traitify-ts" -import { z } from "zod" -import { ZodSchemaClass } from "../shapes/ZodSchemaClass" - - -export const ZodSchemaObjectInstantiationSchemas = trait - .implement(Super => class ZodSchemaObjectInstantiationSchemas extends Super { - static instantiationSchema< - Instance extends Values, - - SchemaT extends z.ZodRawShape, - SchemaUnknownKeys extends z.UnknownKeysParam, - SchemaCatchall extends z.ZodTypeAny, - - Values extends object, - >( - this: ZodSchemaClass, - ) { - return this.schema.transform(values => new this(values)) - } - - static instantiationSchemaWithDefaultValues< - Instance extends Values, - - SchemaWithDefaultValuesT extends z.ZodRawShape, - SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam, - SchemaWithDefaultValuesCatchall extends z.ZodTypeAny, - - Values extends object, - PartialValues extends Partial, - >( - this: ZodSchemaClass, - ) { - return this.schemaWithDefaultValues.transform(values => new this(values)) - } - }) - .build()