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

micro-validate

v1.0.2

Published

Easily validate your microservices url parameters

Downloads

8

Readme

micro-validate

NPM version Build Status code style: prettier XO code style Join the community on Spectrum Greenkeeper badge

micro-validate is an utility that is being used to validate parameters, it uses the createError function of micro to throw (or return) an 409 HTTP error to the request made to the microservice.

Usage

cd my-micro-project/
npm install --save micro-validate

and add use the package like this:

const { parse } = require('url')
const validate = require('micro-validate')

module.exports = (req, res) => {
  const { user, country } = parse(req.url, true).query
  validate({ user, country })
  return 'all ok!'
}

then you can just try the microservice validation by doing some request, here are some examples:

http://localhost:3000?user=fmiras&country=argentina => HTTP 200 'all ok!'

http://localhost:3000?user=someuser => HTTP 409 'The country parameter is missing'

The package just validates if the parameter is defined or not, but you can also setup your custom validator and you can also use a custom message (if not, it will keep using the same):

const { parse } = require('url')
const validate = require('micro-validate')

const validator = p => Number.isInteger(p)

module.exports = (req, res) => {
  const { zip_code } = parse(req.url, true).query
  validate({ zip_code }, validator, 'The parameter {param} is wrong')
  return 'all ok!'
}

http://localhost:3000?zip_code=1416 => HTTP 200 'all ok!'

http://localhost:3000 => HTTP 409 'The zip_code parameter is missing'

http://localhost:3000?zip_code=hello => HTTP 409 'The parameter zip_code is wrong'

You can, of course, chain validations to set different criterion of each param:

const { parse } = require('url')
const validate = require('micro-validate')

const numberValidator = p => Number.isInteger(p)
const passportValidator = p => p.length === 8

module.exports = (req, res) => {
  const { zip_code, passport_id, message, name, from, to } = parse(req.url, true).query
  validate({ zip_code }, numberValidator, 'The parameter {param} must be a number')
  validate({ passport_id }, passportValidator, 'The parameter {param} must be 8 characters-length')
  validate({ message, name, from, to })
  // Use all the parameters!
  return 'all ok!'
}

Why?

By making some validations you can get several if/switch-repeats, or you may need to program a polymorphic function to not get error at the time you use the parameters. With this package you save the time of doing that letting you just focus on the business logic of the microservice.

Contributing

  1. Fork this repository to your own GitHub account and then clone it to your local device
  2. Link the package to the global module directory: npm link
  3. Within the module you want to test your local development instance of micro-cacheable, just link it to the dependencies: npm link micro-validate. Instead of the default one from npm, node will now use your clone of micro-validate!

Credits

Thanks to ZEIT Team for giving us micro to make our life easier!