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