JsonifiableZodSchemaObject
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -18,7 +18,7 @@ export type JsonifiableZodSchemaClass<
|
||||
JsonifiedValues extends JsonifiableObject,
|
||||
> = (
|
||||
Class<Instance, [values: Values]> &
|
||||
JsonifiableZodSchemaClassStatic<
|
||||
JsonifiableZodSchemas<
|
||||
JsonifySchemaT,
|
||||
JsonifySchemaUnknownKeys,
|
||||
JsonifySchemaCatchall,
|
||||
@@ -47,7 +47,7 @@ export type JsonifiableZodSchemaAbstractClass<
|
||||
JsonifiedValues extends JsonifiableObject,
|
||||
> = (
|
||||
AbstractClass<Instance, [values: Values]> &
|
||||
JsonifiableZodSchemaClassStatic<
|
||||
JsonifiableZodSchemas<
|
||||
JsonifySchemaT,
|
||||
JsonifySchemaUnknownKeys,
|
||||
JsonifySchemaCatchall,
|
||||
@@ -61,7 +61,7 @@ export type JsonifiableZodSchemaAbstractClass<
|
||||
>
|
||||
)
|
||||
|
||||
export type JsonifiableZodSchemaClassStatic<
|
||||
export type JsonifiableZodSchemas<
|
||||
JsonifySchemaT extends z.ZodRawShape,
|
||||
JsonifySchemaUnknownKeys extends z.UnknownKeysParam,
|
||||
JsonifySchemaCatchall extends z.ZodTypeAny,
|
||||
|
||||
115
src/traits/JsonifiableZodSchemaObject.ts
Normal file
115
src/traits/JsonifiableZodSchemaObject.ts
Normal file
@@ -0,0 +1,115 @@
|
||||
import { trait } from "@thilawyn/traitify-ts"
|
||||
import { Effect } from "effect"
|
||||
import { JsonifiableObject } from "type-fest/source/jsonifiable"
|
||||
import { z } from "zod"
|
||||
import { JsonifiableZodSchemaClass, JsonifiableZodSchemas } from "../shapes/JsonifiableZodSchemaClass"
|
||||
import { parseZodTypeEffect } from "../util"
|
||||
|
||||
|
||||
export const JsonifiableZodSchemaObject = trait
|
||||
.implement(Super => class JsonifiableZodSchemaObject extends Super {
|
||||
jsonify<
|
||||
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: Values & JsonifiableZodSchemas<
|
||||
JsonifySchemaT,
|
||||
JsonifySchemaUnknownKeys,
|
||||
JsonifySchemaCatchall,
|
||||
|
||||
DejsonifySchemaT,
|
||||
DejsonifySchemaUnknownKeys,
|
||||
DejsonifySchemaCatchall,
|
||||
|
||||
Values,
|
||||
JsonifiedValues
|
||||
>,
|
||||
|
||||
params?: Partial<z.ParseParams>,
|
||||
) {
|
||||
return this.jsonifySchema.parse(this, params)
|
||||
}
|
||||
|
||||
static async dejsonifyPromise<
|
||||
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 new this(
|
||||
await this.dejsonifySchema.parseAsync(values, params)
|
||||
)
|
||||
}
|
||||
|
||||
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