Schema work
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Julien Valverdé
2024-03-19 17:41:41 +01:00
parent 27a8fc1c9b
commit c61f5332fc
5 changed files with 85 additions and 42 deletions

View File

@@ -13,16 +13,17 @@ export function jsonifyBigIntSchema<S extends z.ZodBigInt>(schema: S) {
export function dejsonifyBigIntSchema<S extends z.ZodBigInt>(schema: S) {
return z
.custom<JsonifiedBigInt>(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)
}

View File

@@ -13,16 +13,17 @@ export function jsonifyDateSchema<S extends z.ZodDate>(schema: S) {
export function dejsonifyDateSchema<S extends z.ZodDate>(schema: S) {
return z
.custom<JsonifiedDate>(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)
}

View File

@@ -18,16 +18,17 @@ export function dejsonifyDecimalSchema<
>(schema: S) {
return z
.custom<JsonifiedDecimal>(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)
}

View File

@@ -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<T> = Opaque<T | null, "@thilawyn/zod-schema-class/JsonifiedEffectOption">
export const jsonifyOption = {
// option: <S extends z.ZodSchema>(schema: S) =>
// z.union([option.some(schema), option.none(schema)]),
option: <
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: <
S extends ReturnType<typeof effect.option.some<T>>,
@@ -15,10 +25,40 @@ export const jsonifyOption = {
) =>
(schema as S).transform(v => Option.getOrThrow(v)),
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"),
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
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

View File

@@ -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({