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

@admc.com/bycontract-plus

v2.1.1

Published

Extensions to byconvention library

Downloads

63

Readme

Description

node.js module for validating (primarily) function parameters.

DESIGN:

This module provides is the same as Dmitry Sheiko's bycontract module, with the feature extensions described below. I tried contracting Dmitry to get his direction on the best way to extend his module, but he hasn't replied to me. Not knowing if I will have to repeat this extension work, I went with strategy that I can do most quickly and easily (but not necessarily best long-term)-- I just made this new module that loads the bycontract module and adds to it.

Installation

npm install @admc.com/bycontract-plus

Usage

Developers will usually want to import/require the 'validate' and 'is' objects. The first for performing validations, and the second to add custom validator types.

ES6 module environments:

import { validate, is } from "@admc.com/bycontract-plus";

ES5 CommonJS environments:

const { validate, is } = require("@admc.com/bycontract-plus");

Validate function calls are the same as for bycontract other than...

Allow more than two arguments elements:

validate(arguments, ["string", "int"], false);

In case of validation failure, use the specified Error message.

validate(var, "string", "%ith element in issue list not a string", i);

In particular, the run "[]" no longer means "any" Array, it means precisely [], a 0-length array. To get the original bycontract behavior to test for any Array, use:

validate(var, "", false);
validate(arguments, [""], false);

Enhancements over bycontract (without "-plus") module

  1. The original 'validate' function requires 2 and only 2 parameters. I support some optional extra params.
  2. For validate calls where contract parameter (2nd) is an Array, I enforce that the number of value elements doesn't exceed that of the specified contract.
  3. Test element count enforcement (previous item) is not done if 3rd parameter to validate call given as fase.
  4. If the first non-boolean validate parameter after #2 is a string, then it is a message to be used in validation Errors, overriding the default.
  5. The message for previous feature can use util.format % placeholders, with format varargs parameters being all remaining parameters.

I added the following validator types. I've added description for the ones where the type-name isn't intuitive.

  • int
  • date
  • posint
  • nonnegint Non-negative integer
  • posnum
  • nonnegnum Non-negative number
  • isotimestr Allows optional Z, +nn:nn, -nn:nn time zone suffix
  • isotimestr_s No time zone suffix
  • plainobject
  • strictdatestr Format yyyy-mm-dd, where the segments must be 4 char + 2 char + 2 char