This commit is contained in:
36
src/ZodSchemaClass.ts
Normal file
36
src/ZodSchemaClass.ts
Normal 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))
|
||||
}
|
||||
@@ -12,13 +12,13 @@ type CreateArgs<Input extends object> = (
|
||||
|
||||
|
||||
export const ZodSchemaObject = <
|
||||
SchemaT extends z.ZodRawShape,
|
||||
SchemaCatchall extends z.ZodTypeAny,
|
||||
SchemaWithDefaultValuesT extends z.ZodRawShape,
|
||||
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
|
||||
SchemaT extends z.ZodRawShape,
|
||||
SchemaCatchall extends z.ZodTypeAny,
|
||||
SchemaWithDefaultValuesT extends z.ZodRawShape,
|
||||
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
|
||||
|
||||
Values extends object,
|
||||
PartialValues extends Partial<Values>,
|
||||
Values extends object,
|
||||
PartialValues extends Partial<Values>,
|
||||
>(
|
||||
schema: z.ZodObject<SchemaT, "strip", SchemaCatchall, Values, Values>,
|
||||
schemaWithDefaultValues: z.ZodObject<SchemaWithDefaultValuesT, "strip", SchemaWithDefaultValuesCatchall, Values, PartialValues>,
|
||||
|
||||
Reference in New Issue
Block a user