0.1.2 #3
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,
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
import { Implements, TraitExpression } from "@thilawyn/traitify-ts"
|
import { Implements, TraitExpression } from "@thilawyn/traitify-ts"
|
||||||
import { z } from "zod"
|
import { z } from "zod"
|
||||||
import { ZodSchemaClassBuilder } from "./ZodSchemaClass"
|
import { ZodSchemaClassBuilder } from "./ZodSchemaClass"
|
||||||
|
import { dejsonify, jsonify } from "./schema/jsonify"
|
||||||
import { ObservableZodSchemaObject } from "./traits/ObservableZodSchemaObject"
|
import { ObservableZodSchemaObject } from "./traits/ObservableZodSchemaObject"
|
||||||
|
|
||||||
|
|
||||||
@@ -15,8 +16,12 @@ const newTestExp = new ZodSchemaClassBuilder(TraitExpression.NullSuperclass, [])
|
|||||||
defaultValues: { id: -1n },
|
defaultValues: { id: -1n },
|
||||||
})
|
})
|
||||||
.jsonifiable({
|
.jsonifiable({
|
||||||
jsonifySchema: ({ schema }) => schema.extend({}),
|
jsonifySchema: ({ schema, shape }) => schema.extend({
|
||||||
dejsonifySchema: ({ schema }) => schema.extend({}),
|
id: jsonify.bigint(shape.id)
|
||||||
|
}),
|
||||||
|
dejsonifySchema: ({ schema, shape }) => schema.extend({
|
||||||
|
id: dejsonify.bigint(shape.id)
|
||||||
|
}),
|
||||||
})
|
})
|
||||||
.expresses(ObservableZodSchemaObject)
|
.expresses(ObservableZodSchemaObject)
|
||||||
.build()
|
.build()
|
||||||
|
|||||||
Reference in New Issue
Block a user