59 lines
1.5 KiB
TypeScript
59 lines
1.5 KiB
TypeScript
import { pipeInto } from "ts-functional-pipe"
|
|
import { z } from "zod"
|
|
import { defineDefaultValues, extendSchemableClass, makeSchemableClass, newSchemable } from "."
|
|
import { dejsonifyBigIntSchema, dejsonifySchemable, jsonifyBigIntSchema, makeJsonifiableSchemableClass } from "./jsonifiable"
|
|
|
|
|
|
const UserLevel = z.enum(["User", "Admin"])
|
|
|
|
|
|
class User extends pipeInto(
|
|
makeSchemableClass({
|
|
schema: z.object({
|
|
id: z.bigint(),
|
|
name: z.string(),
|
|
level: UserLevel,
|
|
}),
|
|
|
|
defaultValues: defineDefaultValues({
|
|
level: "User" as const
|
|
}),
|
|
}),
|
|
|
|
v => makeJsonifiableSchemableClass(v, {
|
|
jsonifySchema: ({ schema, shape }) => schema.extend({
|
|
id: jsonifyBigIntSchema(shape.id)
|
|
}),
|
|
|
|
dejsonifySchema: ({ schema, shape }) => schema.extend({
|
|
id: dejsonifyBigIntSchema(shape.id)
|
|
}),
|
|
}),
|
|
) {}
|
|
|
|
User.defaultValues
|
|
|
|
|
|
const user1 = newSchemable(User, { id: 1n, name: "User" })
|
|
const jsonifiedUser1 = user1.jsonify()
|
|
console.log(jsonifiedUser1)
|
|
console.log(dejsonifySchemable(User, jsonifiedUser1))
|
|
|
|
|
|
const UserWithPhone = extendSchemableClass(User, {
|
|
schema: ({ schema }) => schema.extend({
|
|
phone: z.string()
|
|
}),
|
|
|
|
defaultValues: defaultValues => defineDefaultValues({
|
|
...defaultValues,
|
|
phone: "+33600000000",
|
|
}),
|
|
})
|
|
|
|
UserWithPhone.defaultValues
|
|
|
|
|
|
// const user2 = newSchemable(UserWithPhone, { id: 1n, name: "User" })
|
|
// console.log(user2.jsonify())
|