npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rubric

v0.5.7

Published

Simple variable type checking library.

Downloads

15

Readme

Rubric

Simple variable type checking library.

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

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

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

minLength(min)

maxLength(max)

hasLength(len)

startsWith(str)

endsWith(str)

contains(str)

regexp(regexp)

max(max) inclusive

min(min) inclusive

greaterThan(min)

lessThan(max)

even()

odd()

positive()

negative()

minLength(min)

maxLength(max)

hasLength(len)

contains(args, ...)

containsAny(args, ...)

forEach(rule) single rule, not ruleset

instanceOf(obj)

hasProperty(prop, ...)

hasAnyProperty(prop, ...)

ruleset(ruleset) full ruleset, use for nested objects

true() literal, value === true

false() literal, value === false

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