0.1.3 #4

Merged
Thilawyn merged 74 commits from next into master 2024-03-24 22:24:25 +01:00
3 changed files with 90 additions and 94 deletions
Showing only changes of commit 73303a1748 - Show all commits

View File

@@ -1,15 +1,9 @@
import { expression } from "@thilawyn/traitify-ts" import { expression } from "@thilawyn/traitify-ts"
import { Class } from "type-fest"
import { z } from "zod" import { z } from "zod"
import { ZodSchemaObject } from "./lib" import { ZodSchemaObject } from "./lib"
class ZodSchemaObjectConstructor<Values> {
constructor(values: Values) {
Object.assign(this, values)
}
}
export function ZodSchemaClass< export function ZodSchemaClass<
T extends z.ZodRawShape, T extends z.ZodRawShape,
Catchall extends z.ZodTypeAny, Catchall extends z.ZodTypeAny,
@@ -19,6 +13,12 @@ export function ZodSchemaClass<
schemaWithDefaults: z.ZodObject<T, "strip", Catchall, Values, PartialValues> schemaWithDefaults: z.ZodObject<T, "strip", Catchall, Values, PartialValues>
) { ) {
return expression return expression
.extends(ZodSchemaObjectConstructor<Values>) .extends(
class ZodSchemaObjectConstructor {
constructor(values: Values) {
Object.assign(this, values)
}
} as Class<Values, [values: Values]>
)
.expresses(ZodSchemaObject(schemaWithDefaults)) .expresses(ZodSchemaObject(schemaWithDefaults))
} }

View File

@@ -12,8 +12,8 @@ export const ExtendableZodSchemaObject = <
schemaWithDefaults: z.ZodObject<T, "strip", Catchall, Values, PartialValues>, schemaWithDefaults: z.ZodObject<T, "strip", Catchall, Values, PartialValues>,
) => expression ) => expression
.expresses(ZodSchemaObject(schemaWithDefaults)) .expresses(ZodSchemaObject(schemaWithDefaults))
.buildAnyway() .build()
.subtrait() .subtrait()
.implement(Super => class ExtendableZodSchemaObject extends Super { .implement(Super => class ExtendableZodSchemaObjectImpl extends Super {
}) })
.build() .build()

View File

@@ -1,7 +1,7 @@
import { ImplStatic, trait } from "@thilawyn/traitify-ts" import { ImplStatic, trait } from "@thilawyn/traitify-ts"
import { Class, HasRequiredKeys } from "type-fest" import { Class, HasRequiredKeys } from "type-fest"
import { z } from "zod" import { z } from "zod"
import { StaticMembers, parseZodSchemaEffect, stripZodObjectDefaults } from "../util" import { parseZodSchemaEffect, stripZodObjectDefaults } from "../util"
type CreateArgs<Input extends object> = ( type CreateArgs<Input extends object> = (
@@ -19,93 +19,89 @@ export const ZodSchemaObject = <
>( >(
schemaWithDefaults: z.ZodObject<T, "strip", Catchall, Values, PartialValues>, schemaWithDefaults: z.ZodObject<T, "strip", Catchall, Values, PartialValues>,
) => trait ) => trait
.implement(Super => { .implement(Super => class ZodSchemaObjectImpl extends Super {
class ZodSchemaObject extends Super { static readonly schema = stripZodObjectDefaults(schemaWithDefaults)
static readonly schema = stripZodObjectDefaults(schemaWithDefaults) static readonly schemaWithDefaults = schemaWithDefaults
static readonly schemaWithDefaults = schemaWithDefaults
static transform< static transform<
Instance extends Values, Instance extends Values,
TransformT extends z.ZodRawShape, TransformT extends z.ZodRawShape,
TransformUnknownKeys extends z.UnknownKeysParam, TransformUnknownKeys extends z.UnknownKeysParam,
TransformCatchall extends z.ZodTypeAny, TransformCatchall extends z.ZodTypeAny,
TransformOutput extends Values, TransformOutput extends Values,
TransformInput, TransformInput,
>( >(
this: Class<Instance, [values: Values]>, this: Class<Instance, [values: Values]>,
schema: z.ZodObject<TransformT, TransformUnknownKeys, TransformCatchall, TransformOutput, TransformInput>, schema: z.ZodObject<TransformT, TransformUnknownKeys, TransformCatchall, TransformOutput, TransformInput>,
) { ) {
return schema.transform(values => new this(values)) 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)))
// }
} }
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() .build()