0.1.2 #3

Merged
Thilawyn merged 136 commits from next into master 2024-03-11 19:44:21 +01:00
Showing only changes of commit bddbe428a7 - Show all commits

View File

@@ -1,7 +1,8 @@
import { Trait, TraitExpressionBuilder, expression } from "@thilawyn/traitify-ts" import { Trait, TraitExpressionBuilder } from "@thilawyn/traitify-ts"
import { AbstractClass, Simplify } from "type-fest" import { AbstractClass, Simplify } from "type-fest"
import { JsonifiableObject } from "type-fest/source/jsonifiable" import { JsonifiableObject } from "type-fest/source/jsonifiable"
import { z } from "zod" import { z } from "zod"
import { ZodSchemaClass } from "./shapes/ZodSchemaClass"
import { ExtendableZodSchemaObject } from "./traits/ExtendableZodSchemaObject" 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"
@@ -81,7 +82,15 @@ extends TraitExpressionBuilder<Superclass, Traits> {
} }
jsonifiable< jsonifiable<
Superclass extends AbstractClass<object>, Superclass extends ZodSchemaClass<Instance, SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, DefaultValues>,
Instance extends Values,
SchemaT extends z.ZodRawShape,
SchemaUnknownKeys extends z.UnknownKeysParam,
SchemaCatchall extends z.ZodTypeAny,
Values extends object,
DefaultValues extends Partial<Values>,
JsonifiedValues extends JsonifiableObject, JsonifiedValues extends JsonifiableObject,
@@ -93,78 +102,84 @@ extends TraitExpressionBuilder<Superclass, Traits> {
DejsonifySchemaUnknownKeys extends z.UnknownKeysParam, DejsonifySchemaUnknownKeys extends z.UnknownKeysParam,
DejsonifySchemaCatchall extends z.ZodTypeAny, DejsonifySchemaCatchall extends z.ZodTypeAny,
>( >(
this: ZodSchemaClassBuilder<Superclass, Traits>, this: ZodSchemaClassBuilder<
) { Superclass | ZodSchemaClass<Instance, SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, DefaultValues>,
Traits
>,
} props: {
} jsonifySchema: (props: {
schema: z.ZodObject<
SchemaT,
SchemaUnknownKeys,
SchemaCatchall,
Values,
Values
>
shape: SchemaT
export function ZodSchemaClassOf< }) => z.ZodObject<
Superclass extends AbstractClass<object, []>, JsonifySchemaT,
JsonifySchemaUnknownKeys,
SchemaT extends z.ZodRawShape, JsonifySchemaCatchall,
SchemaUnknownKeys extends z.UnknownKeysParam, JsonifiedValues,
SchemaCatchall extends z.ZodTypeAny, Values
Values extends object,
DefaultValues extends Partial<Values>,
>(
of: Superclass,
{ schema, defaultValues }: {
schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>
defaultValues: DefaultValues
},
) {
class Schemas extends (of as AbstractClass<object, []>) {
static readonly schema = schema
static readonly defaultValues = defaultValues
constructor(values: Values) {
super()
Object.assign(this, values)
}
}
return expression
.extends(Schemas as unknown as (
AbstractClass<
InstanceType<Superclass> &
Simplify<
Extend<[Schemas, Values]>
>,
ConstructorParameters<typeof Schemas>
> &
Simplify<
Extend<[
StaticMembers<Superclass>,
StaticMembers<typeof Schemas>,
]>
> >
))
.expresses( dejsonifySchema: (props: {
InstantiableZodSchemaObject, schema: z.ZodObject<
ExtendableZodSchemaObject, SchemaT,
SchemaUnknownKeys,
SchemaCatchall,
Values,
Values
>
shape: SchemaT
}) => z.ZodObject<
DejsonifySchemaT,
DejsonifySchemaUnknownKeys,
DejsonifySchemaCatchall,
Values,
JsonifiedValues
>
},
) {
const jsonifySchema = props.jsonifySchema({
schema: this.expressionSuperclass.schema,
shape: this.expressionSuperclass.schema.shape,
})
const dejsonifySchema = props.dejsonifySchema({
schema: this.expressionSuperclass.schema,
shape: this.expressionSuperclass.schema.shape,
})
class Schemas extends this.expressionSuperclass {
static readonly jsonifySchema = jsonifySchema
static readonly dejsonifySchema = dejsonifySchema
}
return new ZodSchemaClassBuilder(
Schemas as unknown as (
AbstractClass<
InstanceType<Superclass> &
Simplify<
Extend<[Schemas, Values]>
>,
ConstructorParameters<typeof Schemas>
> &
Simplify<
Extend<[
StaticMembers<Superclass>,
StaticMembers<typeof Schemas>,
]>
>
),
[
...this.expressionTraits,
],
) )
} }
class DefaultRoot {}
export function ZodSchemaClass<
SchemaT extends z.ZodRawShape,
SchemaUnknownKeys extends z.UnknownKeysParam,
SchemaCatchall extends z.ZodTypeAny,
Values extends object,
DefaultValues extends Partial<Values>,
>(
props: {
schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>
defaultValues: DefaultValues
},
) {
return ZodSchemaClassOf(DefaultRoot, props)
} }