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

@zecos/validators

v0.0.5

Published

`validators` is...

Downloads

4

Readme

Validators

validators is...

  • a minimalistic data validation checker
  • fairly efficient (for js).

Usage

Pass in a list of requirements to the createStringValidator or createNumberValidator function:

const passwordValidator = createStringValidator({
  mustContain: ["symbols", "uppercase", "lowercase", "digits"],
  validChars: ["symbols", "alphanumeric"],
  min: 8,
  max: 45,
})

createStringValidator requirements can have the following properties:

  • mustContain: Will return error if any of the required character types are not found.
    • "symbols": `!@#$%^&*()_-+=[{]}\|><.,?/"';:~``
    • "uppercase": all uppercase letters
    • "lowercase": all lowercase letters
    • "letters": all letters o
    • "digits": all digits [0-9]
    • "alphanumeric": all digits and letters
    • "spaces": the character
    • * any other character you want represented with a string
      • "f29c" would be valid if it contains the characters f, 2, 9, and c
  • validChars: Will return error if any characters other than the ones specified are found.
    • The list of characters if the same as the ones for mustContain
  • min: the minimum length of the string
  • max: the maximum length of the string
  • regexp: a regular expression to test the value

createNumberValidator requirements can have the following properties:

  • min: minimum value
  • max: maximum value

createOneOfValidator requirements can have the following properties:

  • options: array of valid values

Then, you simply pass a string to validate. It will return an array of errors (empty array if no errors).

const passwordValidator = createStringValidator({
  mustContain: ["symbols", "uppercase", "lowercase", "digits"],
  validChars: ["symbols", "alphanumeric"],
  min: 8,
  max: 45,
})

passwordValidator("Password#1805") // => [], empty array, there were no errors

passwordValidator("password#1805") // => [Error: Must contain "uppercase"] one of the requirements was "uppercase"

createNumberValidator works the same way:

const ageValidator = createNumberValidator({
  min: 18,
  max: 80,
})

// Number validators also except strings which can be converted to numbers
passwordValidator("19") // => [], empty array, there were no errors.

passwordValidator(3) // => [Error: Must be greater than or equal to 3.]

createOneOfValidator verifies the value is in the list given in options:

const fruitValidator = createOneOfValidator({options: ["apples", "oranges", "bananas"]})

fruitValidator("oranges") // => [], empty array, there were no errors.
fruitValidator("peanuts") // => [Error: "Must be apples, oranges, or bananas."]

Preset Validators

validators also comes with some preset validators. Please open an issue/pr if there are more that you need.

export const nameValidator = createStringValidator({
  min: 1,
  max: 40,
  validChars: ["letters", "., "],
})
export const ageValidator = createNumberValidator({
  min: 0,
  max: 120,
})
export const usernameValidator = createStringValidator({
  min: 3,
  max: 40,
  validChars: ["letters", "digits", "_-"]
})
export const phoneValidator = createStringValidator({
  min: 10,
  max: 10,
  validChars: ["digits"],
})
export const passwordValidator = createStringValidator({
  mustContain: ["digits", "lowercase", "uppercase", "symbols"],
  min: 8,
  max: 100,
})
export const emailValidator = createStringValidator({
  regexp: "^(([^<>()[\\]\\\\.,;:\\s@\"]+(\\.[^<>()[\\]\\\\.,;:\\s@\"]+)*)|(\".+\"))@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$",
})
export const einValidator = createStringValidator({
  regexp: "^[1-9]\\d?-\\d{7}$",
})

// sets the maximum dob as the current time, in case the subject were just born.
export const dobValidator = () => {
  const min = new Date(1900, 1, 0)
  return date => {
    if (!(date instanceof Date)) {
      try {
        date = new Date(date)
      } catch (e) {
        return [new Error(`Could not convert ${date} into a date`)]
      }
    }
    if (date < min) {
      return [new Error("Date of birth cannot be before January 1, 1900")]
    }
    if (date > new Date) {
      return [new Error("Date of birth cannot be in the future.")]
    }
    return []
  }
}

Installation

Yarn: yarn add validators

Npm: npm i validators

Dependencies

None