JsonifiableSchemable attempt as a trait
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -38,8 +38,9 @@
|
|||||||
"clean:node": "rm -rf node_modules"
|
"clean:node": "rm -rf node_modules"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@thilawyn/thilatrait": "^0.1.1",
|
||||||
"decimal.js": "^10.4.3",
|
"decimal.js": "^10.4.3",
|
||||||
"effect": "^2.0.0-next.62",
|
"effect": "^2.0.2",
|
||||||
"lodash-es": "^4.17.21",
|
"lodash-es": "^4.17.21",
|
||||||
"type-fest": "^4.9.0",
|
"type-fest": "^4.9.0",
|
||||||
"zod": "^3.22.4"
|
"zod": "^3.22.4"
|
||||||
@@ -50,7 +51,7 @@
|
|||||||
"bun-types": "latest",
|
"bun-types": "latest",
|
||||||
"npm-check-updates": "^16.14.12",
|
"npm-check-updates": "^16.14.12",
|
||||||
"npm-sort": "^0.0.4",
|
"npm-sort": "^0.0.4",
|
||||||
"rollup": "^4.9.1",
|
"rollup": "^4.9.4",
|
||||||
"rollup-plugin-cleanup": "^3.2.1",
|
"rollup-plugin-cleanup": "^3.2.1",
|
||||||
"rollup-plugin-ts": "^3.4.5",
|
"rollup-plugin-ts": "^3.4.5",
|
||||||
"tsx": "^4.7.0",
|
"tsx": "^4.7.0",
|
||||||
|
|||||||
52
src/jsonifiable/JsonifiableSchemable.ts
Normal file
52
src/jsonifiable/JsonifiableSchemable.ts
Normal file
@@ -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
|
||||||
|
})
|
||||||
1
src/jsonifiable/index.ts
Normal file
1
src/jsonifiable/index.ts
Normal file
@@ -0,0 +1 @@
|
|||||||
|
export * from "./JsonifiableSchemable"
|
||||||
51
src/tests.ts
51
src/tests.ts
@@ -1,39 +1,30 @@
|
|||||||
|
import { extendsAndExpresses } from "@thilawyn/thilatrait"
|
||||||
import { z } from "zod"
|
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 {
|
const UserLevel = z.enum(["User", "Admin"])
|
||||||
// static readonly schema = Test1Schema
|
|
||||||
// readonly schema = Test1Schema
|
|
||||||
|
|
||||||
// static readonly defaultValues = { prout: "heugneu" }
|
|
||||||
// readonly defaultValues = { prout: "heugneu" }
|
|
||||||
|
|
||||||
// prout: string = "heugneu"
|
const UserProto = makeSchemableClass(z.object({
|
||||||
// }
|
id: z.bigint(),
|
||||||
|
name: z.string(),
|
||||||
|
level: UserLevel,
|
||||||
|
}), {
|
||||||
|
level: "User"
|
||||||
|
} as const)
|
||||||
|
|
||||||
const Test1 = makeSchemableClass(
|
UserProto.defaultValues
|
||||||
z.object({ prout: z.string() }),
|
|
||||||
{},
|
|
||||||
|
class User extends extendsAndExpresses(UserProto,
|
||||||
|
JsonifiableSchemable(
|
||||||
|
UserProto.schema.extend({ id: jsonifyBigIntSchema(z.bigint()) }),
|
||||||
|
UserProto.schema.extend({ id: dejsonifyBigIntSchema(z.bigint()) }),
|
||||||
)
|
)
|
||||||
|
) {}
|
||||||
new Test1({ prout: "adfd" }).prout
|
|
||||||
|
|
||||||
|
|
||||||
const Test2 = extendSchemableClass(
|
const user1 = newSchemable(User, { id: 1n, name: "User" })
|
||||||
Test1,
|
|
||||||
schema => schema.extend({ prout: z.literal("ruquier"), ruquier: z.number() }),
|
|
||||||
() => ({ prout: "ruquier" as const }),
|
|
||||||
)
|
|
||||||
|
|
||||||
Test2.defaultValues
|
|
||||||
new Test2({ prout: "ruquier", ruquier: 69 })
|
|
||||||
|
|
||||||
|
|
||||||
class Test3 extends Test2 {
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
type Test = SchemableClassInput<z.output<typeof Test3.schema>, typeof Test3.defaultValues>
|
|
||||||
|
|
||||||
const test3inst = newSchemableEffect(Test3, { ruquier: 48 })
|
|
||||||
|
|||||||
Reference in New Issue
Block a user