diff --git a/src/schema/jsonified/bigint.ts b/src/schema/jsonified/bigint.ts index b6d02d0..4627b45 100644 --- a/src/schema/jsonified/bigint.ts +++ b/src/schema/jsonified/bigint.ts @@ -13,16 +13,17 @@ export function jsonifyBigIntSchema(schema: S) { export function dejsonifyBigIntSchema(schema: S) { return z .custom(identity) - .pipe(z - .string() - .transform(v => { - try { - return BigInt(v) - } - catch (e) { - return v - } - }) - .pipe(schema) + .pipe( + z + .string() + .transform(v => { + try { + return BigInt(v) + } + catch (e) { + return v + } + }) ) + .pipe(schema) } diff --git a/src/schema/jsonified/date.ts b/src/schema/jsonified/date.ts index e147a5d..1524456 100644 --- a/src/schema/jsonified/date.ts +++ b/src/schema/jsonified/date.ts @@ -13,16 +13,17 @@ export function jsonifyDateSchema(schema: S) { export function dejsonifyDateSchema(schema: S) { return z .custom(identity) - .pipe(z - .string() - .transform(v => { - try { - return new Date(v) - } - catch (e) { - return v - } - }) - .pipe(schema) + .pipe( + z + .string() + .transform(v => { + try { + return new Date(v) + } + catch (e) { + return v + } + }) ) + .pipe(schema) } diff --git a/src/schema/jsonified/decimal.ts b/src/schema/jsonified/decimal.ts index ca87662..2aacd7a 100644 --- a/src/schema/jsonified/decimal.ts +++ b/src/schema/jsonified/decimal.ts @@ -18,16 +18,17 @@ export function dejsonifyDecimalSchema< >(schema: S) { return z .custom(identity) - .pipe(z - .string() - .transform(v => { - try { - return new Decimal(v) - } - catch (e) { - return v - } - }) - .pipe(schema) + .pipe( + z + .string() + .transform(v => { + try { + return new Decimal(v) + } + catch (e) { + return v + } + }) ) + .pipe(schema) } diff --git a/src/schema/jsonified/option.ts b/src/schema/jsonified/option.ts index 11c5500..3e68732 100644 --- a/src/schema/jsonified/option.ts +++ b/src/schema/jsonified/option.ts @@ -1,11 +1,21 @@ import { Option } from "effect" import { z } from "zod" import { effect } from "../effect" +import { Opaque } from "type-fest" +import { identity } from "lodash-es" + + +export type JsonifiedEffectOption = Opaque export const jsonifyOption = { - // option: (schema: S) => - // z.union([option.some(schema), option.none(schema)]), + option: < + S extends ReturnType>, + T extends z.ZodTypeAny, + >( + schema: S | ReturnType> + ) => + (schema as S).transform(v => Option.getOrNull(v)), some: < S extends ReturnType>, @@ -15,10 +25,40 @@ export const jsonifyOption = { ) => (schema as S).transform(v => Option.getOrThrow(v)), - none: (_schema?: S) => - z.custom - : unknown - >>(v => Option.isOption(v) && Option.isNone(v), "Not an Option"), + none: < + S extends ReturnType>, + T extends z.ZodSchema | unknown = unknown, + >( + schema: S | ReturnType> + ) => + (schema as S).transform(() => null), +} as const + +export const dejsonifyOption = { + option: < + S extends ReturnType>, + T extends z.ZodSchema, + >( + schema: S | ReturnType> + ) => z + .custom>(identity) + .pipe( + z. + ), + + some: < + S extends ReturnType>, + T extends z.ZodSchema, + >( + schema: S | ReturnType> + ) => + (schema as S).transform(v => Option.getOrThrow(v)), + + none: < + S extends ReturnType>, + T extends z.ZodSchema | unknown = unknown, + >( + schema: S | ReturnType> + ) => + (schema as S).transform(() => null), } as const diff --git a/src/tests.ts b/src/tests.ts index 81b3fb1..3f93b47 100644 --- a/src/tests.ts +++ b/src/tests.ts @@ -32,7 +32,7 @@ const userInstEffect = User.createEffect({ id: Option.some(1n), name: "User" }) const jsonifiedOption = jsonify.option.some( effect.option.some(z.literal("prout")) -).parse({}) +) const jsonifiedUserExp = JsonifiedZodSchemaClass(User, { jsonifySchema: s => s.extend({