0.1.2 #3
@@ -63,6 +63,7 @@ extends TraitExpressionBuilder<Superclass, Traits> {
|
||||
|
||||
[
|
||||
...this.expressionTraits,
|
||||
// ZodSchemaObjectInstantiationSchemas,
|
||||
InstantiableZodSchemaObject,
|
||||
ExtendableZodSchemaObject,
|
||||
],
|
||||
|
||||
@@ -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,102 +26,66 @@ type NewZodSchemaInstanceArgs<Input extends object> = (
|
||||
)
|
||||
|
||||
|
||||
export const InstantiableZodSchemaObject = trait
|
||||
.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,
|
||||
|
||||
Values extends object,
|
||||
PartialValues extends Partial<Values>,
|
||||
>(
|
||||
this: ZodSchemaClass<Instance, SchemaT, SchemaUnknownKeys, SchemaCatchall, SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>,
|
||||
...[values, params]: NewZodSchemaInstanceArgs<PartialValues>
|
||||
) {
|
||||
return new this(
|
||||
this.schemaWithDefaultValues.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,
|
||||
|
||||
Values extends object,
|
||||
PartialValues extends Partial<Values>,
|
||||
>(
|
||||
this: ZodSchemaClass<Instance, SchemaT, SchemaUnknownKeys, SchemaCatchall, SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>,
|
||||
...[values, params]: NewZodSchemaInstanceArgs<PartialValues>
|
||||
) {
|
||||
return new this(
|
||||
await this.schemaWithDefaultValues.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,
|
||||
|
||||
Values extends object,
|
||||
PartialValues extends Partial<Values>,
|
||||
>(
|
||||
this: ZodSchemaClass<Instance, SchemaT, SchemaUnknownKeys, SchemaCatchall, SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>,
|
||||
...[values, params]: NewZodSchemaInstanceArgs<PartialValues>
|
||||
) {
|
||||
return parseZodTypeEffect(this.schemaWithDefaultValues, values, params).pipe(
|
||||
Effect.map(values => new this(values)),
|
||||
)
|
||||
}
|
||||
})
|
||||
export const InstantiableZodSchemaObject = expression
|
||||
.expresses(ZodSchemaObjectInstantiationSchemas)
|
||||
.build()
|
||||
.subtrait()
|
||||
.implement(Super => class InstantiableZodSchemaObject extends Super {
|
||||
static create<
|
||||
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> &
|
||||
TraitStaticMembers<typeof ZodSchemaObjectInstantiationSchemas>
|
||||
),
|
||||
...[values, params]: NewZodSchemaInstanceArgs<PartialValues>
|
||||
) {
|
||||
return this.instantiationSchemaWithDefaultValues().parse(values, params)
|
||||
}
|
||||
|
||||
static async createPromise<
|
||||
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> &
|
||||
TraitStaticMembers<typeof ZodSchemaObjectInstantiationSchemas>
|
||||
),
|
||||
...[values, params]: NewZodSchemaInstanceArgs<PartialValues>
|
||||
) {
|
||||
return this.instantiationSchemaWithDefaultValues().parseAsync(values, params)
|
||||
}
|
||||
|
||||
static createEffect<
|
||||
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> &
|
||||
TraitStaticMembers<typeof ZodSchemaObjectInstantiationSchemas>
|
||||
),
|
||||
...[values, params]: NewZodSchemaInstanceArgs<PartialValues>
|
||||
) {
|
||||
return parseZodTypeEffect(this.instantiationSchemaWithDefaultValues(), values, params)
|
||||
}
|
||||
})
|
||||
.build()
|
||||
|
||||
37
src/traits/ZodSchemaObjectInstantiationSchemas.ts
Normal file
37
src/traits/ZodSchemaObjectInstantiationSchemas.ts
Normal 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()
|
||||
Reference in New Issue
Block a user