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