0.1.0 #1

Merged
Thilawyn merged 24 commits from next into master 2024-01-05 00:39:33 +01:00
4 changed files with 96 additions and 7 deletions
Showing only changes of commit caa8f9d2be - Show all commits

86
src/SchemaClass.ts Normal file
View 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

View File

@@ -1 +1,2 @@
export * from "./SchemaClass"
export * from "./makeSchemaClass"

View File

@@ -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_>
)
}

View File

@@ -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