-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidator.js
More file actions
94 lines (94 loc) · 4.32 KB
/
validator.js
File metadata and controls
94 lines (94 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Validator = void 0;
/**
* Validator class for validating strings and emails.
*/
class Validator {
/**
* Validates an email address format.
*
* @param email - The email address to validate.
* @returns true if the email format is valid, false otherwise.
*/
static validateEmail(email) {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(email);
}
/**
* Validates a string against specified rules.
*
* @param value - The string value to validate.
* @param rules - An object defining the validation rules.
* @param logging - Optional flag to enable logging of validation steps.
* @returns true if the string is valid according to the rules, false otherwise.
*/
static validateString(value, rules = {}, logging = false) {
let isValid = true;
// Default rules
rules.minLength = rules.minLength || 0;
rules.maxLength = rules.maxLength || Infinity;
// Check overall length
const lengthValid = value.length >= rules.minLength && value.length <= rules.maxLength;
if (logging) {
console.log("Overall length valid:", lengthValid);
}
isValid = isValid && lengthValid;
// Check uppercase
if (rules.uppercase) {
const uppercaseCount = (value.match(/[A-Z]/g) || []).length;
const uppercaseValid = uppercaseCount >= (rules.uppercase.minLength || 0) &&
uppercaseCount <= (rules.uppercase.maxLength || Infinity);
if (logging) {
console.log("Uppercase valid:", uppercaseValid);
}
isValid = isValid && uppercaseValid;
}
// Check lowercase
if (rules.lowercase) {
const lowercaseCount = (value.match(/[a-z]/g) || []).length;
const lowercaseValid = lowercaseCount >= (rules.lowercase.minLength || 0) &&
lowercaseCount <= (rules.lowercase.maxLength || Infinity);
if (logging) {
console.log("Lowercase valid:", lowercaseValid);
}
isValid = isValid && lowercaseValid;
}
// Check special characters
if (rules.specialChars) {
const { allowed = [], compulsory = [], minLength = 0, maxLength = Infinity } = rules.specialChars;
// Check for compulsory special characters if compulsory array is defined
const compulsoryValid = compulsory.length === 0 || compulsory.every(char => value.includes(char));
isValid = isValid && compulsoryValid;
// Check total count of compulsory special characters
const compulsoryCount = compulsory.filter(char => value.includes(char)).length;
const compulsoryLengthValid = compulsoryCount >= minLength && compulsoryCount <= maxLength;
isValid = isValid && compulsoryLengthValid;
// Check that all special characters are in the allowed list
const specialChars = [...value].filter(char => !/[a-zA-Z0-9]/.test(char));
const allAllowedValid = specialChars.every(char => allowed.includes(char));
isValid = isValid && allAllowedValid;
// Check total count of allowed special characters
const allowedCount = specialChars.filter(char => allowed.includes(char)).length;
const allowedLengthValid = allowedCount >= minLength && allowedCount <= maxLength;
isValid = isValid && allowedLengthValid;
// Log only the validity of the compulsory and allowed special characters checks
if (logging) {
console.log("Compulsory special characters valid:", compulsoryValid);
console.log("Allowed special characters valid:", allAllowedValid);
}
}
// Check for numbers
if (rules.number) {
const numberCount = (value.match(/[0-9]/g) || []).length;
const numberValid = numberCount >= (rules.number.minLength || 0) &&
numberCount <= (rules.number.maxLength || Infinity);
if (logging) {
console.log("Numbers valid:", numberValid);
}
isValid = isValid && numberValid;
}
return isValid;
}
}
exports.Validator = Validator;