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

This commit is contained in:
Julien Valverdé
2024-02-27 03:44:08 +01:00
parent a6414b680c
commit 2d69785add
3 changed files with 102 additions and 100 deletions

View File

@@ -63,6 +63,7 @@ extends TraitExpressionBuilder<Superclass, Traits> {
[ [
...this.expressionTraits, ...this.expressionTraits,
// ZodSchemaObjectInstantiationSchemas,
InstantiableZodSchemaObject, InstantiableZodSchemaObject,
ExtendableZodSchemaObject, ExtendableZodSchemaObject,
], ],

View File

@@ -1,9 +1,9 @@
import { trait } from "@thilawyn/traitify-ts" import { TraitStaticMembers, expression } from "@thilawyn/traitify-ts"
import { Effect } from "effect"
import { HasRequiredKeys } from "type-fest" import { HasRequiredKeys } from "type-fest"
import { z } from "zod" import { z } from "zod"
import { ZodSchemaClass } from "../shapes/ZodSchemaClass" import { ZodSchemaClass } from "../shapes/ZodSchemaClass"
import { parseZodTypeEffect } from "../util" import { parseZodTypeEffect } from "../util"
import { ZodSchemaObjectInstantiationSchemas } from "./ZodSchemaObjectInstantiationSchemas"
type NewZodSchemaInstanceInput< type NewZodSchemaInstanceInput<
@@ -26,45 +26,14 @@ type NewZodSchemaInstanceArgs<Input extends object> = (
) )
export const InstantiableZodSchemaObject = trait export const InstantiableZodSchemaObject = expression
.expresses(ZodSchemaObjectInstantiationSchemas)
.build()
.subtrait()
.implement(Super => class InstantiableZodSchemaObject extends Super { .implement(Super => class InstantiableZodSchemaObject extends Super {
static instantiationSchema<
Instance extends Values,
SchemaT extends z.ZodRawShape,
SchemaUnknownKeys extends z.UnknownKeysParam,
SchemaCatchall extends z.ZodTypeAny,
Values extends object,
>(
this: ZodSchemaClass<Instance, SchemaT, SchemaUnknownKeys, SchemaCatchall, any, any, any, Values, any>,
) {
return this.schema.transform(values => new this(values))
}
static instantiationSchemaWithDefaultValues<
Instance extends Values,
SchemaWithDefaultValuesT extends z.ZodRawShape,
SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam,
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
Values extends object,
PartialValues extends Partial<Values>,
>(
this: ZodSchemaClass<Instance, any, any, any, SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>,
) {
return this.schemaWithDefaultValues.transform(values => new this(values))
}
static create< static create<
Instance extends Values, Instance extends Values,
SchemaT extends z.ZodRawShape,
SchemaUnknownKeys extends z.UnknownKeysParam,
SchemaCatchall extends z.ZodTypeAny,
SchemaWithDefaultValuesT extends z.ZodRawShape, SchemaWithDefaultValuesT extends z.ZodRawShape,
SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam, SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam,
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny, SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
@@ -72,21 +41,18 @@ export const InstantiableZodSchemaObject = trait
Values extends object, Values extends object,
PartialValues extends Partial<Values>, PartialValues extends Partial<Values>,
>( >(
this: ZodSchemaClass<Instance, SchemaT, SchemaUnknownKeys, SchemaCatchall, SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>, this: (
ZodSchemaClass<Instance, any, any, any, SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues> &
TraitStaticMembers<typeof ZodSchemaObjectInstantiationSchemas>
),
...[values, params]: NewZodSchemaInstanceArgs<PartialValues> ...[values, params]: NewZodSchemaInstanceArgs<PartialValues>
) { ) {
return new this( return this.instantiationSchemaWithDefaultValues().parse(values, params)
this.schemaWithDefaultValues.parse(values, params)
)
} }
static async createPromise< static async createPromise<
Instance extends Values, Instance extends Values,
SchemaT extends z.ZodRawShape,
SchemaUnknownKeys extends z.UnknownKeysParam,
SchemaCatchall extends z.ZodTypeAny,
SchemaWithDefaultValuesT extends z.ZodRawShape, SchemaWithDefaultValuesT extends z.ZodRawShape,
SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam, SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam,
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny, SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
@@ -94,21 +60,18 @@ export const InstantiableZodSchemaObject = trait
Values extends object, Values extends object,
PartialValues extends Partial<Values>, PartialValues extends Partial<Values>,
>( >(
this: ZodSchemaClass<Instance, SchemaT, SchemaUnknownKeys, SchemaCatchall, SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>, this: (
ZodSchemaClass<Instance, any, any, any, SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues> &
TraitStaticMembers<typeof ZodSchemaObjectInstantiationSchemas>
),
...[values, params]: NewZodSchemaInstanceArgs<PartialValues> ...[values, params]: NewZodSchemaInstanceArgs<PartialValues>
) { ) {
return new this( return this.instantiationSchemaWithDefaultValues().parseAsync(values, params)
await this.schemaWithDefaultValues.parseAsync(values, params)
)
} }
static createEffect< static createEffect<
Instance extends Values, Instance extends Values,
SchemaT extends z.ZodRawShape,
SchemaUnknownKeys extends z.UnknownKeysParam,
SchemaCatchall extends z.ZodTypeAny,
SchemaWithDefaultValuesT extends z.ZodRawShape, SchemaWithDefaultValuesT extends z.ZodRawShape,
SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam, SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam,
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny, SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
@@ -116,12 +79,13 @@ export const InstantiableZodSchemaObject = trait
Values extends object, Values extends object,
PartialValues extends Partial<Values>, PartialValues extends Partial<Values>,
>( >(
this: ZodSchemaClass<Instance, SchemaT, SchemaUnknownKeys, SchemaCatchall, SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>, this: (
ZodSchemaClass<Instance, any, any, any, SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues> &
TraitStaticMembers<typeof ZodSchemaObjectInstantiationSchemas>
),
...[values, params]: NewZodSchemaInstanceArgs<PartialValues> ...[values, params]: NewZodSchemaInstanceArgs<PartialValues>
) { ) {
return parseZodTypeEffect(this.schemaWithDefaultValues, values, params).pipe( return parseZodTypeEffect(this.instantiationSchemaWithDefaultValues(), values, params)
Effect.map(values => new this(values)),
)
} }
}) })
.build() .build()

View File

@@ -0,0 +1,37 @@
import { trait } from "@thilawyn/traitify-ts"
import { z } from "zod"
import { ZodSchemaClass } from "../shapes/ZodSchemaClass"
export const ZodSchemaObjectInstantiationSchemas = trait
.implement(Super => class ZodSchemaObjectInstantiationSchemas extends Super {
static instantiationSchema<
Instance extends Values,
SchemaT extends z.ZodRawShape,
SchemaUnknownKeys extends z.UnknownKeysParam,
SchemaCatchall extends z.ZodTypeAny,
Values extends object,
>(
this: ZodSchemaClass<Instance, SchemaT, SchemaUnknownKeys, SchemaCatchall, any, any, any, Values, any>,
) {
return this.schema.transform(values => new this(values))
}
static instantiationSchemaWithDefaultValues<
Instance extends Values,
SchemaWithDefaultValuesT extends z.ZodRawShape,
SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam,
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
Values extends object,
PartialValues extends Partial<Values>,
>(
this: ZodSchemaClass<Instance, any, any, any, SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>,
) {
return this.schemaWithDefaultValues.transform(values => new this(values))
}
})
.build()