0.1.0 #1
86
src/SchemaClass.ts
Normal file
86
src/SchemaClass.ts
Normal file
@@ -0,0 +1,86 @@
|
||||
import { Class } from "type-fest"
|
||||
import { z } from "zod"
|
||||
|
||||
|
||||
export type SchemaClass<
|
||||
Values extends {} = {},
|
||||
Input extends {} = {},
|
||||
|
||||
SchemaT extends z.ZodRawShape = z.ZodRawShape,
|
||||
SchemaUnknownKeys extends z.UnknownKeysParam = z.UnknownKeysParam,
|
||||
SchemaCatchall extends z.ZodTypeAny = z.ZodTypeAny,
|
||||
|
||||
SchemaWithDefaultValuesT extends z.ZodRawShape = z.ZodRawShape,
|
||||
SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam = z.UnknownKeysParam,
|
||||
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny = z.ZodTypeAny,
|
||||
> = (
|
||||
Class<
|
||||
SchemaObject<
|
||||
Values,
|
||||
Input,
|
||||
|
||||
SchemaT,
|
||||
SchemaUnknownKeys,
|
||||
SchemaCatchall,
|
||||
|
||||
SchemaWithDefaultValuesT,
|
||||
SchemaWithDefaultValuesUnknownKeys,
|
||||
SchemaWithDefaultValuesCatchall
|
||||
>,
|
||||
|
||||
SchemaClassConstructorParams<Values>
|
||||
> & {
|
||||
readonly schema: z.ZodObject<
|
||||
SchemaT,
|
||||
SchemaUnknownKeys,
|
||||
SchemaCatchall,
|
||||
Values,
|
||||
Values
|
||||
>
|
||||
|
||||
readonly schemaWithDefaultValues: z.ZodObject<
|
||||
SchemaWithDefaultValuesT,
|
||||
SchemaWithDefaultValuesUnknownKeys,
|
||||
SchemaWithDefaultValuesCatchall,
|
||||
Values,
|
||||
Input
|
||||
>
|
||||
}
|
||||
)
|
||||
|
||||
export type SchemaClassConstructorParams<
|
||||
Values extends {} = {}
|
||||
> = (
|
||||
Parameters<
|
||||
(data: Values) => void
|
||||
>
|
||||
)
|
||||
|
||||
export type SchemaObject<
|
||||
Values extends {} = {},
|
||||
Input extends {} = {},
|
||||
|
||||
SchemaT extends z.ZodRawShape = z.ZodRawShape,
|
||||
SchemaUnknownKeys extends z.UnknownKeysParam = z.UnknownKeysParam,
|
||||
SchemaCatchall extends z.ZodTypeAny = z.ZodTypeAny,
|
||||
|
||||
SchemaWithDefaultValuesT extends z.ZodRawShape = z.ZodRawShape,
|
||||
SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam = z.UnknownKeysParam,
|
||||
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny = z.ZodTypeAny,
|
||||
> = {
|
||||
readonly schema: z.ZodObject<
|
||||
SchemaT,
|
||||
SchemaUnknownKeys,
|
||||
SchemaCatchall,
|
||||
Values,
|
||||
Values
|
||||
>
|
||||
|
||||
readonly schemaWithDefaultValues: z.ZodObject<
|
||||
SchemaWithDefaultValuesT,
|
||||
SchemaWithDefaultValuesUnknownKeys,
|
||||
SchemaWithDefaultValuesCatchall,
|
||||
Values,
|
||||
Input
|
||||
>
|
||||
} & Values
|
||||
@@ -1 +1,2 @@
|
||||
export * from "./SchemaClass"
|
||||
export * from "./makeSchemaClass"
|
||||
|
||||
@@ -25,7 +25,7 @@ export function makeSchemaClass<
|
||||
|
||||
const schema = zodObjectRemoveDefaults(schemaWithDefaultValues)
|
||||
|
||||
class SchemaObject {
|
||||
const class_ = class {
|
||||
static readonly schema = schema
|
||||
static readonly schemaWithDefaultValues = schemaWithDefaultValues
|
||||
|
||||
@@ -37,12 +37,12 @@ export function makeSchemaClass<
|
||||
}
|
||||
}
|
||||
|
||||
return SchemaObject as (
|
||||
return class_ as (
|
||||
Class<
|
||||
SchemaObject & z.output<typeof schema>,
|
||||
ConstructorParameters<typeof SchemaObject>
|
||||
InstanceType<typeof class_> & z.output<typeof schema>,
|
||||
ConstructorParameters<typeof class_>
|
||||
> &
|
||||
StaticMembers<typeof SchemaObject>
|
||||
StaticMembers<typeof class_>
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
@@ -10,5 +10,7 @@ const UserSchema = z.object({
|
||||
|
||||
const UserSchemaObject = makeSchemaClass({ schema: UserSchema })
|
||||
|
||||
const user = new UserSchemaObject({ id: 1n })
|
||||
user.id
|
||||
class User extends UserSchemaObject {}
|
||||
|
||||
const user = new User({ id: 1n })
|
||||
user.schema
|
||||
|
||||
Reference in New Issue
Block a user