This commit is contained in:
@@ -3,6 +3,7 @@ import { AbstractClass, Simplify } from "type-fest"
|
||||
import { JsonifiableObject } from "type-fest/source/jsonifiable"
|
||||
import { z } from "zod"
|
||||
import { ZodSchemaAbstractClass } from "./shapes/ZodSchemaClass"
|
||||
import { DejsonifiableZodSchemaObject } from "./traits/DejsonifiableZodSchemaObject"
|
||||
import { ExtendableZodSchemaObject } from "./traits/ExtendableZodSchemaObject"
|
||||
import { InstantiableZodSchemaObject } from "./traits/InstantiableZodSchemaObject"
|
||||
import { Extend, StaticMembers } from "./util"
|
||||
@@ -135,6 +136,7 @@ extends TraitExpressionBuilder<Superclass, Traits> {
|
||||
|
||||
[
|
||||
...this.expressionTraits,
|
||||
DejsonifiableZodSchemaObject,
|
||||
],
|
||||
)
|
||||
}
|
||||
|
||||
@@ -6,9 +6,6 @@ import { z } from "zod"
|
||||
export type JsonifiableZodSchemaClass<
|
||||
Instance extends Values,
|
||||
|
||||
Values extends object,
|
||||
JsonifiedValues extends JsonifiableObject,
|
||||
|
||||
JsonifySchemaT extends z.ZodRawShape,
|
||||
JsonifySchemaUnknownKeys extends z.UnknownKeysParam,
|
||||
JsonifySchemaCatchall extends z.ZodTypeAny,
|
||||
@@ -16,28 +13,28 @@ export type JsonifiableZodSchemaClass<
|
||||
DejsonifySchemaT extends z.ZodRawShape,
|
||||
DejsonifySchemaUnknownKeys extends z.UnknownKeysParam,
|
||||
DejsonifySchemaCatchall extends z.ZodTypeAny,
|
||||
|
||||
Values extends object,
|
||||
JsonifiedValues extends JsonifiableObject,
|
||||
> = (
|
||||
Class<Instance, [values: Values]> &
|
||||
JsonifiableZodSchemaClassStatic<
|
||||
Values,
|
||||
JsonifiedValues,
|
||||
|
||||
JsonifySchemaT,
|
||||
JsonifySchemaUnknownKeys,
|
||||
JsonifySchemaCatchall,
|
||||
|
||||
DejsonifySchemaT,
|
||||
DejsonifySchemaUnknownKeys,
|
||||
DejsonifySchemaCatchall
|
||||
DejsonifySchemaCatchall,
|
||||
|
||||
Values,
|
||||
JsonifiedValues
|
||||
>
|
||||
)
|
||||
|
||||
export type JsonifiableZodSchemaAbstractClass<
|
||||
Instance extends Values,
|
||||
|
||||
Values extends object,
|
||||
JsonifiedValues extends JsonifiableObject,
|
||||
|
||||
JsonifySchemaT extends z.ZodRawShape,
|
||||
JsonifySchemaUnknownKeys extends z.UnknownKeysParam,
|
||||
JsonifySchemaCatchall extends z.ZodTypeAny,
|
||||
@@ -45,26 +42,26 @@ export type JsonifiableZodSchemaAbstractClass<
|
||||
DejsonifySchemaT extends z.ZodRawShape,
|
||||
DejsonifySchemaUnknownKeys extends z.UnknownKeysParam,
|
||||
DejsonifySchemaCatchall extends z.ZodTypeAny,
|
||||
|
||||
Values extends object,
|
||||
JsonifiedValues extends JsonifiableObject,
|
||||
> = (
|
||||
AbstractClass<Instance, [values: Values]> &
|
||||
JsonifiableZodSchemaClassStatic<
|
||||
Values,
|
||||
JsonifiedValues,
|
||||
|
||||
JsonifySchemaT,
|
||||
JsonifySchemaUnknownKeys,
|
||||
JsonifySchemaCatchall,
|
||||
|
||||
DejsonifySchemaT,
|
||||
DejsonifySchemaUnknownKeys,
|
||||
DejsonifySchemaCatchall
|
||||
DejsonifySchemaCatchall,
|
||||
|
||||
Values,
|
||||
JsonifiedValues
|
||||
>
|
||||
)
|
||||
|
||||
export type JsonifiableZodSchemaClassStatic<
|
||||
Values extends object,
|
||||
JsonifiedValues extends JsonifiableObject,
|
||||
|
||||
JsonifySchemaT extends z.ZodRawShape,
|
||||
JsonifySchemaUnknownKeys extends z.UnknownKeysParam,
|
||||
JsonifySchemaCatchall extends z.ZodTypeAny,
|
||||
@@ -72,6 +69,9 @@ export type JsonifiableZodSchemaClassStatic<
|
||||
DejsonifySchemaT extends z.ZodRawShape,
|
||||
DejsonifySchemaUnknownKeys extends z.UnknownKeysParam,
|
||||
DejsonifySchemaCatchall extends z.ZodTypeAny,
|
||||
|
||||
Values extends object,
|
||||
JsonifiedValues extends JsonifiableObject,
|
||||
> = {
|
||||
readonly jsonifySchema: z.ZodObject<JsonifySchemaT, JsonifySchemaUnknownKeys, JsonifySchemaCatchall, JsonifiedValues, Values>
|
||||
readonly dejsonifySchema: z.ZodObject<DejsonifySchemaT, DejsonifySchemaUnknownKeys, DejsonifySchemaCatchall, Values, JsonifiedValues>
|
||||
|
||||
@@ -33,6 +33,7 @@ class Test extends newTestExp.extends implements Implements<typeof newTestExp> {
|
||||
Test.defaultValues
|
||||
const inst = Test.create({ id: 1n, name: "" })
|
||||
console.log(inst)
|
||||
Test.dejsonify({ id: 1n })
|
||||
|
||||
|
||||
class SubTest extends Test.extend({
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
import { trait } from "@thilawyn/traitify-ts"
|
||||
import { Effect, pipe } from "effect"
|
||||
import { HasRequiredKeys } from "type-fest"
|
||||
import { z } from "zod"
|
||||
import { ZodSchemaClass } from "../shapes/ZodSchemaClass"
|
||||
import { parseZodTypeEffect } from "../util"
|
||||
import { JsonifiableZodSchemaClass } from "../shapes/JsonifiableZodSchemaClass"
|
||||
import { Effect } from "effect"
|
||||
import { JsonifiableObject } from "type-fest/source/jsonifiable"
|
||||
import { z } from "zod"
|
||||
import { JsonifiableZodSchemaClass } from "../shapes/JsonifiableZodSchemaClass"
|
||||
import { parseZodTypeEffect } from "../util"
|
||||
|
||||
|
||||
export const DejsonifiableZodSchemaObject = trait
|
||||
@@ -38,58 +36,87 @@ export const DejsonifiableZodSchemaObject = trait
|
||||
Values,
|
||||
JsonifiedValues
|
||||
>,
|
||||
|
||||
values: JsonifiedValues,
|
||||
params?: Partial<z.ParseParams>,
|
||||
) {
|
||||
return new this(
|
||||
this.schema.parse({ ...this.defaultValues, ...values }, params)
|
||||
this.dejsonifySchema.parse(values, params)
|
||||
)
|
||||
}
|
||||
|
||||
// static async createPromise<
|
||||
// Instance extends Values,
|
||||
static async dejsonifyPromise<
|
||||
Instance extends Values,
|
||||
|
||||
// SchemaT extends z.ZodRawShape,
|
||||
// SchemaUnknownKeys extends z.UnknownKeysParam,
|
||||
// SchemaCatchall extends z.ZodTypeAny,
|
||||
JsonifySchemaT extends z.ZodRawShape,
|
||||
JsonifySchemaUnknownKeys extends z.UnknownKeysParam,
|
||||
JsonifySchemaCatchall extends z.ZodTypeAny,
|
||||
|
||||
// Values extends object,
|
||||
// DefaultValues extends Partial<Values>,
|
||||
// >(
|
||||
// this: ZodSchemaClass<Instance, SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, DefaultValues>,
|
||||
DejsonifySchemaT extends z.ZodRawShape,
|
||||
DejsonifySchemaUnknownKeys extends z.UnknownKeysParam,
|
||||
DejsonifySchemaCatchall extends z.ZodTypeAny,
|
||||
|
||||
// ...[values, params]: NewZodSchemaInstanceArgs<
|
||||
// NewZodSchemaInstanceInput<Values, DefaultValues>
|
||||
// >
|
||||
// ) {
|
||||
// return new this(
|
||||
// await this.schema.parseAsync({ ...this.defaultValues, ...values }, params)
|
||||
// )
|
||||
// }
|
||||
Values extends object,
|
||||
JsonifiedValues extends JsonifiableObject,
|
||||
>(
|
||||
this: JsonifiableZodSchemaClass<
|
||||
Instance,
|
||||
|
||||
// static createEffect<
|
||||
// Instance extends Values,
|
||||
JsonifySchemaT,
|
||||
JsonifySchemaUnknownKeys,
|
||||
JsonifySchemaCatchall,
|
||||
|
||||
// SchemaT extends z.ZodRawShape,
|
||||
// SchemaUnknownKeys extends z.UnknownKeysParam,
|
||||
// SchemaCatchall extends z.ZodTypeAny,
|
||||
DejsonifySchemaT,
|
||||
DejsonifySchemaUnknownKeys,
|
||||
DejsonifySchemaCatchall,
|
||||
|
||||
// Values extends object,
|
||||
// DefaultValues extends Partial<Values>,
|
||||
// >(
|
||||
// this: ZodSchemaClass<Instance, SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, DefaultValues>,
|
||||
Values,
|
||||
JsonifiedValues
|
||||
>,
|
||||
|
||||
// ...[values, params]: NewZodSchemaInstanceArgs<
|
||||
// NewZodSchemaInstanceInput<Values, DefaultValues>
|
||||
// >
|
||||
// ) {
|
||||
// return pipe(
|
||||
// parseZodTypeEffect(
|
||||
// this.schema,
|
||||
// { ...this.defaultValues, ...values },
|
||||
// params,
|
||||
// ),
|
||||
values: JsonifiedValues,
|
||||
params?: Partial<z.ParseParams>,
|
||||
) {
|
||||
return new this(
|
||||
await this.dejsonifySchema.parseAsync(values, params)
|
||||
)
|
||||
}
|
||||
|
||||
// Effect.map(values => new this(values)),
|
||||
// )
|
||||
// }
|
||||
static dejsonifyEffect<
|
||||
Instance extends 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,
|
||||
|
||||
Values extends object,
|
||||
JsonifiedValues extends JsonifiableObject,
|
||||
>(
|
||||
this: JsonifiableZodSchemaClass<
|
||||
Instance,
|
||||
|
||||
JsonifySchemaT,
|
||||
JsonifySchemaUnknownKeys,
|
||||
JsonifySchemaCatchall,
|
||||
|
||||
DejsonifySchemaT,
|
||||
DejsonifySchemaUnknownKeys,
|
||||
DejsonifySchemaCatchall,
|
||||
|
||||
Values,
|
||||
JsonifiedValues
|
||||
>,
|
||||
|
||||
values: JsonifiedValues,
|
||||
params?: Partial<z.ParseParams>,
|
||||
) {
|
||||
return parseZodTypeEffect(this.dejsonifySchema, values, params).pipe(
|
||||
Effect.map(values => new this(values)),
|
||||
)
|
||||
}
|
||||
})
|
||||
.build()
|
||||
|
||||
Reference in New Issue
Block a user