0.1.2 #3

Merged
Thilawyn merged 136 commits from next into master 2024-03-11 19:44:21 +01:00
3 changed files with 102 additions and 100 deletions
Showing only changes of commit 2d69785add - Show all commits

View File

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

View File

@@ -1,9 +1,9 @@
import { trait } from "@thilawyn/traitify-ts"
import { Effect } from "effect"
import { TraitStaticMembers, expression } from "@thilawyn/traitify-ts"
import { HasRequiredKeys } from "type-fest"
import { z } from "zod"
import { ZodSchemaClass } from "../shapes/ZodSchemaClass"
import { parseZodTypeEffect } from "../util"
import { ZodSchemaObjectInstantiationSchemas } from "./ZodSchemaObjectInstantiationSchemas"
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 {
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<
Instance extends Values,
SchemaT extends z.ZodRawShape,
SchemaUnknownKeys extends z.UnknownKeysParam,
SchemaCatchall extends z.ZodTypeAny,
SchemaWithDefaultValuesT extends z.ZodRawShape,
SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam,
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
@@ -72,21 +41,18 @@ export const InstantiableZodSchemaObject = trait
Values extends object,
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>
) {
return new this(
this.schemaWithDefaultValues.parse(values, params)
)
return this.instantiationSchemaWithDefaultValues().parse(values, params)
}
static async createPromise<
Instance extends Values,
SchemaT extends z.ZodRawShape,
SchemaUnknownKeys extends z.UnknownKeysParam,
SchemaCatchall extends z.ZodTypeAny,
SchemaWithDefaultValuesT extends z.ZodRawShape,
SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam,
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
@@ -94,21 +60,18 @@ export const InstantiableZodSchemaObject = trait
Values extends object,
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>
) {
return new this(
await this.schemaWithDefaultValues.parseAsync(values, params)
)
return this.instantiationSchemaWithDefaultValues().parseAsync(values, params)
}
static createEffect<
Instance extends Values,
SchemaT extends z.ZodRawShape,
SchemaUnknownKeys extends z.UnknownKeysParam,
SchemaCatchall extends z.ZodTypeAny,
SchemaWithDefaultValuesT extends z.ZodRawShape,
SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam,
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
@@ -116,12 +79,13 @@ export const InstantiableZodSchemaObject = trait
Values extends object,
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>
) {
return parseZodTypeEffect(this.schemaWithDefaultValues, values, params).pipe(
Effect.map(values => new this(values)),
)
return parseZodTypeEffect(this.instantiationSchemaWithDefaultValues(), values, params)
}
})
.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()