GithubHelp home page GithubHelp logo

rubric's Introduction

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)

between(min, max) Inclusive

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()

rubric's People

Contributors

burkson-shared avatar jinger89 avatar

Watchers

 avatar  avatar  avatar

rubric's Issues

Rubric report

In previous version of rubric rubric.breakdown would be able to take an object containing multiple levels deep and produce a report. The current is limited to two levels. Is there a flag for how deep the validation goes.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.