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

global_validator

v1.4.0

Published

The GlobalValidator package provides a set of validation methods for various purposes such as validating text, passwords, integers, and phone numbers. It also allows configuring custom validation rules.

Downloads

93

Readme

Global Validator

The GlobalValidator package provides a set of validation methods for various purposes such as validating text, passwords, integers, and phone numbers. It also allows configuring custom validation rules.

✨Features

  • Validate text based on configurable validation rules.
  • Validate passwords based on complexity requirements.
  • Validate integers for numerical constraints.
  • Validate phone numbers based on country-specific formats and rules.

𖣠 Installation

Install the GlobalValidator package using npm:

😎 Usage

Import the GlobalValidator class and instantiate it in your code. Here's an example of how to use the package:

import validator from 'global_validator';


// ================== Text Validation ==================
// Validate text
const textValidationConfig = {
  value: 'sample text',
  validations: {
    // minLength -> 3 and maxLength -> 10
    length: '3:10',
    // if value contain whitespace, it will fail validation.
    allowedCharacters: ['w', 'd', 's'],
    // value must not include password
    notInclude: 'password',
  },
};

const textErrors = validator.validate(textValidationConfig);
if (Array.isArray(textErrors)) {
  console.log('Validation errors:', textErrors);
} else {
  console.log('Validation successful');
}
// ===================== End of test validation ===================


// =================== Phone Number Validation ====================

// Validate phone number
const phoneNumber = '+1234567890';
const phoneValidationResult = validator.phoneNumber(phoneNumber).isUnitedStates();

if (phoneValidationResult === true) {
  console.log('Phone number is valid');
} else {
  console.log('Phone number validation failed');
}

// ================ End of phone number validation =============




// ================== Password Validation =================


// Define the validation criteria for the password
const password_validations: PasswordValidationType = {
  lowercase: 1, // password must contain lowercase
  uppercase: 3, // Requires at least 3 uppercase letter
  digit: 1, // Requires at least 1 digit
  letter: true, // password must contain letter (uppercase | lowercase)
  length: "8:", // Requires a minimum length of 8 characters
};

// Example password to validate
const passwordToValidate = "Password123";

// Create an instance of the class or object that contains the 'password' function
const passwordValidator = validator.password;

// Call the 'password' function with the password and validation criteria
const result = passwordValidator(
  passwordToValidate,
  password_validations
);

// Check the result
console.log({ result });

// log
{
  "result": {
    "isValid": false,
    "strength": 66.66666666666666,
    "errors": [
      {
        "validationErrorMessage": "Uppercase letters in the password is too short, include more uppercase letters",
        "errorCode": "upper_case_letters_too_short"
      }
    ]
  }
}

The validate method accepts a configuration object with a value to be validated and a validations object defining the validation rules. The method returns true if the value passes all validations, or an array of "GlobalValidatorError" objects if there are validation errors.

The phoneNumber method allows you to validate phone numbers. Simply provide a phone number as a parameter and chain the validate method to perform the validation.

Here is a description of the class methods in tabular form:

| Method | Description | | ----------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | checkCharacters | Checks if the value contains characters specified in the allowedCharacters array and adds an error if invalid characters are present. | | has_length | Checks if the value has a length within the specified range and adds an error if the length is too short or too long. | | not_include | Checks if the value contains a specific substring specified by not_include and adds an error if the substring is present. | | has_uppercase | Checks if the value contains uppercase letters and adds an error if no uppercase letters are found. | | has_lowercase | Checks if the value contains lowercase letters and adds an error if no lowercase letters are found. | | has_letter | Checks if the value contains any letters and adds an error if no letters are found. | | has_digit | Checks if the value contains any digits and adds an error if no digits are found. | | has_symbol | Checks if the value contains any symbols and adds an error if no symbols are found. | | validate | Performs multiple validations based on the provided configuration and returns true if all validations pass, false if any validation fails, or an array of GlobalValidatorError objects if errors occur during validation. | | password | Validates a password based on the provided configuration and returns an object with information about the password's validity and strength. | | phoneNumber | Sets the phone number to be validated and then call target country i.e validator.phoneNumber("+234 811 176 4056").isNigeria()</b> . |

Please refer to the package documentation for detailed usage instructions and available validation methods.

🦮 Dependencies

The GlobalValidator package has no external dependencies.

🎗️Typescript Support

The GlobalValidator package includes TypeScript type declarations for enhanced development experience. The type declarations provide autocompletion and type checking support when using the package in TypeScript projects.

🪪 License

This package is released under the MIT License.

❃ Contributing

Contributions are welcome! If you encounter any issues or have suggestions for improvements, please create an issue on the package's GitHub repository.