From 737cb781e6982a67cf74a17f838b1541bb16049a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Valverd=C3=A9?= Date: Sun, 31 Dec 2023 00:37:30 +0100 Subject: [PATCH] SchemaClass work --- src/SchemaClass.ts | 141 +++++++++++++++++++-------------------------- 1 file changed, 58 insertions(+), 83 deletions(-) diff --git a/src/SchemaClass.ts b/src/SchemaClass.ts index f009d1f..962f622 100644 --- a/src/SchemaClass.ts +++ b/src/SchemaClass.ts @@ -3,89 +3,17 @@ import { z } from "zod" /** - * Represents a schema class with generic parameters for defining validation schemas. - * - * @template Values - The type of values expected by the schema. - * @template Input - The type of input data expected by the schema. - * @template SchemaT - The type of the raw shape of the schema. - * @template SchemaUnknownKeys - The type of unknown keys parameter for the schema. - * @template SchemaCatchall - The type of catchall parameter for the schema. - * @template SchemaWithDefaultValuesT - The type of the raw shape for default values schema. - * @template SchemaWithDefaultValuesUnknownKeys - The type of unknown keys parameter for default values schema. - * @template SchemaWithDefaultValuesCatchall - The type of catchall parameter for default values schema. + * Configuration for creating a schemable object with validation schemas. + * @template Values - The type representing the expected values. + * @template Input - The type representing the input values. + * @template SchemaT - The type representing the base validation schema. + * @template SchemaUnknownKeys - The type representing the unknown keys behavior in the base validation schema. + * @template SchemaCatchall - The type representing the catchall behavior in the base validation schema. + * @template SchemaWithDefaultValuesT - The type representing the validation schema with default values. + * @template SchemaWithDefaultValuesUnknownKeys - The type representing the unknown keys behavior in the validation schema with default values. + * @template SchemaWithDefaultValuesCatchall - The type representing the catchall behavior in the validation schema with default values. */ -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 - > & { - readonly schema: z.ZodObject< - SchemaT, - SchemaUnknownKeys, - SchemaCatchall, - Values, - Values - > - - readonly schemaWithDefaultValues: z.ZodObject< - SchemaWithDefaultValuesT, - SchemaWithDefaultValuesUnknownKeys, - SchemaWithDefaultValuesCatchall, - Values, - Input - > - } -) - -/** - * Represents the constructor parameters for a schema class. - * - * @template Values - The type of values expected by the schema. - */ -export type SchemaClassConstructorParams< - Values extends {} = {} -> = ( - Parameters< - (data: Values) => void - > -) - -/** - * Represents a schema object with generic parameters for defining validation schemas. - * - * @template Values - The type of values expected by the schema. - * @template Input - The type of input data expected by the schema. - * @template SchemaT - The type of the raw shape of the schema. - * @template SchemaUnknownKeys - The type of unknown keys parameter for the schema. - * @template SchemaCatchall - The type of catchall parameter for the schema. - * @template SchemaWithDefaultValuesT - The type of the raw shape for default values schema. - * @template SchemaWithDefaultValuesUnknownKeys - The type of unknown keys parameter for default values schema. - * @template SchemaWithDefaultValuesCatchall - The type of catchall parameter for default values schema. - */ -export type SchemaObject< +export type SchemableConfig< Values extends {} = {}, Input extends {} = {}, @@ -97,6 +25,9 @@ export type SchemaObject< SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam = z.UnknownKeysParam, SchemaWithDefaultValuesCatchall extends z.ZodTypeAny = z.ZodTypeAny, > = { + readonly values: Values + readonly input: Input + readonly schema: z.ZodObject< SchemaT, SchemaUnknownKeys, @@ -112,4 +43,48 @@ export type SchemaObject< Values, Input > -} & Values +} + + +/** + * Represents a class with validation schemas. + * @template $Config - The configuration type for the schemable object. + */ +export type SchemaClass< + $Config extends SchemableConfig +> = ( + Class< + SchemaObject<$Config>, + SchemaClassConstructorParams<$Config> + > & { + readonly $schemableConfig: $Config + readonly schema: $Config["schema"] + readonly schemaWithDefaultValues: $Config["schemaWithDefaultValues"] + } +) + +/** + * Represents the constructor parameters for the schemable object class. + * @template $Config - The configuration type for the schemable object. + */ +export type SchemaClassConstructorParams< + $Config extends SchemableConfig +> = ( + Parameters< + (data: $Config["values"]) => void + > +) + +/** + * Represents an object with validation schemas. + * @template $Config - The configuration type for the schemable object. + */ +export type SchemaObject< + $Config extends SchemableConfig +> = ( + { + readonly $schemableConfig: $Config + readonly schema: $Config["schema"] + readonly schemaWithDefaultValues: $Config["schemaWithDefaultValues"] + } & $Config["values"] +)