Co-authored-by: Julien Valverdé <julien.valverde@mailo.com> Reviewed-on: https://git.jvalver.de/Thilawyn/schemable-class/pulls/1
This commit was merged in pull request #1.
This commit is contained in:
64
src/tests.ts
Normal file
64
src/tests.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
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)
|
||||
Reference in New Issue
Block a user