Co-authored-by: Julien Valverdé <julien.valverde@mailo.com> Reviewed-on: https://git.jvalver.de/Thilawyn/schemable-class/pulls/2
This commit was merged in pull request #2.
This commit is contained in:
101
src/tests.ts
101
src/tests.ts
@@ -1,64 +1,63 @@
|
||||
import { pipeInto } from "ts-functional-pipe"
|
||||
import { z } from "zod"
|
||||
import { makeSchemableClass, newSchemable } from "."
|
||||
import { dejsonifyBigIntSchema, dejsonifySchemable, dejsonifySchemableSchema, jsonifyBigIntSchema, jsonifySchemableSchema, makeJsonifiableSchemableClass } from "./jsonifiable"
|
||||
import { defineDefaultValues, extendSchemableClass, makeSchemableClass, newSchemable } from "."
|
||||
import { dejsonifyBigIntSchema, dejsonifySchemable, jsonifyBigIntSchema, makeJsonifiableSchemableClass } from "./jsonifiable"
|
||||
import { makeSchemableClassObservable } from "./observable"
|
||||
|
||||
|
||||
const GroupSchema = z.object({
|
||||
/** Group ID */
|
||||
id: z.bigint(),
|
||||
const UserLevel = z.enum(["User", "Admin"])
|
||||
|
||||
/** Group name */
|
||||
name: z.string(),
|
||||
})
|
||||
|
||||
const GroupSchemableObject = makeSchemableClass({ schema: GroupSchema })
|
||||
class User extends pipeInto(
|
||||
makeSchemableClass({
|
||||
schema: z.object({
|
||||
id: z.bigint(),
|
||||
name: z.string(),
|
||||
level: UserLevel,
|
||||
}),
|
||||
|
||||
const GroupJsonifiableSchemableObject = makeJsonifiableSchemableClass(GroupSchemableObject, {
|
||||
jsonifySchema: ({ schema, s }) => schema.extend({
|
||||
id: jsonifyBigIntSchema(s.id)
|
||||
defaultValues: defineDefaultValues({
|
||||
level: "User" as const
|
||||
}),
|
||||
}),
|
||||
|
||||
dejsonifySchema: ({ schema, s }) => schema.extend({
|
||||
id: dejsonifyBigIntSchema(s.id)
|
||||
v => makeSchemableClassObservable(v),
|
||||
|
||||
v => makeJsonifiableSchemableClass(v, {
|
||||
jsonifySchema: ({ schema, shape }) => schema.extend({
|
||||
id: jsonifyBigIntSchema(shape.id)
|
||||
}),
|
||||
|
||||
dejsonifySchema: ({ schema, shape }) => schema.extend({
|
||||
id: dejsonifyBigIntSchema(shape.id)
|
||||
}),
|
||||
}),
|
||||
) {}
|
||||
|
||||
User.schema
|
||||
|
||||
|
||||
const user1 = newSchemable(User, { id: 1n, name: "User" })
|
||||
user1.schema
|
||||
|
||||
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",
|
||||
}),
|
||||
})
|
||||
|
||||
class Group extends GroupJsonifiableSchemableObject {}
|
||||
UserWithPhone.defaultValues
|
||||
|
||||
|
||||
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)
|
||||
// const user2 = newSchemable(UserWithPhone, { id: 1n, name: "User" })
|
||||
// console.log(user2.jsonify())
|
||||
|
||||
Reference in New Issue
Block a user