1.0.14 • Published 1 year ago

@therocketcodemx/library-validators v1.0.14

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

library-validators

:rocket: Table of Contents :rocket:

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.

ColumnTypeNot Null
idintYes
zip_codevarcharYes
statevarcharYes
cityvarcharYes
municipalityvarcharYes
settlement_namevarcharYes

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

ColumnTypeNot Null
idintYes
namevarcharYes
codevarcharYes
nationalityvarcharYes
created_attimestampNo
updated_attimestampNo
// 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',
//   }
// ]
1.0.9

1 year ago

1.0.8

1 year ago

1.0.11

1 year ago

1.0.10

1 year ago

1.0.14

1 year ago

1.0.13

1 year ago

1.0.12

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago