Jsonifiable
This commit is contained in:
79
src/jsonifiable/makeJsonifiableSchemableClass.ts
Normal file
79
src/jsonifiable/makeJsonifiableSchemableClass.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
import { JsonifiableObject } from "type-fest/source/jsonifiable"
|
||||
import { z } from "zod"
|
||||
import { SchemableClass, SchemableConfig } from ".."
|
||||
import { parseZodTypeEffect } from "../util"
|
||||
|
||||
|
||||
export function makeJsonifiableSchemableClass<
|
||||
C extends SchemableClass<$Config>,
|
||||
$Config extends SchemableConfig,
|
||||
|
||||
JsonifiedValues extends JsonifiableObject,
|
||||
|
||||
JsonifySchemaT extends z.ZodRawShape,
|
||||
JsonifySchemaUnknownKeys extends z.UnknownKeysParam,
|
||||
JsonifySchemaCatchall extends z.ZodTypeAny,
|
||||
|
||||
DejsonifySchemaT extends z.ZodRawShape,
|
||||
DejsonifySchemaUnknownKeys extends z.UnknownKeysParam,
|
||||
DejsonifySchemaCatchall extends z.ZodTypeAny,
|
||||
>(
|
||||
class_: C | SchemableClass<$Config>,
|
||||
|
||||
props: {
|
||||
jsonifySchema: (props: {
|
||||
schema: $Config["schema"]
|
||||
s: $Config["schema"]["shape"]
|
||||
}) => z.ZodObject<
|
||||
JsonifySchemaT,
|
||||
JsonifySchemaUnknownKeys,
|
||||
JsonifySchemaCatchall,
|
||||
JsonifiedValues,
|
||||
$Config["values"]
|
||||
>
|
||||
|
||||
dejsonifySchema: (props: {
|
||||
schema: $Config["schema"]
|
||||
s: $Config["schema"]["shape"]
|
||||
}) => z.ZodObject<
|
||||
DejsonifySchemaT,
|
||||
DejsonifySchemaUnknownKeys,
|
||||
DejsonifySchemaCatchall,
|
||||
$Config["values"],
|
||||
JsonifiedValues
|
||||
>
|
||||
},
|
||||
) {
|
||||
|
||||
const jsonifySchema = props.jsonifySchema({
|
||||
schema: class_.schema,
|
||||
s: class_.schema.shape,
|
||||
})
|
||||
|
||||
const dejsonifySchema = props.dejsonifySchema({
|
||||
schema: class_.schema,
|
||||
s: class_.schema.shape,
|
||||
})
|
||||
|
||||
|
||||
return class JsonifiableSchemableObject extends class_ {
|
||||
static readonly jsonifySchema = jsonifySchema
|
||||
static readonly dejsonifySchema = dejsonifySchema
|
||||
|
||||
readonly jsonifySchema = jsonifySchema
|
||||
readonly dejsonifySchema = dejsonifySchema
|
||||
|
||||
jsonify() {
|
||||
return this.jsonifySchema.parse(this)
|
||||
}
|
||||
|
||||
jsonifyPromise() {
|
||||
return this.jsonifySchema.parseAsync(this)
|
||||
}
|
||||
|
||||
jsonifyEffect() {
|
||||
return parseZodTypeEffect(this.jsonifySchema, this)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user