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 106 additions and 68 deletions
Showing only changes of commit ce66ffd0c5 - Show all commits

View File

@@ -12,10 +12,12 @@ import { StaticMembers } from "../util"
export class ZodSchemaClassBuilder< export class ZodSchemaClassBuilder<
Superclass extends AbstractClass<object>, Superclass extends AbstractClass<object>,
const Traits extends readonly Trait<any, any, any, any>[], const Traits extends readonly Trait<any, any, any, any>[],
> > {
extends TraitExpressionBuilder<Superclass, Traits> {
declare ["constructor"]: typeof ZodSchemaClassBuilder declare ["constructor"]: typeof ZodSchemaClassBuilder
constructor(readonly expression: TraitExpressionBuilder<Superclass, Traits>) {}
schema< schema<
Super extends AbstractClass<object, []> & { schema?: never, schemaWithDefaultValues?: never }, Super extends AbstractClass<object, []> & { schema?: never, schemaWithDefaultValues?: never },
@@ -40,7 +42,7 @@ extends TraitExpressionBuilder<Superclass, Traits> {
) => z.ZodObject<SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues> ) => z.ZodObject<SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>
}, },
) { ) {
class ZodSchemaObjectConstructor extends (this.expressionSuperclass as AbstractClass<object, []>) { class ZodSchemaObjectConstructor extends (this.expression.expressionSuperclass as AbstractClass<object, []>) {
constructor(values: Values) { constructor(values: Values) {
super() super()
Object.assign(this, values) Object.assign(this, values)
@@ -48,15 +50,17 @@ extends TraitExpressionBuilder<Superclass, Traits> {
} }
return new this.constructor( return new this.constructor(
ZodSchemaObjectConstructor as unknown as ( this.expression
AbstractClass< .extends(
InstanceType<Super> & Values, ZodSchemaObjectConstructor as unknown as (
[values: Values] AbstractClass<
> & InstanceType<Super> & Values,
StaticMembers<Super> [values: Values]
), > &
StaticMembers<Super>
[...this.expressionTraits, ZodSchemaObject(schema, schemaWithDefaultValues(schema))], )
)
.expresses(ZodSchemaObject(schema, schemaWithDefaultValues(schema)))
) )
} }
@@ -101,10 +105,10 @@ extends TraitExpressionBuilder<Superclass, Traits> {
) => z.ZodObject<DejsonifySchemaT, DejsonifySchemaUnknownKeys, DejsonifySchemaCatchall, Values, JsonifiedValues> ) => z.ZodObject<DejsonifySchemaT, DejsonifySchemaUnknownKeys, DejsonifySchemaCatchall, Values, JsonifiedValues>
}, },
) { ) {
const jsonifySchema = props.jsonifySchema(this.expressionSuperclass.schema) const jsonifySchema = props.jsonifySchema(this.expression.expressionSuperclass.schema)
const dejsonifySchema = props.dejsonifySchema(this.expressionSuperclass.schema) const dejsonifySchema = props.dejsonifySchema(this.expression.expressionSuperclass.schema)
class JsonifiableSchemas extends (this.expressionSuperclass as AbstractClass<object>) { class JsonifiableSchemas extends (this.expression.expressionSuperclass as AbstractClass<object>) {
static readonly jsonifySchema = jsonifySchema static readonly jsonifySchema = jsonifySchema
readonly jsonifySchema = jsonifySchema readonly jsonifySchema = jsonifySchema
static readonly dejsonifySchema = dejsonifySchema static readonly dejsonifySchema = dejsonifySchema
@@ -132,52 +136,5 @@ extends TraitExpressionBuilder<Superclass, Traits> {
} }
} }
export interface ZodSchemaClassBuilder<
Superclass extends AbstractClass<object>,
Traits extends readonly Trait<any, any, any, any>[],
> {
extends<
Super extends AbstractClass<object>
>(
// \/ Ensures `extends` can only be called once at the beginning
this: ZodSchemaClassBuilder<typeof TraitExpression.NullSuperclass, Traits>,
superclass: Super,
): ZodSchemaClassBuilder<Super, Traits>
expresses<
const T extends readonly Trait<
TraitExpression<
typeof TraitExpression.NullSuperclass,
readonly Trait<any, any, any, any>[]
>,
any,
any,
any
>[]
>(
...traits: T
): ZodSchemaClassBuilder<
Superclass,
TraitExpressionBuilder.ExpressesReturnTypeTraits<Traits, T>
>
expressesFirst<
const T extends readonly Trait<
TraitExpression<
typeof TraitExpression.NullSuperclass,
readonly Trait<any, any, any, any>[]
>,
any,
any,
any
>[]
>(
...traits: T
): ZodSchemaClassBuilder<
Superclass,
TraitExpressionBuilder.ExpressesFirstReturnTypeTraits<Traits, T>
>
}
export const zodSchemaClass = new ZodSchemaClassBuilder(TraitExpression.NullSuperclass, []) export const zodSchemaClass = new ZodSchemaClassBuilder(TraitExpression.NullSuperclass, [])

