0.1.2 #3
@@ -4,10 +4,8 @@ import { JsonifiableObject } from "type-fest/source/jsonifiable"
|
||||
import { z } from "zod"
|
||||
import { ZodSchemaAbstractClass } from "../shapes/ZodSchemaClass"
|
||||
import { DejsonifiableZodSchemaObject } from "../traits/DejsonifiableZodSchemaObject"
|
||||
import { ExtendableZodSchemaObject } from "../traits/ExtendableZodSchemaObject"
|
||||
import { InstantiableZodSchemaObject } from "../traits/InstantiableZodSchemaObject"
|
||||
import { JsonifiableZodSchemaObject } from "../traits/JsonifiableZodSchemaObject"
|
||||
import { ZodSchemaObjectInstantiationSchemas } from "../traits/ZodSchemaObjectInstantiationSchemas"
|
||||
import { ZodSchemaObject } from "../traits/ZodSchemaObject"
|
||||
import { StaticMembers } from "../util"
|
||||
|
||||
|
||||
@@ -34,7 +32,7 @@ extends TraitExpressionBuilder<Superclass, Traits> {
|
||||
>(
|
||||
this: ZodSchemaClassBuilder<Super, Traits>,
|
||||
|
||||
props: {
|
||||
{ schema, schemaWithDefaultValues }: {
|
||||
schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>
|
||||
|
||||
schemaWithDefaultValues: (
|
||||
@@ -42,10 +40,7 @@ extends TraitExpressionBuilder<Superclass, Traits> {
|
||||
) => z.ZodObject<SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>
|
||||
},
|
||||
) {
|
||||
class Schemas extends (this.expressionSuperclass as AbstractClass<object, []>) {
|
||||
static readonly schema = props.schema
|
||||
static readonly schemaWithDefaultValues = props.schemaWithDefaultValues(props.schema)
|
||||
|
||||
class ZodSchemaObjectConstructor extends (this.expressionSuperclass as AbstractClass<object, []>) {
|
||||
constructor(values: Values) {
|
||||
super()
|
||||
Object.assign(this, values)
|
||||
@@ -53,21 +48,15 @@ extends TraitExpressionBuilder<Superclass, Traits> {
|
||||
}
|
||||
|
||||
return new this.constructor(
|
||||
Schemas as unknown as (
|
||||
ZodSchemaObjectConstructor as unknown as (
|
||||
AbstractClass<
|
||||
InstanceType<Super> & Values,
|
||||
ConstructorParameters<typeof Schemas>
|
||||
[values: Values]
|
||||
> &
|
||||
StaticMembers<Super> &
|
||||
StaticMembers<typeof Schemas>
|
||||
StaticMembers<Super>
|
||||
),
|
||||
|
||||
[
|
||||
...this.expressionTraits,
|
||||
ZodSchemaObjectInstantiationSchemas,
|
||||
InstantiableZodSchemaObject,
|
||||
ExtendableZodSchemaObject,
|
||||
],
|
||||
[...this.expressionTraits, ZodSchemaObject(schema, schemaWithDefaultValues)],
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
77
src/tests.ts
77
src/tests.ts
@@ -1,69 +1,24 @@
|
||||
import { Implements, expression } from "@thilawyn/traitify-ts"
|
||||
import { Class } from "type-fest"
|
||||
import { Implements } from "@thilawyn/traitify-ts"
|
||||
import { z } from "zod"
|
||||
import { ZodSchemaObject } from "./traits/ZodSchemaObject"
|
||||
import { StaticMembers } from "./util"
|
||||
import { zodSchemaClass } from "./builders/ZodSchemaClassBuilder"
|
||||
import { MobXObservableZodSchemaObject } from "./traits/MobXObservableZodSchemaObject"
|
||||
|
||||
|
||||
// const exp = zodSchemaClass
|
||||
// .schema({
|
||||
// schema: z.object({
|
||||
// /** User ID */
|
||||
// id: z.bigint(),
|
||||
const exp = zodSchemaClass
|
||||
.schema({
|
||||
schema: z.object({
|
||||
/** User ID */
|
||||
id: z.bigint(),
|
||||
|
||||
// /** Username */
|
||||
// name: z.string(),
|
||||
// }),
|
||||
/** Username */
|
||||
name: z.string(),
|
||||
}),
|
||||
|
||||
// schemaWithDefaultValues: s => s.extend({
|
||||
// id: s.shape.id.default(-1n),
|
||||
// }),
|
||||
// })
|
||||
// .jsonifiable({
|
||||
// jsonifySchema: s => s.extend({
|
||||
// id: jsonify.bigint(s.shape.id)
|
||||
// }),
|
||||
|
||||
// dejsonifySchema: s => s.extend({
|
||||
// id: dejsonify.bigint(s.shape.id)
|
||||
// }),
|
||||
// })
|
||||
// .expresses(MobXObservableZodSchemaObject)
|
||||
// .build()
|
||||
|
||||
const schema = z.object({
|
||||
/** User ID */
|
||||
id: z.bigint(),
|
||||
|
||||
/** Username */
|
||||
name: z.string(),
|
||||
})
|
||||
const schemaWithDefaultsValues = schema.extend({
|
||||
id: schema.shape.id.default(-1n),
|
||||
})
|
||||
class UserSchemas {
|
||||
static readonly schema = schema
|
||||
static readonly schemaWithDefaultValues = schemaWithDefaultsValues
|
||||
|
||||
constructor(values: z.output<typeof schema>) {
|
||||
Object.assign(this, values)
|
||||
}
|
||||
}
|
||||
|
||||
const exp = expression
|
||||
.extends(UserSchemas as (
|
||||
Class<UserSchemas & z.output<typeof schema>> &
|
||||
StaticMembers<typeof UserSchemas>
|
||||
))
|
||||
.expresses(
|
||||
ZodSchemaObject(
|
||||
schema,
|
||||
|
||||
s => s.extend({
|
||||
id: s.shape.id.default(-1n),
|
||||
}),
|
||||
)
|
||||
)
|
||||
schemaWithDefaultValues: s => s.extend({
|
||||
id: s.shape.id.default(-1n),
|
||||
}),
|
||||
})
|
||||
.expresses(MobXObservableZodSchemaObject)
|
||||
.build()
|
||||
|
||||
|
||||
|
||||
@@ -1,90 +0,0 @@
|
||||
import { TraitStaticMembers, trait } from "@thilawyn/traitify-ts"
|
||||
import { HasRequiredKeys } from "type-fest"
|
||||
import { z } from "zod"
|
||||
import { ZodSchemaClass } from "../shapes/ZodSchemaClass"
|
||||
import { parseZodTypeEffect } from "../util"
|
||||
import { ZodSchemaObjectInstantiationSchemas } from "./ZodSchemaObjectInstantiationSchemas"
|
||||
|
||||
|
||||
type ZodSchemaObjectInstantiationSchemasStaticMembers = TraitStaticMembers<typeof ZodSchemaObjectInstantiationSchemas>
|
||||
|
||||
type NewZodSchemaInstanceInput<
|
||||
Values extends object,
|
||||
DefaultValues extends Partial<Values>,
|
||||
> = {
|
||||
[Key in Exclude<keyof Values, keyof DefaultValues>]: Values[Key]
|
||||
} & {
|
||||
[Key in keyof DefaultValues]?: Key extends keyof Values
|
||||
? Values[Key]
|
||||
: never
|
||||
}
|
||||
|
||||
type ParseParamsArgs = [] | [params: Partial<z.ParseParams>]
|
||||
|
||||
type NewZodSchemaInstanceArgs<Input extends object> = (
|
||||
HasRequiredKeys<Input> extends true
|
||||
? [values: Input, ...args: ParseParamsArgs]
|
||||
: [] | [values: Input, ...args: ParseParamsArgs]
|
||||
)
|
||||
|
||||
|
||||
export const InstantiableZodSchemaObject = trait
|
||||
.implement(Super => class InstantiableZodSchemaObject extends Super {
|
||||
static create<
|
||||
Instance extends Values,
|
||||
|
||||
SchemaWithDefaultValuesT extends z.ZodRawShape,
|
||||
SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam,
|
||||
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
|
||||
|
||||
Values extends object,
|
||||
PartialValues extends Partial<Values>,
|
||||
>(
|
||||
this: (
|
||||
ZodSchemaClass<Instance, any, any, any, SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues> &
|
||||
ZodSchemaObjectInstantiationSchemasStaticMembers
|
||||
),
|
||||
...[values, params]: NewZodSchemaInstanceArgs<PartialValues>
|
||||
) {
|
||||
return this.instantiationSchemaWithDefaultValues().parse(values, params)
|
||||
}
|
||||
|
||||
static async createPromise<
|
||||
Instance extends Values,
|
||||
|
||||
SchemaWithDefaultValuesT extends z.ZodRawShape,
|
||||
SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam,
|
||||
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
|
||||
|
||||
Values extends object,
|
||||
PartialValues extends Partial<Values>,
|
||||
>(
|
||||
this: (
|
||||
ZodSchemaClass<Instance, any, any, any, SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues> &
|
||||
ZodSchemaObjectInstantiationSchemasStaticMembers
|
||||
),
|
||||
...[values, params]: NewZodSchemaInstanceArgs<PartialValues>
|
||||
) {
|
||||
return this.instantiationSchemaWithDefaultValues().parseAsync(values, params)
|
||||
}
|
||||
|
||||
static createEffect<
|
||||
Instance extends Values,
|
||||
|
||||
SchemaWithDefaultValuesT extends z.ZodRawShape,
|
||||
SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam,
|
||||
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
|
||||
|
||||
Values extends object,
|
||||
PartialValues extends Partial<Values>,
|
||||
>(
|
||||
this: (
|
||||
ZodSchemaClass<Instance, any, any, any, SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues> &
|
||||
ZodSchemaObjectInstantiationSchemasStaticMembers
|
||||
),
|
||||
...[values, params]: NewZodSchemaInstanceArgs<PartialValues>
|
||||
) {
|
||||
return parseZodTypeEffect(this.instantiationSchemaWithDefaultValues(), values, params)
|
||||
}
|
||||
})
|
||||
.build()
|
||||
@@ -1,37 +0,0 @@
|
||||
import { trait } from "@thilawyn/traitify-ts"
|
||||
import { z } from "zod"
|
||||
import { ZodSchemaClass } from "../shapes/ZodSchemaClass"
|
||||
|
||||
|
||||
export const ZodSchemaObjectInstantiationSchemas = trait
|
||||
.implement(Super => class ZodSchemaObjectInstantiationSchemas extends Super {
|
||||
static instantiationSchema<
|
||||
Instance extends Values,
|
||||
|
||||
SchemaT extends z.ZodRawShape,
|
||||
SchemaUnknownKeys extends z.UnknownKeysParam,
|
||||
SchemaCatchall extends z.ZodTypeAny,
|
||||
|
||||
Values extends object,
|
||||
>(
|
||||
this: ZodSchemaClass<Instance, SchemaT, SchemaUnknownKeys, SchemaCatchall, any, any, any, Values, any>,
|
||||
) {
|
||||
return this.schema.transform(values => new this(values))
|
||||
}
|
||||
|
||||
static instantiationSchemaWithDefaultValues<
|
||||
Instance extends Values,
|
||||
|
||||
SchemaWithDefaultValuesT extends z.ZodRawShape,
|
||||
SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam,
|
||||
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
|
||||
|
||||
Values extends object,
|
||||
PartialValues extends Partial<Values>,
|
||||
>(
|
||||
this: ZodSchemaClass<Instance, any, any, any, SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>,
|
||||
) {
|
||||
return this.schemaWithDefaultValues.transform(values => new this(values))
|
||||
}
|
||||
})
|
||||
.build()
|
||||
Reference in New Issue
Block a user