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

@vamship/arg-utils

v2.4.19

Published

Utility library for argument validations

Downloads

64

Readme

Arg Utils

Utility library for argument validations. Provides modules for function argument validations and JSON schema validations. Function argument checks are important in loosely typed languages such as javascript, and a good development practice is to explicitly check each function argument prior to using it, like this:

function sayHello(name, lang) {
    if(typeof name !== 'string' || name.length <= 0) {
        throw new Error('Invalid name argument (arg #1)');
    }
    if(typeof lang !== 'string' || lang.length <= 0) {
        lang = 'english';
    }
}

In other situations (ex: AWS Lambda functions), the function may accept a simple object as an input, but expect that the object conforms to a specific schema.

This library makes it syntactically nicer to perform such checks, by providing utility methods for both argument checks and schema validation.

API Documentation

API documentation can be found here.

Installation

This library can be installed via npm using:

npm install @vamship/arg-utils

Usage

This libary exports two modules:

  • schemaHelper: Generates a schema validator function based on a schema object. This function can then be used to validate inputs against this schema.
  • argValidator: Provides basic argument validation functionality for function input validation.

Release Notes

V2.0.0

This version introduces breaking changes in order to find a better balance between "syntactic niceness" and performance. V1.x allowed error checks and default settings to be applied by allowing chained function calls, for example:

    _argValidator.checkString(someArg).throw('some error');

While one could argue that this made the code more readable, quick performance tests showed that these checks were multiple orders of magnitude slower than simple error checks using if-then-else statements. The fact that they are slower was expected, but how much slower they really were was a surprise.

Version 2.0.0 changes the validator signatures to accept the error message as a part of the check function, and throws immediately, like this:

    _argValidator.checkString(someArg, 1, 'some error'); // Throws an error.

    _argValidator.checkString(someArg, 1); // Does not throw, returns false.

Examples

Argument Validation

const _argValidator = require('@vamship/arg-validator').argValidator;

...

function sayHello(name, lang) {
    _argValidator.checkString(name, 1, 'Invalid name (arg #1)');
    if(!_argValidator.checkString(lang, 1)){
        lang = 'english';
    }
}

Schema Validation

const _schemaHelper = require('@vamship/arg-validator').schemaHelper;
const schema = require('<path to json schema>');
const checkSchema = _schemaHelper.createSchemaChecker(schema);

...

function processInput(input) {
    checkSchema(input, true) // Throws a schema error
}

...

function processInput(input) {
    if(!checkSchema(input)) { // Does not throw an error, but returns false

        // Perform some actions on schema validation failure
    }
}