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