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 🙏

© 2026 – Pkg Stats / Ryan Hefner

custom-basic-validator

v1.0.8

Published

The library can also be installed through [npm][npm]

Readme

custom-basic-validator.js

The library can also be installed through npm

$ npm install --save-dev custom-basic-validator

No ES6

var validator = require("custom-basic-validator");
var ValidatorForm = Validator.ValidatorForm; // => [class ValidatorForm]

ES6

import { ValidatorForm } from "custom-basic-validator";

Or, import only a subset of the library:

import ValidatorForm from "custom-basic-validator/lib/validator-form";

Basic usage

const { ValidatorForm } = require("../index");

function validatorAddress(value, callback) {
  callback(new Error("Please enter address!"));
}

var formData = {
  name: "test name",
  age: "",
  address: "",
};

var formRule = {
  name: [
    { required: true, message: "Please enter name!" },
    { min: 1, max: 5, message: "Name length cannot be between 1 and 5" },
  ],
  age: [{ type: "number", message: "Age must be numeric" }],
  address: [{ validator: validatorAddress }],
};

var innerForm = new ValidatorForm(formData, formRule, false);

innerForm.validate(function (validate, fields) {
  console.info("validate ==>", validate, fields);
  // false
  // [
  //   { field: 'name', message: 'Name length cannot be between 1 and 5' },
  //   { field: 'age', message: 'Age must be numeric' },
  //   { field: 'address', message: 'Please enter address!' }
  // ]
});

Advanced usage

const { ValidatorForm } = require("../index");

const formData = {
  name: "",
  child: {
    name: "",
    grand: {
      name: "",
    },
  },
};

const formRule = {
  name: [{ required: true, message: "Please enter name!" }],
  child: [{ validator: validatorChild }],
};

const formChildRule = {
  name: [{ required: true, message: "Please enter child name!" }],
  grand: [{ validator: validatorGrand }],
};

const formGrandRule = {
  name: [{ required: true, message: "Please enter grandson name!" }],
};

function validatorChild(value, callback) {
  const innerForm = new ValidatorForm(formData.child, formChildRule, false);
  innerForm.validate(function (validate, fields) {
    if (validate) {
      callback();
    } else {
      callback(fields);
    }
  });
}

function validatorGrand(value, callback) {
  const innerForm = new ValidatorForm(
    formData.child.grand,
    formGrandRule,
    false
  );
  innerForm.validate(function (validate, fields) {
    if (validate) {
      callback();
    } else {
      callback(fields);
    }
  });
}

const innerForm = new ValidatorForm(formData, formRule, false);

innerForm.validate(function (validate, fields) {
  console.info("validate ==>", validate, fields);
  // false
  // [
  //   { field: 'name', message: 'Please enter name!' },
  //   { field: 'child.name', message: 'Please enter child name!' },
  //   { field: 'child.grand.name', message: 'Please enter grandson name!' }
  // ]
});

Advanced usage - array of nested objects

const { ValidatorForm } = require("../index");

const formData = {
  name: "",
  child: [
    {
      childName: "",
    },
  ],
};

const formRule = {
  name: [{ required: true, message: "Please enter name!" }],
  child: [{ validator: validatorChild }],
};

const formChildRule = {
  childName: [{ required: true, message: "Please enter child name!" }],
};

function validatorChild(value, callback) {
  let result = [];
  for (const item of value) {
    const innerForm = new ValidatorForm(item, formChildRule, false);
    innerForm.validate(function (validate, fields) {
      if (!validate) {
        result = result.concat(fields);
      }
    });
  }

  if (result.length) {
    callback(result);
  } else {
    callback();
  }
}

const innerForm = new ValidatorForm(formData, formRule, false);

innerForm.validate(function (validate, fields) {
  console.info("validate ==>", validate, fields);
  // false
  // [
  //   { field: 'name', message: 'Please enter name!' },
  //   { field: 'child.grand.name', message: 'Please enter child name!' }
  // ]
});

Validator

| Validator | Description | | ------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | ValidatorForm(data, rule, status) | Check if the form data is correct.data is an object. check the form objectrule is an object. check form object validation rulesstatus comparison all the data and return the result, default false. |

ValidatorForm Methods

| Method | Description | | ---------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | validate(callback) | Check if the form data is correct.callback is an function that defaults return to (validator, fields)Callback: validator: validation results, default true.fields: validation exception field array, default []. |

ValidatorForm Rule Attributes

| Method | Description | | ------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | message | Check form data error message. default "" | | required | Check if the form data is empty. default false | | type | Check the form data type. default stringdata type is one of ['number','string', 'array', 'date']. | | max | Check the form data max length. | | min | Check the form data min length. | | minimum | Check the form data minimum. | | maximum | Check the form data maximum. | | validator(value, callback) | Check the form data custom validator function. value: Check form data.callback: Callback function must be executed.Example: callback() or callback(new Error("Error message")). |