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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@rule.js/constraint

v0.17.1

Published

Serializable constraints with complex conditions (provided by [@rule.js/core][1] module).

Readme

Rule.js: Constraints Module

Serializable constraints with complex conditions (provided by @rule.js/core module).

Example:

const constraint = require('@rule.js/constraint')()

const errors = constraint
	// `when()` starts a condition builder which ends the moment you call a
	// constraint.
	.when()
		.or()
			.and().gt('clearance', 4).false('admin').false('supervisor').end()
			.false('admin').end()
		.end()
	.mandatory(['region'])
	// We want the name to always require a minium of 5 characters, hence we
	// don't specify a condition here.
	.minLength(5, ['name'])
	.errors({ admin: false })

In this example, the region field is mandatory for supervisors with a clearance lower than 5 and any non-administrators. The name must also be 5 characters long at a minimum.

API

Built-in Constraints

mandatory(fields)

Fields must not be null, undefined, or and empty string.

minLength(length, fields)

Fields must contain a length of at least the specified length parameter. Handles arrays and strings.

maxLength(length, fields)

Same as minLength, but for the maximum length.

constant(value, fields)

Fields must be equal the specified value. This is only really useful when combined with a condition. For example:

const bug = {
	status: 'closed',
	resolution: 'nofix'
}

const errors = constraint
	.when()
		.equal('status', 'closed')
	.constant('fixed', ['resolution'])
	.errors(bug)

In the above example, a bug cannot be closed if it is a "nofix".

pattern(regex, fields)

A regex constraint.

Additional methods

fromJSON(data)

Takes a parsed json object and returns a constraints instance.

toJSON()

Returns a json serializable object.

concat(constraints)

Takes two sets of constraints and combines them.

errors(object)

Returns an array of errors

assert(object)

Throws an exception at the first constaint failure.

Customization

You can extend this module to add methods to the condition builder or the constraints builder. This is done by specifying some options when initializing the module.

options.constraintTypes

This option allows you to specify additional constraint types or even override existing ones. constraintTypes is an object where the key is the name of the new constraint and the value is a function.

The function should accept two arguments, one is the object you want to check if the constraint fails on and the second are the arguments passed into the builder when calling your constraint type.

A minimal example would be the following:

const get = require('lodash.get')

const constraint = require('@rule.js/constraint')({
	constraintTypes: {
		nan: function(context, args) {
			const errors = []
			// The custom constraint will only accept one argument which is a array
			// of fields, hence the `args[0]`.
			for(const key of args[0]) {
				const value = get(context, key)
				if(!isNaN(value)) {
					errors.push({
						type: 'nan',
						value: value,
						key: key
					})
				}
			}
			return errors
		}
	}
})

// Example usage...
constraint
	.nan(['name'])
	.assert({ name: 'foobar' })

In this example, we define a constraint which checks if the value of the property is not a number (NaN).

options.members

Allows you to add methods to the Rule condition builder.

options.assertions

Allows you to add new condition types to the Rule condition builder.