Skip to content

A composable data validation, transformation & manipulation pipeline

Notifications You must be signed in to change notification settings

lapwinglabs/rube

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

58 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rube

Leaner, simpler and browser-compatible alternative to Joi.

A composable data validation, transformation & manipulation pipeline. Supports synchronous, asynchronous and generator functions. Proudly named after the Rube Goldberg Machine.

rube goldberg machine

Installation

Node:

npm install rube

Browser (duo):

var Rube = require('lapwinglabs/rube');

Browser (standalone, exports Rube):

dist/rube.js

Example

var schema = Rube({
  email: Rube(String).assert(/@/),
  name: Rube(String).between(5, 30),
  accounts: Rube({
    twitter: Rube(String).use(rtwitter),
    gittip: Rube(String).between(5, 20)
  })
});

schema({
  name: 'matt mueller',
  email: '[email protected]',
  accounts: {
    twitter: 'mattmueller',
    gittip: 'matthewmueller'
  }
}, function(err, obj) { });

What about Joi?

Joi was a major source of inspiration for Rube. Here are some differences between Rube and Joi:

Pros:

  • Rube is much leaner (20kb minified)
  • Rube works great in the browser
  • Rube supports synchronous, asynchronous and generator validators
  • Rube has a simpler syntax

Cons:

  • Joi has way more features
  • Joi has reference support (seeking PRs for Rube)
  • Joi supports arrays (seeking PRs for Rube)
  • Joi has more tests

API (old, needs to be updated)

Rube()

Initialize Rube. Returns a function that can be used to check a given value.

var rube = Rube().required().type(isEmail).format('matt', 'brian');
rube('[email protected]', function(err, v) { ... }) // v is '[email protected]'

Rube#use(fn)

Add custom validations, transforms, and formatters. If you add a second argument, the function will be asynchronous.

rube.use(function(value, fn) {
  fn(null, value * value);
});

You can also compose rube instances:

var a = rube();
var b = rube();
var c = rube().use(a).use(b);

Rube.plugin(name, fn)

Attach a custom plugin to all rube instances

Rube.plugin('phone', function phone(country) {
  return function(value) {
    switch(country) {
      case 'US': return /^\d{10}$/.test(value) ? value : new Error('no good');
      default: return value;
    }
  };
});

var rube = Rube().phone('US');
rube(1234567890, function(err, v) { ... }) // v is 1234567890

Rube.message(message)

Provide a custom message when there is an error. A message can be either a string, function, or error.

When message is a function, the error is passed through:

Rube().message(function(err) {
  return new Error('this was the error: ' + err.message);
});

Bundled Plugins

As you probably noticed, Rube comes bundled with a few plugins off the bat to make things easier.

Rube#cast([from], to)

Cast a value from one type to another type. Uses typecast for its type casting.

Rube().cast(String, Number)

If the first type isn't satisfied, the cast is skipped. Optionally, you can specify one type to always try to cast to that type

Rube.cast(String) // cast any type to string

Rube#default(default)

Specify a default value if the value passed through is undefined.

var rube = Rube.default(10)
rube(undefined, function(err, v) { ... }) // v is 10

Rube#format(replace, [with])

Format an argument. You can pass a function into replace to modified the value.

var rube = Rube.format(function (v) {
  return v * 2;
});
rube(10, function(err, v) {}) // v is 20

You can also pass a regular expression into replace and pass a function into with.

var rube = Rube().format(/(\d{3})(\d{3})(\d{4})/, function(m) {
  return '(' + m[1] + ') ' + m[2] + '-' + m[3];
});

rube(1234567890, function(err, v) { ... }) // v is "(123) 456-7890"

Or to simply things you can pass a string to with:

var rube = Rube().format(/(\d{3})(\d{3})(\d{4})/, '($1) $2-$3');

rube(1234567890, function(err, v) { ... }) // v is "(123) 456-7890"

Rube#required()

Ensure that the value is defined.

var rube = Rube().required();
rube(undefined, function(err, v) { ... }) // err is "value must be defined"

Rube#type(type)

Ensure that the value is a certain type. Uses invalid for it's typechecking.

var rube = Rube().type(String)
rube(10, function(err, v)) // err is "10 is not a string"

Rube#assert(assertion)

Assert that the value passed through passes the assertion. The assertion changes based on it's type:

  • default: assert.equal(value, assertion)
  • regexp: assert(assertion.test(value))
  • array or object: assert.deepEqual(value, assertion)

Rube#between(min, max)

Checks to see if the value is in between min and max. Between is an inclusive range

Rube()
  .between(3, 10)

You can use this to specify min's and max's separately:

Rube()
  .between(6, Infinity) // min(6)
  .between(-Infinity, 100) // max(100)

Tests

npm install
make test

License

(The MIT License)

Copyright (c) 2014 Matthew Mueller <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

A composable data validation, transformation & manipulation pipeline

Resources

Stars

Watchers

Forks

Packages

No packages published