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

This commit is contained in:
Julien Valverdé
2024-02-28 04:02:14 +01:00
parent c3c454eb15
commit 0edbe3f45c
4 changed files with 23 additions and 206 deletions

View File

@@ -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<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),
}),
)
)
schemaWithDefaultValues: s => s.extend({
id: s.shape.id.default(-1n),
}),
})
.expresses(MobXObservableZodSchemaObject)
.build()