@therocketcodemx/library-validators
TypeScript icon, indicating that this package has built-in type declarations

1.0.14 • Public • Published

library-validators

🚀 Table of Contents 🚀

Install

This is a Node.js module available through the npm registry.

Before installing, download and install Node.js. Node.js 0.6 or higher is required.

Installation is done using the npm install command:

$ npm install @therocketcodemx/library-validators

Introduction

This library contains several functions for validating different types of data, such as passwords, emails, phone numbers, and dates.

Usage

Here's a brief description of each function:

passwordFormat

This function takes a password string as input and validates whether it meets the specified requirements. The password must contain at least one uppercase letter, one lowercase letter, one number, one special character, and be at least 8 characters long.

// Example usage
import { validator } from '@therocketcodemx/library-validators';
const { passwordFormat } = validator;

const isValid = passwordFormat('Password123!'); // true
const isInvalid = passwordFormat('passw0rd'); // false

passwordsMatch

This function takes two password strings as input and checks if they match. Returns true if the passwords match, otherwise false.

// Example
import { validator } from '@therocketcodemx/library-validators';
const { passwordsMatch } = validator;

const password1 = 'password';
const password2 = 'password';

const match = passwordsMatch(password1, password2); // true

if (match) {
  console.log('The passwords match!');
} else {
  console.log('The passwords do not match.');
}

const password3 = 'password1';
const password4 = 'password2';

const mismatch = passwordsMatch(password3, password4); // false

if (mismatch) {
  console.log('The passwords match!');
} else {
  console.log('The passwords do not match.');
}

validateEmail

This function takes an email string as input and validates whether it has a valid email format.

// Example usage
import { validator } from '@therocketcodemx/library-validators';
const { validateEmail } = validator;

const isValid = validateEmail('example@example.com'); // true
const isInvalid = validateEmail('example@.com'); // false

validatePhoneNumber

This function takes a phone number string as input and validates whether it matches a regular expression. If a custom regular expression is not provided, it uses the regex for Mexican phone numbers.

// Example usage
import { validator } from '@therocketcodemx/library-validators';
const { validatePhoneNumber } = validator;

const mxPhoneNumber = '1234567890';
const mxIsValidPhoneNumber = validatePhoneNumber(mxPhoneNumber);
console.log(mxIsValidPhoneNumber); // Output: true

const usPhoneNumber = '123-456-7890';
const usPhoneNumberRegex = /^[0-9]{3}-[0-9]{3}-[0-9]{4}$/; // Custom regex for US phone numbers
const usIsValidPhoneNumber = validatePhoneNumber(
  usPhoneNumber,
  usPhoneNumberRegex
);
console.log(usIsValidPhoneNumber); // Output: false

validateOnlyLetters

This function only supports a text string to validate that it only contains letters. It returns true if the string only contains letters, otherwise false.

// Example usage
import { validateOnlyLetters } from '@therocketcodemx/library-validators';

const isValid = validateOnlyLetters('Pedro'); // true
const isInvalid = validateOnlyLetters('Pedro123'); // false

validateDateFormat

This function only supports a text string to format a date (mm/dd/yyyy) and validate that it is a valid date. Returns true if the string is in the correct format; otherwise, it returns false.

// Example usage
import { validateDateFormat } from '@therocketcodemx/library-validators';

const isFormatValid = validateDateFormat('12/12/2020'); // true
const isFormatInvalid = validateDateFormat('12/15/2020'); // false

validateCURP

This function only supports a text string to validate that it is a valid CURP. Returns true if the string is in the correct format; otherwise, it returns false.

// Example usage
import { validateCURP } from '@therocketcodemx/library-validators';

const isCURPValid = validateCURP('curp-to-validate');

validateRFC

This function only supports a text string to validate that it is a valid RFC. Returns true if the string is in the correct format; otherwise, it returns false. Also supports RFC with homoclave.

// Example usage
import { validateRFC } from '@therocketcodemx/library-validators';

const isRFCValid = validateRFC('rfc-to-validate');

AddressService

The AddressService class provides methods to interact with address-related data, such as fetching settlements by zipcode and getting all countries. It uses a PostgreSQL database to store and retrieve this data.

To use the AddressService class, you need to have two tables set up in your PostgreSQL database: cat_sepomex and countries. Here's the format and structure of each table:

cat_sepomex This table contains information about settlements in Mexico, including their zip codes, states, cities, and municipalities.

Column Type Not Null
id int Yes
zip_code varchar Yes
state varchar Yes
city varchar Yes
municipality varchar Yes
settlement_name varchar Yes

countries This table contains information about countries, including their names, codes, nationalities, and timestamps for when the records were created and updated.

Column Type Not Null
id int Yes
name varchar Yes
code varchar Yes
nationality varchar Yes
created_at timestamp No
updated_at timestamp No
// Example initialization
import { validator } from '@therocketcodemx/library-validators';
const { AddressService } = validator;

const addressService = new AddressService({
  host: 'your-host',
  database: 'your-db',
  user: 'your-user',
  password: 'your-pass',
});

getAllSettlementsByZipcode

Returns a Promise that resolves to an array of SettlementResult objects representing settlements with the specified zipcode. Throws an error if the given zipcode is not in the correct format.

// Example usage
const settlements = await addressService.getAllSettlementsByZipcode('06100');
console.log(settlements);
// [
//   {
//     state: 'Ciudad de México',
//     city: 'Ciudad de México',
//     municipality: 'Cuauhtémoc',
//     settlement: 'Hipódromo',
//   }
// ]

getAllCountries

Returns a Promise that resolves to an array of CountriesResult objects representing all countries.

// Example usage
const countries = await addressService.getAllCountries();
console.log(countries);
// [
//   {
//     id: 123,
//     name: 'México',
//     code: 'MEX',
//     nationality: 'MEXICANA',
//     created_at: '2023-05-05T05:00:00.000Z',
//     updated_at: '2023-05-05T05:00:00.000Z',
//   },
//   {
//     id: 12,
//     name: 'Aruba',
//     code: 'ABW',
//     nationality: 'ARUBEÑA',
//     created_at: '2023-05-05T05:00:00.000Z',
//     updated_at: '2023-05-05T05:00:00.000Z',
//   },
//   {
//     id: 2,
//     name: 'Afganistán',
//     code: 'AFG',
//     nationality: 'AFGANA',
//     created_at: '2023-05-05T05:00:00.000Z',
//     updated_at: '2023-05-05T05:00:00.000Z',
//   }
// ]

Readme

Keywords

none

Package Sidebar

Install

npm i @therocketcodemx/library-validators

Weekly Downloads

1

Version

1.0.14

License

ISC

Unpacked Size

30.4 kB

Total Files

32

Last publish

Collaborators

  • rocketcodemx