0.1.2 #3
@@ -4,11 +4,10 @@ import { JsonifiableObject } from "type-fest/source/jsonifiable"
|
|||||||
import { z } from "zod"
|
import { z } from "zod"
|
||||||
import { JsonifiableZodSchemaObject } from "../traits/JsonifiableZodSchemaObject"
|
import { JsonifiableZodSchemaObject } from "../traits/JsonifiableZodSchemaObject"
|
||||||
import { ZodSchemaObject, ZodSchemaObjectTrait } from "../traits/ZodSchemaObject"
|
import { ZodSchemaObject, ZodSchemaObjectTrait } from "../traits/ZodSchemaObject"
|
||||||
import { StaticMembers } from "../util"
|
|
||||||
|
|
||||||
|
|
||||||
export class ZodSchemaClassBuilder<
|
export class ZodSchemaClassBuilder<
|
||||||
Superclass extends AbstractClass<object>,
|
Superclass extends AbstractClass<object, any[]>,
|
||||||
const Traits extends readonly Trait<any, any, any, any>[],
|
const Traits extends readonly Trait<any, any, any, any>[],
|
||||||
> {
|
> {
|
||||||
declare ["constructor"]: typeof ZodSchemaClassBuilder
|
declare ["constructor"]: typeof ZodSchemaClassBuilder
|
||||||
@@ -17,8 +16,6 @@ export class ZodSchemaClassBuilder<
|
|||||||
|
|
||||||
|
|
||||||
schema<
|
schema<
|
||||||
Super extends AbstractClass<object, []> & { schema?: never, schemaWithDefaultValues?: never },
|
|
||||||
|
|
||||||
SchemaT extends z.ZodRawShape,
|
SchemaT extends z.ZodRawShape,
|
||||||
SchemaUnknownKeys extends z.UnknownKeysParam,
|
SchemaUnknownKeys extends z.UnknownKeysParam,
|
||||||
SchemaCatchall extends z.ZodTypeAny,
|
SchemaCatchall extends z.ZodTypeAny,
|
||||||
@@ -30,7 +27,7 @@ export class ZodSchemaClassBuilder<
|
|||||||
Values extends object,
|
Values extends object,
|
||||||
PartialValues extends Partial<Values>,
|
PartialValues extends Partial<Values>,
|
||||||
>(
|
>(
|
||||||
this: ZodSchemaClassBuilder<Super, Traits>,
|
this: ZodSchemaClassBuilder<Superclass, Traits>,
|
||||||
|
|
||||||
{ schema, schemaWithDefaultValues }: {
|
{ schema, schemaWithDefaultValues }: {
|
||||||
schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>
|
schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>
|
||||||
@@ -40,9 +37,8 @@ export class ZodSchemaClassBuilder<
|
|||||||
) => z.ZodObject<SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>
|
) => z.ZodObject<SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>
|
||||||
},
|
},
|
||||||
) {
|
) {
|
||||||
class ZodSchemaObjectConstructor extends (this.expression.expressionSuperclass as AbstractClass<object, []>) {
|
abstract class ZodSchemaObjectConstructor {
|
||||||
constructor(values: Values) {
|
constructor(values: Values) {
|
||||||
super()
|
|
||||||
Object.assign(this, values)
|
Object.assign(this, values)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -50,13 +46,7 @@ export class ZodSchemaClassBuilder<
|
|||||||
return new this.constructor(
|
return new this.constructor(
|
||||||
this.expression
|
this.expression
|
||||||
.extends(
|
.extends(
|
||||||
ZodSchemaObjectConstructor as unknown as (
|
ZodSchemaObjectConstructor as AbstractClass<Values, [values: Values]>
|
||||||
AbstractClass<
|
|
||||||
InstanceType<Super> & Values,
|
|
||||||
[values: Values]
|
|
||||||
> &
|
|
||||||
StaticMembers<Super>
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
.expresses(ZodSchemaObject(schema, schemaWithDefaultValues(schema)))
|
.expresses(ZodSchemaObject(schema, schemaWithDefaultValues(schema)))
|
||||||
)
|
)
|
||||||
|
|||||||
12
src/tests.ts
12
src/tests.ts
@@ -18,15 +18,15 @@ const exp = zodSchemaClass
|
|||||||
id: s.shape.id.default(-1n),
|
id: s.shape.id.default(-1n),
|
||||||
}),
|
}),
|
||||||
})
|
})
|
||||||
// .jsonifiable({
|
.jsonifiable({
|
||||||
// jsonifySchema: s => s.extend({
|
jsonifySchema: s => s.extend({
|
||||||
|
|
||||||
// }),
|
}),
|
||||||
|
|
||||||
// dejsonifySchema: s => s.extend({
|
dejsonifySchema: s => s.extend({
|
||||||
|
|
||||||
// }),
|
}),
|
||||||
// })
|
})
|
||||||
.expression
|
.expression
|
||||||
.expresses(MobXObservableZodSchemaObject)
|
.expresses(MobXObservableZodSchemaObject)
|
||||||
.build()
|
.build()
|
||||||
|
|||||||
Reference in New Issue
Block a user