Files
zod-schema-class/src/tests.ts
Julien Valverdé 892ee29060
All checks were successful
continuous-integration/drone/push Build is passing
Refactoring
2024-01-16 11:47:48 +01:00

53 lines
1.2 KiB
TypeScript

import { pipeInto } from "ts-functional-pipe"
import { z } from "zod"
import { makeSchemableClass, newSchemable } from "."
import { makeJsonifiableSchemableClass } from "./jsonifiable"
import { dejsonifyBigIntSchema, jsonifyBigIntSchema } from "./legacy/jsonifiable"
const UserLevel = z.enum(["User", "Admin"])
const UserProto = makeSchemableClass({
schema: z.object({
id: z.bigint(),
name: z.string(),
level: UserLevel,
}),
defaultValues: {
level: "User"
} as const,
})
UserProto.defaultValues
class User extends pipeInto(
makeSchemableClass({
schema: z.object({
id: z.bigint(),
name: z.string(),
level: UserLevel,
}),
defaultValues: {
level: "User"
} as const,
}),
v => makeJsonifiableSchemableClass(v, {
jsonifySchema: ({ schema, shape }) => schema.extend({
id: jsonifyBigIntSchema(shape.id)
}),
dejsonifySchema: ({ schema, shape }) => schema.extend({
id: dejsonifyBigIntSchema(shape.id)
}),
})
) {}
const user1 = newSchemable(User, { id: 1n, name: "User" })
user1.jsonify()