Files
zod-schema-class/src/schema/effect/option.ts
Julien Valverdé 52090cc7b1
All checks were successful
continuous-integration/drone/push Build is passing
Fix
2024-03-11 01:09:59 +01:00

22 lines
774 B
TypeScript

import { Option } from "effect"
import { identity } from "lodash-es"
import { z } from "zod"
export const option = {
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