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,20 +1,28 @@
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
.string() .custom<JsonifiedBigInt>(identity)
.transform(v => { .pipe(z
try { .string()
return BigInt(v) .transform(v => {
} try {
catch (e) { return BigInt(v)
return v }
} catch (e) {
}) return v
.pipe(schema) }
})
.pipe(schema)
)
} }

View File

@@ -1,20 +1,28 @@
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
.string() .custom<JsonifiedDate>(identity)
.transform(v => { .pipe(z
try { .string()
return new Date(v) .transform(v => {
} try {
catch (e) { return new Date(v)
return v }
} catch (e) {
}) return v
.pipe(schema) }
})
.pipe(schema)
)
} }

View File

@@ -1,25 +1,33 @@
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
.string() .custom<JsonifiedDecimal>(identity)
.transform(v => { .pipe(z
try { .string()
return new Decimal(v) .transform(v => {
} try {
catch (e) { return new Decimal(v)
return v }
} catch (e) {
}) return v
.pipe(schema) }
})
.pipe(schema)
)
} }

View File

@@ -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"])
@@ -10,9 +9,9 @@ const UserLevel = z.enum(["User", "Admin"])
const UserProto = makeSchemableClass({ const UserProto = makeSchemableClass({
schema: z.object({ schema: z.object({
id: z.bigint(), id: z.bigint(),
name: z.string(), name: z.string(),
level: UserLevel, level: UserLevel,
}), }),
defaultValues: { defaultValues: {
@@ -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)
}), }),
}) }),
) {} ) {}

View File

@@ -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<