Option type
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Julien Valverdé
2024-03-11 00:50:05 +01:00
parent abac0f3747
commit 037d11aa2a
4 changed files with 33 additions and 0 deletions

View File

@@ -0,0 +1,6 @@
import { option } from "./option"
export const effect = {
option,
} as const

View File

@@ -0,0 +1,21 @@
import { Option } from "effect"
import { identity } from "lodash-es"
import { z } from "zod"
export const option = {
some: <S extends z.ZodSchema>(schema: S) => z
.custom(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"),
option: <S extends z.ZodSchema>(schema: S) =>
z.union([option.some(schema), option.none(schema)]),
} as const

View File

@@ -1 +1,2 @@
export * from "./effect"
export * from "./jsonifiable"

View File

@@ -3,6 +3,11 @@ import { z } from "zod"
import { zodSchemaClass } from "./builders/ZodSchemaClassBuilder"
import { dejsonify, jsonify } from "./schema/jsonifiable"
import { MobXObservableZodSchemaObject } from "./traits/MobXObservableZodSchemaObject"
import { effect } from "./schema/effect"
const stringOption = effect.option.option(z.string())
type T = z.output<typeof stringOption>
const exp = zodSchemaClass