0.1.2 #3

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

View File

@@ -1,18 +1,23 @@
import { Trait, TraitExpressionBuilder, expression } from "@thilawyn/traitify-ts"
import { AbstractClass } from "type-fest"
import { EmptyObject } from "type-fest/source/empty-object"
import { JsonifiableObject } from "type-fest/source/jsonifiable"
import { z } from "zod"
import { JsonifiableZodSchemaObject } from "../traits/JsonifiableZodSchemaObject"
import { ZodSchemaObject, ZodSchemaObjectTrait } from "../traits/ZodSchemaObject"
import { ZodSchemaObject } from "../traits/ZodSchemaObject"
export class ZodSchemaClassBuilder<
Superclass extends AbstractClass<object, any[]>,
Superclass extends AbstractClass<object>,
const Traits extends readonly Trait<any, any, any, any>[],
Schemas extends object,
> {
declare ["constructor"]: typeof ZodSchemaClassBuilder
constructor(readonly expression: TraitExpressionBuilder<Superclass, Traits>) {}
constructor(
protected readonly expression: TraitExpressionBuilder<Superclass, Traits>,
protected readonly schemas: Schemas,
) {}
schema<
@@ -27,9 +32,9 @@ export class ZodSchemaClassBuilder<
Values extends object,
PartialValues extends Partial<Values>,
>(
this: ZodSchemaClassBuilder<Superclass, Traits>,
this: ZodSchemaClassBuilder<Superclass, Traits, EmptyObject>,
{ schema, schemaWithDefaultValues }: {
props: {
schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>
schemaWithDefaultValues: (
@@ -45,16 +50,23 @@ export class ZodSchemaClassBuilder<
return new this.constructor(
this.expression
.extends(
ZodSchemaObjectConstructor as AbstractClass<Values, [values: Values]>
)
.expresses(ZodSchemaObject(schema, schemaWithDefaultValues(schema)))
.extends(ZodSchemaObjectConstructor as AbstractClass<Values, [values: Values]>)
.expresses(ZodSchemaObject(props.schema, props.schemaWithDefaultValues(props.schema))),
{
schema: props.schema,
schemaWithDefaultValues: props.schemaWithDefaultValues(props.schema),
} as const,
)
}
jsonifiable<
Super extends AbstractClass<Values, [values: Values]>,
T extends readonly [ZodSchemaObjectTrait<SchemaT, SchemaUnknownKeys, SchemaCatchall, SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>],
S extends {
readonly schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>,
readonly schemaWithDefaultValues: z.ZodObject<SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>,
jsonifySchema?: never
dejsonifySchema?: never
},
SchemaT extends z.ZodRawShape,
SchemaUnknownKeys extends z.UnknownKeysParam,
@@ -77,10 +89,12 @@ export class ZodSchemaClassBuilder<
JsonifiedValues extends JsonifiableObject,
>(
this: ZodSchemaClassBuilder<
Super,
T | readonly [ZodSchemaObjectTrait<SchemaT, SchemaUnknownKeys, SchemaCatchall, SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>]
>,
this: ZodSchemaClassBuilder<Superclass, Traits, S | {
readonly schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>,
readonly schemaWithDefaultValues: z.ZodObject<SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>,
jsonifySchema?: never
dejsonifySchema?: never
}>,
props: {
jsonifySchema: (
@@ -95,15 +109,26 @@ export class ZodSchemaClassBuilder<
return new this.constructor(
this.expression.expresses(
JsonifiableZodSchemaObject(
this.expression.expressionSuperclass.schema,
this.expression.expressionSuperclass.schemaWithDefaultValues,
props.jsonifySchema(this.expression.expressionSuperclass.schema),
props.dejsonifySchema(this.expression.expressionSuperclass.schema),
this.schemas.schema,
this.schemas.schemaWithDefaultValues,
props.jsonifySchema(this.schemas.schema),
props.dejsonifySchema(this.schemas.schema),
)
)
),
{
...this.schemas as S,
jsonifySchema: props.jsonifySchema(this.schemas.schema),
dejsonifySchema: props.dejsonifySchema(this.schemas.schema),
} as const,
)
}
toExpression() {
return this.expression
}
}
export const zodSchemaClass = new ZodSchemaClassBuilder(expression)
export const zodSchemaClass = new ZodSchemaClassBuilder(expression, {})

View File

@@ -1,6 +1,7 @@
import { Implements } from "@thilawyn/traitify-ts"
import { z } from "zod"
import { zodSchemaClass } from "./builders/ZodSchemaClassBuilder"
import { dejsonify, jsonify } from "./schema/jsonify"
import { MobXObservableZodSchemaObject } from "./traits/MobXObservableZodSchemaObject"
@@ -20,14 +21,14 @@ const exp = zodSchemaClass
})
.jsonifiable({
jsonifySchema: s => s.extend({
id: jsonify.bigint(s.shape.id)
}),
dejsonifySchema: s => s.extend({
id: dejsonify.bigint(s.shape.id)
}),
})
.expression
.toExpression()
.expresses(MobXObservableZodSchemaObject)
.build()
@@ -39,7 +40,8 @@ const inst = User.create({ id: 1n, name: "User" }, )
// console.log(inst.name)
const instEffect = User.createEffect({ id: 1n, name: "User" })
// const jsonifiedUser = await inst.jsonifyPromise()
const jsonifiedUser = await inst.jsonifyPromise()
// const AdminUserProto = User.extend()
// .schema({