0.1.0 #1

Merged
Thilawyn merged 24 commits from next into master 2024-01-05 00:39:33 +01:00
9 changed files with 87 additions and 8 deletions
Showing only changes of commit 00040fb7df - Show all commits

BIN
bun.lockb

Binary file not shown.

View File

@@ -11,12 +11,22 @@
"exports": { "exports": {
".": { ".": {
"import": { "import": {
"types": "./dist/lib.d.mts", "types": "./dist/schemable.d.mts",
"default": "./dist/lib.mjs" "default": "./dist/schemable.mjs"
}, },
"require": { "require": {
"types": "./dist/lib.d.cts", "types": "./dist/schemable.d.cts",
"default": "./dist/lib.cjs" "default": "./dist/schemable.cjs"
}
},
"./jsonifiable": {
"import": {
"types": "./dist/jsonifiable.d.mts",
"default": "./dist/jsonifiable.mjs"
},
"require": {
"types": "./dist/jsonifiable.d.cts",
"default": "./dist/jsonifiable.cjs"
} }
} }
}, },
@@ -29,6 +39,7 @@
}, },
"dependencies": { "dependencies": {
"@thilawyn/thilatrait": "^20231230.0.0", "@thilawyn/thilatrait": "^20231230.0.0",
"decimal.js": "^10.4.3",
"effect": "^2.0.0-next.62", "effect": "^2.0.0-next.62",
"lodash-es": "^4.17.21", "lodash-es": "^4.17.21",
"type-fest": "^4.9.0", "type-fest": "^4.9.0",

2
src/jsonifiable/index.ts Normal file
View File

@@ -0,0 +1,2 @@
export * from "./makeJsonifiableSchemableClass"
export * from "./schema"

View File

@@ -1,7 +1,7 @@
import { JsonifiableObject } from "type-fest/source/jsonifiable" import { JsonifiableObject } from "type-fest/source/jsonifiable"
import { z } from "zod" import { z } from "zod"
import { SchemableClass, SchemableConfig } from "." import { SchemableClass, SchemableConfig } from ".."
import { parseZodTypeEffect } from "./util" import { parseZodTypeEffect } from "../util"
export function makeJsonifiableSchemableClass< export function makeJsonifiableSchemableClass<

View 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)

View 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)

View 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)

View File

@@ -0,0 +1,3 @@
export * from "./bigint"
export * from "./date"
export * from "./decimal"

View File

@@ -8,9 +8,17 @@ const UserSchema = z.object({
}) })
const UserSchemaObject = makeSchemableClass({ schema: UserSchema }) const UserSchemableObject = makeSchemableClass({ schema: UserSchema })
class User extends UserSchemaObject {} // const UserJsonifiableSchemableObject = makeJsonifiableSchemableClass(UserSchemableObject, {
// jsonifySchema: ({ schema, s }) => schema.extend({
// }),
// dejsonifySchema: ({ schema, s }) => schema.extend({
// }),
// })
class User extends UserSchemableObject {}
const user1 = new User({ id: 1n }) const user1 = new User({ id: 1n })
const user2 = newSchemable(User, { id: 2n }) const user2 = newSchemable(User, { id: 2n })