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