Skip to content

Commit c6a7c4d

Browse files
committed
Publishing adjustment version for error return and function typing
1 parent 576509a commit c6a7c4d

34 files changed

+136
-68
lines changed

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"code-runner.executorMap": {
33
"typescript": "npx ts-node",
4-
}
4+
},
55
}

dist/src/types/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });

dist/src/validateBRPhoneNumber.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
3-
var defaultErrorMsg = ['Invalid value passed', 'Invalid phone number', 'Unknown error'];
3+
var defaultErrorMsg = ['Field phone number cannot be empty', 'Invalid phone number', 'Unknown error'];
44
function validateBRPhoneNumber(phoneNumber, errorMsg) {
55
if (errorMsg === void 0) { errorMsg = defaultErrorMsg; }
66
if (typeof phoneNumber !== 'string')

dist/src/validateEmail.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
55
Object.defineProperty(exports, "__esModule", { value: true });
66
var isEmail_1 = __importDefault(require("./isEmail"));
77
var defaultErrorMsg = [
8-
'Invalid value passed',
8+
'Email cannot be empty',
99
'This e-mail is not valid',
1010
'Email too big, try again',
1111
'This email is not valid in the country',
@@ -38,8 +38,12 @@ function validateEmail(email, maxLength, country, errorMsg, validDomains) {
3838
}
3939
}
4040
}
41+
var maxEmailLength = maxLength || 400;
4142
function getErrorMessage(index) {
4243
var errorMessage = errorMsg[index];
44+
if (errorMessage === 'Email too big, try again') {
45+
return "Email cannot be greater than ".concat(maxEmailLength, " characters");
46+
}
4347
return errorMessage != null ? errorMessage : defaultErrorMsg[index];
4448
}
4549
if (!email) {
@@ -48,7 +52,6 @@ function validateEmail(email, maxLength, country, errorMsg, validDomains) {
4852
errorMsg: getErrorMessage(0),
4953
};
5054
}
51-
var maxEmailLength = maxLength || 400;
5255
if (maxEmailLength < 1 || typeof maxEmailLength !== 'number')
5356
throw new Error('maxLength must be a number and cannot be less than 1');
5457
try {

dist/src/validateName.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
33
var defaultErrorMsg = [
4-
'Invalid value passed',
4+
'Name cannot be empty',
55
'Name cannot contain numbers', 'Name cannot contain special characters',
66
'This name is not valid',
77
'Name too big, try again',

dist/src/validatePassword.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
33
var defaultErrorMsg = [
44
'This password is too long',
55
'password too short',
6-
'Requires at least one capital letter',
7-
'Requires at least one special character',
8-
'Requires at least one number',
9-
'Requires at least one letter',
6+
'Password requires at least one capital letter',
7+
'Password requires at least one special character',
8+
'Password requires at least one number',
9+
'Password requires at least one letter',
1010
'Unknown error',
1111
];
1212
function validatePassword(password, minLength, maxLength, options, errorMsg) {
@@ -28,12 +28,20 @@ function validatePassword(password, minLength, maxLength, options, errorMsg) {
2828
}
2929
}
3030
}
31+
var minLenthPassword = minLength || 1;
32+
var maxLenthPassword = maxLength || Infinity;
3133
function getErrorMessage(index) {
3234
var errorMessage = errorMsg[index];
35+
if (errorMessage === 'This password is too long' || errorMessage === 'password too short') {
36+
if (maxLenthPassword === Infinity) {
37+
return "Password must be greater than ".concat(minLenthPassword, " characters");
38+
}
39+
else {
40+
return "Password must be between ".concat(minLenthPassword, " and ").concat(maxLenthPassword, " characters");
41+
}
42+
}
3343
return errorMessage != null ? errorMessage : defaultErrorMsg[index];
3444
}
35-
var minLenthPassword = minLength || 1;
36-
var maxLenthPassword = maxLength || Infinity;
3745
if (typeof minLenthPassword !== 'number' || typeof maxLenthPassword !== 'number') {
3846
throw new Error('maxLength and/or minLength must be a number');
3947
}

dist/src/validatePhoneNumber.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
3-
var defaultErrorMsg = ['Invalid value passed', 'Invalid phone number', 'Unknown error'];
3+
var defaultErrorMsg = ['Phone number cannot be empty', 'Invalid phone number', 'Unknown error'];
44
function validatePhoneNumber(phoneNumber, errorMsg) {
55
if (errorMsg === void 0) { errorMsg = defaultErrorMsg; }
66
if (typeof phoneNumber !== 'string')

dist/src/validateSurname.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
33
var defaultErrorMsg = [
4-
'Invalid value passed',
4+
'Surname cannot be empty',
55
'Surname cannot contain numbers', 'Surname cannot contain special characters',
66
'This surname is not valid',
77
'Surname too big, try again',

dist/src/validateTextarea.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@ function validateTextarea(textarea, isRequired, maxLength, errorMsg) {
1919
}
2020
}
2121
}
22+
var maxTextAreaLength = maxLength || 50;
2223
function getErrorMessage(index) {
2324
var errorMessage = errorMsg[index];
25+
if (errorMessage === 'This textarea is too big') {
26+
return "Textarea cannot exceed ".concat(maxTextAreaLength, " characters");
27+
}
2428
return errorMessage != null ? errorMessage : defaultErrorMsg[index];
2529
}
26-
var maxTextAreaLength = maxLength || 50;
2730
if (maxTextAreaLength < 1 || typeof maxTextAreaLength !== 'number') {
2831
throw new Error('maxLength or minLength must be a number and cannot be less than 1');
2932
}

dist/src/validateUSPhoneNumber.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
3-
var defaultErrorMsg = ['Invalid value passed', 'Invalid phone number', 'Unknown error'];
3+
var defaultErrorMsg = ['US phone number cannot be empty', 'Invalid phone number', 'Unknown error'];
44
function validateUSPhoneNumber(phoneNumber, errorMsg) {
55
if (errorMsg === void 0) { errorMsg = defaultErrorMsg; }
66
if (typeof phoneNumber !== 'string')

dist/src/validateUsername.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var regexHasSpaces = /\s/;
44
var regexOnlyNumbers = /^\d+$/;
55
var regexStartsWithNumber = /^\d/;
66
var defaultErrorMsg = [
7-
'Invalid value passed',
7+
'Username cannot be empty',
88
'username too short',
99
'This username is too long',
1010
'Username cannot contain spaces',
@@ -25,8 +25,18 @@ function validateUsername(username, minLength, maxLength, errorMsg) {
2525
}
2626
}
2727
}
28+
var minLenthUsername = minLength || 1;
29+
var maxLenthUsername = maxLength || Infinity;
2830
function getErrorMessage(index) {
2931
var errorMessage = errorMsg[index];
32+
if (errorMessage === 'username too short' || errorMessage === 'This username is too long') {
33+
if (maxLenthUsername === Infinity) {
34+
return "Username must be greater than ".concat(maxLenthUsername, " characters");
35+
}
36+
else {
37+
return "Username must be between ".concat(maxLenthUsername, " and ").concat(maxLenthUsername, " characters");
38+
}
39+
}
3040
return errorMessage != null ? errorMessage : defaultErrorMsg[index];
3141
}
3242
if (!username) {
@@ -35,8 +45,6 @@ function validateUsername(username, minLength, maxLength, errorMsg) {
3545
errorMsg: getErrorMessage(0),
3646
};
3747
}
38-
var minLenthUsername = minLength || 1;
39-
var maxLenthUsername = maxLength || Infinity;
4048
if (typeof minLenthUsername !== 'number' || typeof maxLenthUsername !== 'number') {
4149
throw new Error('maxLength or minLength must be a number');
4250
}

index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import passwordStrengthTester from './src/passwordStrengthTester';
2727
import validateSurname from './src/validateSurname';
2828
import validateName from './src/validateName';
2929
import validateTextarea from './src/validateTextarea';
30+
import { ValidateFunctions, IsValidFunctions } from './src/types';
3031

3132
export {
3233
cpfIsValid,
@@ -57,5 +58,7 @@ export {
5758
passwordStrengthTester,
5859
validateName,
5960
validateSurname,
60-
validateTextarea
61+
validateTextarea,
62+
ValidateFunctions,
63+
IsValidFunctions
6164
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "multiform-validator",
3-
"version": "1.0.26",
3+
"version": "1.0.27",
44
"description": "Javascript library made to validate, several form fields, such as: email, phone, password, cpf etc.",
55
"main": "./dist/index.js",
66
"types": "./types/index.d.ts",

src/types/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export interface ValidateFunctions {
2+
isValid: boolean;
3+
errorMsg: string | null;
4+
}
5+
6+
export type IsValidFunctions = boolean

src/validateBRPhoneNumber.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const defaultErrorMsg: string[] = ['Invalid value passed', 'Invalid phone number', 'Unknown error'];
1+
const defaultErrorMsg: string[] = ['Field phone number cannot be empty', 'Invalid phone number', 'Unknown error'];
22
/**
33
* @param phoneNumber
44
* @param errorMsg optional
@@ -7,7 +7,7 @@ const defaultErrorMsg: string[] = ['Invalid value passed', 'Invalid phone number
77
* @description This function returns three errors in the following order:
88
*
99
* Default:
10-
* ['Invalid value passed', 'Invalid phone number', 'Unknown error']
10+
* ['Field phone number cannot be empty', 'Invalid phone number', 'Unknown error']
1111
*
1212
* Create a list of errors separated by commas in strings
1313
* @returns An object with 'isValid' (boolean) and 'errorMsg' (string) properties.

src/validateEmail.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import isEmail from './isEmail';
22

33
const defaultErrorMsg: string[] = [
4-
'Invalid value passed',
4+
'Email cannot be empty',
55
'This e-mail is not valid',
66
'Email too big, try again',
77
'This email is not valid in the country',
@@ -29,7 +29,7 @@ const validDomainsDefault: string[] = ['@gmail.com', '@outlook.com', '@yahoo.com
2929
* If you want to use a default parameter, use null.
3030
*
3131
* Default:
32-
* ['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']
32+
* ['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']
3333
*
3434
* Create a list of errors separated by commas in strings
3535
*
@@ -71,9 +71,15 @@ function validateEmail(email: string, maxLength?: number|null, country: string|n
7171
}
7272
}
7373

74+
const maxEmailLength: number = maxLength || 400;
75+
76+
7477
// Função interna para obter a mensagem de erro
7578
function getErrorMessage(index: number): string {
7679
const errorMessage: string|null = errorMsg[index];
80+
if(errorMessage === 'Email too big, try again'){
81+
return `Email cannot be greater than ${maxEmailLength} characters`;
82+
}
7783
return errorMessage != null ? errorMessage : defaultErrorMsg[index];
7884
}
7985

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

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

src/validateName.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const defaultErrorMsg: string[] = [
2-
'Invalid value passed',
2+
'Name cannot be empty',
33
'Name cannot contain numbers', 'Name cannot contain special characters',
44
'This name is not valid',
55
'Name too big, try again',
@@ -19,7 +19,7 @@ const defaultErrorMsg: string[] = [
1919
* default:
2020
*
2121
* [
22-
'Invalid value passed',
22+
'Name cannot be empty',
2323
'Name cannot contain numbers',
2424
'Name cannot contain special characters',
2525
'This name is not valid',

src/validatePassword.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
const defaultErrorMsg: string[] = [
22
'This password is too long',
33
'password too short',
4-
'Requires at least one capital letter',
5-
'Requires at least one special character',
6-
'Requires at least one number',
7-
'Requires at least one letter',
4+
'Password requires at least one capital letter',
5+
'Password requires at least one special character',
6+
'Password requires at least one number',
7+
'Password requires at least one letter',
88
'Unknown error',
99
];
1010
interface Options {
@@ -39,8 +39,8 @@ interface Options {
3939
*
4040
* Default:
4141
* [
42-
'This password is too long',
43-
'password too short',
42+
'This password is too long',// Password must be between ${minLenthPassword} and ${maxLenthPassword} characters
43+
'password too short',// Password must be between ${minLenthPassword} and ${maxLenthPassword} characters
4444
'Requires at least one capital letter',
4545
'Requires at least one special character',
4646
'Requires at least one number',
@@ -72,15 +72,24 @@ function validatePassword(password: string, minLength?: number|null, maxLength?:
7272
}
7373
}
7474

75+
const minLenthPassword: number = minLength || 1;
76+
const maxLenthPassword: number = maxLength || Infinity;
77+
7578

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

82-
const minLenthPassword: number = minLength || 1;
83-
const maxLenthPassword: number = maxLength || Infinity;
92+
8493

8594
if (typeof minLenthPassword !== 'number' || typeof maxLenthPassword !== 'number') {
8695
throw new Error('maxLength and/or minLength must be a number');

src/validatePhoneNumber.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
const defaultErrorMsg: string[] = ['Invalid value passed', 'Invalid phone number', 'Unknown error'];
1+
const defaultErrorMsg: string[] = ['Phone number cannot be empty', 'Invalid phone number', 'Unknown error'];
22
/**
33
* @param phoneNumber
44
* @param errorMsg optional
55
* @example validatePhoneNumber('555-123-4567');
66
* @example validatePhoneNumber('(555) 123-4567', [null, 'Custom error 2']);
7-
* @default {errorMsg} ['Invalid value passed', 'Invalid phone number', 'Unknown error']
7+
* @default {errorMsg} ['Phone number cannot be empty', 'Invalid phone number', 'Unknown error']
88
* @description This function is a generic phone number validator. It can validate phone numbers in various formats depending on the specific implementation.
99
* @returns An object with 'isValid' (boolean) and 'errorMsg' (string) properties.
1010
*/

src/validateSurname.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const defaultErrorMsg: string[] = [
2-
'Invalid value passed',
2+
'Surname cannot be empty',
33
'Surname cannot contain numbers', 'Surname cannot contain special characters',
44
'This surname is not valid',
55
'Surname too big, try again',
@@ -19,7 +19,7 @@ const defaultErrorMsg: string[] = [
1919
* default:
2020
*
2121
* [
22-
'Invalid value passed',
22+
'Surname cannot be empty',
2323
'Surname cannot contain numbers',
2424
'Surname cannot contain special characters',
2525
'This surname is not valid',

src/validateTextarea.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const defaultErrorMsg: string[] = [
1010
*
1111
* default:
1212
* [
13-
'This textarea is too big',
13+
'Textarea cannot exceed ${maxTextAreaLength} characters',
1414
'Can not be empty',
1515
'Unknown error',
1616
];
@@ -32,13 +32,18 @@ function validateTextarea(textarea: string, isRequired: boolean = false, maxLeng
3232
}
3333
}
3434

35+
const maxTextAreaLength: number = maxLength || 50;
36+
37+
3538
// Função interna para obter a mensagem de erro
3639
function getErrorMessage(index: number): string {
3740
const errorMessage: string|null = errorMsg[index];
41+
if(errorMessage === 'This textarea is too big'){
42+
return `Textarea cannot exceed ${maxTextAreaLength} characters`;
43+
}
3844
return errorMessage != null ? errorMessage : defaultErrorMsg[index];
3945
}
4046

41-
const maxTextAreaLength: number = maxLength || 50;
4247

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

0 commit comments

Comments
 (0)