This commit is contained in:
5
src/schema/decimal.ts
Normal file
5
src/schema/decimal.ts
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
import Decimal from "decimal.js"
|
||||||
|
import { z } from "zod"
|
||||||
|
|
||||||
|
|
||||||
|
export const decimal = z.custom<Decimal>(v => Decimal.isDecimal(v))
|
||||||
@@ -6,11 +6,11 @@ import { z } from "zod"
|
|||||||
export type JsonifiedBigInt = Opaque<string, "@thilawyn/zod-schema-class/JsonifiedBigInt">
|
export type JsonifiedBigInt = Opaque<string, "@thilawyn/zod-schema-class/JsonifiedBigInt">
|
||||||
|
|
||||||
|
|
||||||
export function jsonifyBigIntSchema<S extends z.ZodBigInt>(schema: S) {
|
export function jsonifyBigIntSchema<S extends z.ZodType<bigint>>(schema: S) {
|
||||||
return schema.transform(v => v.toString() as JsonifiedBigInt)
|
return schema.transform(v => v.toString() as JsonifiedBigInt)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function dejsonifyBigIntSchema<S extends z.ZodBigInt>(schema: S) {
|
export function dejsonifyBigIntSchema<S extends z.ZodType<bigint>>(schema: S) {
|
||||||
return z
|
return z
|
||||||
.custom<JsonifiedBigInt>(identity)
|
.custom<JsonifiedBigInt>(identity)
|
||||||
.pipe(
|
.pipe(
|
||||||
|
|||||||
@@ -6,11 +6,11 @@ import { z } from "zod"
|
|||||||
export type JsonifiedDate = Opaque<string, "@thilawyn/zod-schema-class/JsonifiedDate">
|
export type JsonifiedDate = Opaque<string, "@thilawyn/zod-schema-class/JsonifiedDate">
|
||||||
|
|
||||||
|
|
||||||
export function jsonifyDateSchema<S extends z.ZodDate>(schema: S) {
|
export function jsonifyDateSchema<S extends z.ZodType<Date>>(schema: S) {
|
||||||
return schema.transform(v => v.toString() as JsonifiedDate)
|
return schema.transform(v => v.toString() as JsonifiedDate)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function dejsonifyDateSchema<S extends z.ZodDate>(schema: S) {
|
export function dejsonifyDateSchema<S extends z.ZodType<Date>>(schema: S) {
|
||||||
return z
|
return z
|
||||||
.custom<JsonifiedDate>(identity)
|
.custom<JsonifiedDate>(identity)
|
||||||
.pipe(
|
.pipe(
|
||||||
|
|||||||
@@ -7,15 +7,11 @@ import { z } from "zod"
|
|||||||
export type JsonifiedDecimal = Opaque<string, "@thilawyn/zod-schema-class/JsonifiedDecimal">
|
export type JsonifiedDecimal = Opaque<string, "@thilawyn/zod-schema-class/JsonifiedDecimal">
|
||||||
|
|
||||||
|
|
||||||
export function jsonifyDecimalSchema<
|
export function jsonifyDecimalSchema<S extends z.ZodType<Decimal>>(schema: S) {
|
||||||
S extends z.ZodType<Decimal, z.ZodTypeDef, Decimal>
|
|
||||||
>(schema: S) {
|
|
||||||
return schema.transform(v => v.toJSON() as JsonifiedDecimal)
|
return schema.transform(v => v.toJSON() as JsonifiedDecimal)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function dejsonifyDecimalSchema<
|
export function dejsonifyDecimalSchema<S extends z.ZodType<Decimal>>(schema: S) {
|
||||||
S extends z.ZodType<Decimal, z.ZodTypeDef, Decimal>
|
|
||||||
>(schema: S) {
|
|
||||||
return z
|
return z
|
||||||
.custom<JsonifiedDecimal>(identity)
|
.custom<JsonifiedDecimal>(identity)
|
||||||
.pipe(
|
.pipe(
|
||||||
|
|||||||
@@ -1,2 +1,3 @@
|
|||||||
|
export * from "./decimal"
|
||||||
export * from "./effect"
|
export * from "./effect"
|
||||||
export * from "./jsonified"
|
export * from "./jsonified"
|
||||||
|
|||||||
Reference in New Issue
Block a user