0.1.3 #4
@@ -11,23 +11,14 @@ class ZodSchemaObjectConstructor<Values> {
|
||||
|
||||
|
||||
export function ZodSchemaClass<
|
||||
SchemaT extends z.ZodRawShape,
|
||||
SchemaCatchall extends z.ZodTypeAny,
|
||||
SchemaWithDefaultValuesT extends z.ZodRawShape,
|
||||
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
|
||||
|
||||
T extends z.ZodRawShape,
|
||||
Catchall extends z.ZodTypeAny,
|
||||
Values extends object,
|
||||
PartialValues extends Partial<Values>,
|
||||
>(
|
||||
props: {
|
||||
schema: z.ZodObject<SchemaT, "strip", SchemaCatchall, Values, Values>
|
||||
|
||||
schemaWithDefaultValues: (
|
||||
schema: z.ZodObject<SchemaT, "strip", SchemaCatchall, Values, Values>
|
||||
) => z.ZodObject<SchemaWithDefaultValuesT, "strip", SchemaWithDefaultValuesCatchall, Values, PartialValues>
|
||||
}
|
||||
schema: z.ZodObject<T, "strip", Catchall, Values, PartialValues>
|
||||
) {
|
||||
return expression
|
||||
.extends(ZodSchemaObjectConstructor<Values>)
|
||||
.expresses(ZodSchemaObject(props.schema, props.schemaWithDefaultValues(props.schema)))
|
||||
.expresses(ZodSchemaObject(schema))
|
||||
}
|
||||
|
||||
12
src/tests.ts
12
src/tests.ts
@@ -31,19 +31,15 @@ import { MobXObservableZodSchemaObject } from "./traits/MobXObservableZodSchemaO
|
||||
// .expresses(MobXObservableZodSchemaObject)
|
||||
// .build()
|
||||
|
||||
const exp = ZodSchemaClass({
|
||||
schema: z.object({
|
||||
const exp = ZodSchemaClass(
|
||||
z.object({
|
||||
/** User ID */
|
||||
id: z.bigint(),
|
||||
id: z.bigint().default(-1n),
|
||||
|
||||
/** Username */
|
||||
name: z.string(),
|
||||
}),
|
||||
|
||||
schemaWithDefaultValues: s => s.extend({
|
||||
id: s.shape.id.default(-1n),
|
||||
}),
|
||||
})
|
||||
)
|
||||
.expresses(MobXObservableZodSchemaObject)
|
||||
.build()
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { ImplStatic, trait } from "@thilawyn/traitify-ts"
|
||||
import { Class, HasRequiredKeys } from "type-fest"
|
||||
import { z } from "zod"
|
||||
import { StaticMembers, parseZodSchemaEffect } from "../util"
|
||||
import { StaticMembers, parseZodSchemaEffect, stripZodObjectDefaults } from "../util"
|
||||
|
||||
|
||||
type CreateArgs<Input extends object> = (
|
||||
@@ -12,33 +12,29 @@ type CreateArgs<Input extends object> = (
|
||||
|
||||
|
||||
export const ZodSchemaObject = <
|
||||
SchemaT extends z.ZodRawShape,
|
||||
SchemaCatchall extends z.ZodTypeAny,
|
||||
SchemaWithDefaultValuesT extends z.ZodRawShape,
|
||||
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
|
||||
|
||||
T extends z.ZodRawShape,
|
||||
Catchall extends z.ZodTypeAny,
|
||||
Values extends object,
|
||||
PartialValues extends Partial<Values>,
|
||||
>(
|
||||
schema: z.ZodObject<SchemaT, "strip", SchemaCatchall, Values, Values>,
|
||||
schemaWithDefaultValues: z.ZodObject<SchemaWithDefaultValuesT, "strip", SchemaWithDefaultValuesCatchall, Values, PartialValues>,
|
||||
schemaWithDefaults: z.ZodObject<T, "strip", Catchall, Values, PartialValues>,
|
||||
) => trait
|
||||
.implement(Super => {
|
||||
class ZodSchemaObject extends Super {
|
||||
static readonly schema = schema
|
||||
static readonly schemaWithDefaultValues = schemaWithDefaultValues
|
||||
static readonly schema = stripZodObjectDefaults(schemaWithDefaults)
|
||||
static readonly schemaWithDefaults = schemaWithDefaults
|
||||
|
||||
static transform<
|
||||
Instance extends Values,
|
||||
|
||||
T extends z.ZodRawShape,
|
||||
UnknownKeys extends z.UnknownKeysParam,
|
||||
Catchall extends z.ZodTypeAny,
|
||||
Output extends Values,
|
||||
Input,
|
||||
TransformT extends z.ZodRawShape,
|
||||
TransformUnknownKeys extends z.UnknownKeysParam,
|
||||
TransformCatchall extends z.ZodTypeAny,
|
||||
TransformOutput extends Values,
|
||||
TransformInput,
|
||||
>(
|
||||
this: Class<Instance, [values: Values]>,
|
||||
schema: z.ZodObject<T, UnknownKeys, Catchall, Output, Input>,
|
||||
schema: z.ZodObject<TransformT, TransformUnknownKeys, TransformCatchall, TransformOutput, TransformInput>,
|
||||
) {
|
||||
return schema.transform(values => new this(values))
|
||||
}
|
||||
@@ -54,7 +50,7 @@ export const ZodSchemaObject = <
|
||||
...[values, params]: CreateArgs<PartialValues>
|
||||
) {
|
||||
return this
|
||||
.transform(this.schemaWithDefaultValues)
|
||||
.transform(this.schemaWithDefaults)
|
||||
.parse(values, params)
|
||||
}
|
||||
|
||||
@@ -68,7 +64,7 @@ export const ZodSchemaObject = <
|
||||
...[values, params]: CreateArgs<PartialValues>
|
||||
) {
|
||||
return this
|
||||
.transform(this.schemaWithDefaultValues)
|
||||
.transform(this.schemaWithDefaults)
|
||||
.parseAsync(values, params)
|
||||
}
|
||||
|
||||
@@ -82,7 +78,7 @@ export const ZodSchemaObject = <
|
||||
...[values, params]: CreateArgs<PartialValues>
|
||||
) {
|
||||
return parseZodSchemaEffect(
|
||||
this.transform(this.schemaWithDefaultValues),
|
||||
this.transform(this.schemaWithDefaults),
|
||||
values,
|
||||
params,
|
||||
)
|
||||
@@ -95,23 +91,12 @@ export const ZodSchemaObject = <
|
||||
|
||||
|
||||
export type ZodSchemaObjectTrait<
|
||||
SchemaT extends z.ZodRawShape,
|
||||
SchemaCatchall extends z.ZodTypeAny,
|
||||
SchemaWithDefaultValuesT extends z.ZodRawShape,
|
||||
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
|
||||
|
||||
T extends z.ZodRawShape,
|
||||
Catchall extends z.ZodTypeAny,
|
||||
Values extends object,
|
||||
PartialValues extends Partial<Values>,
|
||||
> = (
|
||||
ReturnType<
|
||||
typeof ZodSchemaObject<
|
||||
SchemaT,
|
||||
SchemaCatchall,
|
||||
SchemaWithDefaultValuesT,
|
||||
SchemaWithDefaultValuesCatchall,
|
||||
|
||||
Values,
|
||||
PartialValues
|
||||
>
|
||||
typeof ZodSchemaObject<T, Catchall, Values, PartialValues>
|
||||
>
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user