• Stars
    star
    273
  • Rank 150,780 (Top 3 %)
  • Language
    JavaScript
  • License
    Other
  • Created about 9 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

joi validation bundled for the browser (deprecated) - use joi@16+

joi-browser (deprecated)

Deprecation Notice: @hapi/[email protected] now has direct browser support.

joi object schema validation bundled for the browser (babelified and bundled)

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 now with 7.x.x the switch to ES6 modules has added additional challenges.

The community is trying to rally to get the Joi package itself setup so that we can generate bundles directly from it, but until this is done, this package will allow you to build a bundled version.

This package can be used included in other builds (browserify or webpack) or used on its own.

The default version exposed by package.json is the babelified unminified bundle. It has been transpiled to ES5. Since I expected to primarily be using this inside of another bundle. However the minified version is also built on install as dist/bundle.min.js.

Usage

npm install joi-browser
var Joi = require('joi-browser');

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-browser from being run through babel again.

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

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

If you want to use joi with Node.js and joi-browser for browser use then you can follow one of these recipies.

Browserify

npm install joi-browser
npm install joi

Add the following to your app's package.json which will tell browserify to use joi-browser instead of joi when bundling for the browser.

  "browser": {
    "joi": "joi-browser"
  },

So in your code, you just require joi and browserify will automatically switch it with joi-browser when it bundles.

var Joi = require('joi');

See examples/browserify-basic

Webpack

npm install joi-browser
npm install joi

