This commit is contained in:
@@ -3,7 +3,7 @@ import { identity } from "lodash-es"
|
||||
import { Jsonifiable, Opaque } from "type-fest"
|
||||
import { z } from "zod"
|
||||
import { effect } from "../effect"
|
||||
import { ZodEffectOption, ZodEffectOptionSome, effectOptionSomeInnerType } from "../effect/option"
|
||||
import { ZodEffectOption, ZodEffectOptionNone, ZodEffectOptionSome, effectOptionSomeInnerType } from "../effect/option"
|
||||
|
||||
|
||||
export type JsonifiedEffectOption<T extends Jsonifiable> = Opaque<T | null, "@thilawyn/zod-schema-class/JsonifiedEffectOption">
|
||||
@@ -11,13 +11,15 @@ export type JsonifiedEffectOption<T extends Jsonifiable> = Opaque<T | null, "@th
|
||||
|
||||
export const jsonifyOption = {
|
||||
option: <
|
||||
S extends ZodEffectOption<T>,
|
||||
T extends z.ZodTypeAny,
|
||||
// Jsonify extends (schema: z.ZodType<>),
|
||||
InnerS extends z.ZodTypeAny,
|
||||
JsonifiedInnerS extends z.ZodTypeAny = InnerS,
|
||||
>(
|
||||
schema: S | ZodEffectOption<T>,
|
||||
) =>
|
||||
(schema as S).transform(v => Option.getOrNull(v)),
|
||||
schema: ZodEffectOption<InnerS>,
|
||||
jsonifySchema: (schema: InnerS) => JsonifiedInnerS = identity,
|
||||
) => z.union([
|
||||
jsonifyOption.some(schema._def.options[0], jsonifySchema),
|
||||
jsonifyOption.none(schema._def.options[1], jsonifySchema),
|
||||
]),
|
||||
|
||||
some: <
|
||||
InnerS extends z.ZodTypeAny,
|
||||
@@ -30,12 +32,13 @@ export const jsonifyOption = {
|
||||
.pipe(jsonifySchema(effectOptionSomeInnerType(schema))),
|
||||
|
||||
none: <
|
||||
S extends ReturnType<typeof effect.option.none<T>>,
|
||||
T extends z.ZodSchema | unknown = unknown,
|
||||
InnerS extends z.ZodTypeAny,
|
||||
JsonifiedInnerS extends z.ZodTypeAny = InnerS,
|
||||
>(
|
||||
schema: S | ReturnType<typeof effect.option.none<T>>
|
||||
schema: ZodEffectOptionNone<InnerS>,
|
||||
_jsonifySchema?: (schema: InnerS) => JsonifiedInnerS,
|
||||
) =>
|
||||
(schema as S).transform(() => null),
|
||||
schema.transform(() => null),
|
||||
} as const
|
||||
|
||||
export const dejsonifyOption = {
|
||||
|
||||
19
src/tests.ts
19
src/tests.ts
@@ -4,11 +4,9 @@ import { z } from "zod"
|
||||
import { JsonifiedZodSchemaClass } from "./JsonifiedZodSchemaClass"
|
||||
import { ZodSchemaClass } from "./ZodSchemaClass"
|
||||
import { dejsonify, jsonify } from "./schema/jsonified"
|
||||
import { ZodEffectOption, ZodEffectOptionNone, ZodEffectOptionSome, effect } from "./schema/lib"
|
||||
import { MobXObservableZodSchemaObject } from "./traits/MobXObservableZodSchemaObject"
|
||||
import { satisfies } from "effect/Function"
|
||||
import { identity } from "lodash-es"
|
||||
import { jsonifyBigIntSchema } from "./schema/jsonified/bigint"
|
||||
import { effect } from "./schema/lib"
|
||||
import { MobXObservableZodSchemaObject } from "./traits/MobXObservableZodSchemaObject"
|
||||
|
||||
|
||||
const userExp = ZodSchemaClass(
|
||||
@@ -33,15 +31,18 @@ const userInst = User.create({ id: Option.some(1n), name: "User" })
|
||||
const userInstEffect = User.createEffect({ id: Option.some(1n), name: "User" })
|
||||
|
||||
|
||||
const t = z.literal("prout")
|
||||
const optionT = effect.option.option(t)
|
||||
const someT = optionT._def.options[0] satisfies ZodEffectOptionSome<typeof t>
|
||||
const jsonifiedBigIntOption = jsonify.option.option(
|
||||
effect.option.option(z.bigint()),
|
||||
jsonifyBigIntSchema,
|
||||
)
|
||||
type T1 = z.output<typeof jsonifiedBigIntOption>
|
||||
|
||||
const jsonifiedBigIntOption = jsonify.option.some(
|
||||
|
||||
const jsonifiedBigIntOptionSome = jsonify.option.some(
|
||||
effect.option.some(z.bigint()),
|
||||
jsonifyBigIntSchema,
|
||||
)
|
||||
type T = z.output<typeof jsonifiedBigIntOption>
|
||||
type T2 = z.output<typeof jsonifiedBigIntOptionSome>
|
||||
|
||||
|
||||
const jsonifiedUserExp = JsonifiedZodSchemaClass(User, {
|
||||
|
||||
Reference in New Issue
Block a user