1.1.1 • Published 12 months ago

@nyi_nyi_phone_hlaing/validator-x v1.1.1

Weekly downloads
-
License
MIT
Repository
-
Last release
12 months ago

README.md:

validator-x

A set of essential and user-specific validation utilities for Express applications, built using express-validator. This package provides reusable validation functions for common use cases like URL, phone number, email, username, and more.


What You Need to Update

https://github.com/nyi-nyi-phone-hlaing/validator-x.git


Installation

To install the package, run the following command:

npm install @nyi_nyi_phone_hlaing/validator-x

Usage

You can use the utility functions in your Express application by importing them from the package.

const express = require("express");
const {
  checkURL,
  checkPhoneNumber,
  checkUsername,
  checkEmail,
} = require("@nyi_nyi_phone_hlaing/validator-x");
const { validationResult } = require("express-validator");

const app = express();

// Example route using the validation functions
app.post(
  "/register",
  [
    checkUsername("username"), // Validate username
    checkEmail("email"), // Validate email
    checkPhoneNumber("phone"), // Validate phone number
    checkURL("website"), // Validate URL
  ],
  (req, res) => {
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
      return res.status(400).json({ errors: errors.array() });
    }
    res.send("User registered successfully!");
  }
);

app.listen(3000, () => {
  console.log("Server running on port 3000");
});

Validation Functions

checkName = ( fieldName = "name", min = 2, max = 50, allowSpaces = true, allowSpecialChars = false )

checkName("name", 4, 20, false, true);

checkEmail = ( fieldName = "email", required = true, allowedDomains = [] )

// Optional list of allowed domains (e.g., ["gmail.com", "yahoo.com"])
checkName("email", false, ["gamil.com"]);

checkPassword = ( fieldName = "password", minLength = 8, maxLength = 23, requireUppercase = true, requireLowercase = true, requireNumber = true, requireSpecialChar = true )

checkPassword("password", 6, 30, true, false, false, true);

checkURL(fieldName = 'url')

Validates if the input field is a valid URL.

checkURL("website");

checkPhoneNumber(fieldName = 'phone')

Validates if the input field is a valid phone number.

checkPhoneNumber("phone");

checkDate(fieldName = 'date')

Validates if the input field is a valid date.

checkDate("birthdate");

checkNumber(fieldName = 'number', min = 1, max = 100)

Validates if the input field is a number within a specified range.

checkNumber("age", 18, 65);

checkLength(fieldName = 'text', min = 4, max = 16)

Validates if the input field is a string with a specified length range.

checkLength("username", 4, 16);

checkAlphanumeric(fieldName = 'text')

Validates if the input field contains only alphanumeric characters (letters and numbers).

checkAlphanumeric("username");

checkAlpha(fieldName = 'text')

Validates if the input field contains only alphabetic characters (letters).

checkAlpha("firstName");

checkCustomPattern(fieldName = 'text', regex, errorMessage = 'Invalid input format')

Validates if the input field matches a custom regex pattern.

checkCustomPattern(
  "postalCode",
  /^[A-Za-z0-9]{5,10}$/,
  "Invalid postal code format"
);

checkCreditCard(fieldName = 'card')

Validates if the input field is a valid credit card number.

checkCreditCard("creditCard");

checkJSON(fieldName = 'data')

Validates if the input field is a valid JSON string.

checkJSON("data");

checkBoolean(fieldName = 'flag')

Validates if the input field is a boolean value.

checkBoolean("isActive");

checkUniqueEmail(fieldName = 'email')

Validates if the email is unique in the database.

checkUniqueEmail("email");

checkUnique( fieldName = "username", model, key = "username", message = "")

Validates if the username is unique in the specified model's database.

checkUnique("username", User, "username", "username has already taken");

checkCustomUnique(fieldName, model, field)

Validates if the field is unique in the specified model's database.

checkCustomUnique("username", UserModel, "username");

checkArray(fieldName = 'items')

Validates if the input field is an array.

checkArray("items");

checkPhoneNumberByLocale(fieldName = 'phone', locale = 'any')

Validates if the phone number is valid for the specified locale.

checkPhoneNumberByLocale("phone", "en-US");

checkLocalizedEmail(fieldName = 'email', locale = 'en-US')

Validates if the email is valid for the specified locale.

checkLocalizedEmail("email", "en-US");

checkCurrency(fieldName = 'amount', currencySymbol = '$')

Validates if the amount is a valid currency value.

checkCurrency("amount", "$");

checkLocalizedDate(fieldName = 'date', format = 'YYYY-MM-DD')

Validates if the date is in the specified format.

checkLocalizedDate("date", "MM/DD/YYYY");

checkStreetAddress(fieldName = 'address')

Validates if the street address is within a valid length.

checkStreetAddress("address");

checkPostalCode(fieldName = 'postalCode', locale = 'US')

Validates if the postal code is valid for the specified locale.

checkPostalCode("postalCode", "US");

checkCityName(fieldName = 'city')

Validates if the city name contains only alphabetic characters.

checkCityName("city");

checkUniqueFieldByLocale(fieldName = 'username', model, locale = 'en-US')

Validates if the field is unique in the specified locale's database.

checkUniqueFieldByLocale("username", UserModel, "en-US");

checkLatinText(fieldName = 'text')

Validates if the text contains only Latin characters (A-Z, a-z).

checkLatinText("username");

checkGender(fieldName = 'gender', locale = 'en-US')

Validates if the gender value is one of the valid options.

checkGender("gender", "en-US");

checkTimezone(fieldName = 'timezone', validTimezones = [...])

Validates if the timezone is part of the predefined list of valid IANA timezones.

checkTimezone("timezone", [
  "Africa/Abidjan",
  "Europe/London",
  "America/New_York",
]);

checkEnum(fieldName = "role" , enumValues = ["user", "admin"])

  checkEnum("gender", ["male", "female", "other"]),

Explanation:

  • Installation: This section provides users with the command to install the package.
  • Usage: This section shows how to use the validation functions in an Express app.
  • Validation Functions: A list of all the validation functions you provide, with examples of how to use each one.

Author

This package was created by Nyi Nyi Phone Hlaing, a passionate software developer focused on building tools that make development easier and more efficient. Nyi Nyi is a MERN Stack Developer and a tech enthusiast, working towards becoming a professional full-stack developer and contributing to open-source projects.

You can find more information about Nyi Nyi's work at GitHub or YouTube.

Feel free to adjust the details based on your actual package and use case.

1.1.1

12 months ago

1.1.0

12 months ago

1.0.0

12 months ago