diff --git a/src/SchemableClass.ts b/src/SchemableClass.ts new file mode 100644 index 0000000..02a75b9 --- /dev/null +++ b/src/SchemableClass.ts @@ -0,0 +1,38 @@ +import { AbstractClass } from "type-fest" +import { z } from "zod" + + +export type SchemableClass< + SchemaT extends z.ZodRawShape, + SchemaUnknownKeys extends z.UnknownKeysParam, + SchemaCatchall extends z.ZodTypeAny, + SchemaValues extends {}, + + DefaultValues extends Partial, +> = ( + AbstractClass< + { + readonly schema: z.ZodObject< + SchemaT, + SchemaUnknownKeys, + SchemaCatchall, + SchemaValues, + SchemaValues + > + + readonly defaultValues: DefaultValues + } & SchemaValues, + + any[] + > & { + readonly schema: z.ZodObject< + SchemaT, + SchemaUnknownKeys, + SchemaCatchall, + SchemaValues, + SchemaValues + > + + readonly defaultValues: DefaultValues + } +) diff --git a/src/extendSchemable.ts b/src/extendSchemable.ts index 17f4fc4..eee5111 100644 --- a/src/extendSchemable.ts +++ b/src/extendSchemable.ts @@ -1,102 +1,73 @@ -import { AbstractClass, Class } from "type-fest" +import { AbstractClass } from "type-fest" import { z } from "zod" +import { SchemableClass } from "." import { StaticMembers } from "./util" -type SchemableClass< - SchemaT extends z.ZodRawShape, - SchemaUnknownKeys extends z.UnknownKeysParam, - SchemaCatchall extends z.ZodTypeAny, - SchemaValues extends {}, -> = ( - AbstractClass< - { - readonly schema: z.ZodObject< - SchemaT, - SchemaUnknownKeys, - SchemaCatchall, - SchemaValues, - SchemaValues - > - } & SchemaValues, - - any[] - > & { - readonly schema: z.ZodObject< - SchemaT, - SchemaUnknownKeys, - SchemaCatchall, - SchemaValues, - SchemaValues - > - } -) - - export function extendSchemable< C extends SchemableClass< ExtendSchemaT, ExtendSchemaUnknownKeys, ExtendSchemaCatchall, - ExtendSchemaValues + ExtendSchemaValues, + ExtendDefaultValues >, ExtendSchemaT extends z.ZodRawShape, ExtendSchemaUnknownKeys extends z.UnknownKeysParam, ExtendSchemaCatchall extends z.ZodTypeAny, ExtendSchemaValues extends {}, + ExtendDefaultValues extends Partial, SchemaT extends z.ZodRawShape, SchemaUnknownKeys extends z.UnknownKeysParam, SchemaCatchall extends z.ZodTypeAny, SchemaValues extends ExtendSchemaValues, + DefaultValues extends Partial, >( extend: C | SchemableClass< ExtendSchemaT, ExtendSchemaUnknownKeys, ExtendSchemaCatchall, - ExtendSchemaValues + ExtendSchemaValues, + ExtendDefaultValues >, - schema: z.ZodObject< + schemaApplier: (schema: C["schema"]) => z.ZodObject< SchemaT, SchemaUnknownKeys, SchemaCatchall, SchemaValues, SchemaValues >, + + defaultValuesApplier: (defaultValues: ExtendDefaultValues) => DefaultValues, ) { + const schema = schemaApplier(extend.schema) + const defaultValues = defaultValuesApplier(extend.defaultValues) + return class extends extend { static readonly schema = schema readonly schema = schema + + static readonly defaultValues = defaultValues + readonly defaultValues = defaultValues } as unknown as ( - Class< - Omit< - Omit, - keyof ExtendSchemaValues - > & - { readonly schema: typeof schema } & + AbstractClass< + Omit & + { + readonly schema: typeof schema, + readonly defaultValues: typeof defaultValues, + } & SchemaValues, ConstructorParameters > & - Omit, "schema"> & - { readonly schema: typeof schema } + Omit, "schema" | "defaultValues"> & + { + readonly schema: typeof schema, + readonly defaultValues: typeof defaultValues, + } ) } - - -const Test1Schema = z.object({ prout: z.string() }) -class Test1 { - static readonly schema = Test1Schema - readonly schema = Test1Schema - - prout: string = "heugneu" -} - -const Test2Schema = Test1.schema.extend({ prout: z.literal("ruquier"), ruquier: z.number() }) -const Test2 = extendSchemable(Test1, Test2Schema) - -Test2.schema -new Test2().prout diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..755e573 --- /dev/null +++ b/src/index.ts @@ -0,0 +1,2 @@ +export * from "./SchemableClass" +export * from "./extendSchemable" diff --git a/src/tests.ts b/src/tests.ts index e69de29..f17f90b 100644 --- a/src/tests.ts +++ b/src/tests.ts @@ -0,0 +1,23 @@ +import { z } from "zod" +import { extendSchemable } from "." + + +const Test1Schema = z.object({ prout: z.string() }) +class Test1 { + static readonly schema = Test1Schema + readonly schema = Test1Schema + + static readonly defaultValues = { prout: "heugneu" } + readonly defaultValues = { prout: "heugneu" } + + prout: string = "heugneu" +} + +const Test2 = extendSchemable( + Test1, + schema => schema.extend({ prout: z.literal("ruquier"), ruquier: z.number() }), + defaultValues => ({ prout: "ruquier" as const }), +) + +Test2.defaultValues +new Test2().prout