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

value-validator

v2.2.2

Published

Low-level rule manager for validating values.

Downloads

26

Readme

Build Status

value-validator

Low-level rule manager for validating values.

Install

$ npm install value-validator --save

Usage

const Validator = require('value-validator')
const validator = new Validator([
  /.{4,}/,
  /^[a-z0-9_]+$/i,
  function (v) {
    return checkExistsPromise(v)
  }
])

validator
.valiate(value)
.then((result) => {
  result // whether passes the validation
})
.catch((err) => {
  err    // if any error throws or returns
})

// Examples
validator.validate('foo').then(pass => {
  pass // false, to short
})

validator.validate('foo.bar').then(pass => {
  pass // false, only letters, numbers and underscores.
})

validator.validate('steve').catch(err => {
  err  // maybe `new Error('username "steve" already exists.')`
})

new Validator(rule, options)

  • rule RegExp|function()|String|Array.<mixed> rule could be
    • regular expression,
    • function either returns a Promise or normal value
    • string (validator preset),
    • or an array of mixed-type of the former three.

.validate(value [, callback])

  • value any value to be validated
  • callback function(err, pass)= using callback is deprecated since 2.2.0, and the parameter will be removed in the next major version.

returns a Promise if no callback, or undefined

.context(context)

  • context Object specify this object for all validator functions.

Returns this

Sync Function-type rule

The function should accept only one argument, which is the value to be validated.

If the function returns a Boolean, it indicates whether the validation is passed, and the err will be null

const validator = new Validator(v => v > 10)
validator.validate(5).then(pass => {
  pass // false
})

If the function returns an Error, it means the validation fails, and the error will passed to the callback function of validate(v, callback) as the first parameter err.

const validator = new Validator(v => {
  if (v > 10) {
    return true
  }

  return new Error('should larger than 10')
})
validator.validate(5).catch(err => {
  err // new Error('should larger than 10')
})

Async validator

To define an asynchronous validator, just returns a Promise.

Validator.defaults({preset=, codec=})

Pre-defines certain option of Validator and returns a constructor.

Validator presets are an abbreviation of a certain validation, or a set of validations.

const presets = {

  // To define a function-typed preset
  unique: function (v) {
    return new Promise((resolve, reject) => {
      asyncCheckExists(v, exists => {
        if (exists) {
          return reject(new Error(`username "${v}" already exists.`))
        }

        resolve(true)
      })
    })
  },

  min4: /.{4,}/,

  // A preset could be a set of presets.
  username: [
    'min4',
    /^[a-z0-9_]+$/i,
    'unique'
  ]
}

const MyValidator = Validator.defaults({presets})

// Then we could use `username` as the test rule.
const validator = new MyValidator('username')

License

MIT