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

parameter-validator

v1.0.5

Published

Parameter validator makes it easy to verify that an object contains required, valid parameters.

Downloads

818

Readme

Parameter Validator

Parameter-validator makes it easy to verify that a JavaScript object contains required, valid parameters.

Examples

Basic Examples

import { validate, ParameterValidationError } from 'parameter-validator';

let params = { name: 'Paula PureCloud', id: 'user1' };

let { name, id } = validate(params, [ 'name', 'id' ]);
// parameters exist, so no error is thrown

try {
    let { age } = validate(params, [ 'age' ]);
} catch (error) {
    if (error instanceof ParameterValidationError) {
        console.log(error.message);
        // "Invalid value of 'undefined' was provided for parameter 'age'."
    }
}

Async Example

To ensure that the any errors thrown are wrapped in a Promise, use the async version:

import { validateAsync, ParameterValidationError } from 'parameter-validator';

validateAsync(params, [ 'price', 'quantity' ])
.then(({ price, quantity }) => {

    console.log(`Price: ${price}, Quantity: ${quantity}`) ;
})
.catch(error => {

    if (error instanceof ParameterValidationError) {
        // Handle invalid parameters
    } else {
        throw error;
    }
});

Advanced usage

Other types of validation

  • You can specify that at least one of a group of parameters must be included by placing those properties together within a nested array (either username or email must be specified in the example below)

  • You can provide a specific validation function for a parameter by providing it in an object

validate(params, [
    'firstName',
    'lastName',
    [ 'username', 'email' ],
    { age: val => val > 30 }
]);

Optional parameters

Passing in an object to which the extracted parameters will be assigned.
// Sets `this.logger` and `this.username`
validate(params, [ 'logger', 'username' ], this);
Adding a prefix to param names
// Sets `this._logger` and `this._username`
validate(params, [ 'logger', 'username' ], this, { addPrefix: '_' });

ParameterValidator class

For convenience, validate() and validateAsync() are exported as standalone functions as shown above, but it's also possible to import and instantiate the ParameterValidator class that implements those methods.

import ParameterValidator from 'parameter-validator';

let parameterValidator = new ParameterValidator();
let { firstName, lastName } = parameterValidator.validate(options, [ 'firstName', 'lastName' ]);
// or
parameterValidator.validateAsync(options, [ 'firstName', 'lastName' ])
.then(({ firstName, lastName }) => {
   ...
});

Parameters for validate and validateAsync

param:   {Object}       paramsProvided       - The names and values of provided parameters
param:   {Array}        paramRequirements    - Each item in this array is interpretted in order as a validation rule.
                                             - If an item is a string, it's interpretted as the name of a parameter that must be contained in paramsProvided.
                                             - If an item is an Array, it's interpretted as an array of parameter names where at least one of the
                                               parameters in the Array must be in paramsProvided.
                                             - If an item is an Object, it's assumed that the object's only key is the name of a parameter to be validated
                                               and its corresponding value is a function that returns true if that parameter's value in paramsProvided is
                                               valid.
param:   {Object|null}  [extractedParams]    - This method returns an object containing the names and values of the validated parameters extracted.
                                               By default, it creates a new object and assigns the extracted parameters to it, but if you want this
                                               method to add the extracted params to an existing object (such as the class instance that internally
                                               invokes this method), you can optionally supply that object as the extractedParams parameter.

param:   {Object}       [options]            - Object of additional options.
param:   {string}       [options.addPrefix]  - Specifies a prefix that will be added to each param name before it's assigned to the
                                               extractedParams object. This is useful, for example, for prefixing property names with an underscore
                                               to indicate that they're private properties.
param:   {class}        [options.errorClass] - Specifies a specific `Error` subclass to throw instead of the default `ParameterValidationError
                                               when invalid parameters are detected.

returns: {Object}       extractedParams      - The names and values of the validated parameters extracted.

throws:  {ParameterValidationError}          - Indicates that one or more parameter validation rules failed. The error message identifies the names and
                                               values of each invalid parameter.

Installation

npm install parameter-validator --save

This module ships with two different builds: one for the CommonJS module spec and one for the AMD module spec. The CommonJS module is specified as the default entry point, so you can import the module as expected in Node.js without any additional work.

Installation in Ember.js

Ember.js uses the AMD module system, and a couple of extra steps are required to correctly import the AMD module.

  1. Install parameter-validator using npm as specified above.
  2. Install the ember-cli-node-modules-to-vendor addon, which copies specific node modules to your vendor directory so that they can be imported.
  3. Add the following lines to ember-cli-build.js:
let app = new EmberApp(defaults, {
    // Instructs the ember-cli-node-modules-to-vendor addon to copy this module to `/vendor`
    nodeModulesToVendor: [
        'node_modules/parameter-validator/dist/amd'
    ]
});

// Instructs the app to import the AMD module so that you can import it in your code.
app.import('vendor/ParameterValidator.js', {
    using: [
        { transformation: 'amd', as: 'parameter-validator' }
    ]
});

Development

The module is implemented in ES 6 (located in the src directory) but has been transpiled to ES 5 using Babel (located in the dist directory). The package.json file specifies the dist directory for the module's entry point, so the transpiled code will be used automatically.

Building

npm run build

There's also a git pre-commit hook that automatically builds upon commit, since the dist directory is committed.

Running tests

npm test