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

validate-params-schema

v1.2.0

Published

Parameter validation for builtins and custom types. Accepts `params` and a `schema` and returns an array of `Error`s or `false`.

Downloads

8

Readme

validate-params-schema

Parameter validation for builtins and custom types. Accepts params and a schema and returns an array of Errors or false.

var validate = require('validate-params-schema')

function hi(params, callback) {
  var schema = {
    'name':       {required:true,  type:Object},
    'name.first': {required:true,  type:String},
    'name.last':  {required:false, type:String}
  }
  var errors = validate(params, schema) 
  if (errors) {
    callback(errors)
  }
  else {
    callback(null, 'hi ' + params.first)
  }
}

// logs: null, hi brian
hi({name:{first:'brian', last:'leroux'}}, console.log)

// logs: [ [ReferenceError: missing required param name.first] ] 
hi({name:{}}, console.log)

considerations

  • For testing an Object that presumably came from a JSON payload
  • Thusly, primarily concerned with JSON value types: Object, String, Number, Array and Boolean
  • Custom types are easily supported (see ./examples)
  • Designed to test keys and nested keys
  • Optionally validate required
  • Optionally validate min and max for String, Number and Array builtin types (and easily implement for custom types)

things it does not do

  • mutate things with: serialization, formatting or defaults
  • nested subtypes (eg. the things in an array)
  • localized error messages

further justifications

There are a tonne of libraries that do things like this but also do a whole lot more. This library deliberately limits its scope: make errback style param contract validation super clean and simple. Work primarily with builtins but easily extend. Provide a nice API for usage (hence returning false instead of a truthy empty array for the return value of validate).

example usage

var validate = require('validate-params-schema')

functon sum(params, callback) {
  // define our assumed params
  var errors = validate(params, {
    x: {required:true, type:Number},
    y: {required:true, type:Number}
  })
  // err first!
  if (errors) {
    callback(errors)
  }
  else {
    callback(null, params.x + params.y)
  }
}

api

validate(params, schema)

  • params a plain old javascript object that we assume came from JSON
  • schema a plain javascript object for describing the shape of the data

schema keys

  • required either true or false (or leave it out completely)
  • type can be one of Object, String, Number, Array and Boolean
  • min any Number (or anything allowed by a custom type)
  • max any Number (or anything allowed by a custom type)

Check out ./examples for more on custom types and ranges (and the tests).