joiful-ts / joiful

TypeScript Declarative Validation for Joi
239 stars 18 forks source link

"Cannot mix different versions of joi schemas" / Upgrading `joi` / Providing external `Joi` instance #236

Closed LeoDupont closed 1 year ago

LeoDupont commented 1 year ago

Hi!

I'm trying to use joiful in combination with express-validation, but when I try to provide a joiful-made schema to the validate() middleware, it throws this error:

Error: Cannot mix different versions of joi schemas

Sample code I'm using:

import { Router } from "express";
import { validate, Joi } from "express-validation";
import * as jf from "joiful";

class BodyExample {
    @jf.string().required()
    exampleKey!: string;
}

const router = Router();

router.post("/test",
    validate({
        body: jf.getSchema(BodyExample),
    }),

    (req, res) => {
        res.send("ok");
    }
);

I'm using express-validation@4.1.0, which uses joi@17.6.0. And joiful@3.0.2, which uses joi@17.3.0.

Is there any way to upgrade joiful's joi dependency?

Or to provide an external joi object to jf? Something like:

import { validate, Joi } from "express-validation";
import * as jf from "joiful";

jf.joi = Joi;
LeoDupont commented 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:

laurence-myers commented 1 year ago

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. 😞