rubric

0.5.7 • Public • Published

Rubric

Simple variable type checking library.

Quick Start

Install using npm:

npm install rubric

Or, download the source file rubric.js.

Setting up your first ruleset and test.

var ruleset = {
    firstName: rubric.string().minLength(1),
    lastName: rubric.string().minLength(1),
    address: rubric.object().ruleset({
        street: rubric.string(),
        street2: rubric.string().optional(),
        city: rubric.string(),
        state: rubric.string().hasLength(2),
        zip: rubric.string().minLength(5)
    }),
    age: rubric.number.min(0).max(100),
    favoriteMovies: rubric.array().forEach(rubric.object().instanceOf(MovieObject))
};
 
if (rubric.test(ruleset, someData))
    console.log('Passed test!');
else
    console.log('Failed test...');

Rules and Rulesets

Rules are individual tests, while rulesets are a collection of rules in the form of a plain object. Rules can be written using the many tests included in this library, or you can write you own in the form of functions, regular expressions, or any other values which will be compared literally (===).

// Rule
rubric.array().contains('foo', 'bar');
 
// Ruleset
{
    firstName: rubric.string(),
    lastName: rubric.string(),
    nickName: rubric.string().optional()
}

Functions as Tests

There are two ways to use functions as tests. The first way is to use the function by itself.

{
    someProperty: function (val) {
        // write your custom test here
        // return true or false
    }
}

The second way is the chain the function into a Rubric test so you can use it with other tests.

{
    someProperty: rubric.string().minLength(10).fn( function (str) {
        // write your custom test here
        // return true or false
    }).optional() // keep chaining on rules like normal
}

Regular Expressions as Tests

You can do the same thing with regular expressions as you can with functions, use them directly or chain them with other Rubric tests.

{
    someProperty: /[a-z]/g
}

Or...

{
    someProperty: rubric.string().minLength(10).regexp(/[a-z]/g).optional()
}

Methods

rubric

test(ruleset, data) Tests a ruleset against data, returns true or false

[rules...].test(value) Tests a single rule against a given value

report(ruleset, data) Tests a ruleset against data, returns an object explaining which property passed/failed

rubric.[ANY]()

These methods can be with any of the following types.

optional() sets value as optional, if value is given it will be tested

fn(fn) return boolean true if valid, all other return values will fail

regexp(regexp) given regular expression is used like regexp.test(val)

is(str, ...) tests if value is any of the given arguments

rubric.string()

minLength(min)

maxLength(max)

hasLength(len)

startsWith(str)

endsWith(str)

contains(str)

regexp(regexp)

rubric.number()

max(max) inclusive

min(min) inclusive

greaterThan(min)

lessThan(max)

even()

odd()

positive()

negative()

rubric.array()

minLength(min)

maxLength(max)

hasLength(len)

contains(args, ...)

containsAny(args, ...)

forEach(rule) single rule, not ruleset

rubric.object()

instanceOf(obj)

hasProperty(prop, ...)

hasAnyProperty(prop, ...)

ruleset(ruleset) full ruleset, use for nested objects

rubric.boolean()

true() literal, value === true

false() literal, value === false

rubric.date()

before(date)

after(date)

year(year) use full year, i.e. use 2017 not 17

quarter(qtr) 1 to 4

month(month) 0 to 12

date(date) 1 to 31

weekDay(day) 0 to 7, 0 is Sunday, 6 is Saturday

hour(hr) 0 to 23

minute(min) 0 to 59

second(sec) 0 to 59

rubric.function()

rubric.truthy()

rubric.falsy()

rubric.null()

rubric.undefined()

Readme

Keywords

Package Sidebar

Install

npm i rubric

Weekly Downloads

16

Version

0.5.7

License

MIT

Unpacked Size

17.4 kB

Total Files

4

Last publish

Collaborators

  • jinger89