Co-authored-by: Julien Valverdé <julien.valverde@mailo.com> Reviewed-on: https://git.jvalver.de/Thilawyn/zod-schema-class/pulls/3
This commit was merged in pull request #3.
This commit is contained in:
6
src/schema/effect/index.ts
Normal file
6
src/schema/effect/index.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { option } from "./option"
|
||||
|
||||
|
||||
export const effect = {
|
||||
option,
|
||||
} as const
|
||||
21
src/schema/effect/option.ts
Normal file
21
src/schema/effect/option.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { Option } from "effect"
|
||||
import { identity } from "lodash-es"
|
||||
import { z } from "zod"
|
||||
|
||||
|
||||
export const option = {
|
||||
option: <S extends z.ZodSchema>(schema: S) =>
|
||||
z.union([option.some(schema), option.none(schema)]),
|
||||
|
||||
some: <S extends z.ZodSchema>(schema: S) => z
|
||||
.custom<Option.Some<z.output<S>>>(v => Option.isOption(v) && Option.isSome(v), "Not an Option")
|
||||
.pipe(z.object({ value: schema }).passthrough())
|
||||
.transform<Option.Some<z.output<S>>>(identity),
|
||||
|
||||
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"),
|
||||
} as const
|
||||
28
src/schema/jsonifiable/bigint.ts
Normal file
28
src/schema/jsonifiable/bigint.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { identity } from "lodash-es"
|
||||
import { Opaque } from "type-fest"
|
||||
import { z } from "zod"
|
||||
|
||||
|
||||
export type JsonifiedBigInt = Opaque<string, "@thilawyn/zod-schema-class/JsonifiedBigInt">
|
||||
|
||||
|
||||
export function jsonifyBigIntSchema<S extends z.ZodBigInt>(schema: S) {
|
||||
return schema.transform(v => v.toString() as JsonifiedBigInt)
|
||||
}
|
||||
|
||||
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)
|
||||
)
|
||||
}
|
||||
28
src/schema/jsonifiable/date.ts
Normal file
28
src/schema/jsonifiable/date.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
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 function jsonifyDateSchema<S extends z.ZodDate>(schema: S) {
|
||||
return schema.transform(v => v.toString() as JsonifiedDate)
|
||||
}
|
||||
|
||||
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)
|
||||
)
|
||||
}
|
||||
33
src/schema/jsonifiable/decimal.ts
Normal file
33
src/schema/jsonifiable/decimal.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
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 function jsonifyDecimalSchema<
|
||||
S extends z.ZodType<Decimal, z.ZodTypeDef, Decimal>
|
||||
>(schema: S) {
|
||||
return schema.transform(v => v.toJSON() as JsonifiedDecimal)
|
||||
}
|
||||
|
||||
export function dejsonifyDecimalSchema<
|
||||
S extends z.ZodType<Decimal, z.ZodTypeDef, Decimal>
|
||||
>(schema: S) {
|
||||
return z
|
||||
.custom<JsonifiedDecimal>(identity)
|
||||
.pipe(z
|
||||
.string()
|
||||
.transform(v => {
|
||||
try {
|
||||
return new Decimal(v)
|
||||
}
|
||||
catch (e) {
|
||||
return v
|
||||
}
|
||||
})
|
||||
.pipe(schema)
|
||||
)
|
||||
}
|
||||
16
src/schema/jsonifiable/index.ts
Normal file
16
src/schema/jsonifiable/index.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { dejsonifyBigIntSchema, jsonifyBigIntSchema } from "./bigint"
|
||||
import { dejsonifyDateSchema, jsonifyDateSchema } from "./date"
|
||||
import { dejsonifyDecimalSchema, jsonifyDecimalSchema } from "./decimal"
|
||||
|
||||
|
||||
export const jsonify = {
|
||||
bigint: jsonifyBigIntSchema,
|
||||
date: jsonifyDateSchema,
|
||||
decimal: jsonifyDecimalSchema,
|
||||
} as const
|
||||
|
||||
export const dejsonify = {
|
||||
bigint: dejsonifyBigIntSchema,
|
||||
date: dejsonifyDateSchema,
|
||||
decimal: dejsonifyDecimalSchema,
|
||||
} as const
|
||||
2
src/schema/lib.ts
Normal file
2
src/schema/lib.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from "./effect"
|
||||
export * from "./jsonifiable"
|
||||
Reference in New Issue
Block a user