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 = {
|
||||
|
||||
Reference in New Issue
Block a user