All checks were successful
continuous-integration/drone/push Build is passing
Co-authored-by: Julien Valverdé <julien.valverde@mailo.com> Reviewed-on: https://git.jvalver.de/Thilawyn/schemable-class/pulls/1
65 lines
1.8 KiB
TypeScript
65 lines
1.8 KiB
TypeScript
import { z } from "zod"
|
|
import { makeSchemableClass, newSchemable } from "."
|
|
import { dejsonifyBigIntSchema, dejsonifySchemable, dejsonifySchemableSchema, jsonifyBigIntSchema, jsonifySchemableSchema, makeJsonifiableSchemableClass } from "./jsonifiable"
|
|
|
|
|
|
const GroupSchema = z.object({
|
|
/** Group ID */
|
|
id: z.bigint(),
|
|
|
|
/** Group name */
|
|
name: z.string(),
|
|
})
|
|
|
|
const GroupSchemableObject = makeSchemableClass({ schema: GroupSchema })
|
|
|
|
const GroupJsonifiableSchemableObject = makeJsonifiableSchemableClass(GroupSchemableObject, {
|
|
jsonifySchema: ({ schema, s }) => schema.extend({
|
|
id: jsonifyBigIntSchema(s.id)
|
|
}),
|
|
|
|
dejsonifySchema: ({ schema, s }) => schema.extend({
|
|
id: dejsonifyBigIntSchema(s.id)
|
|
}),
|
|
})
|
|
|
|
class Group extends GroupJsonifiableSchemableObject {}
|
|
|
|
|
|
const UserSchema = z.object({
|
|
/** User ID */
|
|
id: z.bigint(),
|
|
|
|
/** Name string */
|
|
name: z.string(),
|
|
|
|
/** User group */
|
|
group: z.instanceof(Group),
|
|
})
|
|
|
|
const UserSchemableObject = makeSchemableClass({ schema: UserSchema })
|
|
|
|
const UserJsonifiableSchemableObject = makeJsonifiableSchemableClass(UserSchemableObject, {
|
|
jsonifySchema: ({ schema, s }) => schema.extend({
|
|
id: jsonifyBigIntSchema(s.id),
|
|
group: jsonifySchemableSchema(Group, s.group),
|
|
}),
|
|
|
|
dejsonifySchema: ({ schema, s }) => schema.extend({
|
|
id: dejsonifyBigIntSchema(s.id),
|
|
group: dejsonifySchemableSchema(Group, s.group),
|
|
}),
|
|
})
|
|
|
|
class User extends UserJsonifiableSchemableObject {}
|
|
|
|
|
|
const group1 = new Group({ id: 1n, name: "Group 1" })
|
|
|
|
const user1 = new User({ id: 1n, name: "User 1", group: group1 })
|
|
const user2 = newSchemable(User, { id: 2n, name: "User 2", group: group1 })
|
|
|
|
const jsonifiedUser2 = user2.jsonify()
|
|
const dejsonifiedUser2 = dejsonifySchemable(User, jsonifiedUser2)
|
|
console.log(dejsonifiedUser2)
|