This commit is contained in:
28
src/schema/jsonify/bigint.ts
Normal file
28
src/schema/jsonify/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/schemable-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/jsonify/date.ts
Normal file
28
src/schema/jsonify/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/schemable-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/jsonify/decimal.ts
Normal file
33
src/schema/jsonify/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/schemable-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/jsonify/index.ts
Normal file
16
src/schema/jsonify/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,
|
||||
}
|
||||
|
||||
export const dejsonify = {
|
||||
bigint: dejsonifyBigIntSchema,
|
||||
date: dejsonifyDateSchema,
|
||||
decimal: dejsonifyDecimalSchema,
|
||||
}
|
||||
Reference in New Issue
Block a user