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 { 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<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()
@exp.staticImplements
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" })
// console.log(inst.name)
const instEffect = User.createEffect({ id: 1n, name: "User" })