0.1.0 #1
54
src/tests.ts
54
src/tests.ts
@@ -1,17 +1,19 @@
|
||||
import { z } from "zod"
|
||||
import { makeSchemableClass, newSchemable } from "."
|
||||
import { dejsonifyBigIntSchema, jsonifyBigIntSchema, makeJsonifiableSchemableClass } from "./jsonifiable"
|
||||
import { dejsonifyBigIntSchema, dejsonifySchemableSchema, jsonifyBigIntSchema, jsonifySchemableSchema, makeJsonifiableSchemableClass } from "./jsonifiable"
|
||||
|
||||
|
||||
const UserSchema = z.object({
|
||||
/** User ID */
|
||||
id: z.bigint().default(-1n)
|
||||
const GroupSchema = z.object({
|
||||
/** Group ID */
|
||||
id: z.bigint(),
|
||||
|
||||
/** Group name */
|
||||
name: z.string(),
|
||||
})
|
||||
|
||||
const GroupSchemableObject = makeSchemableClass({ schema: GroupSchema })
|
||||
|
||||
const UserSchemableObject = makeSchemableClass({ schema: UserSchema })
|
||||
|
||||
const UserJsonifiableSchemableObject = makeJsonifiableSchemableClass(UserSchemableObject, {
|
||||
const GroupJsonifiableSchemableObject = makeJsonifiableSchemableClass(GroupSchemableObject, {
|
||||
jsonifySchema: ({ schema, s }) => schema.extend({
|
||||
id: jsonifyBigIntSchema(s.id)
|
||||
}),
|
||||
@@ -21,12 +23,40 @@ const UserJsonifiableSchemableObject = makeJsonifiableSchemableClass(UserSchemab
|
||||
}),
|
||||
})
|
||||
|
||||
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 user1 = new User({ id: 1n })
|
||||
const user2 = newSchemable(User, { id: 2n })
|
||||
|
||||
console.log(user1)
|
||||
console.log(user2)
|
||||
const group1 = new Group({ id: 1n, name: "Group 1" })
|
||||
|
||||
console.log(await user2.jsonifyPromise())
|
||||
const user1 = new User({ id: 1n, name: "User 1", group: group1 })
|
||||
const user2 = newSchemable(User, { id: 2n, name: "User 2", group: group1 })
|
||||
|
||||
console.log(user2.jsonify())
|
||||
|
||||
Reference in New Issue
Block a user