Closed LeoDupont closed 1 year ago
Answering my own question.
I've been able to provide an external Joi
object to joiful
using it's constructor:
import { Joi } from "express-validation";
import { Joiful } from "joiful";
const jf = new Joiful({
joi: Joi as any, // got a typing error otherwise...
});
class BodyExample {
@jf.string().required()
exampleKey!: string;
}
const router = Router();
router.post("/test",
validate({
body: jf.getSchema(BodyExample),
}),
(req, res) => {
res.send("ok");
}
);
This way, I have no longer an error regarding joi
versions mismatch.
Notes:
Joi
object must have at least the same major version as the embedded joiful
's joi
.Joi
as any
in the constructor's options:
Type 'import("c:/.../node_modules/joi/lib/index").Root' is not assignable to type 'import("c:/.../node_modules/joiful/node_modules/joi/lib/index").Root'.
The types of 'any().alter' are incompatible between these types.
Type '(targets: Record<string, (schema: AnySchema<any>) => Schema<any>>) => AnySchema<any>' is not assignable to type '(targets: Record<string, SchemaFunction>) => AnySchema'.
Types of parameters 'targets' and 'targets' are incompatible.
Type 'Record<string, SchemaFunction>' is not assignable to type 'Record<string, (schema: AnySchema<any>) => Schema<any>>'.
'string' index signatures are incompatible.
Type 'SchemaFunction' is not assignable to type '(schema: AnySchema<any>) => Schema<any>'.ts(2322)
I'm glad you worked out a solution. :)
Yeah, it's a bit of a dilemma for joiful. If we make Joi a peer dependency, we run into type errors when using a newer version of Joi, and can't guarantee that it works without error. If we make Joi a dependency of joiful, we run into the problems you found. 😞
Hi!
I'm trying to use
joiful
in combination withexpress-validation
, but when I try to provide ajoiful
-made schema to thevalidate()
middleware, it throws this error:Sample code I'm using:
I'm using
express-validation@4.1.0
, which usesjoi@17.6.0
. Andjoiful@3.0.2
, which usesjoi@17.3.0
.Is there any way to upgrade
joiful
'sjoi
dependency?Or to provide an external
joi
object tojf
? Something like: