This commit is contained in:
48
src/tests.ts
48
src/tests.ts
@@ -1,3 +1,4 @@
|
||||
import { pipeInto } from "ts-functional-pipe"
|
||||
import { z } from "zod"
|
||||
import { makeSchemableClass, newSchemable } from "."
|
||||
import { makeJsonifiableSchemableClass } from "./jsonifiable"
|
||||
@@ -7,29 +8,44 @@ import { dejsonifyBigIntSchema, jsonifyBigIntSchema } from "./legacy/jsonifiable
|
||||
const UserLevel = z.enum(["User", "Admin"])
|
||||
|
||||
|
||||
const UserProto = makeSchemableClass(z.object({
|
||||
id: z.bigint(),
|
||||
name: z.string(),
|
||||
level: UserLevel,
|
||||
}), {
|
||||
level: "User"
|
||||
} as const)
|
||||
const UserProto = makeSchemableClass({
|
||||
schema: z.object({
|
||||
id: z.bigint(),
|
||||
name: z.string(),
|
||||
level: UserLevel,
|
||||
}),
|
||||
|
||||
defaultValues: {
|
||||
level: "User"
|
||||
} as const,
|
||||
})
|
||||
|
||||
UserProto.defaultValues
|
||||
|
||||
|
||||
const JsonifiableUserProto = makeJsonifiableSchemableClass(UserProto, {
|
||||
jsonifySchema: ({ schema, shape }) => schema.extend({
|
||||
id: jsonifyBigIntSchema(shape.id)
|
||||
class User extends pipeInto(
|
||||
makeSchemableClass({
|
||||
schema: z.object({
|
||||
id: z.bigint(),
|
||||
name: z.string(),
|
||||
level: UserLevel,
|
||||
}),
|
||||
|
||||
defaultValues: {
|
||||
level: "User"
|
||||
} as const,
|
||||
}),
|
||||
|
||||
dejsonifySchema: ({ schema, shape }) => schema.extend({
|
||||
id: dejsonifyBigIntSchema(shape.id)
|
||||
}),
|
||||
})
|
||||
v => makeJsonifiableSchemableClass(v, {
|
||||
jsonifySchema: ({ schema, shape }) => schema.extend({
|
||||
id: jsonifyBigIntSchema(shape.id)
|
||||
}),
|
||||
|
||||
|
||||
class User extends JsonifiableUserProto {}
|
||||
dejsonifySchema: ({ schema, shape }) => schema.extend({
|
||||
id: dejsonifyBigIntSchema(shape.id)
|
||||
}),
|
||||
})
|
||||
) {}
|
||||
|
||||
|
||||
const user1 = newSchemable(User, { id: 1n, name: "User" })
|
||||
|
||||
Reference in New Issue
Block a user