0.1.3 #4

Merged
Thilawyn merged 74 commits from next into master 2024-03-24 22:24:25 +01:00
2 changed files with 42 additions and 6 deletions
Showing only changes of commit fe13efa1a3 - Show all commits

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

View File

@@ -12,13 +12,13 @@ type CreateArgs<Input extends object> = (
export const ZodSchemaObject = < export const ZodSchemaObject = <
SchemaT extends z.ZodRawShape, SchemaT extends z.ZodRawShape,
SchemaCatchall extends z.ZodTypeAny, SchemaCatchall extends z.ZodTypeAny,
SchemaWithDefaultValuesT extends z.ZodRawShape, SchemaWithDefaultValuesT extends z.ZodRawShape,
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny, SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
Values extends object, Values extends object,
PartialValues extends Partial<Values>, PartialValues extends Partial<Values>,
>( >(
schema: z.ZodObject<SchemaT, "strip", SchemaCatchall, Values, Values>, schema: z.ZodObject<SchemaT, "strip", SchemaCatchall, Values, Values>,
schemaWithDefaultValues: z.ZodObject<SchemaWithDefaultValuesT, "strip", SchemaWithDefaultValuesCatchall, Values, PartialValues>, schemaWithDefaultValues: z.ZodObject<SchemaWithDefaultValuesT, "strip", SchemaWithDefaultValuesCatchall, Values, PartialValues>,