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 66 additions and 64 deletions
Showing only changes of commit 0e956c0c78 - Show all commits

View File

@@ -7,6 +7,7 @@ import { DejsonifiableZodSchemaObject } from "../traits/DejsonifiableZodSchemaOb
import { ExtendableZodSchemaObject } from "../traits/ExtendableZodSchemaObject" import { ExtendableZodSchemaObject } from "../traits/ExtendableZodSchemaObject"
import { InstantiableZodSchemaObject } from "../traits/InstantiableZodSchemaObject" import { InstantiableZodSchemaObject } from "../traits/InstantiableZodSchemaObject"
import { JsonifiableZodSchemaObject } from "../traits/JsonifiableZodSchemaObject" import { JsonifiableZodSchemaObject } from "../traits/JsonifiableZodSchemaObject"
import { ZodSchemaObjectInstantiationSchemas } from "../traits/ZodSchemaObjectInstantiationSchemas"
import { StaticMembers } from "../util" import { StaticMembers } from "../util"
@@ -63,7 +64,7 @@ extends TraitExpressionBuilder<Superclass, Traits> {
[ [
...this.expressionTraits, ...this.expressionTraits,
// ZodSchemaObjectInstantiationSchemas, ZodSchemaObjectInstantiationSchemas,
InstantiableZodSchemaObject, InstantiableZodSchemaObject,
ExtendableZodSchemaObject, ExtendableZodSchemaObject,
], ],

View File

@@ -41,6 +41,8 @@ const test = User.schema.extend({
) )
const inst = User.create({ id: 1n, name: "" }) const inst = User.create({ id: 1n, name: "" })
const instEffect = User.createEffect({ id: 1n, name: "" })
// console.log(inst) // console.log(inst)
const jsonifiedUser = await inst.jsonifyPromise() const jsonifiedUser = await inst.jsonifyPromise()

View File

@@ -1,4 +1,4 @@
import { TraitStaticMembers, expression } from "@thilawyn/traitify-ts" import { TraitStaticMembers, trait } from "@thilawyn/traitify-ts"
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"
@@ -6,6 +6,8 @@ import { parseZodTypeEffect } from "../util"
import { ZodSchemaObjectInstantiationSchemas } from "./ZodSchemaObjectInstantiationSchemas" import { ZodSchemaObjectInstantiationSchemas } from "./ZodSchemaObjectInstantiationSchemas"
type ZodSchemaObjectInstantiationSchemasStaticMembers = TraitStaticMembers<typeof ZodSchemaObjectInstantiationSchemas>
type NewZodSchemaInstanceInput< type NewZodSchemaInstanceInput<
Values extends object, Values extends object,
DefaultValues extends Partial<Values>, DefaultValues extends Partial<Values>,
@@ -26,66 +28,63 @@ type NewZodSchemaInstanceArgs<Input extends object> = (
) )
export const InstantiableZodSchemaObject = expression export const InstantiableZodSchemaObject = trait
.expresses(ZodSchemaObjectInstantiationSchemas) .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> &
ZodSchemaObjectInstantiationSchemasStaticMembers
),
...[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> &
ZodSchemaObjectInstantiationSchemasStaticMembers
),
...[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> &
ZodSchemaObjectInstantiationSchemasStaticMembers
),
...[values, params]: NewZodSchemaInstanceArgs<PartialValues>
) {
return parseZodTypeEffect(this.instantiationSchemaWithDefaultValues(), values, params)
}
})
.build() .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()