hapijs / joi

The most powerful data validation library for JS
Other
20.95k stars 1.51k forks source link

joi not usable in a browser environment #3019

Closed sagevoice closed 9 months ago

sagevoice commented 9 months ago

Runtime

browser

Runtime version

Chrome 121.0.6167.184 (Official Build) (x86_64)

Module version

17.12.1

Last module version without issue

joi-browser 13.4.0

Used with

react

Any other relevant information

See issue 2299 from Feb 12, 2020. Seems to be a longstanding problem not addressed in the past four years. Opening a new bug to get some fresh eyes on it. If there is some way to make this work I would love to know it. I tried all of the suggestions mentioned in 2299, and none of them worked for me.

With the require solution:

const Joi = require('joi');

The error is TypeError: Joi.validate is not a function

With the import the module without giving a name solution:

/*global Joi*/
import 'joi/dist/joi-browser.min.js'

The error is Uncaught ReferenceError: Joi is not defined. Same result if I try using lowercase joi as suggested in 2299.

As far as the discussion in 2299 about this being a breaking change, it seems to me that this has never worked in joi 17 - that it is just broken. However this was implemented in joi-browser 13.4.0 from Jun 14th 2018 still works today. Why can't Joi 17 at least be no worse that what was done back in 2018? I really want to get onto the current version, but not having it in the browser is a blocker for me.

What are you trying to achieve or the steps to reproduce?

I am trying to upgrade to use the use the latest joi in a web browser environment. See https://github.com/sagevoice/joi-bug for a simple create-react-app web app that tries to define a trivial schema and validate some data with it. To reproduce:

git clone https://github.com/sagevoice/joi-bug.git
cd joi-bug
npm i
npm run start

Then check the developer console in the browser for errors.

What was the result you got?

TypeError: joi_dist_joi_browser_min_jsWEBPACK_IMPORTED_MODULE2default(...).validate is not a function

What result did you expect?

To have no errors and to see a console log with the message 'result null'.

Marsup commented 9 months ago

Hi, thanks for the reproduction, that was helpful. Your demo works, you're just using a method that has been removed in v16 (4 years ago), Joi.validate hasn't been around for quite a while, I'm not sure which tutorial you followed but it's not very up-to-date 🙂

The only diff I had to apply was this: image