This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { ImplStatic, trait } from "@thilawyn/traitify-ts"
|
||||
import { Class, HasRequiredKeys } from "type-fest"
|
||||
import { z } from "zod"
|
||||
import { StaticMembers, parseZodSchemaEffect, stripZodObjectDefaults } from "../util"
|
||||
import { parseZodSchemaEffect, stripZodObjectDefaults } from "../util"
|
||||
|
||||
|
||||
type CreateArgs<Input extends object> = (
|
||||
@@ -19,93 +19,89 @@ export const ZodSchemaObject = <
|
||||
>(
|
||||
schemaWithDefaults: z.ZodObject<T, "strip", Catchall, Values, PartialValues>,
|
||||
) => trait
|
||||
.implement(Super => {
|
||||
class ZodSchemaObject extends Super {
|
||||
static readonly schema = stripZodObjectDefaults(schemaWithDefaults)
|
||||
static readonly schemaWithDefaults = schemaWithDefaults
|
||||
.implement(Super => class ZodSchemaObjectImpl extends Super {
|
||||
static readonly schema = stripZodObjectDefaults(schemaWithDefaults)
|
||||
static readonly schemaWithDefaults = schemaWithDefaults
|
||||
|
||||
static transform<
|
||||
Instance extends Values,
|
||||
static transform<
|
||||
Instance extends Values,
|
||||
|
||||
TransformT extends z.ZodRawShape,
|
||||
TransformUnknownKeys extends z.UnknownKeysParam,
|
||||
TransformCatchall extends z.ZodTypeAny,
|
||||
TransformOutput extends Values,
|
||||
TransformInput,
|
||||
>(
|
||||
this: Class<Instance, [values: Values]>,
|
||||
schema: z.ZodObject<TransformT, TransformUnknownKeys, TransformCatchall, TransformOutput, TransformInput>,
|
||||
) {
|
||||
return schema.transform(values => new this(values))
|
||||
}
|
||||
|
||||
|
||||
static create<
|
||||
Instance extends Values
|
||||
>(
|
||||
this: (
|
||||
Class<Instance, [values: Values]> &
|
||||
ImplStatic<typeof ZodSchemaObject>
|
||||
),
|
||||
...[values, params]: CreateArgs<PartialValues>
|
||||
) {
|
||||
return this
|
||||
.transform(this.schemaWithDefaults)
|
||||
.parse(values, params)
|
||||
}
|
||||
|
||||
static createPromise<
|
||||
Instance extends Values
|
||||
>(
|
||||
this: (
|
||||
Class<Instance, [values: Values]> &
|
||||
ImplStatic<typeof ZodSchemaObject>
|
||||
),
|
||||
...[values, params]: CreateArgs<PartialValues>
|
||||
) {
|
||||
return this
|
||||
.transform(this.schemaWithDefaults)
|
||||
.parseAsync(values, params)
|
||||
}
|
||||
|
||||
static createEffect<
|
||||
Instance extends Values
|
||||
>(
|
||||
this: (
|
||||
Class<Instance, [values: Values]> &
|
||||
ImplStatic<typeof ZodSchemaObject>
|
||||
),
|
||||
...[values, params]: CreateArgs<PartialValues>
|
||||
) {
|
||||
return parseZodSchemaEffect(
|
||||
this.transform(this.schemaWithDefaults),
|
||||
values,
|
||||
params,
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
// static extend<
|
||||
// Self extends AbstractClass<ZodSchemaObject> & ImplStatic<typeof ZodSchemaObject>,
|
||||
|
||||
// ExtendedT extends z.ZodRawShape,
|
||||
// ExtendedCatchall extends z.ZodTypeAny,
|
||||
// ExtendedValues extends Values,
|
||||
// ExtendedPartialValues extends Partial<ExtendedValues>,
|
||||
// >(
|
||||
// this: Self,
|
||||
|
||||
// schemaWithDefaults: (
|
||||
// schemaWithDefaults: typeof this.schemaWithDefaults
|
||||
// ) => z.ZodObject<ExtendedT, "strip", ExtendedCatchall, ExtendedValues, ExtendedPartialValues>,
|
||||
// ) {
|
||||
// return expression
|
||||
// .extends(this)
|
||||
// .expresses(ZodSchemaObject(schemaWithDefaults(this.schemaWithDefaults)))
|
||||
// }
|
||||
TransformT extends z.ZodRawShape,
|
||||
TransformUnknownKeys extends z.UnknownKeysParam,
|
||||
TransformCatchall extends z.ZodTypeAny,
|
||||
TransformOutput extends Values,
|
||||
TransformInput,
|
||||
>(
|
||||
this: Class<Instance, [values: Values]>,
|
||||
schema: z.ZodObject<TransformT, TransformUnknownKeys, TransformCatchall, TransformOutput, TransformInput>,
|
||||
) {
|
||||
return schema.transform(values => new this(values))
|
||||
}
|
||||
|
||||
return ZodSchemaObject as Class<ZodSchemaObject & Values> & StaticMembers<typeof ZodSchemaObject>
|
||||
|
||||
static create<
|
||||
Instance extends Values
|
||||
>(
|
||||
this: (
|
||||
Class<Instance, [values: Values]> &
|
||||
ImplStatic<typeof ZodSchemaObjectImpl>
|
||||
),
|
||||
...[values, params]: CreateArgs<PartialValues>
|
||||
) {
|
||||
return this
|
||||
.transform(this.schemaWithDefaults)
|
||||
.parse(values, params)
|
||||
}
|
||||
|
||||
static createPromise<
|
||||
Instance extends Values
|
||||
>(
|
||||
this: (
|
||||
Class<Instance, [values: Values]> &
|
||||
ImplStatic<typeof ZodSchemaObjectImpl>
|
||||
),
|
||||
...[values, params]: CreateArgs<PartialValues>
|
||||
) {
|
||||
return this
|
||||
.transform(this.schemaWithDefaults)
|
||||
.parseAsync(values, params)
|
||||
}
|
||||
|
||||
static createEffect<
|
||||
Instance extends Values
|
||||
>(
|
||||
this: (
|
||||
Class<Instance, [values: Values]> &
|
||||
ImplStatic<typeof ZodSchemaObjectImpl>
|
||||
),
|
||||
...[values, params]: CreateArgs<PartialValues>
|
||||
) {
|
||||
return parseZodSchemaEffect(
|
||||
this.transform(this.schemaWithDefaults),
|
||||
values,
|
||||
params,
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
// static extend<
|
||||
// Self extends AbstractClass<ZodSchemaObject> & ImplStatic<typeof ZodSchemaObject>,
|
||||
|
||||
// ExtendedT extends z.ZodRawShape,
|
||||
// ExtendedCatchall extends z.ZodTypeAny,
|
||||
// ExtendedValues extends Values,
|
||||
// ExtendedPartialValues extends Partial<ExtendedValues>,
|
||||
// >(
|
||||
// this: Self,
|
||||
|
||||
// schemaWithDefaults: (
|
||||
// schemaWithDefaults: typeof this.schemaWithDefaults
|
||||
// ) => z.ZodObject<ExtendedT, "strip", ExtendedCatchall, ExtendedValues, ExtendedPartialValues>,
|
||||
// ) {
|
||||
// return expression
|
||||
// .extends(this)
|
||||
// .expresses(ZodSchemaObject(schemaWithDefaults(this.schemaWithDefaults)))
|
||||
// }
|
||||
})
|
||||
.build()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user