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

enforce

v0.2.0

Published

NodeJS data validations

Downloads

2,840

Readme

Data Validations Build Status

This is the package responsible for data validations in ORM.

Enforce

You can create a list of validations for several properties of an Object and then run the checks to see if everything is OK.

var enforce = require("enforce");
var checks  = new enforce.Enforce();

checks
	.add("name", enforce.notEmptyString())
	.add("age", enforce.ranges.number(18, undefined, "under-age"));

checks.check({
	name : "John Doe",
	age  : 16
}, function (err) {
	// err should have an error with "msg" = "under-age"
});

You can pass some options in the constructor. One of them is returnAllErrors which makes the validations be all checked before returning errors. With this option, if any error is found, even if it's only one, it will be returned in an Array.

var enforce = require("enforce");
var checks  = new enforce.Enforce({
	returnAllErrors : true
});

checks
	.add("name", enforce.notEmptyString())
	.add("name", enforce.ranges.length(2)) // yes, you can have multiple validators per property
	.add("age", enforce.ranges.number(18, undefined, "under-age"));

checks.check({
	name : "J",
	age  : 16
}, function (err) {
	console.log(err);
	// [ { [Error: "out-of-range-length"], property: "name", value: "J" },
	//   { [Error: "under-age"], property: "age", value: 16 }]
});

Validators

All validators accept a msg argument at the end. These argument is the error message returned if the validation fails. All validators return a Validator object that is used by Enforce to support chaining and other functionality. Validator objects have a validate method which is called by Enforce with the value of the property in question, a next callback and an optional global context table which may be used to pass information between validators.

Required

enforce.required([ msg ])

Checks if a property is not null and not undefined. If can be false, 0 or "".

Empty string

enforce.notEmptyString([ msg ])

Checks if a property length is not zero. It can actually work with Arrays.

Same as

enforce.sameAs(property, [ msg ])

Checks if a property has the same (strict) value as another one. This is usefull for testing password matching.

Lists

Inside a list

enforce.lists.inside(list[, msg ])

Checks if the property is inside a list of items.

Outside a list

enforce.lists.outside(list[, msg ])

Checks if the property is not inside a list of items.

Ranges

In a number range

enforce.ranges.number(min[, max[, msg ]])

Checks if a value is inside a specific range of numbers. Either min or max can be set to undefined meaning that range side is Infinity.

Please note that this does not check for the type of value passed, you can even use this with Date objects.

In a length range

enforce.ranges.length(min[, max[, msg ]])

Does the same as the above but for the length property.

Security

Username

enforce.security.username([[ opts, ]msg ])

Checks if a value matches a username format. opts is also optional and is an object with the following keys:

  • length: the minimal length of the username (default = 2)
  • expr: the regular expression to be used to match a valid username (if not passed, it's built based on the minimal length)

Password

enforce.security.password([[ checks, ]msg ])

Checks if a value has some types of characters and a minimal length. checks has a default string luns6 which means:

  • l: lowercase letters
  • u: uppercase letters
  • n: numbers
  • s: special characters
  • 6: minimal length of 6

You can of course change this to "lu4" (lowercase, uppercase, minimal length of 4). Please note that if you pass only one argument to this validator, it will assume it's the msg argument. If you want to change the default checks, you have to pass both arguments.

Credit Card

enforce.security.creditcard([[ types, ] msg ])

Checks if a value is a valid credit card number. It supports amex (American Express), visa, maestro, discover and mastercard. You can change the list of supported cards (types) by passing a list with only some of them. You can also pass luhn which will ignore card prefixes and lenght and only pass the number using the Luhn algorithm.

Patterns

Match

enforce.patterns.match(pattern, modifiers[, msg ])

Checks if property passes a specific regular expression. You can pass the pattern as a RegExp object (setting modifiers as null) or just pass a regular expression and it will be converted.

Hex string

enforce.patterns.hexString([ msg ])

Checks if a property matches a predefined RegExp object accepting insensitive hexadecimal characters.

E-mail

enforce.patterns.email([ msg ])

Checks if a property matches a predefined RegExp object accepting valid e-mail addresses.

IPv4

enforce.patterns.ipv4([ msg ])

Checks if a property matches a predefined RegExp object accepting valid IPv4 address.

IPv6

enforce.patterns.ipv6([ msg ])

Checks if a property matches a predefined RegExp object accepting valid IPv6 address.

MAC

enforce.patterns.mac([ msg ])

Checks if a property matches a predefined RegExp object accepting valid MAC address.

UUID v3

enforce.patterns.uuid3([ msg ])

Checks if a property matches a predefined RegExp object accepting valid UUID version 3.

UUID v4

enforce.patterns.uuid4([ msg ])

Checks if a property matches a predefined RegExp object accepting valid UUID version 4.

Chaining

Enforce supports chaining operations on all Validator objects, these allow you to add additional common conditions to each validation step. All chain operations return Validator objects, allowing you to chain multiple commands together with ease.

ifDefined

validation.ifDefined()

Only proceedes to check the validation if the property's value is not null or undefined, passing validation if it is.

ifNotEmptyString

validation.ifNotEmptyString()

Only proceedes to check the validation if the property's value is a string with a length greater than 0.

ifType

validation.ifType(type)

Only proceedes to check the validation if the property's value is of the specified type. Checked with a typeof value == type operation.

ifNotType

validation.ifNotType(type)

Only proceedes to check the validation if the property's value is not of the specified type. Checked with a typeof value != type operation.