/joi-full

joi object schema validation with extensions as universal/isomorphic libarary for Node.js and bundled for the browser (babelified and bundled)

Primary LanguageJavaScriptOtherNOASSERTION

joi-full

joi object schema validation with extensions as universal/isomorphic libarary for Node.js and bundled for the browser (babelified and bundled). Includes joi-date-extensions.

Build Status NPM Version Badge

Why?

There has been some difficulty in getting a reasonable version of Joi packaged for the browser both due to the size of bundling and with Joi 7.x.x switching to ES6 modules has added additional challenges.

I made a browser build of joi available at joi-browser, but Joi@10 split date functionality into its own package joi-date-extensions, so I also created joi-date-extensions-browser but currently are having trouble when combining joi-browser and joi-date-extensions together.

In the meantime, this build was created which includes joi and joi-date-extensions and works on Node.js and provides a bundle for brower builds.

Usage

Moment is a peer dependency so it needs to be installed as well.

npm install joi-full moment
var Joi = require('joi-full'); // includes joi-date-extensions

Note: Joi uses the new ES6 features, Promise and Map, so you may need to add polyfills to use with older browsers.

Include the polyfill early in your code before requiring Joi. You can use the parts of core-js for the polyfill or use packages es6-promise + es6-map.

npm install core-js or es6-promise and es6-map

npm install core-js
require('core-js/fn/map'); // sets up global Map
require('core-js/fn/promise'); // sets up global Promise

Note: if you are using webpack with a babel loader you may need to exclude joi-full from being run through babel again.

In your webpack.config.js loaders, add an exclude: [ /joi-full/ ]

Isomorphic / Universal JS - using in browser and on server (Node.js)

The joi-full package.json includes the browser field which directs browser builds to use to the prebuilt dist/joi-full.js

Webpack

An example of using joi-full with webpack is in examples/webpack-basic

npm install joi-full moment

You might need to add the following to your app's webpack.config.js to enable the package aliasing we configured in package.json

  resolve: {
    packageAlias: 'browser'
  }

Note: if you are using webpack with a babel loader you may need to exclude joi-full from being run through babel again.

In your webpack.config.js loaders, add an exclude: [ /joi-full/ ].

So in your code, you just require joi-full

var Joi = require('joi-full'); // includes joi-date-extensions

Browserify

You can also use browserify to use joi-full. It should pick up the proper bundled file when it reads joi-full/package.json browser field.

npm install joi-full moment

So in your code, you just require joi-full

var Joi = require('joi-full'); // includes joi-date-extensions

There is an example in examples/browserify-basic.

Examples

There are three examples:

  • examples/browserify-basic - uses browserify to bundle joi-full with our src for browser
  • examples/node-basic - use joi-full with node.js
  • examples/webpack-basic - use webpack to bundle joi-full with our src for the browser

To use any of them:

cd examples/___
npm install
npm start

Development

# builds dist/joi-full.js and dist/joi-full.min.js
npm install
npm run prepublish # when you want to rebuild

Discussion

The main discussion about these difficulties has been in this github issue.

hapijs/joi#528 (comment)

See joi-browser for more details on the bundling issues.

Since this package is simply a bundling of the original joi and joi-date-extensions, please discuss defects or suggestions for those packages at: