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

policeman

v0.2.5

Published

Lightweight yet powerful schema validator

Downloads

72

Readme

policeman

CircleCI

Lightweight yet powerful schema validator


API Docs | Examples


  • Validate objects based on provided schema
  • Inspired by mappet

Installation (npm)

npm i -S policeman

Examples

import policeman, { isRequired, isEmail, isMatching, combineValidators } from "policeman";

// setup entry validators
const requiredValidator = isRequired(() => "is required");
const emailValidator = isEmail(() => "is invalid email");
const phoneNumberValidator = isMatching(/\d{3}-?\d{3}-?\d{3}/, () => "is invalid phone");

// setup entry filter predicates
const isGift = (value, source) => source.gift === true;

// define schema
const schema = [
  // 1. array of validators - multiple errors
  // 2. combine validators - first of many errors
  // 3. single validator - single error
  // 4. skip validation based on filter predicate

  ["email", "email", [requiredValidator, emailValidator]], // #1
  ["phone", "phone", combineValidators(requiredValidator, phoneNumberValidator)], // #2
  ["name", "name", requiredValidator], // #3
  ["giftCode", "giftCode", requiredValidator, isGift], // #4
  // [dest, source, Validator, Filter]
];

// create validator
const validator = policeman(schema);

// validate
validator({ gift: false, email: "invalid@example", phone: "777-666-55" });

// {
//   valid: false,
//   errors: {
//     email: ["is invalid email"],
//     phone: "is invalid phone",
//     name: "is required"
//   }
// }

See tests for more examples.

Built-in validators

All built-in validators are curried.

isRequired(() => message, value)

Validates presence. Fails on null, empty string or undefined.

isMinLength(min, () => message, value)

Passed value must be a string longer or with length equal to min.

isMaxLength(max, () => message, value)

Passed value must be a string shorther or with length equal to max.

isEqualLength(equal, () => message, value)

Passed value must be a string shorther or with length equal to max.

isEmail(() => message, value)

Passed value must be a valid email. It's a simple check, if you need more complex solution use isMatching or isPassing.

isMatching(regexp, () => message, value)

Passed value must pass regexp.

isPassing(predicate, () => message, value)

Passed predicate answers on "Is value valid?". When predicate returns true validator passes, when predicate returns false error message is returned.

It makes policeman compatible with all available validators i.e. validator.

import validator from "validator";
import { isPassing } from "policeman";

const creditCardValidator = isPassing(validator.isCreditCard, () => "is invalid credit card");
const uuid4Validator = isPassing(value => validator.isUUID(value, 4), () => "is invalid UUID v4");
const ftpValidator = isPassing(value => validator.isURL(value, { protocols: ["ftp"] }, () => "is invalid FTP address");

See tests for more examples.