This commit is contained in:
21
src/schema/jsonified/option.ts
Normal file
21
src/schema/jsonified/option.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { Option } from "effect"
|
||||
import { identity } from "lodash-es"
|
||||
import { z } from "zod"
|
||||
|
||||
|
||||
export const jsonifyOption = {
|
||||
option: <S extends z.ZodSchema>(schema: S) =>
|
||||
z.union([option.some(schema), option.none(schema)]),
|
||||
|
||||
some: <S extends z.ZodSchema>(schema: S) => z
|
||||
.custom<Option.Some<z.output<S>>>(v => Option.isOption(v) && Option.isSome(v), "Not an Option")
|
||||
.pipe(z.object({ value: schema }).passthrough())
|
||||
.transform<Option.Some<z.output<S>>>(identity),
|
||||
|
||||
none: <S extends z.ZodSchema | unknown = unknown>(_schema?: S) =>
|
||||
z.custom<Option.None<
|
||||
S extends z.ZodSchema
|
||||
? z.output<S>
|
||||
: unknown
|
||||
>>(v => Option.isOption(v) && Option.isNone(v), "Not an Option"),
|
||||
} as const
|
||||
@@ -1,2 +1,2 @@
|
||||
export * from "./effect"
|
||||
export * from "./jsonifiable"
|
||||
export * from "./jsonified"
|
||||
|
||||
Reference in New Issue
Block a user