From 037d11aa2ab7b06eecade20109d3ddf3ab6b845d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Valverd=C3=A9?= Date: Mon, 11 Mar 2024 00:50:05 +0100 Subject: [PATCH] Option type --- src/schema/effect/index.ts | 6 ++++++ src/schema/effect/option.ts | 21 +++++++++++++++++++++ src/schema/lib.ts | 1 + src/tests.ts | 5 +++++ 4 files changed, 33 insertions(+) create mode 100644 src/schema/effect/index.ts create mode 100644 src/schema/effect/option.ts diff --git a/src/schema/effect/index.ts b/src/schema/effect/index.ts new file mode 100644 index 0000000..904b057 --- /dev/null +++ b/src/schema/effect/index.ts @@ -0,0 +1,6 @@ +import { option } from "./option" + + +export const effect = { + option, +} as const diff --git a/src/schema/effect/option.ts b/src/schema/effect/option.ts new file mode 100644 index 0000000..b23f0b9 --- /dev/null +++ b/src/schema/effect/option.ts @@ -0,0 +1,21 @@ +import { Option } from "effect" +import { identity } from "lodash-es" +import { z } from "zod" + + +export const option = { + some: (schema: S) => z + .custom(v => Option.isOption(v) && Option.isSome(v), "Not an Option") + .pipe(z.object({ value: schema }).passthrough()) + .transform>>(identity), + + none: (_schema?: S) => + z.custom + : unknown + >>(v => Option.isOption(v) && Option.isNone(v), "Not an Option"), + + option: (schema: S) => + z.union([option.some(schema), option.none(schema)]), +} as const diff --git a/src/schema/lib.ts b/src/schema/lib.ts index 4ddb6dc..9339106 100644 --- a/src/schema/lib.ts +++ b/src/schema/lib.ts @@ -1 +1,2 @@ +export * from "./effect" export * from "./jsonifiable" diff --git a/src/tests.ts b/src/tests.ts index dff7e44..a9cd7f0 100644 --- a/src/tests.ts +++ b/src/tests.ts @@ -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 const exp = zodSchemaClass