0.1.1 #2

Merged
Thilawyn merged 47 commits from next into master 2024-01-17 20:47:13 +01:00
5 changed files with 67 additions and 39 deletions
Showing only changes of commit 222676dfaf - Show all commits

View File

@@ -1,12 +1,19 @@
import { Opaque } from "type-fest"
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) {
return schema.transform(v => v.toString())
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 {
@@ -17,4 +24,5 @@ export function dejsonifyBigIntSchema<S extends z.ZodBigInt>(schema: S) {
}
})
.pipe(schema)
)
}

View File

@@ -1,12 +1,19 @@
import { Opaque } from "type-fest"
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) {
return schema.transform(v => v.toString())
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 {
@@ -17,4 +24,5 @@ export function dejsonifyDateSchema<S extends z.ZodDate>(schema: S) {
}
})
.pipe(schema)
)
}

View File

@@ -1,17 +1,24 @@
import { Decimal } from "decimal.js"
import { Opaque } from "type-fest"
import { z } from "zod"
import { identity } from "../../util"
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())
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 {
@@ -22,4 +29,5 @@ export function dejsonifyDecimalSchema<
}
})
.pipe(schema)
)
}

View File

@@ -1,8 +1,7 @@
import { pipeInto } from "ts-functional-pipe"
import { z } from "zod"
import { extendSchemableClass, makeSchemableClass, newSchemable } from "."
import { dejsonifySchemable, makeJsonifiableSchemableClass } from "./jsonifiable"
import { dejsonifyBigIntSchema, jsonifyBigIntSchema } from "./legacy/jsonifiable"
import { dejsonifyBigIntSchema, dejsonifySchemable, jsonifyBigIntSchema, makeJsonifiableSchemableClass } from "./jsonifiable"
const UserLevel = z.enum(["User", "Admin"])
@@ -44,7 +43,7 @@ class User extends pipeInto(
dejsonifySchema: ({ schema, shape }) => schema.extend({
id: dejsonifyBigIntSchema(shape.id)
}),
})
}),
) {}

View File

@@ -3,6 +3,11 @@ import { AbstractClass, Class as ConcreteClass } from "type-fest"
import { z } from "zod"
export function identity<T>(value: T) {
return value
}
export type ClassType = "AbstractClass" | "Class"
export type Class<