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

correct

v0.1.1

Published

Simple JavaScript validation module

Downloads

29

Readme

correct

Simple JavaScript validation module

Install

npm install correct

Usage

var correct = require('correct')
var fn = correct.string.min(1).max(12)

fn('abc') === true
fn('') === false

At any time, if you want to understand why you have a failing validation, you can use

var fn = correct.string.min(1).max(12)

fn.validate('')
// will return an object with {valid: false, errors: [...]} and the array of validations that did not pass

fn.validate('abc') //will return {valid: true}

Types

For now there are only 3 base validation types available

  • string - correct.string
  • number - correct.number
  • int - correct.int

Example:

var isInt = correct.int

isInt('5') == false
isInt(5.1) == false
isInt(5) == true

You can chain validation functions for all types (detailed below).

var intMax10 = correct.int.max(10)

intMax10(4) == true
intMax10(40) == false

NOTE: types are specified with simple dot access, while all other chained validations should be chained as function calls:

var validate = correct.string.max(10).not('hello word').required()

validate('my string') == true

string

var stringMin5 = correct.string.min(5)

stringMin5('abc') == false
stringMin5('abcde') == true

Available string tests:

  • min(n: Number) - validates the value to have at least n characters in length
  • max(n: Number) - validates the value to have at most n characters in length
  • re(r: RegExp) - validates the value against the given regular expression
  • email() - validates the value to be a valid email address (npm module isemail is used)
  • fn(f: Function) - validates the value if the specified function returns truthy
  • not(v: String) - validates the value if it is not strict equal to v
  • numeric() - validates the value if it is a numeric string: eg - '1.34'
  • required() - validates the value if it is different from empty string

Number

Validates numbers.

var between10And100 = correct.number.min(10).max(100)
between10And100(40) == true
between10And100(4) == false

var even = correct.number.fn(function(v){
    return v % 2 == 0
})

even(2) == true

var x = 67
even(x) == false

var isNumber = correct.number

isNumber(4.5) == true
isNumber('4.5') == false

Available number tests:

  • min(n: Number) - validates the value if it is >= n
  • max(n: Number) - validates the value if it is <= n
  • fn(f: Function) - validates the value if the specified function returns truthy
  • not(v: String) - validates the value if it is not strict equal to v

Int

Inherits all the number tests, and adds some more

  • odd(n: Number) - validates the value if it is odd
  • even(n: Number) - validates the value if it is even

Tests

make

License

MIT