This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
export * from "./JsonifiableSchemableClass"
|
||||
export * from "./dejsonifySchemable"
|
||||
export * from "./makeJsonifiableSchemableClass"
|
||||
export * from "./schema"
|
||||
|
||||
18
src/jsonifiable/schema/bigint.ts
Normal file
18
src/jsonifiable/schema/bigint.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { z } from "zod"
|
||||
|
||||
|
||||
export const jsonifyBigIntSchema = <S extends z.ZodBigInt>(schema: S) =>
|
||||
schema.transform(v => v.toString())
|
||||
|
||||
export const dejsonifyBigIntSchema = <S extends z.ZodBigInt>(schema: S) =>
|
||||
z
|
||||
.string()
|
||||
.transform(v => {
|
||||
try {
|
||||
return BigInt(v)
|
||||
}
|
||||
catch (e) {
|
||||
return v
|
||||
}
|
||||
})
|
||||
.pipe(schema)
|
||||
18
src/jsonifiable/schema/date.ts
Normal file
18
src/jsonifiable/schema/date.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { z } from "zod"
|
||||
|
||||
|
||||
export const jsonifyDateSchema = <S extends z.ZodDate>(schema: S) =>
|
||||
schema.transform(v => v.toString())
|
||||
|
||||
export const dejsonifyDateSchema = <S extends z.ZodDate>(schema: S) =>
|
||||
z
|
||||
.string()
|
||||
.transform(v => {
|
||||
try {
|
||||
return new Date(v)
|
||||
}
|
||||
catch (e) {
|
||||
return v
|
||||
}
|
||||
})
|
||||
.pipe(schema)
|
||||
19
src/jsonifiable/schema/decimal.ts
Normal file
19
src/jsonifiable/schema/decimal.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { Decimal } from "decimal.js"
|
||||
import { z } from "zod"
|
||||
|
||||
|
||||
export const jsonifyDecimalSchema = <S extends z.ZodType<Decimal, z.ZodTypeDef, Decimal>>(schema: S) =>
|
||||
schema.transform(v => v.toJSON())
|
||||
|
||||
export const dejsonifyDecimalSchema = <S extends z.ZodType<Decimal, z.ZodTypeDef, Decimal>>(schema: S) =>
|
||||
z
|
||||
.string()
|
||||
.transform(v => {
|
||||
try {
|
||||
return new Decimal(v)
|
||||
}
|
||||
catch (e) {
|
||||
return v
|
||||
}
|
||||
})
|
||||
.pipe(schema)
|
||||
4
src/jsonifiable/schema/index.ts
Normal file
4
src/jsonifiable/schema/index.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export * from "./bigint"
|
||||
export * from "./date"
|
||||
export * from "./decimal"
|
||||
export * from "./schemable"
|
||||
25
src/jsonifiable/schema/schemable.ts
Normal file
25
src/jsonifiable/schema/schemable.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { z } from "zod"
|
||||
import { JsonifiableSchemableClass, JsonifiableSchemableConfig } from ".."
|
||||
|
||||
|
||||
// TODO: try to find a way to get rid of the 'class_' arg
|
||||
export const jsonifySchemableSchema = <
|
||||
C extends JsonifiableSchemableClass<$Config>,
|
||||
$Config extends JsonifiableSchemableConfig,
|
||||
S extends z.ZodType<InstanceType<C>, z.ZodTypeDef, InstanceType<C>>,
|
||||
>(
|
||||
class_: C | JsonifiableSchemableClass<$Config>,
|
||||
schema: S,
|
||||
) =>
|
||||
schema.pipe(class_.jsonifySchema)
|
||||
|
||||
// TODO: try to find a way to get rid of the 'class_' arg
|
||||
export const dejsonifySchemableSchema = <
|
||||
C extends JsonifiableSchemableClass<$Config>,
|
||||
$Config extends JsonifiableSchemableConfig,
|
||||
S extends z.ZodType<InstanceType<C>, z.ZodTypeDef, InstanceType<C>>,
|
||||
>(
|
||||
class_: C | JsonifiableSchemableClass<$Config>,
|
||||
schema: S,
|
||||
) =>
|
||||
class_.dejsonifySchema.transform(v => new class_(v)).pipe(schema)
|
||||
Reference in New Issue
Block a user