diff --git a/src/tests.ts b/src/tests.ts index 2b5265a..302199c 100644 --- a/src/tests.ts +++ b/src/tests.ts @@ -1,45 +1,75 @@ -import { Implements } from "@thilawyn/traitify-ts" +import { Implements, expression } from "@thilawyn/traitify-ts" +import { Class } from "type-fest" import { z } from "zod" -import { zodSchemaClass } from "./builders/ZodSchemaClassBuilder" -import { dejsonify, jsonify } from "./schema/jsonify" -import { MobXObservableZodSchemaObject } from "./traits/MobXObservableZodSchemaObject" +import { ZodSchemaObject } from "./traits/ZodSchemaObject" +import { StaticMembers } from "./util" -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) - }), +// 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) +// 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), + }), + ) + ) .build() + @exp.staticImplements class User extends exp.extends implements Implements {} -const test = User.schema.extend({ - prout: z.string() -}).merge( - User.schemaWithDefaultValues -) - const inst = User.create({ id: 1n, name: "User" }) // console.log(inst.name) const instEffect = User.createEffect({ id: 1n, name: "User" }) diff --git a/src/traits/ZodSchemaObject.ts b/src/traits/ZodSchemaObject.ts index 20d5fba..b39d34c 100644 --- a/src/traits/ZodSchemaObject.ts +++ b/src/traits/ZodSchemaObject.ts @@ -36,23 +36,19 @@ export const ZodSchemaObject = < static readonly schemaWithDefaultsValues = schemaWithDefaultValues(schema) static instantiationSchema< - Instance extends Values + Self extends StaticMembers>, + Instance extends Values, >( - this: ( - Class & - StaticMembers> - ) + this: Class & Self ) { return this.schema.transform(values => new this(values)) } static instantiationSchemaWithDefaultValues< - Instance extends Values + Self extends StaticMembers>, + Instance extends Values, >( - this: ( - Class & - StaticMembers> - ) + this: Class & Self ) { return this.schema.transform(values => new this(values)) } @@ -67,7 +63,7 @@ export const ZodSchemaObject = < ), ...[values, params]: NewZodSchemaInstanceArgs ) { - return this.instantiationSchema().parse(values, params) + return this.instantiationSchemaWithDefaultValues().parse(values, params) } }) .build()