diff --git a/bun.lockb b/bun.lockb index 159acb9..74e015c 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index 09e5a88..414881e 100644 --- a/package.json +++ b/package.json @@ -11,12 +11,22 @@ "exports": { ".": { "import": { - "types": "./dist/lib.d.mts", - "default": "./dist/lib.mjs" + "types": "./dist/schemable.d.mts", + "default": "./dist/schemable.mjs" }, "require": { - "types": "./dist/lib.d.cts", - "default": "./dist/lib.cjs" + "types": "./dist/schemable.d.cts", + "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": { "@thilawyn/thilatrait": "^20231230.0.0", + "decimal.js": "^10.4.3", "effect": "^2.0.0-next.62", "lodash-es": "^4.17.21", "type-fest": "^4.9.0", diff --git a/src/jsonifiable/index.ts b/src/jsonifiable/index.ts new file mode 100644 index 0000000..ae3e8e2 --- /dev/null +++ b/src/jsonifiable/index.ts @@ -0,0 +1,2 @@ +export * from "./makeJsonifiableSchemableClass" +export * from "./schema" diff --git a/src/makeJsonifiableSchemableClass.ts b/src/jsonifiable/makeJsonifiableSchemableClass.ts similarity index 95% rename from src/makeJsonifiableSchemableClass.ts rename to src/jsonifiable/makeJsonifiableSchemableClass.ts index 7a053f4..99d0029 100644 --- a/src/makeJsonifiableSchemableClass.ts +++ b/src/jsonifiable/makeJsonifiableSchemableClass.ts @@ -1,7 +1,7 @@ import { JsonifiableObject } from "type-fest/source/jsonifiable" import { z } from "zod" -import { SchemableClass, SchemableConfig } from "." -import { parseZodTypeEffect } from "./util" +import { SchemableClass, SchemableConfig } from ".." +import { parseZodTypeEffect } from "../util" export function makeJsonifiableSchemableClass< diff --git a/src/jsonifiable/schema/bigint.ts b/src/jsonifiable/schema/bigint.ts new file mode 100644 index 0000000..b0f0093 --- /dev/null +++ b/src/jsonifiable/schema/bigint.ts @@ -0,0 +1,18 @@ +import { z } from "zod" + + +export const jsonifyBigIntSchema = (schema: S) => + schema.transform(v => v.toString()) + +export const dejsonifyBigIntSchema = (schema: S) => + z + .string() + .transform(v => { + try { + return BigInt(v) + } + catch (e) { + return v + } + }) + .pipe(schema) diff --git a/src/jsonifiable/schema/date.ts b/src/jsonifiable/schema/date.ts new file mode 100644 index 0000000..b5ac677 --- /dev/null +++ b/src/jsonifiable/schema/date.ts @@ -0,0 +1,18 @@ +import { z } from "zod" + + +export const jsonifyDateSchema = (schema: S) => + schema.transform(v => v.toString()) + +export const dejsonifyDateSchema = (schema: S) => + z + .string() + .transform(v => { + try { + return new Date(v) + } + catch (e) { + return v + } + }) + .pipe(schema) diff --git a/src/jsonifiable/schema/decimal.ts b/src/jsonifiable/schema/decimal.ts new file mode 100644 index 0000000..58c492d --- /dev/null +++ b/src/jsonifiable/schema/decimal.ts @@ -0,0 +1,19 @@ +import { Decimal } from "decimal.js" +import { z } from "zod" + + +export const jsonifyDecimalSchema = >(schema: S) => + schema.transform(v => v.toJSON()) + +export const dejsonifyDecimalSchema = >(schema: S) => + z + .string() + .transform(v => { + try { + return new Decimal(v) + } + catch (e) { + return v + } + }) + .pipe(schema) diff --git a/src/jsonifiable/schema/index.ts b/src/jsonifiable/schema/index.ts new file mode 100644 index 0000000..f4bad9b --- /dev/null +++ b/src/jsonifiable/schema/index.ts @@ -0,0 +1,3 @@ +export * from "./bigint" +export * from "./date" +export * from "./decimal" diff --git a/src/tests.ts b/src/tests.ts index 0d17ecb..ca96997 100644 --- a/src/tests.ts +++ b/src/tests.ts @@ -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 user2 = newSchemable(User, { id: 2n })