0.1.3 #4

Merged
Thilawyn merged 74 commits from next into master 2024-03-24 22:24:25 +01:00
5 changed files with 85 additions and 42 deletions
Showing only changes of commit c61f5332fc - Show all commits

View File

@@ -13,16 +13,17 @@ export function jsonifyBigIntSchema<S extends z.ZodBigInt>(schema: S) {
export function dejsonifyBigIntSchema<S extends z.ZodBigInt>(schema: S) { export function dejsonifyBigIntSchema<S extends z.ZodBigInt>(schema: S) {
return z return z
.custom<JsonifiedBigInt>(identity) .custom<JsonifiedBigInt>(identity)
.pipe(z .pipe(
.string() z
.transform(v => { .string()
try { .transform(v => {
return BigInt(v) try {
} return BigInt(v)
catch (e) { }
return v catch (e) {
} return v
}) }
.pipe(schema) })
) )
.pipe(schema)
} }

View File

@@ -13,16 +13,17 @@ export function jsonifyDateSchema<S extends z.ZodDate>(schema: S) {
export function dejsonifyDateSchema<S extends z.ZodDate>(schema: S) { export function dejsonifyDateSchema<S extends z.ZodDate>(schema: S) {
return z return z
.custom<JsonifiedDate>(identity) .custom<JsonifiedDate>(identity)
.pipe(z .pipe(
.string() z
.transform(v => { .string()
try { .transform(v => {
return new Date(v) try {
} return new Date(v)
catch (e) { }
return v catch (e) {
} return v
}) }
.pipe(schema) })
) )
.pipe(schema)
} }

View File

@@ -18,16 +18,17 @@ export function dejsonifyDecimalSchema<
>(schema: S) { >(schema: S) {
return z return z
.custom<JsonifiedDecimal>(identity) .custom<JsonifiedDecimal>(identity)
.pipe(z .pipe(
.string() z
.transform(v => { .string()
try { .transform(v => {
return new Decimal(v) try {
} return new Decimal(v)
catch (e) { }
return v catch (e) {
} return v
}) }
.pipe(schema) })
) )
.pipe(schema)
} }

View File

@@ -1,11 +1,21 @@
import { Option } from "effect" import { Option } from "effect"
import { z } from "zod" import { z } from "zod"
import { effect } from "../effect" import { effect } from "../effect"
import { Opaque } from "type-fest"
import { identity } from "lodash-es"
export type JsonifiedEffectOption<T> = Opaque<T | null, "@thilawyn/zod-schema-class/JsonifiedEffectOption">
export const jsonifyOption = { export const jsonifyOption = {
// option: <S extends z.ZodSchema>(schema: S) => option: <
// z.union([option.some(schema), option.none(schema)]), S extends ReturnType<typeof effect.option.option<T>>,
T extends z.ZodTypeAny,
>(
schema: S | ReturnType<typeof effect.option.option<T>>
) =>
(schema as S).transform(v => Option.getOrNull(v)),
some: < some: <
S extends ReturnType<typeof effect.option.some<T>>, S extends ReturnType<typeof effect.option.some<T>>,
@@ -15,10 +25,40 @@ export const jsonifyOption = {
) => ) =>
(schema as S).transform(v => Option.getOrThrow(v)), (schema as S).transform(v => Option.getOrThrow(v)),
none: <S extends z.ZodSchema | unknown = unknown>(_schema?: S) => none: <
z.custom<Option.None< S extends ReturnType<typeof effect.option.none<T>>,
S extends z.ZodSchema T extends z.ZodSchema | unknown = unknown,
? z.output<S> >(
: unknown schema: S | ReturnType<typeof effect.option.none<T>>
>>(v => Option.isOption(v) && Option.isNone(v), "Not an Option"), ) =>
(schema as S).transform(() => null),
} as const
export const dejsonifyOption = {
option: <
S extends ReturnType<typeof effect.option.option<T>>,
T extends z.ZodSchema,
>(
schema: S | ReturnType<typeof effect.option.option<T>>
) => z
.custom<JsonifiedEffectOption<T>>(identity)
.pipe(
z.
),
some: <
S extends ReturnType<typeof effect.option.some<T>>,
T extends z.ZodSchema,
>(
schema: S | ReturnType<typeof effect.option.some<T>>
) =>
(schema as S).transform(v => Option.getOrThrow(v)),
none: <
S extends ReturnType<typeof effect.option.none<T>>,
T extends z.ZodSchema | unknown = unknown,
>(
schema: S | ReturnType<typeof effect.option.none<T>>
) =>
(schema as S).transform(() => null),
} as const } as const

View File

@@ -32,7 +32,7 @@ const userInstEffect = User.createEffect({ id: Option.some(1n), name: "User" })
const jsonifiedOption = jsonify.option.some( const jsonifiedOption = jsonify.option.some(
effect.option.some(z.literal("prout")) effect.option.some(z.literal("prout"))
).parse({}) )
const jsonifiedUserExp = JsonifiedZodSchemaClass(User, { const jsonifiedUserExp = JsonifiedZodSchemaClass(User, {
jsonifySchema: s => s.extend({ jsonifySchema: s => s.extend({