This commit is contained in:
@@ -5,4 +5,4 @@ export const effect = {
|
||||
option,
|
||||
} as const
|
||||
|
||||
export { ZodEffectOption, ZodEffectOptionNone, ZodEffectOptionSome } from "./option"
|
||||
export { effectOptionNoneSchema, effectOptionSomeInnerSchema, effectOptionSomeSchema, type ZodEffectOption, type ZodEffectOptionNone, type ZodEffectOptionSome } from "./option"
|
||||
|
||||
@@ -1,29 +1,19 @@
|
||||
import { identity } from "lodash-es"
|
||||
import { Opaque } from "type-fest"
|
||||
import { z } from "zod"
|
||||
|
||||
|
||||
export type JsonifiedBigInt = Opaque<string, "@thilawyn/zod-schema-class/JsonifiedBigInt">
|
||||
export type JsonifiedBigIntBrand = "@thilawyn/zod-schema-class/JsonifiedBigInt"
|
||||
|
||||
|
||||
export function jsonifyBigIntSchema<S extends z.ZodType<bigint>>(schema: S) {
|
||||
return schema.transform(v => v.toString() as JsonifiedBigInt)
|
||||
return schema
|
||||
.transform(v => v.toString())
|
||||
.brand<JsonifiedBigIntBrand>()
|
||||
}
|
||||
|
||||
export function dejsonifyBigIntSchema<S extends z.ZodType<bigint>>(schema: S) {
|
||||
return z
|
||||
.custom<JsonifiedBigInt>(identity)
|
||||
.pipe(
|
||||
z
|
||||
.string()
|
||||
.transform(v => {
|
||||
try {
|
||||
return BigInt(v)
|
||||
}
|
||||
catch (e) {
|
||||
return v
|
||||
}
|
||||
})
|
||||
)
|
||||
.custom<string & z.BRAND<JsonifiedBigIntBrand>>()
|
||||
.pipe(z.string())
|
||||
.pipe(z.coerce.bigint())
|
||||
.pipe(schema)
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ import { Option } from "effect"
|
||||
import { identity } from "lodash-es"
|
||||
import { Jsonifiable, Opaque } from "type-fest"
|
||||
import { z } from "zod"
|
||||
import { ZodEffectOption, ZodEffectOptionNone, ZodEffectOptionSome, effectOptionNoneSchema, effectOptionSomeInnerSchema, effectOptionSomeSchema } from "../effect/option"
|
||||
import { ZodEffectOption, ZodEffectOptionNone, ZodEffectOptionSome, effectOptionNoneSchema, effectOptionSomeInnerSchema, effectOptionSomeSchema } from "../effect"
|
||||
|
||||
|
||||
export type JsonifiedEffectOption<T extends Jsonifiable> = Opaque<T | null, "@thilawyn/zod-schema-class/JsonifiedEffectOption">
|
||||
@@ -31,7 +31,7 @@ export const jsonifyOption = {
|
||||
) => schema
|
||||
.transform(v => Option.getOrThrow(v))
|
||||
.pipe(jsonifySchema(effectOptionSomeInnerSchema(schema)))
|
||||
.transform<JsonifiedEffectOptionSome<z.output<JsonifiedInnerS>>>(identity),
|
||||
.brand<"@thilawyn/zod-schema-class/JsonifiedEffectOptionSome">(),
|
||||
|
||||
none: <
|
||||
InnerS extends z.ZodTypeAny,
|
||||
@@ -40,7 +40,7 @@ export const jsonifyOption = {
|
||||
schema: ZodEffectOptionNone<InnerS>,
|
||||
_jsonifySchema?: (schema: InnerS) => JsonifiedInnerS,
|
||||
) =>
|
||||
schema.transform(() => null as JsonifiedEffectOptionNone),
|
||||
schema.transform(() => null),
|
||||
} as const
|
||||
|
||||
export const dejsonifyOption = {
|
||||
|
||||
38
src/tests.ts
38
src/tests.ts
@@ -1,7 +1,6 @@
|
||||
import { Implements } from "@thilawyn/traitify-ts"
|
||||
import { Option } from "effect"
|
||||
import { z } from "zod"
|
||||
import { JsonifiedZodSchemaClass } from "./JsonifiedZodSchemaClass"
|
||||
import { ZodSchemaClass } from "./ZodSchemaClass"
|
||||
import { dejsonify, jsonify } from "./schema/jsonified"
|
||||
import { jsonifyBigIntSchema } from "./schema/jsonified/bigint"
|
||||
@@ -44,23 +43,34 @@ const jsonifiedBigIntOptionSome = jsonify.option.some(
|
||||
)
|
||||
type T2 = z.output<typeof jsonifiedBigIntOptionSome>
|
||||
|
||||
const jsonifiedBigIntOptionNone = jsonify.option.none(
|
||||
effect.option.none(z.bigint()),
|
||||
jsonifyBigIntSchema,
|
||||
)
|
||||
type T3 = z.output<typeof jsonifiedBigIntOptionNone>
|
||||
|
||||
const jsonifiedUserExp = JsonifiedZodSchemaClass(User, {
|
||||
jsonifySchema: s => s.extend({
|
||||
id: jsonify.bigint(s.shape.id)
|
||||
}),
|
||||
|
||||
dejsonifySchema: s => s.extend({
|
||||
id: dejsonify.bigint(s.shape.id)
|
||||
}),
|
||||
}).build()
|
||||
const dejsonifyBigInt = dejsonify.bigint(z.bigint())
|
||||
type T4 = z.input<typeof dejsonifyBigInt>
|
||||
|
||||
@jsonifiedUserExp.staticImplements
|
||||
class JsonifiedUser extends jsonifiedUserExp.extends implements Implements<typeof jsonifiedUserExp> {}
|
||||
|
||||
const jsonifiedUserInst = JsonifiedUser.jsonify(userInst)
|
||||
console.log(JSON.stringify(jsonifiedUserInst))
|
||||
jsonifiedUserInst.dejsonify()
|
||||
// const jsonifiedUserExp = JsonifiedZodSchemaClass(User, {
|
||||
// jsonifySchema: s => s.extend({
|
||||
// id: jsonify.bigint(s.shape.id)
|
||||
// }),
|
||||
|
||||
// dejsonifySchema: s => s.extend({
|
||||
// id: dejsonify.bigint(s.shape.id)
|
||||
// }),
|
||||
// }).build()
|
||||
|
||||
// @jsonifiedUserExp.staticImplements
|
||||
// class JsonifiedUser extends jsonifiedUserExp.extends implements Implements<typeof jsonifiedUserExp> {}
|
||||
|
||||
// const jsonifiedUserInst = JsonifiedUser.jsonify(userInst)
|
||||
// console.log(JSON.stringify(jsonifiedUserInst))
|
||||
// jsonifiedUserInst.dejsonify()
|
||||
|
||||
|
||||
const adminUserExp = User.extend(s => s.extend({
|
||||
role: z.literal("Admin").default("Admin")
|
||||
|
||||
Reference in New Issue
Block a user