0.1.1 #2
@@ -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",
|
||||
|
||||
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"
|
||||
49
src/tests.ts
49
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<z.output<typeof Test3.schema>, typeof Test3.defaultValues>
|
||||
|
||||
const test3inst = newSchemableEffect(Test3, { ruquier: 48 })
|
||||
const user1 = newSchemable(User, { id: 1n, name: "User" })
|
||||
|
||||
Reference in New Issue
Block a user