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

This commit is contained in:
Julien Valverdé
2024-02-28 02:44:02 +01:00
parent 1956aae555
commit d204b71a05
2 changed files with 68 additions and 42 deletions

View File

@@ -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 { z } from "zod"
import { zodSchemaClass } from "./builders/ZodSchemaClassBuilder" import { ZodSchemaObject } from "./traits/ZodSchemaObject"
import { dejsonify, jsonify } from "./schema/jsonify" import { StaticMembers } from "./util"
import { MobXObservableZodSchemaObject } from "./traits/MobXObservableZodSchemaObject"
const exp = zodSchemaClass // const exp = zodSchemaClass
.schema({ // .schema({
schema: z.object({ // schema: z.object({
/** User ID */ // /** User ID */
id: z.bigint(), // id: z.bigint(),
/** Username */ // /** Username */
name: z.string(), // name: z.string(),
}), // }),
schemaWithDefaultValues: s => s.extend({ // schemaWithDefaultValues: s => s.extend({
id: s.shape.id.default(-1n), // id: s.shape.id.default(-1n),
}), // }),
}) // })
.jsonifiable({ // .jsonifiable({
jsonifySchema: s => s.extend({ // jsonifySchema: s => s.extend({
id: jsonify.bigint(s.shape.id) // id: jsonify.bigint(s.shape.id)
}), // }),
dejsonifySchema: s => s.extend({ // dejsonifySchema: s => s.extend({
id: dejsonify.bigint(s.shape.id) // id: dejsonify.bigint(s.shape.id)
}), // }),
}) // })
.expresses(MobXObservableZodSchemaObject) // .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<typeof schema>) {
Object.assign(this, values)
}
}
const exp = expression
.extends(UserSchemas as (
Class<UserSchemas & z.output<typeof schema>> &
StaticMembers<typeof UserSchemas>
))
.expresses(
ZodSchemaObject(
schema,
s => s.extend({
id: s.shape.id.default(-1n),
}),
)
)
.build() .build()
@exp.staticImplements @exp.staticImplements
class User extends exp.extends implements Implements<typeof exp> {} class User extends exp.extends implements Implements<typeof exp> {}
const test = User.schema.extend({
prout: z.string()
}).merge(
User.schemaWithDefaultValues
)
const inst = User.create({ id: 1n, name: "User" }) const inst = User.create({ id: 1n, name: "User" })
// console.log(inst.name) // console.log(inst.name)
const instEffect = User.createEffect({ id: 1n, name: "User" }) const instEffect = User.createEffect({ id: 1n, name: "User" })

View File

@@ -36,23 +36,19 @@ export const ZodSchemaObject = <
static readonly schemaWithDefaultsValues = schemaWithDefaultValues(schema) static readonly schemaWithDefaultsValues = schemaWithDefaultValues(schema)
static instantiationSchema< static instantiationSchema<
Instance extends Values Self extends StaticMembers<ImplStaticThis<typeof ZodSchemaObject>>,
Instance extends Values,
>( >(
this: ( this: Class<Instance, [values: Values]> & Self
Class<Instance, [values: Values]> &
StaticMembers<ImplStaticThis<typeof ZodSchemaObject>>
)
) { ) {
return this.schema.transform(values => new this(values)) return this.schema.transform(values => new this(values))
} }
static instantiationSchemaWithDefaultValues< static instantiationSchemaWithDefaultValues<
Instance extends Values Self extends StaticMembers<ImplStaticThis<typeof ZodSchemaObject>>,
Instance extends Values,
>( >(
this: ( this: Class<Instance, [values: Values]> & Self
Class<Instance, [values: Values]> &
StaticMembers<ImplStaticThis<typeof ZodSchemaObject>>
)
) { ) {
return this.schema.transform(values => new this(values)) return this.schema.transform(values => new this(values))
} }
@@ -67,7 +63,7 @@ export const ZodSchemaObject = <
), ),
...[values, params]: NewZodSchemaInstanceArgs<PartialValues> ...[values, params]: NewZodSchemaInstanceArgs<PartialValues>
) { ) {
return this.instantiationSchema().parse(values, params) return this.instantiationSchemaWithDefaultValues().parse(values, params)
} }
}) })
.build() .build()