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

@beenhere4hours/filter-validate

v0.3.21

Published

An object validation utility

Downloads

5

Readme

Node.js CI npm bundle size (scoped) npm (scoped)

filter-validate

An object validation utility.

Install

Example for install from NPM

Below, the @std/esm package lets us use ES modules in Node.js v6+.

npm i @beenhere4hours/filter-validate
npm i @std/esm

In the package.json we're loading the esm module for the local run and enabling cjs in the esm field.

package.json

{
  "name": "example",
  "version": "0.0.0",
  "description": "example of how to use filter-validate with esm",
  "main": "index.js",
  "scripts": {
    "dev": "node -r esm index.js",
  },
  "dependencies": {
    "@beenhere4hours/filter-validate": "^0.3.21",
    "esm": "^3.2.25"
  },
  "esm": {
    "cjs": true
  }
}

index.js

import {FilterValidate} from "@beenhere4hours/filter-validate";
const filterValidate = new FilterValidate();

Example for install in browser

<script type="module">
    import {FilterValidate} from "https://cdn.jsdelivr.net/npm/@beenhere4hours/[email protected]/src/filter-validate.min.js";
    const filterValidate = new FilterValidate();
</script>

Usage

// Example 1 - a single validator
const example1Rules = {test: 'maxLen, 7'};
const example1Object = {test: '12345'};
const example1Result = filterValidate.validate(example1Object, example1Rules);
console.log(`result will be {} as the string length is valid`);
console.log(example1Result);

// Example 2 - a single filter
const example2Rules = {test: 'sanitizeNumbers'};
const example2Object = {test: 'abc123'};
const example2Result = filterValidate.filter(example2Object, example2Rules);
console.log(`result will be {test: "123"} as the returned input is transformed`);
console.log(example2Result);

// Example 3 - multiple validators
const example3Rules = {test: 'alpha|minLen, 3|maxLen, 6'};
const example3Object = {test: 'abcABC'};
const example3Result = filterValidate.validate(example3Object, example3Rules);
console.log(`result will be {} as the string length is between 3 and 6 and characters are alpha. a-z A-Z`);
console.log(example3Result);

// Example 4 - multiple filters
const example4Rules = {test: 'ltrim|rtrim|upper'};
const example4Object = {test: '   abcDEFghi   '};
const example4Result = filterValidate.filter(example4Object, example4Rules);
console.log(`result will be {test: "ABCDEFGHI"}`);
console.log(example4Result);

// Example 5 - passing an object to the constructor
const example5Filters = {test: 'ltrim|rtrim|upper'};
const example5Validators = {test: 'alpha|minLen, 3'};
const example5Config = {filters: example5Filters, validators: example5Validators};
const example5Object = {test: '   abc   '};
const example5Result = new FilterValidate(example5Object, example5Config);
console.log(`result will be {filters:{test: "ABC"}, validators: {} }`);
console.log(example5Result);

// Example 6 - passing a custom validator
const testMaxLen = function(property, value, args) {
 let [len] = args;

 if (typeof len === 'string') {
     len = parseInt(len, 10);
 }

 return (value.length <= len);
};

const example6Rules = {test: 'maxLen, 7'};
const example6Object = {test: '12345'};
filterValidate.addValidator('testMaxLen', testMaxLen);
const example6Result = filterValidate.validate(example6Object, example6Rules);
console.log(`result will be {} as the string length is valid`);
console.log(example6Result);

// Example 7 - passing a custom filter
const testToUpperCase = function(property, value) {
    return value.toUpperCase();
};

filterValidate.addFilter('testToUpperCase', testToUpperCase);
const example7Rules = {test: 'testToUpperCase'};
const example7Object = { test: 'abc' };
const example7Result = filterValidate.filter(example7Object, example7Rules);
console.log(`result will be {test: "ABC"}`);
console.log(example7Result);

Tests

npm test

Available Validators

required

Specified key/value pair exists and that the value is not an empty string '', null, or undefined

validEmail

Value is valid formatted email address.

This should cover most of the RFC 822 and RFC 5322 specifications.

Below are examples of accepted formats:

maxLen, n

Value length is not greater than length given as n

minLen, n

Value length is not less than length given as n

exactLen, n

Value length is equal to length given as n

alpha

Only alpha characters (a-z, A-Z) are present in the value

alphaNumeric

Only alpha-numeric characters, (a-z, A-Z, 0-9), are present in the value

alphaDash

Only alpha-numeric characters, dashes, and underscores are present in the value (a-z, A-Z, 0-9, _-)

alphaSpace

Only alpha-numeric characters and spaces, (a-z, A-Z, 0-9, \s), are present in the value

numeric

Value is numeric

examples of valid numbers:

|number|description| |------|-----------| |0b101010|binary| |42.0|float| |0x2A|hex / base 16| |42|integer| |0o2471|octal| |"42"|string without thousands or decimal separator|

integer

Value is an integer

float

Value is a float

inList, needle, haystack

Value is contained within the semicolon separated list

notInList, needle, haystack

Value is not contained within the semicolon separated list

minNumeric, n

Value is greater than or equal to minimum given as n

maxNumeric, n

Value is less than or equal to maximum given as n

date

Value is a date compliant with ISO 8601

examples of valid date formats:

  • 1997
  • 1997-07
  • 1997-07-16
  • 1997-07-16T19:20+01:00
  • 1997-07-16T19:20+01:00Z
  • 1997-07-16T19:20-01:00
  • 1997-07-16T19:20-01:00Z
  • 1997-07-16T19:20:30+01:00
  • 1997-07-16T19:20:30+01:00Z
  • 1997-07-16T19:20:30.45+01:00
  • 1997-07-16T19:20:30.45+01:00Z
  • 1997-07-16T19:20:30.45-01:00
  • 1997-07-16T19:20:30.45-01:00Z
  • 1997-13-39T19:58:30.45-01:00Z
  • -1997-13-39T19:58:30.45-01:00Z
starts, needle, n

Value starts with a character/set of characters starting at a given position

phone

Value is a phone number that matches one of the following patterns

  • 1234567890
  • "1234567890"
  • "(078)789-8908"
  • "123-345-3456"
regex, pattern

Value passes provided regex validation

Available Filters

sanitizeNumbers

Remove any non-numeric characters

sanitizeEmail

Remove illegal characters from email addresses

trim

Remove spaces from both sides of a string

ltrim

Remove spaces from left side of a string

rtrim

Remove spaces from the right side of a string

lower

Change all characters to lowercase

upper

Change all characters to uppercase

Kudos

Andre Torgal for his esm nyc mocha boilerplate