0.1.2 (#3)
All checks were successful
continuous-integration/drone/push Build is passing

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:
Julien Valverdé
2024-03-11 19:44:21 +01:00
parent 019066bb9c
commit 2e09d9a0e0
56 changed files with 1042 additions and 1778 deletions

View File

@@ -0,0 +1,6 @@
import { option } from "./option"
export const effect = {
option,
} as const

View 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

View 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)
)
}

View 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)
)
}

View 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)
)
}

View 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
View File

@@ -0,0 +1,2 @@
export * from "./effect"
export * from "./jsonifiable"