0.1.1 #2
@@ -1,12 +1,19 @@
|
|||||||
|
import { Opaque } from "type-fest"
|
||||||
import { z } from "zod"
|
import { z } from "zod"
|
||||||
|
import { identity } from "../../util"
|
||||||
|
|
||||||
|
|
||||||
|
export type JsonifiedBigInt = Opaque<string, "@thilawyn/schemable-class/JsonifiedBigInt">
|
||||||
|
|
||||||
|
|
||||||
export function jsonifyBigIntSchema<S extends z.ZodBigInt>(schema: S) {
|
export function jsonifyBigIntSchema<S extends z.ZodBigInt>(schema: S) {
|
||||||
return schema.transform(v => v.toString())
|
return schema.transform(v => v.toString() as JsonifiedBigInt)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function dejsonifyBigIntSchema<S extends z.ZodBigInt>(schema: S) {
|
export function dejsonifyBigIntSchema<S extends z.ZodBigInt>(schema: S) {
|
||||||
return z
|
return z
|
||||||
|
.custom<JsonifiedBigInt>(identity)
|
||||||
|
.pipe(z
|
||||||
.string()
|
.string()
|
||||||
.transform(v => {
|
.transform(v => {
|
||||||
try {
|
try {
|
||||||
@@ -17,4 +24,5 @@ export function dejsonifyBigIntSchema<S extends z.ZodBigInt>(schema: S) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
.pipe(schema)
|
.pipe(schema)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,19 @@
|
|||||||
|
import { Opaque } from "type-fest"
|
||||||
import { z } from "zod"
|
import { z } from "zod"
|
||||||
|
import { identity } from "../../util"
|
||||||
|
|
||||||
|
|
||||||
|
export type JsonifiedDate = Opaque<string, "@thilawyn/schemable-class/JsonifiedDate">
|
||||||
|
|
||||||
|
|
||||||
export function jsonifyDateSchema<S extends z.ZodDate>(schema: S) {
|
export function jsonifyDateSchema<S extends z.ZodDate>(schema: S) {
|
||||||
return schema.transform(v => v.toString())
|
return schema.transform(v => v.toString() as JsonifiedDate)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function dejsonifyDateSchema<S extends z.ZodDate>(schema: S) {
|
export function dejsonifyDateSchema<S extends z.ZodDate>(schema: S) {
|
||||||
return z
|
return z
|
||||||
|
.custom<JsonifiedDate>(identity)
|
||||||
|
.pipe(z
|
||||||
.string()
|
.string()
|
||||||
.transform(v => {
|
.transform(v => {
|
||||||
try {
|
try {
|
||||||
@@ -17,4 +24,5 @@ export function dejsonifyDateSchema<S extends z.ZodDate>(schema: S) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
.pipe(schema)
|
.pipe(schema)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,17 +1,24 @@
|
|||||||
import { Decimal } from "decimal.js"
|
import { Decimal } from "decimal.js"
|
||||||
|
import { Opaque } from "type-fest"
|
||||||
import { z } from "zod"
|
import { z } from "zod"
|
||||||
|
import { identity } from "../../util"
|
||||||
|
|
||||||
|
|
||||||
|
export type JsonifiedDecimal = Opaque<string, "@thilawyn/schemable-class/JsonifiedDecimal">
|
||||||
|
|
||||||
|
|
||||||
export function jsonifyDecimalSchema<
|
export function jsonifyDecimalSchema<
|
||||||
S extends z.ZodType<Decimal, z.ZodTypeDef, Decimal>
|
S extends z.ZodType<Decimal, z.ZodTypeDef, Decimal>
|
||||||
>(schema: S) {
|
>(schema: S) {
|
||||||
return schema.transform(v => v.toJSON())
|
return schema.transform(v => v.toJSON() as JsonifiedDecimal)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function dejsonifyDecimalSchema<
|
export function dejsonifyDecimalSchema<
|
||||||
S extends z.ZodType<Decimal, z.ZodTypeDef, Decimal>
|
S extends z.ZodType<Decimal, z.ZodTypeDef, Decimal>
|
||||||
>(schema: S) {
|
>(schema: S) {
|
||||||
return z
|
return z
|
||||||
|
.custom<JsonifiedDecimal>(identity)
|
||||||
|
.pipe(z
|
||||||
.string()
|
.string()
|
||||||
.transform(v => {
|
.transform(v => {
|
||||||
try {
|
try {
|
||||||
@@ -22,4 +29,5 @@ export function dejsonifyDecimalSchema<
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
.pipe(schema)
|
.pipe(schema)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
import { pipeInto } from "ts-functional-pipe"
|
import { pipeInto } from "ts-functional-pipe"
|
||||||
import { z } from "zod"
|
import { z } from "zod"
|
||||||
import { extendSchemableClass, makeSchemableClass, newSchemable } from "."
|
import { extendSchemableClass, makeSchemableClass, newSchemable } from "."
|
||||||
import { dejsonifySchemable, makeJsonifiableSchemableClass } from "./jsonifiable"
|
import { dejsonifyBigIntSchema, dejsonifySchemable, jsonifyBigIntSchema, makeJsonifiableSchemableClass } from "./jsonifiable"
|
||||||
import { dejsonifyBigIntSchema, jsonifyBigIntSchema } from "./legacy/jsonifiable"
|
|
||||||
|
|
||||||
|
|
||||||
const UserLevel = z.enum(["User", "Admin"])
|
const UserLevel = z.enum(["User", "Admin"])
|
||||||
@@ -44,7 +43,7 @@ class User extends pipeInto(
|
|||||||
dejsonifySchema: ({ schema, shape }) => schema.extend({
|
dejsonifySchema: ({ schema, shape }) => schema.extend({
|
||||||
id: dejsonifyBigIntSchema(shape.id)
|
id: dejsonifyBigIntSchema(shape.id)
|
||||||
}),
|
}),
|
||||||
})
|
}),
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,11 @@ import { AbstractClass, Class as ConcreteClass } from "type-fest"
|
|||||||
import { z } from "zod"
|
import { z } from "zod"
|
||||||
|
|
||||||
|
|
||||||
|
export function identity<T>(value: T) {
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
export type ClassType = "AbstractClass" | "Class"
|
export type ClassType = "AbstractClass" | "Class"
|
||||||
|
|
||||||
export type Class<
|
export type Class<
|
||||||
|
|||||||
Reference in New Issue
Block a user