This commit is contained in:
@@ -18,25 +18,32 @@ extends TraitExpressionBuilder<Superclass, Traits> {
|
|||||||
declare ["constructor"]: typeof ZodSchemaClassBuilder
|
declare ["constructor"]: typeof ZodSchemaClassBuilder
|
||||||
|
|
||||||
schema<
|
schema<
|
||||||
Super extends AbstractClass<object, []> & { schema?: never, defaultValues?: never },
|
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,
|
||||||
|
|
||||||
Values extends object,
|
SchemaWithDefaultValuesT extends z.ZodRawShape,
|
||||||
DefaultValues extends Partial<Values>,
|
SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam,
|
||||||
|
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
|
||||||
|
|
||||||
|
Values extends object,
|
||||||
|
PartialValues extends Partial<Values>,
|
||||||
>(
|
>(
|
||||||
this: ZodSchemaClassBuilder<Super, Traits>,
|
this: ZodSchemaClassBuilder<Super, Traits>,
|
||||||
|
|
||||||
{ schema, defaultValues }: {
|
props: {
|
||||||
schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>
|
schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>
|
||||||
defaultValues: DefaultValues
|
|
||||||
|
schemaWithDefaultValues: (
|
||||||
|
schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>
|
||||||
|
) => z.ZodObject<SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>
|
||||||
},
|
},
|
||||||
) {
|
) {
|
||||||
class Schemas extends (this.expressionSuperclass as AbstractClass<object, []>) {
|
class Schemas extends (this.expressionSuperclass as AbstractClass<object, []>) {
|
||||||
static readonly schema = schema
|
static readonly schema = props.schema
|
||||||
static readonly defaultValues = defaultValues
|
static readonly schemaWithDefaultValues = props.schemaWithDefaultValues(props.schema)
|
||||||
|
|
||||||
constructor(values: Values) {
|
constructor(values: Values) {
|
||||||
super()
|
super()
|
||||||
@@ -63,29 +70,33 @@ extends TraitExpressionBuilder<Superclass, Traits> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
jsonifiable<
|
jsonifiable<
|
||||||
Super extends ZodSchemaAbstractClass<Instance, SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, DefaultValues>
|
Super extends ZodSchemaAbstractClass<Instance, SchemaT, SchemaUnknownKeys, SchemaCatchall, SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>
|
||||||
& { jsonifySchema?: never, dejsonifySchema?: never },
|
& { jsonifySchema?: never, dejsonifySchema?: never },
|
||||||
Instance extends Values,
|
Instance extends Values,
|
||||||
|
|
||||||
SchemaT extends z.ZodRawShape,
|
SchemaT extends z.ZodRawShape,
|
||||||
SchemaUnknownKeys extends z.UnknownKeysParam,
|
SchemaUnknownKeys extends z.UnknownKeysParam,
|
||||||
SchemaCatchall extends z.ZodTypeAny,
|
SchemaCatchall extends z.ZodTypeAny,
|
||||||
|
|
||||||
Values extends object,
|
SchemaWithDefaultValuesT extends z.ZodRawShape,
|
||||||
DefaultValues extends Partial<Values>,
|
SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam,
|
||||||
|
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
|
||||||
|
|
||||||
JsonifySchemaT extends z.ZodRawShape,
|
Values extends object,
|
||||||
JsonifySchemaUnknownKeys extends z.UnknownKeysParam,
|
PartialValues extends Partial<Values>,
|
||||||
JsonifySchemaCatchall extends z.ZodTypeAny,
|
|
||||||
|
|
||||||
DejsonifySchemaT extends z.ZodRawShape,
|
JsonifySchemaT extends z.ZodRawShape,
|
||||||
DejsonifySchemaUnknownKeys extends z.UnknownKeysParam,
|
JsonifySchemaUnknownKeys extends z.UnknownKeysParam,
|
||||||
DejsonifySchemaCatchall extends z.ZodTypeAny,
|
JsonifySchemaCatchall extends z.ZodTypeAny,
|
||||||
|
|
||||||
JsonifiedValues extends JsonifiableObject,
|
DejsonifySchemaT extends z.ZodRawShape,
|
||||||
|
DejsonifySchemaUnknownKeys extends z.UnknownKeysParam,
|
||||||
|
DejsonifySchemaCatchall extends z.ZodTypeAny,
|
||||||
|
|
||||||
|
JsonifiedValues extends JsonifiableObject,
|
||||||
>(
|
>(
|
||||||
this: ZodSchemaClassBuilder<
|
this: ZodSchemaClassBuilder<
|
||||||
Super | ZodSchemaAbstractClass<Instance, SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, DefaultValues>,
|
Super | ZodSchemaAbstractClass<Instance, SchemaT, SchemaUnknownKeys, SchemaCatchall, SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>,
|
||||||
Traits
|
Traits
|
||||||
>,
|
>,
|
||||||
|
|
||||||
@@ -115,7 +126,7 @@ extends TraitExpressionBuilder<Superclass, Traits> {
|
|||||||
Instance & JsonifiableSchemas,
|
Instance & JsonifiableSchemas,
|
||||||
|
|
||||||
// TODO: for some reason, ConstructorParameters<Super> does not work here. Maybe try to find a fix?
|
// TODO: for some reason, ConstructorParameters<Super> does not work here. Maybe try to find a fix?
|
||||||
ConstructorParameters<ZodSchemaAbstractClass<Instance, SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, DefaultValues>>
|
ConstructorParameters<ZodSchemaAbstractClass<Instance, SchemaT, SchemaUnknownKeys, SchemaCatchall, SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>>
|
||||||
> &
|
> &
|
||||||
StaticMembers<Super> &
|
StaticMembers<Super> &
|
||||||
StaticMembers<typeof JsonifiableSchemas>
|
StaticMembers<typeof JsonifiableSchemas>
|
||||||
|
|||||||
@@ -3,41 +3,53 @@ import { z } from "zod"
|
|||||||
|
|
||||||
|
|
||||||
export type ZodSchemaClass<
|
export type ZodSchemaClass<
|
||||||
Instance extends Values,
|
Instance extends Values,
|
||||||
|
|
||||||
SchemaT extends z.ZodRawShape,
|
SchemaT extends z.ZodRawShape,
|
||||||
SchemaUnknownKeys extends z.UnknownKeysParam,
|
SchemaUnknownKeys extends z.UnknownKeysParam,
|
||||||
SchemaCatchall extends z.ZodTypeAny,
|
SchemaCatchall extends z.ZodTypeAny,
|
||||||
|
|
||||||
Values extends object,
|
SchemaWithDefaultValuesT extends z.ZodRawShape,
|
||||||
DefaultValues extends Partial<Values>,
|
SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam,
|
||||||
|
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
|
||||||
|
|
||||||
|
Values extends object,
|
||||||
|
PartialValues extends Partial<Values>,
|
||||||
> = (
|
> = (
|
||||||
Class<Instance, [values: Values]> &
|
Class<Instance, [values: Values]> &
|
||||||
ZodSchemas<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, DefaultValues>
|
ZodSchemas<SchemaT, SchemaUnknownKeys, SchemaCatchall, SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>
|
||||||
)
|
)
|
||||||
|
|
||||||
export type ZodSchemaAbstractClass<
|
export type ZodSchemaAbstractClass<
|
||||||
Instance extends Values,
|
Instance extends Values,
|
||||||
|
|
||||||
SchemaT extends z.ZodRawShape,
|
SchemaT extends z.ZodRawShape,
|
||||||
SchemaUnknownKeys extends z.UnknownKeysParam,
|
SchemaUnknownKeys extends z.UnknownKeysParam,
|
||||||
SchemaCatchall extends z.ZodTypeAny,
|
SchemaCatchall extends z.ZodTypeAny,
|
||||||
|
|
||||||
Values extends object,
|
SchemaWithDefaultValuesT extends z.ZodRawShape,
|
||||||
DefaultValues extends Partial<Values>,
|
SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam,
|
||||||
|
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
|
||||||
|
|
||||||
|
Values extends object,
|
||||||
|
PartialValues extends Partial<Values>,
|
||||||
> = (
|
> = (
|
||||||
AbstractClass<Instance, [values: Values]> &
|
AbstractClass<Instance, [values: Values]> &
|
||||||
ZodSchemas<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, DefaultValues>
|
ZodSchemas<SchemaT, SchemaUnknownKeys, SchemaCatchall, SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>
|
||||||
)
|
)
|
||||||
|
|
||||||
export type ZodSchemas<
|
export type ZodSchemas<
|
||||||
SchemaT extends z.ZodRawShape,
|
SchemaT extends z.ZodRawShape,
|
||||||
SchemaUnknownKeys extends z.UnknownKeysParam,
|
SchemaUnknownKeys extends z.UnknownKeysParam,
|
||||||
SchemaCatchall extends z.ZodTypeAny,
|
SchemaCatchall extends z.ZodTypeAny,
|
||||||
|
|
||||||
Values extends object,
|
SchemaWithDefaultValuesT extends z.ZodRawShape,
|
||||||
DefaultValues extends Partial<Values>,
|
SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam,
|
||||||
|
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
|
||||||
|
|
||||||
|
Values extends object,
|
||||||
|
PartialValues extends Partial<Values>,
|
||||||
> = {
|
> = {
|
||||||
readonly schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>
|
readonly schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>
|
||||||
readonly defaultValues: DefaultValues
|
readonly schemaWithDefaultValues: z.ZodObject<SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>
|
||||||
}
|
}
|
||||||
|
|||||||
12
src/tests.ts
12
src/tests.ts
@@ -3,7 +3,6 @@ import { z } from "zod"
|
|||||||
import { zodSchemaClass } from "./builders/ZodSchemaClassBuilder"
|
import { zodSchemaClass } from "./builders/ZodSchemaClassBuilder"
|
||||||
import { dejsonify, jsonify } from "./schema/jsonify"
|
import { dejsonify, jsonify } from "./schema/jsonify"
|
||||||
import { MobXObservableZodSchemaObject } from "./traits/MobXObservableZodSchemaObject"
|
import { MobXObservableZodSchemaObject } from "./traits/MobXObservableZodSchemaObject"
|
||||||
import { StaticMembers } from "./util"
|
|
||||||
|
|
||||||
|
|
||||||
const exp = zodSchemaClass
|
const exp = zodSchemaClass
|
||||||
@@ -16,7 +15,9 @@ const exp = zodSchemaClass
|
|||||||
name: z.string(),
|
name: z.string(),
|
||||||
}),
|
}),
|
||||||
|
|
||||||
defaultValues: { id: -1n },
|
schemaWithDefaultValues: s => s.extend({
|
||||||
|
id: s.shape.id.default(-1n)
|
||||||
|
}),
|
||||||
})
|
})
|
||||||
.jsonifiable({
|
.jsonifiable({
|
||||||
jsonifySchema: s => s.extend({
|
jsonifySchema: s => s.extend({
|
||||||
@@ -33,7 +34,12 @@ const exp = zodSchemaClass
|
|||||||
@exp.staticImplements
|
@exp.staticImplements
|
||||||
class User extends exp.extends implements Implements<typeof exp> {}
|
class User extends exp.extends implements Implements<typeof exp> {}
|
||||||
|
|
||||||
User.defaultValues
|
const test = User.schema.extend({
|
||||||
|
prout: z.string()
|
||||||
|
}).merge(
|
||||||
|
User.schemaWithDefaultValues
|
||||||
|
)
|
||||||
|
|
||||||
const inst = User.create({ id: 1n, name: "" })
|
const inst = User.create({ id: 1n, name: "" })
|
||||||
// console.log(inst)
|
// console.log(inst)
|
||||||
const jsonifiedUser = await inst.jsonifyPromise()
|
const jsonifiedUser = await inst.jsonifyPromise()
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import { ZodSchemaClass } from "../shapes/ZodSchemaClass"
|
|||||||
export const ExtendableZodSchemaObject = trait
|
export const ExtendableZodSchemaObject = trait
|
||||||
.implement(Super => class ExtendableZodSchemaObject extends Super {
|
.implement(Super => class ExtendableZodSchemaObject extends Super {
|
||||||
static extend<
|
static extend<
|
||||||
Self extends ZodSchemaClass<any, any, any, any, any, any>,
|
Self extends ZodSchemaClass<any, any, any, any, any, any, any, any, any>,
|
||||||
>(
|
>(
|
||||||
this: Self
|
this: Self
|
||||||
) {
|
) {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { trait } from "@thilawyn/traitify-ts"
|
import { trait } from "@thilawyn/traitify-ts"
|
||||||
import { Effect, pipe } from "effect"
|
import { Effect } from "effect"
|
||||||
import { HasRequiredKeys } from "type-fest"
|
import { HasRequiredKeys } from "type-fest"
|
||||||
import { z } from "zod"
|
import { z } from "zod"
|
||||||
import { ZodSchemaClass } from "../shapes/ZodSchemaClass"
|
import { ZodSchemaClass } from "../shapes/ZodSchemaClass"
|
||||||
@@ -29,70 +29,67 @@ type NewZodSchemaInstanceArgs<Input extends object> = (
|
|||||||
export const InstantiableZodSchemaObject = trait
|
export const InstantiableZodSchemaObject = trait
|
||||||
.implement(Super => class InstantiableZodSchemaObject extends Super {
|
.implement(Super => class InstantiableZodSchemaObject extends Super {
|
||||||
static create<
|
static create<
|
||||||
Instance extends Values,
|
Instance extends Values,
|
||||||
|
|
||||||
SchemaT extends z.ZodRawShape,
|
SchemaT extends z.ZodRawShape,
|
||||||
SchemaUnknownKeys extends z.UnknownKeysParam,
|
SchemaUnknownKeys extends z.UnknownKeysParam,
|
||||||
SchemaCatchall extends z.ZodTypeAny,
|
SchemaCatchall extends z.ZodTypeAny,
|
||||||
|
|
||||||
Values extends object,
|
SchemaWithDefaultValuesT extends z.ZodRawShape,
|
||||||
DefaultValues extends Partial<Values>,
|
SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam,
|
||||||
|
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
|
||||||
|
|
||||||
|
Values extends object,
|
||||||
|
PartialValues extends Partial<Values>,
|
||||||
>(
|
>(
|
||||||
this: ZodSchemaClass<Instance, SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, DefaultValues>,
|
this: ZodSchemaClass<Instance, SchemaT, SchemaUnknownKeys, SchemaCatchall, SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>,
|
||||||
|
...[values, params]: NewZodSchemaInstanceArgs<PartialValues>
|
||||||
...[values, params]: NewZodSchemaInstanceArgs<
|
|
||||||
NewZodSchemaInstanceInput<Values, DefaultValues>
|
|
||||||
>
|
|
||||||
) {
|
) {
|
||||||
return new this(
|
return new this(
|
||||||
this.schema.parse({ ...this.defaultValues, ...values }, params)
|
this.schemaWithDefaultValues.parse(values, params)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
static async createPromise<
|
static async createPromise<
|
||||||
Instance extends Values,
|
Instance extends Values,
|
||||||
|
|
||||||
SchemaT extends z.ZodRawShape,
|
SchemaT extends z.ZodRawShape,
|
||||||
SchemaUnknownKeys extends z.UnknownKeysParam,
|
SchemaUnknownKeys extends z.UnknownKeysParam,
|
||||||
SchemaCatchall extends z.ZodTypeAny,
|
SchemaCatchall extends z.ZodTypeAny,
|
||||||
|
|
||||||
Values extends object,
|
SchemaWithDefaultValuesT extends z.ZodRawShape,
|
||||||
DefaultValues extends Partial<Values>,
|
SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam,
|
||||||
|
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
|
||||||
|
|
||||||
|
Values extends object,
|
||||||
|
PartialValues extends Partial<Values>,
|
||||||
>(
|
>(
|
||||||
this: ZodSchemaClass<Instance, SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, DefaultValues>,
|
this: ZodSchemaClass<Instance, SchemaT, SchemaUnknownKeys, SchemaCatchall, SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>,
|
||||||
|
...[values, params]: NewZodSchemaInstanceArgs<PartialValues>
|
||||||
...[values, params]: NewZodSchemaInstanceArgs<
|
|
||||||
NewZodSchemaInstanceInput<Values, DefaultValues>
|
|
||||||
>
|
|
||||||
) {
|
) {
|
||||||
return new this(
|
return new this(
|
||||||
await this.schema.parseAsync({ ...this.defaultValues, ...values }, params)
|
await this.schemaWithDefaultValues.parseAsync(values, params)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
static createEffect<
|
static createEffect<
|
||||||
Instance extends Values,
|
Instance extends Values,
|
||||||
|
|
||||||
SchemaT extends z.ZodRawShape,
|
SchemaT extends z.ZodRawShape,
|
||||||
SchemaUnknownKeys extends z.UnknownKeysParam,
|
SchemaUnknownKeys extends z.UnknownKeysParam,
|
||||||
SchemaCatchall extends z.ZodTypeAny,
|
SchemaCatchall extends z.ZodTypeAny,
|
||||||
|
|
||||||
Values extends object,
|
SchemaWithDefaultValuesT extends z.ZodRawShape,
|
||||||
DefaultValues extends Partial<Values>,
|
SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam,
|
||||||
|
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
|
||||||
|
|
||||||
|
Values extends object,
|
||||||
|
PartialValues extends Partial<Values>,
|
||||||
>(
|
>(
|
||||||
this: ZodSchemaClass<Instance, SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, DefaultValues>,
|
this: ZodSchemaClass<Instance, SchemaT, SchemaUnknownKeys, SchemaCatchall, SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>,
|
||||||
|
...[values, params]: NewZodSchemaInstanceArgs<PartialValues>
|
||||||
...[values, params]: NewZodSchemaInstanceArgs<
|
|
||||||
NewZodSchemaInstanceInput<Values, DefaultValues>
|
|
||||||
>
|
|
||||||
) {
|
) {
|
||||||
return pipe(
|
return parseZodTypeEffect(this.schemaWithDefaultValues, values, params).pipe(
|
||||||
parseZodTypeEffect(
|
|
||||||
this.schema,
|
|
||||||
{ ...this.defaultValues, ...values },
|
|
||||||
params,
|
|
||||||
),
|
|
||||||
|
|
||||||
Effect.map(values => new this(values)),
|
Effect.map(values => new this(values)),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user