SchemaWithDefaultValues
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Julien Valverdé
2024-02-26 22:55:44 +01:00
parent c652aae238
commit 055caf1320
5 changed files with 121 additions and 95 deletions

View File

@@ -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,
SchemaWithDefaultValuesT extends z.ZodRawShape,
SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam,
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
Values extends object, Values extends object,
DefaultValues extends Partial<Values>, 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,7 +70,7 @@ 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,
@@ -71,8 +78,12 @@ extends TraitExpressionBuilder<Superclass, Traits> {
SchemaUnknownKeys extends z.UnknownKeysParam, SchemaUnknownKeys extends z.UnknownKeysParam,
SchemaCatchall extends z.ZodTypeAny, SchemaCatchall extends z.ZodTypeAny,
SchemaWithDefaultValuesT extends z.ZodRawShape,
SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam,
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
Values extends object, Values extends object,
DefaultValues extends Partial<Values>, PartialValues extends Partial<Values>,
JsonifySchemaT extends z.ZodRawShape, JsonifySchemaT extends z.ZodRawShape,
JsonifySchemaUnknownKeys extends z.UnknownKeysParam, JsonifySchemaUnknownKeys extends z.UnknownKeysParam,
@@ -85,7 +96,7 @@ extends TraitExpressionBuilder<Superclass, Traits> {
JsonifiedValues extends JsonifiableObject, 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>

View File

@@ -9,11 +9,15 @@ export type ZodSchemaClass<
SchemaUnknownKeys extends z.UnknownKeysParam, SchemaUnknownKeys extends z.UnknownKeysParam,
SchemaCatchall extends z.ZodTypeAny, SchemaCatchall extends z.ZodTypeAny,
SchemaWithDefaultValuesT extends z.ZodRawShape,
SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam,
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
Values extends object, Values extends object,
DefaultValues extends Partial<Values>, 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<
@@ -23,11 +27,15 @@ export type ZodSchemaAbstractClass<
SchemaUnknownKeys extends z.UnknownKeysParam, SchemaUnknownKeys extends z.UnknownKeysParam,
SchemaCatchall extends z.ZodTypeAny, SchemaCatchall extends z.ZodTypeAny,
SchemaWithDefaultValuesT extends z.ZodRawShape,
SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam,
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
Values extends object, Values extends object,
DefaultValues extends Partial<Values>, 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<
@@ -35,9 +43,13 @@ export type ZodSchemas<
SchemaUnknownKeys extends z.UnknownKeysParam, SchemaUnknownKeys extends z.UnknownKeysParam,
SchemaCatchall extends z.ZodTypeAny, SchemaCatchall extends z.ZodTypeAny,
SchemaWithDefaultValuesT extends z.ZodRawShape,
SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam,
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
Values extends object, Values extends object,
DefaultValues extends Partial<Values>, 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>
} }

View File

@@ -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()

View File

@@ -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
) { ) {

View File

@@ -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"
@@ -35,17 +35,18 @@ export const InstantiableZodSchemaObject = trait
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,
this: ZodSchemaClass<Instance, SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, DefaultValues>,
...[values, params]: NewZodSchemaInstanceArgs< Values extends object,
NewZodSchemaInstanceInput<Values, DefaultValues> PartialValues extends Partial<Values>,
> >(
this: ZodSchemaClass<Instance, SchemaT, SchemaUnknownKeys, SchemaCatchall, SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>,
...[values, params]: NewZodSchemaInstanceArgs<PartialValues>
) { ) {
return new this( return new this(
this.schema.parse({ ...this.defaultValues, ...values }, params) this.schemaWithDefaultValues.parse(values, params)
) )
} }
@@ -56,17 +57,18 @@ export const InstantiableZodSchemaObject = trait
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,
this: ZodSchemaClass<Instance, SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, DefaultValues>,
...[values, params]: NewZodSchemaInstanceArgs< Values extends object,
NewZodSchemaInstanceInput<Values, DefaultValues> PartialValues extends Partial<Values>,
> >(
this: ZodSchemaClass<Instance, SchemaT, SchemaUnknownKeys, SchemaCatchall, SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>,
...[values, params]: NewZodSchemaInstanceArgs<PartialValues>
) { ) {
return new this( return new this(
await this.schema.parseAsync({ ...this.defaultValues, ...values }, params) await this.schemaWithDefaultValues.parseAsync(values, params)
) )
} }
@@ -77,22 +79,17 @@ export const InstantiableZodSchemaObject = trait
SchemaUnknownKeys extends z.UnknownKeysParam, SchemaUnknownKeys extends z.UnknownKeysParam,
SchemaCatchall extends z.ZodTypeAny, SchemaCatchall extends z.ZodTypeAny,
SchemaWithDefaultValuesT extends z.ZodRawShape,
SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam,
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
Values extends object, Values extends object,
DefaultValues extends Partial<Values>, 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)),
) )
} }