View File

@@ -18,6 +18,7 @@ const exp = zodSchemaClass
id: s.shape.id.default(-1n), id: s.shape.id.default(-1n),
}), }),
}) })
.expression
.expresses(MobXObservableZodSchemaObject) .expresses(MobXObservableZodSchemaObject)
.build() .build()

View File

@@ -1,11 +1,91 @@
import { trait } from "@thilawyn/traitify-ts" import { ImplStatic, expression } from "@thilawyn/traitify-ts"
import { Class } 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 { JsonifiableZodSchemas } from "../shapes/JsonifiableZodSchemaClass" import { parseZodSchemaEffect } from "../util"
import { parseZodTypeEffect } from "../util" import { ZodSchemaObject } from "./ZodSchemaObject"
export const JsonifiableZodSchemaObject = trait export const JsonifiableZodSchemaObject = <
.implement(Super => class JsonifiableZodSchemaObject extends Super { SchemaT extends z.ZodRawShape,
}) SchemaUnknownKeys extends z.UnknownKeysParam,
SchemaCatchall extends z.ZodTypeAny,
SchemaWithDefaultValuesT extends z.ZodRawShape,
SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam,
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
Values extends object,
PartialValues extends Partial<Values>,
JsonifySchemaT extends z.ZodRawShape,
JsonifySchemaUnknownKeys extends z.UnknownKeysParam,
JsonifySchemaCatchall extends z.ZodTypeAny,
DejsonifySchemaT extends z.ZodRawShape,
DejsonifySchemaUnknownKeys extends z.UnknownKeysParam,
DejsonifySchemaCatchall extends z.ZodTypeAny,
JsonifiedValues extends JsonifiableObject,
>(
schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>,
schemaWithDefaultValues: z.ZodObject<SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>,
jsonifySchema: z.ZodObject<JsonifySchemaT, JsonifySchemaUnknownKeys, JsonifySchemaCatchall, JsonifiedValues, Values>,
dejsonifySchema: z.ZodObject<DejsonifySchemaT, DejsonifySchemaUnknownKeys, DejsonifySchemaCatchall, Values, JsonifiedValues>,
) => expression
.expresses(ZodSchemaObject(schema, schemaWithDefaultValues))
.build() .build()
.subtrait()
.implement(Super => class JsonifiableZodSchemaObject extends Super {
static readonly jsonifySchema = jsonifySchema
static readonly dejsonifySchema = dejsonifySchema
static dejsonify<
Instance extends Values
>(
this: (
Class<Instance, [values: Values]> &
ImplStatic<typeof JsonifiableZodSchemaObject>
),
values: JsonifiedValues,
params?: Partial<z.ParseParams>,
) {
return this
.transform(this.dejsonifySchema)
.parse(values, params)
}
static dejsonifyPromise<
Instance extends Values
>(
this: (
Class<Instance, [values: Values]> &
ImplStatic<typeof JsonifiableZodSchemaObject>
),
values: JsonifiedValues,
params?: Partial<z.ParseParams>,
) {
return this
.transform(this.dejsonifySchema)
.parseAsync(values, params)
}
static dejsonifyEffect<
Instance extends Values
>(
this: (
Class<Instance, [values: Values]> &
ImplStatic<typeof JsonifiableZodSchemaObject>
),
values: JsonifiedValues,
params?: Partial<z.ParseParams>,
) {
return parseZodSchemaEffect(
this.transform(this.dejsonifySchema),
values,
params,
)
}
})
.build()