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

epic-validator

v3.2.0

Published

A simple but powerful Data Validator

Downloads

183

Readme

Epic Validator

A simple but powerful Data Validator Library. This library will allow you to validate data with less code and more power. You can use this library with any framework.

Usage

Epic Validator is very easy to use library. View the documentation below to start working with Epic Validator quickly!

Validate a value

Follow the below method to validate a value

import { Validation } from "epic-validator";

// Create a Validator Instance.
// You can also pass an object containing your custom validators
const Validator = new Validation({
  // Your custom IP Address validator
  isIP: (_) =>
    _.matches(
      /^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/,
      "Please provide a valid IP Address!"
    ),
  // Your custom IBAN validator
  isIBAN: (_) =>
    _.matches(
      /^([A-Z]{2}[ -]?[0-9]{2})(?=(?:[ -]?[A-Z0-9]){9,30}$)((?:[ -]?[A-Z0-9]{3,5}){2,7})([ -]?[A-Z0-9]{1,3})?$/,
      "Invalid IBAN Number has been provided!"
    ),
});

(async () => {
  // Example 1:
  const Username = "john";

  // Validate Value
  await Validator.validate(Username)
    .isAlphanumeric(
      {
        allowSpaces: false, // Set to True if spaces allowed.
        strict: true, // Set to False if symbols allowed.
      },
      // Provide a Validation message
      "Please provide a valid Username!"
    )
    /**
     * Use exec() method to run the validator and throw the errors if any.
     * You can also use run() method but if there are any errors, you will need to throw errors manually.
     * You can use Validator.throw() method to throw any errors manually.
     */
    .exec();

  // Example 2:
  const Email = "[email protected]";

  // Validate Email
  await Validator.validate(Email)
    .isEmail("Please provide a valid Email!")
    .run();

  // Throw Errors if any
  Validator.throw();

  // Example 3:
  const Contact = 092384758399;

  // Validate Contact using custom() method
  await Validator.validate(Contact)
    .custom((value, chain) => {
      if (typeof value !== "number")
        throw "Please provide a valid Contact Number!";
    })
    .exec();

  // Example 4:
  const IP = "192.168.2.1";

  // Validate IP Address using your custom validator
  await Validator.validate(IP).use("isIP").exec();
})();

Validate an Array of Values

You can easily validate an Array of Values using the method below.

(async () => {
  const Emails = ["[email protected]", "abc", "@mail.com"];

  // Validate All Emails in the Array
  await Validator.validate(Emails)
    .each((_) => _.isEmail("Please provide a valid Email!"))
    .exec();
})();

Validate a Schema

It is recommended to validate your data using the schema() method. Follow the method below to validate data schema.

(async () => {
  // Dummy Data To Validate
  const Data = {
    username: "john",
    password: "john123",
    confirmPassword: "john123",
    contact: 03056762589,
    agreement: true,
  };

  // Validate Schema
  await Validator.validate(Data).schema({
      username: _ => _.isAlphanumeric({}, "Please provide a valid Username!"),

      password: _ => _.isString("Please provide a valid Password!").not().isEmpty({}, "Password cannot be empty!"),

      confirmPassword: _ => _.isString("Please provide a valid Confirmation Password!").matches(Data.password, "The Confirmation Password doesn't matches your Password!"),

      contact: _ => _.isNumeric({
          sanitize: true, // Set to True for converting "123" to 123 or "100.2" to 100.2
      }, "Please provide a valid Contact Number!"),

      agreement: _ => _.likeBoolean({
          sanitize: true,
          isTrue: true
      }, "You need to agree our terms!")
  }).exec();
})();

Available Validators

The followings are the available Validators.

  1. isString
  2. isNumber
  3. isBoolean
  4. isObject
  5. isArray
  6. isFunction
  7. isBigInt
  8. isUndefined
  9. isSymbol
  10. isJson
  11. likeObject
  12. likeArray
  13. isEmpty
  14. notEmpty
  15. isIn
  16. matches
  17. isAlpha
  18. isAlphanumeric
  19. isNumeric
  20. isEmail
  21. isIpv4
  22. isIpv6
  23. isLength
  24. isAmount
  25. isURL
  26. likeBoolean

Available Utility Methods

The followings are the available Utility Methods.

  1. custom
  2. optional
  3. required
  4. not
  5. skip
  6. schema
  7. each
  8. use
  9. error

We have tried our best to make the documentation as simple as possible. Good Luck!