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 🙏

© 2025 – Pkg Stats / Ryan Hefner

js-validation-tool

v1.0.4

Published

JS validator model support

Readme

js-validation-tool

Uses validator.js under the hood.

Installation and usage

Install the library with npm i js-validation-tool

Import

const jsValidator = require('js-validator-tool');
// or
const validate = require('js-validator-tool/core/validate');
const validations = require('js-validator-tool/core/validations);
// or
const { validate, validateModel, validations } = require('js-validator-tool);

Validation models

Models are functions that returns an object, each property has a collection of validations.

const ModelValidations = require('js-validator-tool/core/modelValidations');
const userModel = () => ({
  name: [ModelValidations.string.isNotEmpty()],
  age: [ModelValidations.number.isBetween({ from: 18, to: 30 })],
  isNew: [ModelValidations.boolean.isBool()]
});

Single validations

Every validation takes the following parameters

| Parameter | Description | Optional | | ------------- | ---------------------------------------------- | -------- | | key | Property name being validated | No | | value | Value to validate | No | | configuration | Validation additional parameters | Yes | | errorMessage | Custom error message when validation is failed | Yes |

returns a Promise<{ fields: [String], isValid: Boolean }>

Available validations

Validations are separated in types (string, boolean, number and common)

String validations

| Validation | Description | Params | | ---------- | ---------------------------------------------------------------------- | -------------------------- | | isNotEmpty | Returns true if the given string value is not empty | | | isString | Returns true if the given value is a string | | | isMongoId | Returns true if the given value is a valid mongo id | | | isDate | Returns true if the given value is a date | | | isEmail | Returns true if the given value is a valid email address | | | isLength | Returns true if the given string matches the provided min - max length | {min: number, max: number} |

Common validations

| Validation | Description | | ---------- | ------------------------------------------------------ | | isOptional | Set the property as optional to be validated | | isOneOf | Returns true when any value matches the provided array |

Number validations

| Validation | Description | | ---------- | ----------------------------------------------------------------------------- | | isNumeric | Returns true when the given value is a valid number | | isNotZero | Returns true when the provided value is different to 0 | | isBetween | Returns true when the provided value matches with the provided range (config) |

Bool validations

| Validation | Description | | ---------- | ------------------------------------------------------------ | | isBool | Returns true if the given value is boolean ('true' is valid) |

Example

const validationResult = await validate([
  validations.string.isString('name', 'Jhon'),
  validations.number.isNotZero('age', 0),
]);
console.log(validationResult); // prints: { isValid: true, fields: ["Field 'age', expected not to be Zero. Got 0"] }

Add your own validations

const customValidator = (prop, value) => {
  const onFailureMessage = `Field '${prop}', is not valid`;
  return {
    validation: 'myCustomValidation', // validation name
    property: prop,
    onFailureMessage,
    execute: () => value !== undefined,
  };
};

const customValResult = await validate([customValidator('name', undefined)]);
console.log(customValResult); // prints { isValid: false, fields: [ "Field 'name', is not valid" ] }

Full example

const { validate, validateModel, validations, modelValidations } = require('js-validator-tool');

(async () => {
  // model validator example
  const productModel = () => ({
    name: [modelValidations.string.isNotEmpty('name', entity.name)],
    description: [
      modelValidations.common.isOptional('description', entity.description),
      modelValidations.string.isNotEmpty('description', entity.description),
    ],
    isNew: [modelValidations.boolean.isBool('isNew', entity.isNew)],
  });

  const product = {
    name: 'Super box',
    // description is marked as optional, so can be omitted
    isNew: false,
  };

  const modelValidationResult = await validateModel(productModel, product);
  console.log(modelValidationResult); // prints {isValid: true, fields: []}

  // single validations example
  const validationResult = await validate([
    validations.string.isString('foo', 'fooValue'),
    validations.number.isNotZero('bar', 0),
  ]);
  console.log(validationResult); // prints { isValid: true, fields: ["Field 'bar', expected not to be Zero. Got 0"] }

  // custom validation example
  const customValidator = (prop, value) => {
    const onFailureMessage = `Field '${prop}', is not valid`;
    return {
      validation: 'myCustomValidation', // validation name
      property: prop,
      onFailureMessage,
      execute: () => value !== undefined,
    };
  };

  const customValResult = await validate([customValidator('name', undefined)]);
  console.log(customValResult); // prints { isValid: false, fields: [ "Field 'name', is not valid" ] }
})();