Add the following to your app's webpack.config.js to alias joi to joi-browser

  resolve: {
    alias: {
      joi: 'joi-browser'
  }

Note: if you are using webpack with a babel loader you may need to exclude joi-browser (or node_modules) from being run through babel again.

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

So in your code, you just require joi and webpack will automatically switch it with joi-browser when it bundles.

var Joi = require('joi');

See examples/webpack-basic

joi-full

joi@10 moved some date validation logic into an extension joi-date-extensions so I built the equivalent joi-date-extensions-browser but there are currently issues with the build when trying to integrate joi-browser and joi-date-extensions-browser.

As an alternative, I created joi-full which is a universal/isomorphic version of joi that includes extensions (namely joi-date-extensions). It can be used for Node.js or in the browser by bundling with webpack or browserify. Since joi-full already bundles both joi and joi-date-extensions it works around the build issue previously encountered.

Development

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

Examples

I have included a few examples of demonstrating how to use with webpack and browserify

  • examples/browserify-basic
  • examples/webpack-basic

Upgrade notes

See the github releases for notes. A few notable upgrades are:

  • 13.0.1 - Updated to [email protected] and updated build tools ([email protected], [email protected]). Buffer is now included. Using ChromeHeadless for tests.

  • 10.0.5 - Joi split momentjs date format functionality into joi-date-extensions. The equivalent version for the browser is joi-date-extensions-browser, but there are building issues. You can instead use joi-full which includes the extension and is a universal/isomorphic package that will work in either Node.js or bundled with webpack/browserify. If you install joi-full you will need to also install its peer dependency moment. See joi-full for all the details.

  • 7.1.0 - excludes moment from the joi-browser bundle, so it must be imported into your project from elsewhere. Bundle was renamed to dist/joi-browser.js and dist/joi-browser.min.js

Discussion

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

hapijs/joi#528 (comment)

As for the bundling size issues that were summarized in the issue, the package size can be reduced by eliminating unnecessary code.

crypto is the first thing that can be safely eliminated since this functionaility in Joi would not be used by the browser. That is the biggest win dropping things to ~45KB gzipped without sacrificing any actual functionality.

If your use case doesn't require moment, isemail, and buffer, you can stub those packages out and get a bundle in the neighborhood of ~23KB gzipped.

I have chosen to make the default bundle only exclude crypto so that it would remain fully compatible, but you could fork this and create a smaller version by excluding things you don't need.

Config Joi and dependencies gzipped
Full Joi 126KB
w/o crypto (in Hoek) 44KB
w/o crypto (in Hoek), moment 31KB
w/o crypto (in Hoek), moment, isemail 29KB
w/o crypto (in Hoek), moment, isemail, buffer 23KB

More Repositories

1

wait-on

wait-on is a cross-platform command line utility and Node.js API which will wait for files, ports, sockets, and http(s) resources to become available
JavaScript
1,860
star
2

redux-logic

Redux middleware for organizing all your business logic. Intercept actions and perform async processing.
JavaScript
1,807
star
3

bench-rest

bench-rest - benchmark REST (HTTP/HTTPS) API's. node.js client module for easy load testing / benchmarking REST API's using a simple structure/DSL can create REST flows with setup and teardown and returns (measured) metrics.
JavaScript
303
star
4

pkglink

Space saving Node.js package hard linker. pkglink locates common JavaScript/Node.js packages from your node_modules directories and hard links the package files so they share disk space.
JavaScript
214
star
5

microservices

Microservices example code using Node.js, Redis, Hapi
JavaScript
150
star
6

autoflow

autoflow (formerly react) is a javascript module to make it easier to work with asynchronous code, by reducing boilerplate code and improving error and exception handling while allowing variable and task dependencies when defining flow.
JavaScript
97
star
7

redux-logic-test

redux-logic test utilities to facilitate the testing of logic. Create mock store
JavaScript
37
star
8

redis-rstream

redis-rstream is a node.js redis read stream which streams binary or utf8 data in chunks from a redis key using an existing redis client (streams2). Tested with mranney/node_redis client.
JavaScript
33
star
9

digest-stream

Simple node.js pass-through stream (RW) which calculates the a crypto digest (sha/md5 hash) of a stream and also the length. Pipe your stream through this to get digest and length. (streams2)
JavaScript
30
star
10

markdown-it-modify-token

markdown-it plugin for modifying tokens in a markdown document. It can for example modify content or attributes for certain type of elements like links or images.
JavaScript
25
star
11

redis-wstream

redis-wstream is a node.js redis write stream which streams binary or utf8 data into a redis key using an existing redis client (streams2). Tested with mranney/node_redis client.
JavaScript
20
star
12

pass-stream

pass-stream - pass-through node.js stream which can filter/adapt and pause data
JavaScript
17
star
13

base-react-min

Minimal React.js boilerplate with an auto build environment
JavaScript
12
star
14

ensure-array

Ensure that a javascript object is an array, coerce if necessary. Move error checking out of your node.js javascript code.
JavaScript
12
star
15

autoflow-graphviz

autflow-graphviz is a plugin for autflow, the flow control rules engine, which allows react to use graphviz to generate flow diagrams for the dependencies
JavaScript
7
star
16

accum

Simple stream sink which accumulates or collects the data from a stream. Pipe your stream into this to get all the data as buffer, string, or raw array. (streams2)
JavaScript
6
star
17

hapi-bunyan-lite

Minimalistic Bunyan logging integration for Hapi - forwards Hapi log events to bunyan
JavaScript
6
star
18

ngzip

ngzip is a portable streaming stdio gzip command line utility that uses Node.js zlib. It should run anywhere Node.js runs including windows, *nix, mac.
JavaScript
5
star
19

tapper

Tapper (tapr) is a node.js tap runner which improves formatting and allows stdout and stderr mixed in with the tap output. Also optionally adds color to the output
JavaScript
4
star
20

joi-full

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

autoflow-deferred

autoflow-deferred is a plugin for react, the flow control rules engine, which adds integration with jQuery-style Deferred promises
JavaScript
4
star
22

jsconf-react

JSConf 2015 React.js training workshop
JavaScript
3
star
23

length-stream

Simple pass-through node.js stream (RW) which accumulates the length of the stream. (streams2)
JavaScript
3
star
24

git-clone-init

Create new project from clone - clone repo, rm .git, git init, sed files, git commit
Shell
3
star
25

autoflow-q

autoflow-q is a plugin for autoflow, the flow control rules engine, which adds integration with Q-style Deferred promises https://github.com/kriskowal/q
JavaScript
3
star
26

redux-util

redux utilities for easily constructing action creators and reducers
JavaScript
2
star
27

redux-logic-examples

Shell
2
star
28

function.bind.js

Polyfill old webkit and iOS browsers with Function.prototype.bind
JavaScript
2
star
29

modulize

Safe, easy method override/extension without manual alias_method chaining
Ruby
2
star
30

base-react-slim

base boilerplate react environment with auto-build env w/o testing framework
JavaScript
2
star
31

chai-stack

Light wrapper around chaijs which automatically sets chai.Assertion.includeStack = true
JavaScript
2
star
32

jsconf-react-win

JSConf 2015 React.js training workshop (Windows version)
JavaScript
2
star
33

joi-date-extensions-browser

joi-date-extensions bundled for the browser
JavaScript
1
star
34

base-react-min-win

Minimal React.js boilerplate with an auto build environment for Windows
JavaScript
1
star
35

feature-u-bookmark-notes

Simple React.js, redux, redux-logic app using feature-u to showcase feature-based development
JavaScript
1
star
36

waverider

waverider CMS - lightweight fast CMS/blog with realtime edit and preview written in javascript for node.js
JavaScript
1
star