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 { Trait, TraitExpressionBuilder, expression } from "@thilawyn/traitify-ts"
import { AbstractClass } from "type-fest" import { AbstractClass } from "type-fest"
import { EmptyObject } from "type-fest/source/empty-object"
import { JsonifiableObject } from "type-fest/source/jsonifiable" import { JsonifiableObject } from "type-fest/source/jsonifiable"
import { z } from "zod" import { z } from "zod"
import { JsonifiableZodSchemaObject } from "../traits/JsonifiableZodSchemaObject" import { JsonifiableZodSchemaObject } from "../traits/JsonifiableZodSchemaObject"
import { ZodSchemaObject, ZodSchemaObjectTrait } from "../traits/ZodSchemaObject" import { ZodSchemaObject } from "../traits/ZodSchemaObject"
export class ZodSchemaClassBuilder< export class ZodSchemaClassBuilder<
Superclass extends AbstractClass<object, any[]>, Superclass extends AbstractClass<object>,
const Traits extends readonly Trait<any, any, any, any>[], const Traits extends readonly Trait<any, any, any, any>[],
Schemas extends object,
> { > {
declare ["constructor"]: typeof ZodSchemaClassBuilder declare ["constructor"]: typeof ZodSchemaClassBuilder
constructor(readonly expression: TraitExpressionBuilder<Superclass, Traits>) {} constructor(
protected readonly expression: TraitExpressionBuilder<Superclass, Traits>,
protected readonly schemas: Schemas,
) {}
schema< schema<
@@ -27,9 +32,9 @@ export class ZodSchemaClassBuilder<
Values extends object, Values extends object,
PartialValues extends Partial<Values>, PartialValues extends Partial<Values>,
>( >(
this: ZodSchemaClassBuilder<Superclass, Traits>, this: ZodSchemaClassBuilder<Superclass, Traits, EmptyObject>,
{ schema, schemaWithDefaultValues }: { props: {
schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values> schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>
schemaWithDefaultValues: ( schemaWithDefaultValues: (
@@ -45,16 +50,23 @@ export class ZodSchemaClassBuilder<
return new this.constructor( return new this.constructor(
this.expression this.expression
.extends( .extends(ZodSchemaObjectConstructor as AbstractClass<Values, [values: Values]>)
ZodSchemaObjectConstructor as AbstractClass<Values, [values: Values]> .expresses(ZodSchemaObject(props.schema, props.schemaWithDefaultValues(props.schema))),
)
.expresses(ZodSchemaObject(schema, schemaWithDefaultValues(schema))) {
schema: props.schema,
schemaWithDefaultValues: props.schemaWithDefaultValues(props.schema),
} as const,
) )
} }
jsonifiable< jsonifiable<
Super extends AbstractClass<Values, [values: Values]>, S extends {
T extends readonly [ZodSchemaObjectTrait<SchemaT, SchemaUnknownKeys, SchemaCatchall, SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>], 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, SchemaT extends z.ZodRawShape,
SchemaUnknownKeys extends z.UnknownKeysParam, SchemaUnknownKeys extends z.UnknownKeysParam,
@@ -77,10 +89,12 @@ export class ZodSchemaClassBuilder<
JsonifiedValues extends JsonifiableObject, JsonifiedValues extends JsonifiableObject,
>( >(
this: ZodSchemaClassBuilder< this: ZodSchemaClassBuilder<Superclass, Traits, S | {
Super, readonly schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>,
T | readonly [ZodSchemaObjectTrait<SchemaT, SchemaUnknownKeys, SchemaCatchall, SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>] readonly schemaWithDefaultValues: z.ZodObject<SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>,
>, jsonifySchema?: never
dejsonifySchema?: never
}>,
props: { props: {
jsonifySchema: ( jsonifySchema: (
@@ -95,15 +109,26 @@ export class ZodSchemaClassBuilder<
return new this.constructor( return new this.constructor(
this.expression.expresses( this.expression.expresses(
JsonifiableZodSchemaObject( JsonifiableZodSchemaObject(
this.expression.expressionSuperclass.schema, this.schemas.schema,
this.expression.expressionSuperclass.schemaWithDefaultValues, this.schemas.schemaWithDefaultValues,
props.jsonifySchema(this.expression.expressionSuperclass.schema), props.jsonifySchema(this.schemas.schema),
props.dejsonifySchema(this.expression.expressionSuperclass.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 { Implements } from "@thilawyn/traitify-ts"
import { z } from "zod" import { z } from "zod"
import { zodSchemaClass } from "./builders/ZodSchemaClassBuilder" import { zodSchemaClass } from "./builders/ZodSchemaClassBuilder"
import { dejsonify, jsonify } from "./schema/jsonify"
import { MobXObservableZodSchemaObject } from "./traits/MobXObservableZodSchemaObject" import { MobXObservableZodSchemaObject } from "./traits/MobXObservableZodSchemaObject"
@@ -20,14 +21,14 @@ const exp = zodSchemaClass
}) })
.jsonifiable({ .jsonifiable({
jsonifySchema: s => s.extend({ jsonifySchema: s => s.extend({
id: jsonify.bigint(s.shape.id)
}), }),
dejsonifySchema: s => s.extend({ dejsonifySchema: s => s.extend({
id: dejsonify.bigint(s.shape.id)
}), }),
}) })
.expression .toExpression()
.expresses(MobXObservableZodSchemaObject) .expresses(MobXObservableZodSchemaObject)
.build() .build()
@@ -39,7 +40,8 @@ const inst = User.create({ id: 1n, name: "User" }, )
// console.log(inst.name) // console.log(inst.name)
const instEffect = User.createEffect({ id: 1n, name: "User" }) const instEffect = User.createEffect({ id: 1n, name: "User" })
// const jsonifiedUser = await inst.jsonifyPromise() const jsonifiedUser = await inst.jsonifyPromise()
// const AdminUserProto = User.extend() // const AdminUserProto = User.extend()
// .schema({ // .schema({