ZodSchemaClass
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Julien Valverdé
2024-03-15 03:24:05 +01:00
parent c3c7c0aba5
commit fe13efa1a3
2 changed files with 42 additions and 6 deletions

36
src/ZodSchemaClass.ts Normal file
View File

@@ -0,0 +1,36 @@
import { z } from "zod"
import { ZodSchemaObject } from "./lib"
import { expression } from "@thilawyn/traitify-ts"
import { AbstractClass } from "type-fest"
export function ZodSchemaClass<
SchemaT extends z.ZodRawShape,
SchemaCatchall extends z.ZodTypeAny,
SchemaWithDefaultValuesT extends z.ZodRawShape,
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
Values extends object,
PartialValues extends Partial<Values>,
>(
props: {
schema: z.ZodObject<SchemaT, "strip", SchemaCatchall, Values, Values>
schemaWithDefaultValues: (
schema: z.ZodObject<SchemaT, "strip", SchemaCatchall, Values, Values>
) => z.ZodObject<SchemaWithDefaultValuesT, "strip", SchemaWithDefaultValuesCatchall, Values, PartialValues>
}
) {
const schema = props.schema
const schemaWithDefaultValues = props.schemaWithDefaultValues(props.schema)
abstract class ZodSchemaObjectConstructor {
constructor(values: Values) {
Object.assign(this, values)
}
}
return expression
.extends(ZodSchemaObjectConstructor as AbstractClass<Values, [values: Values]>)
.expresses(ZodSchemaObject(schema, schemaWithDefaultValues))
}