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