Skip to content

Commit

Permalink
Publishing adjustment version for error return and function typing
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-logan committed Mar 9, 2024
1 parent 576509a commit c6a7c4d
Show file tree
Hide file tree
Showing 34 changed files with 136 additions and 68 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"code-runner.executorMap": {
"typescript": "npx ts-node",
}
},
}
2 changes: 2 additions & 0 deletions dist/src/types/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
2 changes: 1 addition & 1 deletion dist/src/validateBRPhoneNumber.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var defaultErrorMsg = ['Invalid value passed', 'Invalid phone number', 'Unknown error'];
var defaultErrorMsg = ['Field phone number cannot be empty', 'Invalid phone number', 'Unknown error'];
function validateBRPhoneNumber(phoneNumber, errorMsg) {
if (errorMsg === void 0) { errorMsg = defaultErrorMsg; }
if (typeof phoneNumber !== 'string')
Expand Down
7 changes: 5 additions & 2 deletions dist/src/validateEmail.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
Object.defineProperty(exports, "__esModule", { value: true });
var isEmail_1 = __importDefault(require("./isEmail"));
var defaultErrorMsg = [
'Invalid value passed',
'Email cannot be empty',
'This e-mail is not valid',
'Email too big, try again',
'This email is not valid in the country',
Expand Down Expand Up @@ -38,8 +38,12 @@ function validateEmail(email, maxLength, country, errorMsg, validDomains) {
}
}
}
var maxEmailLength = maxLength || 400;
function getErrorMessage(index) {
var errorMessage = errorMsg[index];
if (errorMessage === 'Email too big, try again') {
return "Email cannot be greater than ".concat(maxEmailLength, " characters");
}
return errorMessage != null ? errorMessage : defaultErrorMsg[index];
}
if (!email) {
Expand All @@ -48,7 +52,6 @@ function validateEmail(email, maxLength, country, errorMsg, validDomains) {
errorMsg: getErrorMessage(0),
};
}
var maxEmailLength = maxLength || 400;
if (maxEmailLength < 1 || typeof maxEmailLength !== 'number')
throw new Error('maxLength must be a number and cannot be less than 1');
try {
Expand Down
2 changes: 1 addition & 1 deletion dist/src/validateName.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var defaultErrorMsg = [
'Invalid value passed',
'Name cannot be empty',
'Name cannot contain numbers', 'Name cannot contain special characters',
'This name is not valid',
'Name too big, try again',
Expand Down
20 changes: 14 additions & 6 deletions dist/src/validatePassword.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
var defaultErrorMsg = [
'This password is too long',
'password too short',
'Requires at least one capital letter',
'Requires at least one special character',
'Requires at least one number',
'Requires at least one letter',
'Password requires at least one capital letter',
'Password requires at least one special character',
'Password requires at least one number',
'Password requires at least one letter',
'Unknown error',
];
function validatePassword(password, minLength, maxLength, options, errorMsg) {
Expand All @@ -28,12 +28,20 @@ function validatePassword(password, minLength, maxLength, options, errorMsg) {
}
}
}
var minLenthPassword = minLength || 1;
var maxLenthPassword = maxLength || Infinity;
function getErrorMessage(index) {
var errorMessage = errorMsg[index];
if (errorMessage === 'This password is too long' || errorMessage === 'password too short') {
if (maxLenthPassword === Infinity) {
return "Password must be greater than ".concat(minLenthPassword, " characters");
}
else {
return "Password must be between ".concat(minLenthPassword, " and ").concat(maxLenthPassword, " characters");
}
}
return errorMessage != null ? errorMessage : defaultErrorMsg[index];
}
var minLenthPassword = minLength || 1;
var maxLenthPassword = maxLength || Infinity;
if (typeof minLenthPassword !== 'number' || typeof maxLenthPassword !== 'number') {
throw new Error('maxLength and/or minLength must be a number');
}
Expand Down
2 changes: 1 addition & 1 deletion dist/src/validatePhoneNumber.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var defaultErrorMsg = ['Invalid value passed', 'Invalid phone number', 'Unknown error'];
var defaultErrorMsg = ['Phone number cannot be empty', 'Invalid phone number', 'Unknown error'];
function validatePhoneNumber(phoneNumber, errorMsg) {
if (errorMsg === void 0) { errorMsg = defaultErrorMsg; }
if (typeof phoneNumber !== 'string')
Expand Down
2 changes: 1 addition & 1 deletion dist/src/validateSurname.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var defaultErrorMsg = [
'Invalid value passed',
'Surname cannot be empty',
'Surname cannot contain numbers', 'Surname cannot contain special characters',
'This surname is not valid',
'Surname too big, try again',
Expand Down
5 changes: 4 additions & 1 deletion dist/src/validateTextarea.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ function validateTextarea(textarea, isRequired, maxLength, errorMsg) {
}
}
}
var maxTextAreaLength = maxLength || 50;
function getErrorMessage(index) {
var errorMessage = errorMsg[index];
if (errorMessage === 'This textarea is too big') {
return "Textarea cannot exceed ".concat(maxTextAreaLength, " characters");
}
return errorMessage != null ? errorMessage : defaultErrorMsg[index];
}
var maxTextAreaLength = maxLength || 50;
if (maxTextAreaLength < 1 || typeof maxTextAreaLength !== 'number') {
throw new Error('maxLength or minLength must be a number and cannot be less than 1');
}
Expand Down
2 changes: 1 addition & 1 deletion dist/src/validateUSPhoneNumber.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var defaultErrorMsg = ['Invalid value passed', 'Invalid phone number', 'Unknown error'];
var defaultErrorMsg = ['US phone number cannot be empty', 'Invalid phone number', 'Unknown error'];
function validateUSPhoneNumber(phoneNumber, errorMsg) {
if (errorMsg === void 0) { errorMsg = defaultErrorMsg; }
if (typeof phoneNumber !== 'string')
Expand Down
14 changes: 11 additions & 3 deletions dist/src/validateUsername.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var regexHasSpaces = /\s/;
var regexOnlyNumbers = /^\d+$/;
var regexStartsWithNumber = /^\d/;
var defaultErrorMsg = [
'Invalid value passed',
'Username cannot be empty',
'username too short',
'This username is too long',
'Username cannot contain spaces',
Expand All @@ -25,8 +25,18 @@ function validateUsername(username, minLength, maxLength, errorMsg) {
}
}
}
var minLenthUsername = minLength || 1;
var maxLenthUsername = maxLength || Infinity;
function getErrorMessage(index) {
var errorMessage = errorMsg[index];
if (errorMessage === 'username too short' || errorMessage === 'This username is too long') {
if (maxLenthUsername === Infinity) {
return "Username must be greater than ".concat(maxLenthUsername, " characters");
}
else {
return "Username must be between ".concat(maxLenthUsername, " and ").concat(maxLenthUsername, " characters");
}
}
return errorMessage != null ? errorMessage : defaultErrorMsg[index];
}
if (!username) {
Expand All @@ -35,8 +45,6 @@ function validateUsername(username, minLength, maxLength, errorMsg) {
errorMsg: getErrorMessage(0),
};
}
var minLenthUsername = minLength || 1;
var maxLenthUsername = maxLength || Infinity;
if (typeof minLenthUsername !== 'number' || typeof maxLenthUsername !== 'number') {
throw new Error('maxLength or minLength must be a number');
}
Expand Down
5 changes: 4 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import passwordStrengthTester from './src/passwordStrengthTester';
import validateSurname from './src/validateSurname';
import validateName from './src/validateName';
import validateTextarea from './src/validateTextarea';
import { ValidateFunctions, IsValidFunctions } from './src/types';

export {
cpfIsValid,
Expand Down Expand Up @@ -57,5 +58,7 @@ export {
passwordStrengthTester,
validateName,
validateSurname,
validateTextarea
validateTextarea,
ValidateFunctions,
IsValidFunctions
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "multiform-validator",
"version": "1.0.26",
"version": "1.0.27",
"description": "Javascript library made to validate, several form fields, such as: email, phone, password, cpf etc.",
"main": "./dist/index.js",
"types": "./types/index.d.ts",
Expand Down
6 changes: 6 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface ValidateFunctions {
isValid: boolean;
errorMsg: string | null;
}

export type IsValidFunctions = boolean
4 changes: 2 additions & 2 deletions src/validateBRPhoneNumber.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const defaultErrorMsg: string[] = ['Invalid value passed', 'Invalid phone number', 'Unknown error'];
const defaultErrorMsg: string[] = ['Field phone number cannot be empty', 'Invalid phone number', 'Unknown error'];
/**
* @param phoneNumber
* @param errorMsg optional
Expand All @@ -7,7 +7,7 @@ const defaultErrorMsg: string[] = ['Invalid value passed', 'Invalid phone number
* @description This function returns three errors in the following order:
*
* Default:
* ['Invalid value passed', 'Invalid phone number', 'Unknown error']
* ['Field phone number cannot be empty', 'Invalid phone number', 'Unknown error']
*
* Create a list of errors separated by commas in strings
* @returns An object with 'isValid' (boolean) and 'errorMsg' (string) properties.
Expand Down
11 changes: 8 additions & 3 deletions src/validateEmail.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import isEmail from './isEmail';

const defaultErrorMsg: string[] = [
'Invalid value passed',
'Email cannot be empty',
'This e-mail is not valid',
'Email too big, try again',
'This email is not valid in the country',
Expand Down Expand Up @@ -29,7 +29,7 @@ const validDomainsDefault: string[] = ['@gmail.com', '@outlook.com', '@yahoo.com
* If you want to use a default parameter, use null.
*
* Default:
* ['Invalid value passed', 'This e-mail is not valid', 'Email too big, try again', 'This email is not valid in the country','Email domain is not allowed.', 'Unknown error']
* ['Email cannot be empty', 'This e-mail is not valid', 'Email cannot be greater than ${maxEmailLength} characters', 'This email is not valid in the country','Email domain is not allowed.', 'Unknown error']
*
* Create a list of errors separated by commas in strings
*
Expand Down Expand Up @@ -71,9 +71,15 @@ function validateEmail(email: string, maxLength?: number|null, country: string|n
}
}

const maxEmailLength: number = maxLength || 400;


// Função interna para obter a mensagem de erro
function getErrorMessage(index: number): string {
const errorMessage: string|null = errorMsg[index];
if(errorMessage === 'Email too big, try again'){
return `Email cannot be greater than ${maxEmailLength} characters`;
}
return errorMessage != null ? errorMessage : defaultErrorMsg[index];
}

Expand All @@ -83,7 +89,6 @@ function validateEmail(email: string, maxLength?: number|null, country: string|n
errorMsg: getErrorMessage(0),
};
}
const maxEmailLength: number = maxLength || 400;

if (maxEmailLength < 1 || typeof maxEmailLength !== 'number') throw new Error('maxLength must be a number and cannot be less than 1');

Expand Down
4 changes: 2 additions & 2 deletions src/validateName.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const defaultErrorMsg: string[] = [
'Invalid value passed',
'Name cannot be empty',
'Name cannot contain numbers', 'Name cannot contain special characters',
'This name is not valid',
'Name too big, try again',
Expand All @@ -19,7 +19,7 @@ const defaultErrorMsg: string[] = [
* default:
*
* [
'Invalid value passed',
'Name cannot be empty',
'Name cannot contain numbers',
'Name cannot contain special characters',
'This name is not valid',
Expand Down
25 changes: 17 additions & 8 deletions src/validatePassword.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const defaultErrorMsg: string[] = [
'This password is too long',
'password too short',
'Requires at least one capital letter',
'Requires at least one special character',
'Requires at least one number',
'Requires at least one letter',
'Password requires at least one capital letter',
'Password requires at least one special character',
'Password requires at least one number',
'Password requires at least one letter',
'Unknown error',
];
interface Options {
Expand Down Expand Up @@ -39,8 +39,8 @@ interface Options {
*
* Default:
* [
'This password is too long',
'password too short',
'This password is too long',// Password must be between ${minLenthPassword} and ${maxLenthPassword} characters
'password too short',// Password must be between ${minLenthPassword} and ${maxLenthPassword} characters
'Requires at least one capital letter',
'Requires at least one special character',
'Requires at least one number',
Expand Down Expand Up @@ -72,15 +72,24 @@ function validatePassword(password: string, minLength?: number|null, maxLength?:
}
}

const minLenthPassword: number = minLength || 1;
const maxLenthPassword: number = maxLength || Infinity;


// Função interna para obter a mensagem de erro
function getErrorMessage(index: number): string {
const errorMessage: string|null = errorMsg[index];
if(errorMessage === 'This password is too long' || errorMessage === 'password too short'){
if(maxLenthPassword === Infinity){
return `Password must be greater than ${minLenthPassword} characters`;
}else{
return `Password must be between ${minLenthPassword} and ${maxLenthPassword} characters`;
}
}
return errorMessage != null ? errorMessage : defaultErrorMsg[index];
}

const minLenthPassword: number = minLength || 1;
const maxLenthPassword: number = maxLength || Infinity;


if (typeof minLenthPassword !== 'number' || typeof maxLenthPassword !== 'number') {
throw new Error('maxLength and/or minLength must be a number');
Expand Down
4 changes: 2 additions & 2 deletions src/validatePhoneNumber.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const defaultErrorMsg: string[] = ['Invalid value passed', 'Invalid phone number', 'Unknown error'];
const defaultErrorMsg: string[] = ['Phone number cannot be empty', 'Invalid phone number', 'Unknown error'];
/**
* @param phoneNumber
* @param errorMsg optional
* @example validatePhoneNumber('555-123-4567');
* @example validatePhoneNumber('(555) 123-4567', [null, 'Custom error 2']);
* @default {errorMsg} ['Invalid value passed', 'Invalid phone number', 'Unknown error']
* @default {errorMsg} ['Phone number cannot be empty', 'Invalid phone number', 'Unknown error']
* @description This function is a generic phone number validator. It can validate phone numbers in various formats depending on the specific implementation.
* @returns An object with 'isValid' (boolean) and 'errorMsg' (string) properties.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/validateSurname.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const defaultErrorMsg: string[] = [
'Invalid value passed',
'Surname cannot be empty',
'Surname cannot contain numbers', 'Surname cannot contain special characters',
'This surname is not valid',
'Surname too big, try again',
Expand All @@ -19,7 +19,7 @@ const defaultErrorMsg: string[] = [
* default:
*
* [
'Invalid value passed',
'Surname cannot be empty',
'Surname cannot contain numbers',
'Surname cannot contain special characters',
'This surname is not valid',
Expand Down
9 changes: 7 additions & 2 deletions src/validateTextarea.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const defaultErrorMsg: string[] = [
*
* default:
* [
'This textarea is too big',
'Textarea cannot exceed ${maxTextAreaLength} characters',
'Can not be empty',
'Unknown error',
];
Expand All @@ -32,13 +32,18 @@ function validateTextarea(textarea: string, isRequired: boolean = false, maxLeng
}
}

const maxTextAreaLength: number = maxLength || 50;


// Função interna para obter a mensagem de erro
function getErrorMessage(index: number): string {
const errorMessage: string|null = errorMsg[index];
if(errorMessage === 'This textarea is too big'){
return `Textarea cannot exceed ${maxTextAreaLength} characters`;
}
return errorMessage != null ? errorMessage : defaultErrorMsg[index];
}

const maxTextAreaLength: number = maxLength || 50;

if (maxTextAreaLength < 1 || typeof maxTextAreaLength !== 'number') {
throw new Error('maxLength or minLength must be a number and cannot be less than 1');
Expand Down
Loading

0 comments on commit c6a7c4d

Please sign in to comment.