Option jsonification work
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -1,29 +1,19 @@
|
||||
import { identity } from "lodash-es"
|
||||
import { Opaque } from "type-fest"
|
||||
import { z } from "zod"
|
||||
|
||||
|
||||
export type JsonifiedDate = Opaque<string, "@thilawyn/zod-schema-class/JsonifiedDate">
|
||||
export type JsonifiedDateBrand = "@thilawyn/zod-schema-class/JsonifiedDate"
|
||||
|
||||
|
||||
export function jsonifyDateSchema<S extends z.ZodType<Date>>(schema: S) {
|
||||
return schema.transform(v => v.toString() as JsonifiedDate)
|
||||
return schema
|
||||
.transform(v => v.toString())
|
||||
.brand<JsonifiedDateBrand>()
|
||||
}
|
||||
|
||||
export function dejsonifyDateSchema<S extends z.ZodType<Date>>(schema: S) {
|
||||
return z
|
||||
.custom<JsonifiedDate>(identity)
|
||||
.pipe(
|
||||
z
|
||||
.string()
|
||||
.transform(v => {
|
||||
try {
|
||||
return new Date(v)
|
||||
}
|
||||
catch (e) {
|
||||
return v
|
||||
}
|
||||
})
|
||||
)
|
||||
.custom<string & z.BRAND<JsonifiedDateBrand>>()
|
||||
.pipe(z.string())
|
||||
.pipe(z.coerce.date())
|
||||
.pipe(schema)
|
||||
}
|
||||
|
||||
@@ -1,19 +1,19 @@
|
||||
import { Decimal } from "decimal.js"
|
||||
import { identity } from "lodash-es"
|
||||
import { Opaque } from "type-fest"
|
||||
import { z } from "zod"
|
||||
|
||||
|
||||
export type JsonifiedDecimal = Opaque<string, "@thilawyn/zod-schema-class/JsonifiedDecimal">
|
||||
export type JsonifiedDecimalBrand = "@thilawyn/zod-schema-class/JsonifiedDecimal"
|
||||
|
||||
|
||||
export function jsonifyDecimalSchema<S extends z.ZodType<Decimal>>(schema: S) {
|
||||
return schema.transform(v => v.toJSON() as JsonifiedDecimal)
|
||||
return schema
|
||||
.transform(v => v.toJSON())
|
||||
.brand<JsonifiedDecimalBrand>()
|
||||
}
|
||||
|
||||
export function dejsonifyDecimalSchema<S extends z.ZodType<Decimal>>(schema: S) {
|
||||
return z
|
||||
.custom<JsonifiedDecimal>(identity)
|
||||
.custom<string & z.BRAND<JsonifiedDecimalBrand>>()
|
||||
.pipe(
|
||||
z
|
||||
.string()
|
||||
|
||||
@@ -1,13 +1,10 @@
|
||||
import { Option } from "effect"
|
||||
import { identity } from "lodash-es"
|
||||
import { Jsonifiable, Opaque } from "type-fest"
|
||||
import { z } from "zod"
|
||||
import { ZodEffectOption, ZodEffectOptionNone, ZodEffectOptionSome, effectOptionNoneSchema, effectOptionSomeInnerSchema, effectOptionSomeSchema } from "../effect"
|
||||
|
||||
|
||||
export type JsonifiedEffectOption<T extends Jsonifiable> = Opaque<T | null, "@thilawyn/zod-schema-class/JsonifiedEffectOption">
|
||||
export type JsonifiedEffectOptionSome<T> = Opaque<T, "@thilawyn/zod-schema-class/JsonifiedEffectOptionSome">
|
||||
export type JsonifiedEffectOptionNone = Opaque<null, "@thilawyn/zod-schema-class/JsonifiedEffectOptionNone">
|
||||
export type JsonifiedEffectOptionSomeBrand = "@thilawyn/zod-schema-class/JsonifiedEffectOptionSome"
|
||||
|
||||
|
||||
export const jsonifyOption = {
|
||||
@@ -31,7 +28,7 @@ export const jsonifyOption = {
|
||||
) => schema
|
||||
.transform(v => Option.getOrThrow(v))
|
||||
.pipe(jsonifySchema(effectOptionSomeInnerSchema(schema)))
|
||||
.brand<"@thilawyn/zod-schema-class/JsonifiedEffectOptionSome">(),
|
||||
.brand<JsonifiedEffectOptionSomeBrand>(),
|
||||
|
||||
none: <
|
||||
InnerS extends z.ZodTypeAny,
|
||||
@@ -45,32 +42,36 @@ export const jsonifyOption = {
|
||||
|
||||
export const dejsonifyOption = {
|
||||
option: <
|
||||
InnerS extends z.ZodTypeAny,
|
||||
JsonifiedInnerS extends z.ZodTypeAny = InnerS,
|
||||
InnerS extends z.ZodTypeAny,
|
||||
DejsonifiedInnerS extends z.ZodTypeAny = InnerS,
|
||||
>(
|
||||
schema: ZodEffectOption<InnerS>,
|
||||
jsonifySchema: (schema: InnerS) => JsonifiedInnerS = identity,
|
||||
schema: ZodEffectOption<InnerS>,
|
||||
dejsonifySchema: (schema: InnerS) => DejsonifiedInnerS = identity,
|
||||
) => z.union([
|
||||
jsonifyOption.some(effectOptionSomeSchema(schema), jsonifySchema),
|
||||
jsonifyOption.none(effectOptionNoneSchema(schema), jsonifySchema),
|
||||
dejsonifyOption.some(effectOptionSomeSchema(schema), dejsonifySchema),
|
||||
dejsonifyOption.none(effectOptionNoneSchema(schema), dejsonifySchema),
|
||||
]),
|
||||
|
||||
some: <
|
||||
InnerS extends z.ZodTypeAny,
|
||||
JsonifiedInnerS extends z.ZodTypeAny = InnerS,
|
||||
InnerS extends z.ZodTypeAny,
|
||||
DejsonifiedInnerS extends z.ZodTypeAny = InnerS,
|
||||
>(
|
||||
schema: ZodEffectOptionSome<InnerS>,
|
||||
jsonifySchema: (schema: InnerS) => JsonifiedInnerS = identity,
|
||||
) => schema
|
||||
.transform(v => Option.getOrThrow(v))
|
||||
.pipe(jsonifySchema(effectOptionSomeInnerSchema(schema))),
|
||||
schema: ZodEffectOptionSome<InnerS>,
|
||||
dejsonifySchema: (schema: InnerS) => DejsonifiedInnerS = identity,
|
||||
) => z
|
||||
.custom<z.input<DejsonifiedInnerS> & z.BRAND<JsonifiedEffectOptionSomeBrand>>()
|
||||
.pipe(dejsonifySchema(effectOptionSomeInnerSchema(schema)))
|
||||
.transform(v => Option.some<z.output<DejsonifiedInnerS>>(v))
|
||||
.pipe(schema),
|
||||
|
||||
none: <
|
||||
InnerS extends z.ZodTypeAny,
|
||||
JsonifiedInnerS extends z.ZodTypeAny = InnerS,
|
||||
InnerS extends z.ZodTypeAny,
|
||||
DejsonifiedInnerS extends z.ZodTypeAny = InnerS,
|
||||
>(
|
||||
schema: ZodEffectOptionNone<InnerS>,
|
||||
_jsonifySchema?: (schema: InnerS) => JsonifiedInnerS,
|
||||
) =>
|
||||
z.null().pipe(schema),
|
||||
schema: ZodEffectOptionNone<InnerS>,
|
||||
_dejsonifySchema?: (schema: InnerS) => DejsonifiedInnerS,
|
||||
) => z
|
||||
.null()
|
||||
.transform(() => Option.none<z.output<DejsonifiedInnerS>>())
|
||||
.pipe(schema),
|
||||
} as const
|
||||
|
||||
Reference in New Issue
Block a user