This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import { expression } from "@thilawyn/traitify-ts"
|
import { expression } from "@thilawyn/traitify-ts"
|
||||||
import { AbstractClass } from "type-fest"
|
import { AbstractClass } from "type-fest"
|
||||||
import { z } from "zod"
|
import { z } from "zod"
|
||||||
|
import { ExtendableZodSchemaObject } from "./traits/ExtendableZodSchemaObject"
|
||||||
import { InstantiableZodSchemaObject } from "./traits/InstantiableZodSchemaObject"
|
import { InstantiableZodSchemaObject } from "./traits/InstantiableZodSchemaObject"
|
||||||
import { Extend, StaticMembers } from "./util"
|
import { Extend, StaticMembers } from "./util"
|
||||||
|
|
||||||
@@ -32,7 +33,10 @@ export function ZodSchemaClassOf<
|
|||||||
Object.assign(this, values)
|
Object.assign(this, values)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.expresses(InstantiableZodSchemaObject)
|
.expresses(
|
||||||
|
InstantiableZodSchemaObject,
|
||||||
|
ExtendableZodSchemaObject,
|
||||||
|
)
|
||||||
.build()
|
.build()
|
||||||
|
|
||||||
return exp.extends as AbstractClass<
|
return exp.extends as AbstractClass<
|
||||||
|
|||||||
18
src/tests.ts
18
src/tests.ts
@@ -26,17 +26,17 @@ const inst = Test.create({ id: 1n, name: "" })
|
|||||||
console.log(inst)
|
console.log(inst)
|
||||||
|
|
||||||
|
|
||||||
// class SubTest extends Test.extend({
|
class SubTest extends Test.extend({
|
||||||
// schema: ({ schema }) => schema.extend({
|
schema: ({ schema }) => schema.extend({
|
||||||
// prout: z.string()
|
prout: z.string()
|
||||||
// }),
|
}),
|
||||||
|
|
||||||
// defaultValues: defaultValues => defineDefaultValues({
|
defaultValues: defaultValues => ({
|
||||||
// ...defaultValues
|
...defaultValues
|
||||||
// }),
|
}),
|
||||||
// }) {}
|
}) {}
|
||||||
|
|
||||||
// const subInst = await SubTest.createPromise({ name: "", prout: "" })
|
const subInst = await SubTest.createPromise({ name: "", prout: "" })
|
||||||
|
|
||||||
// console.log(subInst)
|
// console.log(subInst)
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
import { abstract, trait } from "@thilawyn/traitify-ts"
|
import { abstract, trait } from "@thilawyn/traitify-ts"
|
||||||
|
import { AbstractClass } from "type-fest"
|
||||||
import { z } from "zod"
|
import { z } from "zod"
|
||||||
import { ZodSchemaAbstractClass } from "../types/ZodSchemaClass"
|
import { ZodSchemaAbstractClass } from "../types/ZodSchemaClass"
|
||||||
|
import { Extend, StaticMembers } from "../util"
|
||||||
|
|
||||||
|
|
||||||
export const ExtendableZodSchemaObject = trait(
|
export const ExtendableZodSchemaObject = trait(
|
||||||
@@ -32,8 +34,26 @@ export const ExtendableZodSchemaObject = trait(
|
|||||||
|
|
||||||
defaultValues: (defaultValues: SuperDefaultValues) => DefaultValues
|
defaultValues: (defaultValues: SuperDefaultValues) => DefaultValues
|
||||||
},
|
},
|
||||||
|
): (
|
||||||
|
AbstractClass<
|
||||||
|
Extend<[InstanceType<Super>, Values]>,
|
||||||
|
[values: Values]
|
||||||
|
> &
|
||||||
|
Extend<[
|
||||||
|
StaticMembers<Super>,
|
||||||
|
{
|
||||||
|
readonly schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>,
|
||||||
|
readonly defaultValues: DefaultValues,
|
||||||
|
},
|
||||||
|
]>
|
||||||
) {
|
) {
|
||||||
|
const schema = props.schema({ schema: this.schema, shape: this.schema.shape })
|
||||||
|
const defaultValues = props.defaultValues(this.defaultValues)
|
||||||
|
|
||||||
|
return class extends this {
|
||||||
|
static readonly schema = schema
|
||||||
|
static readonly defaultValues = defaultValues
|
||||||
|
} as any
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,11 +1,31 @@
|
|||||||
import { abstract, trait } from "@thilawyn/traitify-ts"
|
import { abstract, trait } from "@thilawyn/traitify-ts"
|
||||||
import { Effect, pipe } from "effect"
|
import { Effect, pipe } from "effect"
|
||||||
|
import { HasRequiredKeys } from "type-fest"
|
||||||
import { z } from "zod"
|
import { z } from "zod"
|
||||||
import { NewZodSchemaInstanceArgs, NewZodSchemaInstanceInput } from ".."
|
|
||||||
import { ZodSchemaClass } from "../types/ZodSchemaClass"
|
import { ZodSchemaClass } from "../types/ZodSchemaClass"
|
||||||
import { parseZodTypeEffect } from "../util"
|
import { parseZodTypeEffect } from "../util"
|
||||||
|
|
||||||
|
|
||||||
|
type NewZodSchemaInstanceInput<
|
||||||
|
Values extends {},
|
||||||
|
DefaultValues extends Partial<Values>,
|
||||||
|
> = {
|
||||||
|
[Key in Exclude<keyof Values, keyof DefaultValues>]: Values[Key]
|
||||||
|
} & {
|
||||||
|
[Key in keyof DefaultValues]?: Key extends keyof Values
|
||||||
|
? Values[Key]
|
||||||
|
: never
|
||||||
|
}
|
||||||
|
|
||||||
|
type ParseParamsArgs = [] | [params: Partial<z.ParseParams>]
|
||||||
|
|
||||||
|
type NewZodSchemaInstanceArgs<Input extends object> = (
|
||||||
|
HasRequiredKeys<Input> extends true
|
||||||
|
? [values: Input, ...args: ParseParamsArgs]
|
||||||
|
: [] | [values: Input, ...args: ParseParamsArgs]
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
export const InstantiableZodSchemaObject = trait(
|
export const InstantiableZodSchemaObject = trait(
|
||||||
abstract(),
|
abstract(),
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user