This commit is contained in:
@@ -54,6 +54,7 @@
|
|||||||
"rollup": "^4.9.4",
|
"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",
|
||||||
|
"ts-functional-pipe": "^3.1.2",
|
||||||
"tsx": "^4.7.0",
|
"tsx": "^4.7.0",
|
||||||
"typescript": "^5.3.3"
|
"typescript": "^5.3.3"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,15 +34,25 @@ export function extendSchemableClass<
|
|||||||
ExtendDefaultValues
|
ExtendDefaultValues
|
||||||
>,
|
>,
|
||||||
|
|
||||||
schemaApplier: (schema: C["schema"]) => z.ZodObject<
|
props: {
|
||||||
|
schema: (
|
||||||
|
schema: z.ZodObject<
|
||||||
|
ExtendSchemaT,
|
||||||
|
ExtendSchemaUnknownKeys,
|
||||||
|
ExtendSchemaCatchall,
|
||||||
|
ExtendSchemaValues,
|
||||||
|
ExtendSchemaValues
|
||||||
|
>
|
||||||
|
) => z.ZodObject<
|
||||||
SchemaT,
|
SchemaT,
|
||||||
SchemaUnknownKeys,
|
SchemaUnknownKeys,
|
||||||
SchemaCatchall,
|
SchemaCatchall,
|
||||||
SchemaValues,
|
SchemaValues,
|
||||||
SchemaValues
|
SchemaValues
|
||||||
>,
|
>
|
||||||
|
|
||||||
defaultValuesApplier: (defaultValues: ExtendDefaultValues) => DefaultValues,
|
defaultValues: (defaultValues: ExtendDefaultValues) => DefaultValues
|
||||||
|
},
|
||||||
) {
|
) {
|
||||||
type Class<T, Arguments extends unknown[]> = (
|
type Class<T, Arguments extends unknown[]> = (
|
||||||
C extends ConcreteClass<any>
|
C extends ConcreteClass<any>
|
||||||
@@ -50,8 +60,8 @@ export function extendSchemableClass<
|
|||||||
: AbstractClass<T, Arguments>
|
: AbstractClass<T, Arguments>
|
||||||
)
|
)
|
||||||
|
|
||||||
const schema = schemaApplier(extend.schema)
|
const schema = props.schema(extend.schema)
|
||||||
const defaultValues = defaultValuesApplier(extend.defaultValues)
|
const defaultValues = props.defaultValues(extend.defaultValues)
|
||||||
|
|
||||||
return class extends extend {
|
return class extends extend {
|
||||||
static readonly schema = schema
|
static readonly schema = schema
|
||||||
|
|||||||
@@ -21,15 +21,17 @@ export function makeSchemableClassFrom<
|
|||||||
>(
|
>(
|
||||||
extend: C,
|
extend: C,
|
||||||
|
|
||||||
|
{ schema, defaultValues }: {
|
||||||
schema: z.ZodObject<
|
schema: z.ZodObject<
|
||||||
SchemaT,
|
SchemaT,
|
||||||
SchemaUnknownKeys,
|
SchemaUnknownKeys,
|
||||||
SchemaCatchall,
|
SchemaCatchall,
|
||||||
Values,
|
Values,
|
||||||
Values
|
Values
|
||||||
>,
|
>
|
||||||
|
|
||||||
defaultValues: DefaultValues,
|
defaultValues: DefaultValues
|
||||||
|
},
|
||||||
) {
|
) {
|
||||||
type Class<T, Arguments extends unknown[]> = (
|
type Class<T, Arguments extends unknown[]> = (
|
||||||
C extends ConcreteClass<any>
|
C extends ConcreteClass<any>
|
||||||
@@ -77,15 +79,17 @@ export function makeSchemableClass<
|
|||||||
Values extends {},
|
Values extends {},
|
||||||
DefaultValues extends Partial<Values>,
|
DefaultValues extends Partial<Values>,
|
||||||
>(
|
>(
|
||||||
|
props: {
|
||||||
schema: z.ZodObject<
|
schema: z.ZodObject<
|
||||||
SchemaT,
|
SchemaT,
|
||||||
SchemaUnknownKeys,
|
SchemaUnknownKeys,
|
||||||
SchemaCatchall,
|
SchemaCatchall,
|
||||||
Values,
|
Values,
|
||||||
Values
|
Values
|
||||||
>,
|
>
|
||||||
|
|
||||||
defaultValues: DefaultValues,
|
defaultValues: DefaultValues
|
||||||
) {
|
}
|
||||||
return makeSchemableClassFrom(Object, schema, defaultValues)
|
) {
|
||||||
|
return makeSchemableClassFrom(Object, props)
|
||||||
}
|
}
|
||||||
|
|||||||
30
src/tests.ts
30
src/tests.ts
@@ -1,3 +1,4 @@
|
|||||||
|
import { pipeInto } from "ts-functional-pipe"
|
||||||
import { z } from "zod"
|
import { z } from "zod"
|
||||||
import { makeSchemableClass, newSchemable } from "."
|
import { makeSchemableClass, newSchemable } from "."
|
||||||
import { makeJsonifiableSchemableClass } from "./jsonifiable"
|
import { makeJsonifiableSchemableClass } from "./jsonifiable"
|
||||||
@@ -7,18 +8,35 @@ import { dejsonifyBigIntSchema, jsonifyBigIntSchema } from "./legacy/jsonifiable
|
|||||||
const UserLevel = z.enum(["User", "Admin"])
|
const UserLevel = z.enum(["User", "Admin"])
|
||||||
|
|
||||||
|
|
||||||
const UserProto = makeSchemableClass(z.object({
|
const UserProto = makeSchemableClass({
|
||||||
|
schema: z.object({
|
||||||
id: z.bigint(),
|
id: z.bigint(),
|
||||||
name: z.string(),
|
name: z.string(),
|
||||||
level: UserLevel,
|
level: UserLevel,
|
||||||
}), {
|
}),
|
||||||
|
|
||||||
|
defaultValues: {
|
||||||
level: "User"
|
level: "User"
|
||||||
} as const)
|
} as const,
|
||||||
|
})
|
||||||
|
|
||||||
UserProto.defaultValues
|
UserProto.defaultValues
|
||||||
|
|
||||||
|
|
||||||
const JsonifiableUserProto = makeJsonifiableSchemableClass(UserProto, {
|
class User extends pipeInto(
|
||||||
|
makeSchemableClass({
|
||||||
|
schema: z.object({
|
||||||
|
id: z.bigint(),
|
||||||
|
name: z.string(),
|
||||||
|
level: UserLevel,
|
||||||
|
}),
|
||||||
|
|
||||||
|
defaultValues: {
|
||||||
|
level: "User"
|
||||||
|
} as const,
|
||||||
|
}),
|
||||||
|
|
||||||
|
v => makeJsonifiableSchemableClass(v, {
|
||||||
jsonifySchema: ({ schema, shape }) => schema.extend({
|
jsonifySchema: ({ schema, shape }) => schema.extend({
|
||||||
id: jsonifyBigIntSchema(shape.id)
|
id: jsonifyBigIntSchema(shape.id)
|
||||||
}),
|
}),
|
||||||
@@ -27,9 +45,7 @@ const JsonifiableUserProto = makeJsonifiableSchemableClass(UserProto, {
|
|||||||
id: dejsonifyBigIntSchema(shape.id)
|
id: dejsonifyBigIntSchema(shape.id)
|
||||||
}),
|
}),
|
||||||
})
|
})
|
||||||
|
) {}
|
||||||
|
|
||||||
class User extends JsonifiableUserProto {}
|
|
||||||
|
|
||||||
|
|
||||||
const user1 = newSchemable(User, { id: 1n, name: "User" })
|
const user1 = newSchemable(User, { id: 1n, name: "User" })
|
||||||
|
|||||||
Reference in New Issue
Block a user