Error Unmergable intersection
when reusing schema
#2850
-
DescriptionI need to transform an id param to a MongoDb's ObjectId for an input property which follows the example posted here https://ez.robintail.cz/v24.7.0/transformations. This works fine until a middleware is added and the following error is returned ExpectedI would expect this to work with a middleware. ReproductionI've tried both z.preprocess and .transform to transform the input string to an ObjectId. export const objectIdPreprocess = z.preprocess((val) => {
if (typeof val === 'string') {
return new ObjectId(val);
}
return val;
}, z.instanceof(ObjectId));
// export const objectIdTransform = z.string().transform((val) => {
// return new ObjectId(val);
// })
const params = z.object({
id: objectIdPreprocess,
});
const helloWorldEndpoint = defaultEndpointsFactory
.addMiddleware({
input: params,
handler: async ({ input }) => {
// logs an ObjectId instance as expected
console.log({ input })
return {}
}
})
.build({
method: 'post',
input: params,
output: z.object({
value: z.string(),
}),
handler: async ({ input: { id }, options, logger }) => {
// never gets to this point
return { value: id.toHexString()};
},
}); Context
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hello @angel-luminos , the problem is that you have used
You should not be doing that, because the final endpoint input schema consists of the input schemas of all the middlewares attached, plus the one you specify in So, it should be enough in your case just to delete the |
Beta Was this translation helpful? Give feedback.
Hello @angel-luminos ,
the problem is that you have used
params
schema twice:.addMiddleware()
.build()
.You should not be doing that, because the final endpoint input schema consists of the input schemas of all the middlewares attached, plus the one you specify in
.build()
. Because you usedparams
schema twice it can not merge those object-schemas due to meeting the sameid
property twice. This iszod
's limitation on intersecting schemas —Unmergable intersection
.So, it should be enough in your case just to delete the
input
within.build()
. Theid
still will be available for you in allhandler
s.