diff --git a/bun.lockb b/bun.lockb index 4e23eb8..dc2bf92 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index 1fa0eb4..04f6667 100644 --- a/package.json +++ b/package.json @@ -38,8 +38,9 @@ "clean:node": "rm -rf node_modules" }, "dependencies": { + "@thilawyn/thilatrait": "^0.1.1", "decimal.js": "^10.4.3", - "effect": "^2.0.0-next.62", + "effect": "^2.0.2", "lodash-es": "^4.17.21", "type-fest": "^4.9.0", "zod": "^3.22.4" @@ -50,7 +51,7 @@ "bun-types": "latest", "npm-check-updates": "^16.14.12", "npm-sort": "^0.0.4", - "rollup": "^4.9.1", + "rollup": "^4.9.4", "rollup-plugin-cleanup": "^3.2.1", "rollup-plugin-ts": "^3.4.5", "tsx": "^4.7.0", diff --git a/src/jsonifiable/JsonifiableSchemable.ts b/src/jsonifiable/JsonifiableSchemable.ts new file mode 100644 index 0000000..914b7f5 --- /dev/null +++ b/src/jsonifiable/JsonifiableSchemable.ts @@ -0,0 +1,52 @@ +import { trait } from "@thilawyn/thilatrait" +import { JsonifiableObject } from "type-fest/source/jsonifiable" +import { z } from "zod" + + +export const JsonifiableSchemable = < + JsonifySchemaT extends z.ZodRawShape, + JsonifySchemaUnknownKeys extends z.UnknownKeysParam, + JsonifySchemaCatchall extends z.ZodTypeAny, + + DejsonifySchemaT extends z.ZodRawShape, + DejsonifySchemaUnknownKeys extends z.UnknownKeysParam, + DejsonifySchemaCatchall extends z.ZodTypeAny, + + Values extends {}, + JsonifiedValues extends JsonifiableObject, +>( + jsonifySchema: z.ZodObject< + JsonifySchemaT, + JsonifySchemaUnknownKeys, + JsonifySchemaCatchall, + JsonifiedValues, + Values + >, + + dejsonifySchema: z.ZodObject< + DejsonifySchemaT, + DejsonifySchemaUnknownKeys, + DejsonifySchemaCatchall, + Values, + JsonifiedValues + >, +) => + trait(Parent => { + abstract class JsonifiableSchemable extends Parent { + abstract readonly schema: z.ZodObject< + z.ZodRawShape, + z.UnknownKeysParam, + z.ZodTypeAny, + Values, + Values + > + + static readonly jsonifySchema = jsonifySchema + readonly jsonifySchema = jsonifySchema + + static readonly dejsonifySchema = dejsonifySchema + readonly dejsonifySchema = dejsonifySchema + } + + return JsonifiableSchemable + }) diff --git a/src/jsonifiable/index.ts b/src/jsonifiable/index.ts new file mode 100644 index 0000000..2662891 --- /dev/null +++ b/src/jsonifiable/index.ts @@ -0,0 +1 @@ +export * from "./JsonifiableSchemable" diff --git a/src/tests.ts b/src/tests.ts index 905a71a..33cca5c 100644 --- a/src/tests.ts +++ b/src/tests.ts @@ -1,39 +1,30 @@ +import { extendsAndExpresses } from "@thilawyn/thilatrait" import { z } from "zod" -import { SchemableClassInput, extendSchemableClass, makeSchemableClass, newSchemableEffect } from "." +import { makeSchemableClass, newSchemable } from "." +import { JsonifiableSchemable } from "./jsonifiable" +import { dejsonifyBigIntSchema, jsonifyBigIntSchema } from "./legacy/jsonifiable" -// class Test1 { -// static readonly schema = Test1Schema -// readonly schema = Test1Schema - -// static readonly defaultValues = { prout: "heugneu" } -// readonly defaultValues = { prout: "heugneu" } - -// prout: string = "heugneu" -// } - -const Test1 = makeSchemableClass( - z.object({ prout: z.string() }), - {}, -) - -new Test1({ prout: "adfd" }).prout +const UserLevel = z.enum(["User", "Admin"]) -const Test2 = extendSchemableClass( - Test1, - schema => schema.extend({ prout: z.literal("ruquier"), ruquier: z.number() }), - () => ({ prout: "ruquier" as const }), -) +const UserProto = makeSchemableClass(z.object({ + id: z.bigint(), + name: z.string(), + level: UserLevel, +}), { + level: "User" +} as const) -Test2.defaultValues -new Test2({ prout: "ruquier", ruquier: 69 }) +UserProto.defaultValues -class Test3 extends Test2 { -} +class User extends extendsAndExpresses(UserProto, + JsonifiableSchemable( + UserProto.schema.extend({ id: jsonifyBigIntSchema(z.bigint()) }), + UserProto.schema.extend({ id: dejsonifyBigIntSchema(z.bigint()) }), + ) +) {} -type Test = SchemableClassInput, typeof Test3.defaultValues> - -const test3inst = newSchemableEffect(Test3, { ruquier: 48 }) +const user1 = newSchemable(User, { id: 1n, name: "User" })