This commit is contained in:
@@ -42,6 +42,9 @@ export class ZodSchemaClassBuilder<
|
|||||||
) => z.ZodObject<SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>
|
) => z.ZodObject<SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>
|
||||||
},
|
},
|
||||||
) {
|
) {
|
||||||
|
const schema = props.schema
|
||||||
|
const schemaWithDefaultValues = props.schemaWithDefaultValues(props.schema)
|
||||||
|
|
||||||
abstract class ZodSchemaObjectConstructor {
|
abstract class ZodSchemaObjectConstructor {
|
||||||
constructor(values: Values) {
|
constructor(values: Values) {
|
||||||
Object.assign(this, values)
|
Object.assign(this, values)
|
||||||
@@ -51,12 +54,9 @@ export class ZodSchemaClassBuilder<
|
|||||||
return new this.constructor(
|
return new this.constructor(
|
||||||
this.expression
|
this.expression
|
||||||
.extends(ZodSchemaObjectConstructor as AbstractClass<Values, [values: Values]>)
|
.extends(ZodSchemaObjectConstructor as AbstractClass<Values, [values: Values]>)
|
||||||
.expresses(ZodSchemaObject(props.schema, props.schemaWithDefaultValues(props.schema))),
|
.expresses(ZodSchemaObject(schema, schemaWithDefaultValues)),
|
||||||
|
|
||||||
{
|
{ schema, schemaWithDefaultValues } as const,
|
||||||
schema: props.schema,
|
|
||||||
schemaWithDefaultValues: props.schemaWithDefaultValues(props.schema),
|
|
||||||
} as const,
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -106,21 +106,20 @@ export class ZodSchemaClassBuilder<
|
|||||||
) => z.ZodObject<DejsonifySchemaT, DejsonifySchemaUnknownKeys, DejsonifySchemaCatchall, Values, JsonifiedValues>
|
) => z.ZodObject<DejsonifySchemaT, DejsonifySchemaUnknownKeys, DejsonifySchemaCatchall, Values, JsonifiedValues>
|
||||||
},
|
},
|
||||||
) {
|
) {
|
||||||
|
const jsonifySchema = props.jsonifySchema(this.schemas.schema)
|
||||||
|
const dejsonifySchema = props.dejsonifySchema(this.schemas.schema)
|
||||||
|
|
||||||
return new this.constructor(
|
return new this.constructor(
|
||||||
this.expression.expresses(
|
this.expression.expresses(
|
||||||
JsonifiableZodSchemaObject(
|
JsonifiableZodSchemaObject(
|
||||||
this.schemas.schema,
|
this.schemas.schema,
|
||||||
this.schemas.schemaWithDefaultValues,
|
this.schemas.schemaWithDefaultValues,
|
||||||
props.jsonifySchema(this.schemas.schema),
|
jsonifySchema,
|
||||||
props.dejsonifySchema(this.schemas.schema),
|
dejsonifySchema,
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
|
|
||||||
{
|
{ ...this.schemas as S, jsonifySchema, dejsonifySchema } as const,
|
||||||
...this.schemas as S,
|
|
||||||
jsonifySchema: props.jsonifySchema(this.schemas.schema),
|
|
||||||
dejsonifySchema: props.dejsonifySchema(this.schemas.schema),
|
|
||||||
} as const,
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,122 +0,0 @@
|
|||||||
import { trait } from "@thilawyn/traitify-ts"
|
|
||||||
import { Effect } from "effect"
|
|
||||||
import { JsonifiableObject } from "type-fest/source/jsonifiable"
|
|
||||||
import { z } from "zod"
|
|
||||||
import { JsonifiableZodSchemaClass } from "../shapes/JsonifiableZodSchemaClass"
|
|
||||||
import { parseZodTypeEffect } from "../util"
|
|
||||||
|
|
||||||
|
|
||||||
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,
|
|
||||||
|
|
||||||
JsonifiedValues extends JsonifiableObject,
|
|
||||||
Values extends object,
|
|
||||||
>(
|
|
||||||
this: JsonifiableZodSchemaClass<
|
|
||||||
Instance,
|
|
||||||
|
|
||||||
JsonifySchemaT,
|
|
||||||
JsonifySchemaUnknownKeys,
|
|
||||||
JsonifySchemaCatchall,
|
|
||||||
|
|
||||||
DejsonifySchemaT,
|
|
||||||
DejsonifySchemaUnknownKeys,
|
|
||||||
DejsonifySchemaCatchall,
|
|
||||||
|
|
||||||
JsonifiedValues,
|
|
||||||
Values
|
|
||||||
>,
|
|
||||||
|
|
||||||
values: JsonifiedValues,
|
|
||||||
params?: Partial<z.ParseParams>,
|
|
||||||
) {
|
|
||||||
return new this(
|
|
||||||
this.dejsonifySchema.parse(values, 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,
|
|
||||||
|
|
||||||
JsonifiedValues extends JsonifiableObject,
|
|
||||||
Values extends object,
|
|
||||||
>(
|
|
||||||
this: JsonifiableZodSchemaClass<
|
|
||||||
Instance,
|
|
||||||
|
|
||||||
JsonifySchemaT,
|
|
||||||
JsonifySchemaUnknownKeys,
|
|
||||||
JsonifySchemaCatchall,
|
|
||||||
|
|
||||||
DejsonifySchemaT,
|
|
||||||
DejsonifySchemaUnknownKeys,
|
|
||||||
DejsonifySchemaCatchall,
|
|
||||||
|
|
||||||
JsonifiedValues,
|
|
||||||
Values
|
|
||||||
>,
|
|
||||||
|
|
||||||
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,
|
|
||||||
|
|
||||||
JsonifiedValues extends JsonifiableObject,
|
|
||||||
Values extends object,
|
|
||||||
>(
|
|
||||||
this: JsonifiableZodSchemaClass<
|
|
||||||
Instance,
|
|
||||||
|
|
||||||
JsonifySchemaT,
|
|
||||||
JsonifySchemaUnknownKeys,
|
|
||||||
JsonifySchemaCatchall,
|
|
||||||
|
|
||||||
DejsonifySchemaT,
|
|
||||||
DejsonifySchemaUnknownKeys,
|
|
||||||
DejsonifySchemaCatchall,
|
|
||||||
|
|
||||||
JsonifiedValues,
|
|
||||||
Values
|
|
||||||
>,
|
|
||||||
|
|
||||||
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