0.1.2 #3
@@ -19,6 +19,7 @@ const newTestExp = new ZodSchemaClassBuilder(TraitExpression.NullSuperclass, [])
|
|||||||
jsonifySchema: ({ schema, shape }) => schema.extend({
|
jsonifySchema: ({ schema, shape }) => schema.extend({
|
||||||
id: jsonify.bigint(shape.id)
|
id: jsonify.bigint(shape.id)
|
||||||
}),
|
}),
|
||||||
|
|
||||||
dejsonifySchema: ({ schema, shape }) => schema.extend({
|
dejsonifySchema: ({ schema, shape }) => schema.extend({
|
||||||
id: dejsonify.bigint(shape.id)
|
id: dejsonify.bigint(shape.id)
|
||||||
}),
|
}),
|
||||||
|
|||||||
95
src/traits/DejsonifiableZodSchemaObject.ts
Normal file
95
src/traits/DejsonifiableZodSchemaObject.ts
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
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 { JsonifiableObject } from "type-fest/source/jsonifiable"
|
||||||
|
|
||||||
|
|
||||||
|
export const DejsonifiableZodSchemaObject = trait
|
||||||
|
.implement(Super => class DejsonifiableZodSchemaObject extends Super {
|
||||||
|
static dejsonify<
|
||||||
|
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
|
||||||
|
>,
|
||||||
|
) {
|
||||||
|
return new this(
|
||||||
|
this.schema.parse({ ...this.defaultValues, ...values }, params)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// static async createPromise<
|
||||||
|
// Instance extends Values,
|
||||||
|
|
||||||
|
// SchemaT extends z.ZodRawShape,
|
||||||
|
// SchemaUnknownKeys extends z.UnknownKeysParam,
|
||||||
|
// SchemaCatchall extends z.ZodTypeAny,
|
||||||
|
|
||||||
|
// Values extends object,
|
||||||
|
// DefaultValues extends Partial<Values>,
|
||||||
|
// >(
|
||||||
|
// this: ZodSchemaClass<Instance, SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, DefaultValues>,
|
||||||
|
|
||||||
|
// ...[values, params]: NewZodSchemaInstanceArgs<
|
||||||
|
// NewZodSchemaInstanceInput<Values, DefaultValues>
|
||||||
|
// >
|
||||||
|
// ) {
|
||||||
|
// return new this(
|
||||||
|
// await this.schema.parseAsync({ ...this.defaultValues, ...values }, params)
|
||||||
|
// )
|
||||||
|
// }
|
||||||
|
|
||||||
|
// static createEffect<
|
||||||
|
// Instance extends Values,
|
||||||
|
|
||||||
|
// SchemaT extends z.ZodRawShape,
|
||||||
|
// SchemaUnknownKeys extends z.UnknownKeysParam,
|
||||||
|
// SchemaCatchall extends z.ZodTypeAny,
|
||||||
|
|
||||||
|
// Values extends object,
|
||||||
|
// DefaultValues extends Partial<Values>,
|
||||||
|
// >(
|
||||||
|
// this: ZodSchemaClass<Instance, SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, DefaultValues>,
|
||||||
|
|
||||||
|
// ...[values, params]: NewZodSchemaInstanceArgs<
|
||||||
|
// NewZodSchemaInstanceInput<Values, DefaultValues>
|
||||||
|
// >
|
||||||
|
// ) {
|
||||||
|
// return pipe(
|
||||||
|
// parseZodTypeEffect(
|
||||||
|
// this.schema,
|
||||||
|
// { ...this.defaultValues, ...values },
|
||||||
|
// params,
|
||||||
|
// ),
|
||||||
|
|
||||||
|
// Effect.map(values => new this(values)),
|
||||||
|
// )
|
||||||
|
// }
|
||||||
|
})
|
||||||
|
.build()
|
||||||
Reference in New Issue
Block a user