makeJsonifiableSchemableClass
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:
@@ -1,2 +1,3 @@
|
|||||||
export * from "./JsonifiableSchemable"
|
export * from "./JsonifiableSchemable"
|
||||||
export * from "./JsonifiableSchemableClass"
|
export * from "./JsonifiableSchemableClass"
|
||||||
|
export * from "./makeJsonifiableSchemableClass"
|
||||||
|
|||||||
118
src/jsonifiable/makeJsonifiableSchemableClass.ts
Normal file
118
src/jsonifiable/makeJsonifiableSchemableClass.ts
Normal file
@@ -0,0 +1,118 @@
|
|||||||
|
import { Effect } from "effect"
|
||||||
|
import { JsonifiableObject } from "type-fest/source/jsonifiable"
|
||||||
|
import { z } from "zod"
|
||||||
|
import { SchemableClass } from ".."
|
||||||
|
import { Class, ClassType, StaticMembers, parseZodTypeEffect } from "../util"
|
||||||
|
|
||||||
|
|
||||||
|
export function makeJsonifiableSchemableClass<
|
||||||
|
C extends SchemableClass<
|
||||||
|
SchemaT,
|
||||||
|
SchemaUnknownKeys,
|
||||||
|
SchemaCatchall,
|
||||||
|
Values,
|
||||||
|
DefaultValues
|
||||||
|
>,
|
||||||
|
|
||||||
|
SchemaT extends z.ZodRawShape,
|
||||||
|
SchemaUnknownKeys extends z.UnknownKeysParam,
|
||||||
|
SchemaCatchall extends z.ZodTypeAny,
|
||||||
|
|
||||||
|
Values extends {},
|
||||||
|
DefaultValues 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,
|
||||||
|
|
||||||
|
Type extends ClassType = "AbstractClass"
|
||||||
|
>(
|
||||||
|
props: {
|
||||||
|
extend: C | SchemableClass<
|
||||||
|
SchemaT,
|
||||||
|
SchemaUnknownKeys,
|
||||||
|
SchemaCatchall,
|
||||||
|
Values,
|
||||||
|
DefaultValues,
|
||||||
|
Type
|
||||||
|
>
|
||||||
|
|
||||||
|
jsonifySchema: (props: {
|
||||||
|
schema: C["schema"]
|
||||||
|
shape: C["schema"]["shape"]
|
||||||
|
}) => z.ZodObject<
|
||||||
|
JsonifySchemaT,
|
||||||
|
JsonifySchemaUnknownKeys,
|
||||||
|
JsonifySchemaCatchall,
|
||||||
|
JsonifiedValues,
|
||||||
|
Values
|
||||||
|
>
|
||||||
|
|
||||||
|
dejsonifySchema: (props: {
|
||||||
|
schema: C["schema"]
|
||||||
|
shape: C["schema"]["shape"]
|
||||||
|
}) => z.ZodObject<
|
||||||
|
DejsonifySchemaT,
|
||||||
|
DejsonifySchemaUnknownKeys,
|
||||||
|
DejsonifySchemaCatchall,
|
||||||
|
Values,
|
||||||
|
JsonifiedValues
|
||||||
|
>
|
||||||
|
}
|
||||||
|
) {
|
||||||
|
const jsonifySchema = props.jsonifySchema({
|
||||||
|
schema: props.extend.schema,
|
||||||
|
shape: props.extend.schema.shape,
|
||||||
|
})
|
||||||
|
|
||||||
|
const dejsonifySchema = props.dejsonifySchema({
|
||||||
|
schema: props.extend.schema,
|
||||||
|
shape: props.extend.schema.shape,
|
||||||
|
})
|
||||||
|
|
||||||
|
return class extends props.extend {
|
||||||
|
static readonly jsonifySchema = jsonifySchema
|
||||||
|
readonly jsonifySchema = jsonifySchema
|
||||||
|
|
||||||
|
static readonly dejsonifySchema = dejsonifySchema
|
||||||
|
readonly dejsonifySchema = dejsonifySchema
|
||||||
|
|
||||||
|
jsonify() {
|
||||||
|
return this.jsonifySchema.parse(this)
|
||||||
|
}
|
||||||
|
|
||||||
|
jsonifyPromise() {
|
||||||
|
return this.jsonifySchema.parseAsync(this)
|
||||||
|
}
|
||||||
|
|
||||||
|
jsonifyEffect() {
|
||||||
|
return parseZodTypeEffect(this.jsonifySchema, this)
|
||||||
|
}
|
||||||
|
} as unknown as (
|
||||||
|
Class<
|
||||||
|
Type,
|
||||||
|
|
||||||
|
InstanceType<C> & {
|
||||||
|
readonly jsonifySchema: typeof jsonifySchema,
|
||||||
|
readonly dejsonifySchema: typeof dejsonifySchema,
|
||||||
|
|
||||||
|
jsonify(): JsonifiedValues
|
||||||
|
jsonifyPromise(): Promise<JsonifiedValues>
|
||||||
|
jsonifyEffect(): Effect.Effect<never, z.ZodError<Values>, JsonifiedValues>
|
||||||
|
},
|
||||||
|
|
||||||
|
ConstructorParameters<C>
|
||||||
|
> &
|
||||||
|
|
||||||
|
StaticMembers<C> & {
|
||||||
|
readonly jsonifySchema: typeof jsonifySchema,
|
||||||
|
readonly dejsonifySchema: typeof dejsonifySchema,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -3,14 +3,16 @@ import { z } from "zod"
|
|||||||
import { StaticMembers } from "./util"
|
import { StaticMembers } from "./util"
|
||||||
|
|
||||||
|
|
||||||
|
type MakeSchemableClassFromInputClass = AbstractClass<{
|
||||||
|
schema?: never
|
||||||
|
defaultValues?: never
|
||||||
|
}, []> & {
|
||||||
|
schema?: never
|
||||||
|
defaultValues?: never
|
||||||
|
}
|
||||||
|
|
||||||
export function makeSchemableClassFrom<
|
export function makeSchemableClassFrom<
|
||||||
C extends AbstractClass<{
|
C extends MakeSchemableClassFromInputClass,
|
||||||
schema?: never
|
|
||||||
defaultValues?: never
|
|
||||||
}, []> & {
|
|
||||||
schema?: never
|
|
||||||
defaultValues?: never
|
|
||||||
},
|
|
||||||
|
|
||||||
SchemaT extends z.ZodRawShape,
|
SchemaT extends z.ZodRawShape,
|
||||||
SchemaUnknownKeys extends z.UnknownKeysParam,
|
SchemaUnknownKeys extends z.UnknownKeysParam,
|
||||||
@@ -87,13 +89,7 @@ export function createMakeSchemableClassFromUnary<
|
|||||||
defaultValues: DefaultValues,
|
defaultValues: DefaultValues,
|
||||||
) {
|
) {
|
||||||
return <
|
return <
|
||||||
C extends AbstractClass<{
|
C extends MakeSchemableClassFromInputClass
|
||||||
schema?: never
|
|
||||||
defaultValues?: never
|
|
||||||
}, []> & {
|
|
||||||
schema?: never
|
|
||||||
defaultValues?: never
|
|
||||||
}
|
|
||||||
>(extend: C) =>
|
>(extend: C) =>
|
||||||
makeSchemableClassFrom(extend, schema, defaultValues)
|
makeSchemableClassFrom(extend, schema, defaultValues)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user