Co-authored-by: Julien Valverdé <julien.valverde@mailo.com> Reviewed-on: https://git.jvalver.de/Thilawyn/schemable-class/pulls/2
This commit was merged in pull request #2.
This commit is contained in:
@@ -1,66 +1,80 @@
|
||||
import { Effect } from "effect"
|
||||
import { Class } from "type-fest"
|
||||
import { JsonifiableObject } from "type-fest/source/jsonifiable"
|
||||
import { z } from "zod"
|
||||
import { SchemableClassConstructorParams, SchemableConfig } from ".."
|
||||
|
||||
|
||||
export type JsonifiableSchemableConfig<
|
||||
$SchemableConfig extends SchemableConfig = SchemableConfig,
|
||||
|
||||
JsonifiedValues extends JsonifiableObject = {},
|
||||
|
||||
JsonifySchemaT extends z.ZodRawShape = z.ZodRawShape,
|
||||
JsonifySchemaUnknownKeys extends z.UnknownKeysParam = z.UnknownKeysParam,
|
||||
JsonifySchemaCatchall extends z.ZodTypeAny = z.ZodTypeAny,
|
||||
|
||||
DejsonifySchemaT extends z.ZodRawShape = z.ZodRawShape,
|
||||
DejsonifySchemaUnknownKeys extends z.UnknownKeysParam = z.UnknownKeysParam,
|
||||
DejsonifySchemaCatchall extends z.ZodTypeAny = z.ZodTypeAny,
|
||||
> = {
|
||||
readonly $schemableConfig: $SchemableConfig
|
||||
|
||||
readonly jsonifiedValues: JsonifiedValues
|
||||
|
||||
readonly jsonifySchema: z.ZodObject<
|
||||
JsonifySchemaT,
|
||||
JsonifySchemaUnknownKeys,
|
||||
JsonifySchemaCatchall,
|
||||
JsonifiedValues,
|
||||
$SchemableConfig["values"]
|
||||
>
|
||||
|
||||
readonly dejsonifySchema: z.ZodObject<
|
||||
DejsonifySchemaT,
|
||||
DejsonifySchemaUnknownKeys,
|
||||
DejsonifySchemaCatchall,
|
||||
$SchemableConfig["values"],
|
||||
JsonifiedValues
|
||||
>
|
||||
}
|
||||
import { SchemableClass } from ".."
|
||||
import { Class, ClassType } from "../util"
|
||||
|
||||
|
||||
export type JsonifiableSchemableClass<
|
||||
$Config extends JsonifiableSchemableConfig
|
||||
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"
|
||||
> = (
|
||||
SchemableClass<
|
||||
SchemaT,
|
||||
SchemaUnknownKeys,
|
||||
SchemaCatchall,
|
||||
Values,
|
||||
DefaultValues,
|
||||
Type
|
||||
> &
|
||||
|
||||
Class<
|
||||
JsonifiableSchemableObject<$Config>,
|
||||
SchemableClassConstructorParams<$Config["$schemableConfig"]>
|
||||
Type,
|
||||
|
||||
{
|
||||
readonly jsonifySchema: z.ZodObject<
|
||||
JsonifySchemaT,
|
||||
JsonifySchemaUnknownKeys,
|
||||
JsonifySchemaCatchall,
|
||||
JsonifiedValues,
|
||||
Values
|
||||
>
|
||||
|
||||
readonly dejsonifySchema: z.ZodObject<
|
||||
DejsonifySchemaT,
|
||||
DejsonifySchemaUnknownKeys,
|
||||
DejsonifySchemaCatchall,
|
||||
Values,
|
||||
JsonifiedValues
|
||||
>
|
||||
|
||||
jsonify(): JsonifiedValues
|
||||
jsonifyPromise(): Promise<JsonifiedValues>
|
||||
jsonifyEffect(): Effect.Effect<never, z.ZodError<Values>, JsonifiedValues>
|
||||
},
|
||||
|
||||
any[]
|
||||
> & {
|
||||
readonly $jsonifiableSchemableConfig: $Config
|
||||
readonly jsonifySchema: $Config["jsonifySchema"]
|
||||
readonly dejsonifySchema: $Config["dejsonifySchema"]
|
||||
readonly jsonifySchema: z.ZodObject<
|
||||
JsonifySchemaT,
|
||||
JsonifySchemaUnknownKeys,
|
||||
JsonifySchemaCatchall,
|
||||
JsonifiedValues,
|
||||
Values
|
||||
>
|
||||
|
||||
readonly dejsonifySchema: z.ZodObject<
|
||||
DejsonifySchemaT,
|
||||
DejsonifySchemaUnknownKeys,
|
||||
DejsonifySchemaCatchall,
|
||||
Values,
|
||||
JsonifiedValues
|
||||
>
|
||||
}
|
||||
)
|
||||
|
||||
export type JsonifiableSchemableObject<
|
||||
$Config extends JsonifiableSchemableConfig
|
||||
> = {
|
||||
readonly $jsonifiableSchemableConfig: $Config
|
||||
readonly jsonifySchema: $Config["jsonifySchema"]
|
||||
readonly dejsonifySchema: $Config["dejsonifySchema"]
|
||||
|
||||
jsonify(): $Config["jsonifiedValues"]
|
||||
jsonifyPromise(): Promise<$Config["jsonifiedValues"]>
|
||||
jsonifyEffect(): Effect.Effect<never, z.ZodError<$Config["$schemableConfig"]["values"]>, $Config["jsonifiedValues"]>
|
||||
}
|
||||
|
||||
@@ -1,47 +1,213 @@
|
||||
import { Effect, pipe } from "effect"
|
||||
import { JsonifiableObject } from "type-fest/source/jsonifiable"
|
||||
import { z } from "zod"
|
||||
import { JsonifiableSchemableClass, JsonifiableSchemableConfig } from "."
|
||||
import { JsonifiableSchemableClass } from "."
|
||||
import { parseZodTypeEffect } from "../util"
|
||||
|
||||
|
||||
export const dejsonifySchemable = <
|
||||
C extends JsonifiableSchemableClass<$Config>,
|
||||
$Config extends JsonifiableSchemableConfig,
|
||||
export function dejsonifySchemable<
|
||||
C extends JsonifiableSchemableClass<
|
||||
SchemaT,
|
||||
SchemaUnknownKeys,
|
||||
SchemaCatchall,
|
||||
|
||||
Values,
|
||||
DefaultValues,
|
||||
|
||||
JsonifySchemaT,
|
||||
JsonifySchemaUnknownKeys,
|
||||
JsonifySchemaCatchall,
|
||||
|
||||
DejsonifySchemaT,
|
||||
DejsonifySchemaUnknownKeys,
|
||||
DejsonifySchemaCatchall,
|
||||
|
||||
JsonifiedValues,
|
||||
|
||||
"Class"
|
||||
>,
|
||||
|
||||
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,
|
||||
>(
|
||||
class_: C | JsonifiableSchemableClass<$Config>,
|
||||
values: $Config["jsonifiedValues"],
|
||||
class_: C | JsonifiableSchemableClass<
|
||||
SchemaT,
|
||||
SchemaUnknownKeys,
|
||||
SchemaCatchall,
|
||||
|
||||
Values,
|
||||
DefaultValues,
|
||||
|
||||
JsonifySchemaT,
|
||||
JsonifySchemaUnknownKeys,
|
||||
JsonifySchemaCatchall,
|
||||
|
||||
DejsonifySchemaT,
|
||||
DejsonifySchemaUnknownKeys,
|
||||
DejsonifySchemaCatchall,
|
||||
|
||||
JsonifiedValues,
|
||||
|
||||
"Class"
|
||||
>,
|
||||
|
||||
values: JsonifiedValues,
|
||||
params?: Partial<z.ParseParams>,
|
||||
) =>
|
||||
new class_(class_.dejsonifySchema.parse(values, params)) as InstanceType<C>
|
||||
) {
|
||||
return new class_(
|
||||
class_.dejsonifySchema.parse(values, params)
|
||||
) as InstanceType<C>
|
||||
}
|
||||
|
||||
|
||||
export const dejsonifySchemablePromise = async <
|
||||
C extends JsonifiableSchemableClass<$Config>,
|
||||
$Config extends JsonifiableSchemableConfig,
|
||||
export async function dejsonifySchemablePromise<
|
||||
C extends JsonifiableSchemableClass<
|
||||
SchemaT,
|
||||
SchemaUnknownKeys,
|
||||
SchemaCatchall,
|
||||
|
||||
Values,
|
||||
DefaultValues,
|
||||
|
||||
JsonifySchemaT,
|
||||
JsonifySchemaUnknownKeys,
|
||||
JsonifySchemaCatchall,
|
||||
|
||||
DejsonifySchemaT,
|
||||
DejsonifySchemaUnknownKeys,
|
||||
DejsonifySchemaCatchall,
|
||||
|
||||
JsonifiedValues,
|
||||
|
||||
"Class"
|
||||
>,
|
||||
|
||||
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,
|
||||
>(
|
||||
class_: C | JsonifiableSchemableClass<$Config>,
|
||||
values: $Config["jsonifiedValues"],
|
||||
class_: C | JsonifiableSchemableClass<
|
||||
SchemaT,
|
||||
SchemaUnknownKeys,
|
||||
SchemaCatchall,
|
||||
|
||||
Values,
|
||||
DefaultValues,
|
||||
|
||||
JsonifySchemaT,
|
||||
JsonifySchemaUnknownKeys,
|
||||
JsonifySchemaCatchall,
|
||||
|
||||
DejsonifySchemaT,
|
||||
DejsonifySchemaUnknownKeys,
|
||||
DejsonifySchemaCatchall,
|
||||
|
||||
JsonifiedValues,
|
||||
|
||||
"Class"
|
||||
>,
|
||||
|
||||
values: JsonifiedValues,
|
||||
params?: Partial<z.ParseParams>,
|
||||
) =>
|
||||
new class_(await class_.dejsonifySchema.parseAsync(values, params)) as InstanceType<C>
|
||||
) {
|
||||
return new class_(
|
||||
await class_.dejsonifySchema.parseAsync(values, params)
|
||||
) as InstanceType<C>
|
||||
}
|
||||
|
||||
|
||||
export const dejsonifySchemableEffect = <
|
||||
C extends JsonifiableSchemableClass<$Config>,
|
||||
$Config extends JsonifiableSchemableConfig,
|
||||
export function dejsonifySchemableEffect<
|
||||
C extends JsonifiableSchemableClass<
|
||||
SchemaT,
|
||||
SchemaUnknownKeys,
|
||||
SchemaCatchall,
|
||||
|
||||
Values,
|
||||
DefaultValues,
|
||||
|
||||
JsonifySchemaT,
|
||||
JsonifySchemaUnknownKeys,
|
||||
JsonifySchemaCatchall,
|
||||
|
||||
DejsonifySchemaT,
|
||||
DejsonifySchemaUnknownKeys,
|
||||
DejsonifySchemaCatchall,
|
||||
|
||||
JsonifiedValues,
|
||||
|
||||
"Class"
|
||||
>,
|
||||
|
||||
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,
|
||||
>(
|
||||
class_: C | JsonifiableSchemableClass<$Config>,
|
||||
values: $Config["jsonifiedValues"],
|
||||
params?: Partial<z.ParseParams>,
|
||||
) => pipe(
|
||||
parseZodTypeEffect<
|
||||
z.output<typeof class_.dejsonifySchema>,
|
||||
z.input<typeof class_.dejsonifySchema>
|
||||
>(
|
||||
class_.dejsonifySchema,
|
||||
values,
|
||||
params,
|
||||
),
|
||||
class_: C | JsonifiableSchemableClass<
|
||||
SchemaT,
|
||||
SchemaUnknownKeys,
|
||||
SchemaCatchall,
|
||||
|
||||
Effect.map(values => new class_(values) as InstanceType<C>),
|
||||
)
|
||||
Values,
|
||||
DefaultValues,
|
||||
|
||||
JsonifySchemaT,
|
||||
JsonifySchemaUnknownKeys,
|
||||
JsonifySchemaCatchall,
|
||||
|
||||
DejsonifySchemaT,
|
||||
DejsonifySchemaUnknownKeys,
|
||||
DejsonifySchemaCatchall,
|
||||
|
||||
JsonifiedValues,
|
||||
|
||||
"Class"
|
||||
>,
|
||||
|
||||
values: JsonifiedValues,
|
||||
params?: Partial<z.ParseParams>,
|
||||
) {
|
||||
return pipe(
|
||||
parseZodTypeEffect(class_.dejsonifySchema, values, params),
|
||||
Effect.map(values => new class_(values) as InstanceType<C>),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,16 +1,26 @@
|
||||
import { Class } from "type-fest"
|
||||
import { Effect } from "effect"
|
||||
import { AbstractClass, Class as ConcreteClass } from "type-fest"
|
||||
import { JsonifiableObject } from "type-fest/source/jsonifiable"
|
||||
import { z } from "zod"
|
||||
import { JsonifiableSchemableClass, JsonifiableSchemableConfig, JsonifiableSchemableObject } from "."
|
||||
import { SchemableClass, SchemableConfig } from ".."
|
||||
import { SchemableClass } from ".."
|
||||
import { StaticMembers, parseZodTypeEffect } from "../util"
|
||||
|
||||
|
||||
export function makeJsonifiableSchemableClass<
|
||||
C extends SchemableClass<$SchemableConfig>,
|
||||
$SchemableConfig extends SchemableConfig,
|
||||
C extends SchemableClass<
|
||||
SchemaT,
|
||||
SchemaUnknownKeys,
|
||||
SchemaCatchall,
|
||||
Values,
|
||||
DefaultValues
|
||||
>,
|
||||
|
||||
JsonifiedValues extends JsonifiableObject,
|
||||
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,
|
||||
@@ -19,59 +29,77 @@ export function makeJsonifiableSchemableClass<
|
||||
DejsonifySchemaT extends z.ZodRawShape,
|
||||
DejsonifySchemaUnknownKeys extends z.UnknownKeysParam,
|
||||
DejsonifySchemaCatchall extends z.ZodTypeAny,
|
||||
|
||||
JsonifiedValues extends JsonifiableObject,
|
||||
>(
|
||||
class_: C | SchemableClass<$SchemableConfig>,
|
||||
extend: C | SchemableClass<
|
||||
SchemaT,
|
||||
SchemaUnknownKeys,
|
||||
SchemaCatchall,
|
||||
Values,
|
||||
DefaultValues
|
||||
>,
|
||||
|
||||
props: {
|
||||
jsonifySchema: (props: {
|
||||
schema: $SchemableConfig["schema"]
|
||||
s: $SchemableConfig["schema"]["shape"]
|
||||
schema: z.ZodObject<
|
||||
SchemaT,
|
||||
SchemaUnknownKeys,
|
||||
SchemaCatchall,
|
||||
Values,
|
||||
Values
|
||||
>
|
||||
|
||||
shape: SchemaT
|
||||
}) => z.ZodObject<
|
||||
JsonifySchemaT,
|
||||
JsonifySchemaUnknownKeys,
|
||||
JsonifySchemaCatchall,
|
||||
JsonifiedValues,
|
||||
$SchemableConfig["values"]
|
||||
Values
|
||||
>
|
||||
|
||||
dejsonifySchema: (props: {
|
||||
schema: $SchemableConfig["schema"]
|
||||
s: $SchemableConfig["schema"]["shape"]
|
||||
schema: z.ZodObject<
|
||||
SchemaT,
|
||||
SchemaUnknownKeys,
|
||||
SchemaCatchall,
|
||||
Values,
|
||||
Values
|
||||
>
|
||||
|
||||
shape: SchemaT
|
||||
}) => z.ZodObject<
|
||||
DejsonifySchemaT,
|
||||
DejsonifySchemaUnknownKeys,
|
||||
DejsonifySchemaCatchall,
|
||||
$SchemableConfig["values"],
|
||||
Values,
|
||||
JsonifiedValues
|
||||
>
|
||||
},
|
||||
) {
|
||||
type Class<T, Arguments extends unknown[]> = (
|
||||
C extends ConcreteClass<any>
|
||||
? ConcreteClass<T, Arguments>
|
||||
: AbstractClass<T, Arguments>
|
||||
)
|
||||
|
||||
const jsonifySchema = props.jsonifySchema({
|
||||
schema: class_.schema,
|
||||
s: class_.schema.shape,
|
||||
schema: extend.schema,
|
||||
shape: extend.schema.shape,
|
||||
})
|
||||
|
||||
const dejsonifySchema = props.dejsonifySchema({
|
||||
schema: class_.schema,
|
||||
s: class_.schema.shape,
|
||||
schema: extend.schema,
|
||||
shape: extend.schema.shape,
|
||||
})
|
||||
|
||||
const $jsonifiableSchemableConfig = {
|
||||
$schemableConfig: class_.$schemableConfig,
|
||||
jsonifiedValues: undefined as unknown as JsonifiedValues,
|
||||
jsonifySchema: undefined as unknown as typeof jsonifySchema,
|
||||
dejsonifySchema: undefined as unknown as typeof dejsonifySchema,
|
||||
} as const satisfies JsonifiableSchemableConfig
|
||||
return class extends extend {
|
||||
static readonly jsonifySchema = jsonifySchema
|
||||
readonly jsonifySchema = jsonifySchema
|
||||
|
||||
const jsonifiableClass = class JsonifiableSchemableObject extends class_ {
|
||||
static readonly $jsonifiableSchemableConfig = $jsonifiableSchemableConfig
|
||||
static readonly jsonifySchema = jsonifySchema
|
||||
static readonly dejsonifySchema = dejsonifySchema
|
||||
|
||||
readonly $jsonifiableSchemableConfig = $jsonifiableSchemableConfig
|
||||
readonly jsonifySchema = jsonifySchema
|
||||
readonly dejsonifySchema = dejsonifySchema
|
||||
static readonly dejsonifySchema = dejsonifySchema
|
||||
readonly dejsonifySchema = dejsonifySchema
|
||||
|
||||
jsonify() {
|
||||
return this.jsonifySchema.parse(this)
|
||||
@@ -84,15 +112,23 @@ export function makeJsonifiableSchemableClass<
|
||||
jsonifyEffect() {
|
||||
return parseZodTypeEffect(this.jsonifySchema, this)
|
||||
}
|
||||
} satisfies JsonifiableSchemableClass<typeof $jsonifiableSchemableConfig>
|
||||
|
||||
return jsonifiableClass as unknown as (
|
||||
} as unknown as (
|
||||
Class<
|
||||
InstanceType<C> & JsonifiableSchemableObject<typeof $jsonifiableSchemableConfig>,
|
||||
InstanceType<C> & {
|
||||
readonly jsonifySchema: z.ZodObject<JsonifySchemaT, JsonifySchemaUnknownKeys, JsonifySchemaCatchall, JsonifiedValues, Values>,
|
||||
readonly dejsonifySchema: z.ZodObject<DejsonifySchemaT, DejsonifySchemaUnknownKeys, DejsonifySchemaCatchall, Values, JsonifiedValues>,
|
||||
|
||||
jsonify(): JsonifiedValues
|
||||
jsonifyPromise(): Promise<JsonifiedValues>
|
||||
jsonifyEffect(): Effect.Effect<never, z.ZodError<Values>, JsonifiedValues>
|
||||
},
|
||||
|
||||
ConstructorParameters<C>
|
||||
> &
|
||||
StaticMembers<C> &
|
||||
StaticMembers<JsonifiableSchemableClass<typeof $jsonifiableSchemableConfig>>
|
||||
)
|
||||
|
||||
StaticMembers<C> & {
|
||||
readonly jsonifySchema: z.ZodObject<JsonifySchemaT, JsonifySchemaUnknownKeys, JsonifySchemaCatchall, JsonifiedValues, Values>,
|
||||
readonly dejsonifySchema: z.ZodObject<DejsonifySchemaT, DejsonifySchemaUnknownKeys, DejsonifySchemaCatchall, Values, JsonifiedValues>,
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,18 +1,28 @@
|
||||
import { Opaque } from "type-fest"
|
||||
import { z } from "zod"
|
||||
import { identity } from "../../util"
|
||||
|
||||
|
||||
export const jsonifyBigIntSchema = <S extends z.ZodBigInt>(schema: S) =>
|
||||
schema.transform(v => v.toString())
|
||||
export type JsonifiedBigInt = Opaque<string, "@thilawyn/schemable-class/JsonifiedBigInt">
|
||||
|
||||
export const dejsonifyBigIntSchema = <S extends z.ZodBigInt>(schema: S) =>
|
||||
z
|
||||
.string()
|
||||
.transform(v => {
|
||||
try {
|
||||
return BigInt(v)
|
||||
}
|
||||
catch (e) {
|
||||
return v
|
||||
}
|
||||
})
|
||||
.pipe(schema)
|
||||
|
||||
export function jsonifyBigIntSchema<S extends z.ZodBigInt>(schema: S) {
|
||||
return schema.transform(v => v.toString() as JsonifiedBigInt)
|
||||
}
|
||||
|
||||
export function dejsonifyBigIntSchema<S extends z.ZodBigInt>(schema: S) {
|
||||
return z
|
||||
.custom<JsonifiedBigInt>(identity)
|
||||
.pipe(z
|
||||
.string()
|
||||
.transform(v => {
|
||||
try {
|
||||
return BigInt(v)
|
||||
}
|
||||
catch (e) {
|
||||
return v
|
||||
}
|
||||
})
|
||||
.pipe(schema)
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,18 +1,28 @@
|
||||
import { Opaque } from "type-fest"
|
||||
import { z } from "zod"
|
||||
import { identity } from "../../util"
|
||||
|
||||
|
||||
export const jsonifyDateSchema = <S extends z.ZodDate>(schema: S) =>
|
||||
schema.transform(v => v.toString())
|
||||
export type JsonifiedDate = Opaque<string, "@thilawyn/schemable-class/JsonifiedDate">
|
||||
|
||||
export const dejsonifyDateSchema = <S extends z.ZodDate>(schema: S) =>
|
||||
z
|
||||
.string()
|
||||
.transform(v => {
|
||||
try {
|
||||
return new Date(v)
|
||||
}
|
||||
catch (e) {
|
||||
return v
|
||||
}
|
||||
})
|
||||
.pipe(schema)
|
||||
|
||||
export function jsonifyDateSchema<S extends z.ZodDate>(schema: S) {
|
||||
return schema.transform(v => v.toString() as JsonifiedDate)
|
||||
}
|
||||
|
||||
export function dejsonifyDateSchema<S extends z.ZodDate>(schema: S) {
|
||||
return z
|
||||
.custom<JsonifiedDate>(identity)
|
||||
.pipe(z
|
||||
.string()
|
||||
.transform(v => {
|
||||
try {
|
||||
return new Date(v)
|
||||
}
|
||||
catch (e) {
|
||||
return v
|
||||
}
|
||||
})
|
||||
.pipe(schema)
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,19 +1,33 @@
|
||||
import { Decimal } from "decimal.js"
|
||||
import { Opaque } from "type-fest"
|
||||
import { z } from "zod"
|
||||
import { identity } from "../../util"
|
||||
|
||||
|
||||
export const jsonifyDecimalSchema = <S extends z.ZodType<Decimal, z.ZodTypeDef, Decimal>>(schema: S) =>
|
||||
schema.transform(v => v.toJSON())
|
||||
export type JsonifiedDecimal = Opaque<string, "@thilawyn/schemable-class/JsonifiedDecimal">
|
||||
|
||||
export const dejsonifyDecimalSchema = <S extends z.ZodType<Decimal, z.ZodTypeDef, Decimal>>(schema: S) =>
|
||||
z
|
||||
.string()
|
||||
.transform(v => {
|
||||
try {
|
||||
return new Decimal(v)
|
||||
}
|
||||
catch (e) {
|
||||
return v
|
||||
}
|
||||
})
|
||||
.pipe(schema)
|
||||
|
||||
export function jsonifyDecimalSchema<
|
||||
S extends z.ZodType<Decimal, z.ZodTypeDef, Decimal>
|
||||
>(schema: S) {
|
||||
return schema.transform(v => v.toJSON() as JsonifiedDecimal)
|
||||
}
|
||||
|
||||
export function dejsonifyDecimalSchema<
|
||||
S extends z.ZodType<Decimal, z.ZodTypeDef, Decimal>
|
||||
>(schema: S) {
|
||||
return z
|
||||
.custom<JsonifiedDecimal>(identity)
|
||||
.pipe(z
|
||||
.string()
|
||||
.transform(v => {
|
||||
try {
|
||||
return new Decimal(v)
|
||||
}
|
||||
catch (e) {
|
||||
return v
|
||||
}
|
||||
})
|
||||
.pipe(schema)
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,25 +1,139 @@
|
||||
import { JsonifiableObject } from "type-fest/source/jsonifiable"
|
||||
import { z } from "zod"
|
||||
import { JsonifiableSchemableClass, JsonifiableSchemableConfig } from ".."
|
||||
import { JsonifiableSchemableClass } from ".."
|
||||
|
||||
|
||||
// TODO: try to find a way to get rid of the 'class_' arg
|
||||
export const jsonifySchemableSchema = <
|
||||
C extends JsonifiableSchemableClass<$Config>,
|
||||
$Config extends JsonifiableSchemableConfig,
|
||||
S extends z.ZodType<InstanceType<C>, z.ZodTypeDef, InstanceType<C>>,
|
||||
export function jsonifySchemableSchema<
|
||||
C extends JsonifiableSchemableClass<
|
||||
SchemaT,
|
||||
SchemaUnknownKeys,
|
||||
SchemaCatchall,
|
||||
|
||||
Values,
|
||||
DefaultValues,
|
||||
|
||||
JsonifySchemaT,
|
||||
JsonifySchemaUnknownKeys,
|
||||
JsonifySchemaCatchall,
|
||||
|
||||
DejsonifySchemaT,
|
||||
DejsonifySchemaUnknownKeys,
|
||||
DejsonifySchemaCatchall,
|
||||
|
||||
JsonifiedValues,
|
||||
|
||||
"Class"
|
||||
>,
|
||||
|
||||
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,
|
||||
|
||||
S extends z.ZodType<InstanceType<C>, z.ZodTypeDef, InstanceType<C>>,
|
||||
>(
|
||||
class_: C | JsonifiableSchemableClass<$Config>,
|
||||
schema: S,
|
||||
) =>
|
||||
schema.pipe(class_.jsonifySchema)
|
||||
class_: C | JsonifiableSchemableClass<
|
||||
SchemaT,
|
||||
SchemaUnknownKeys,
|
||||
SchemaCatchall,
|
||||
|
||||
// TODO: try to find a way to get rid of the 'class_' arg
|
||||
export const dejsonifySchemableSchema = <
|
||||
C extends JsonifiableSchemableClass<$Config>,
|
||||
$Config extends JsonifiableSchemableConfig,
|
||||
S extends z.ZodType<InstanceType<C>, z.ZodTypeDef, InstanceType<C>>,
|
||||
Values,
|
||||
DefaultValues,
|
||||
|
||||
JsonifySchemaT,
|
||||
JsonifySchemaUnknownKeys,
|
||||
JsonifySchemaCatchall,
|
||||
|
||||
DejsonifySchemaT,
|
||||
DejsonifySchemaUnknownKeys,
|
||||
DejsonifySchemaCatchall,
|
||||
|
||||
JsonifiedValues,
|
||||
|
||||
"Class"
|
||||
>,
|
||||
|
||||
schema: S,
|
||||
) {
|
||||
return schema.pipe(class_.jsonifySchema)
|
||||
}
|
||||
|
||||
|
||||
export function dejsonifySchemableSchema<
|
||||
C extends JsonifiableSchemableClass<
|
||||
SchemaT,
|
||||
SchemaUnknownKeys,
|
||||
SchemaCatchall,
|
||||
|
||||
Values,
|
||||
DefaultValues,
|
||||
|
||||
JsonifySchemaT,
|
||||
JsonifySchemaUnknownKeys,
|
||||
JsonifySchemaCatchall,
|
||||
|
||||
DejsonifySchemaT,
|
||||
DejsonifySchemaUnknownKeys,
|
||||
DejsonifySchemaCatchall,
|
||||
|
||||
JsonifiedValues,
|
||||
|
||||
"Class"
|
||||
>,
|
||||
|
||||
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,
|
||||
|
||||
S extends z.ZodType<InstanceType<C>, z.ZodTypeDef, InstanceType<C>>,
|
||||
>(
|
||||
class_: C | JsonifiableSchemableClass<$Config>,
|
||||
schema: S,
|
||||
) =>
|
||||
class_.dejsonifySchema.transform(v => new class_(v)).pipe(schema)
|
||||
class_: C | JsonifiableSchemableClass<
|
||||
SchemaT,
|
||||
SchemaUnknownKeys,
|
||||
SchemaCatchall,
|
||||
|
||||
Values,
|
||||
DefaultValues,
|
||||
|
||||
JsonifySchemaT,
|
||||
JsonifySchemaUnknownKeys,
|
||||
JsonifySchemaCatchall,
|
||||
|
||||
DejsonifySchemaT,
|
||||
DejsonifySchemaUnknownKeys,
|
||||
DejsonifySchemaCatchall,
|
||||
|
||||
JsonifiedValues,
|
||||
|
||||
"Class"
|
||||
>,
|
||||
|
||||
schema: S,
|
||||
) {
|
||||
return class_.dejsonifySchema.transform(v => new class_(v)).pipe(schema)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user