phani-validations 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +174 -2
- package/dist/cjs/index.js +83 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/utils/helpers.js +47 -0
- package/dist/cjs/validators/characters.js +260 -0
- package/dist/cjs/validators/common.js +261 -0
- package/dist/cjs/validators/email.js +103 -0
- package/dist/cjs/validators/password.js +161 -0
- package/dist/cjs/validators/schema.js +78 -0
- package/dist/esm/index.js +122 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/utils/helpers.js +39 -0
- package/dist/esm/validators/characters.js +245 -0
- package/dist/esm/validators/common.js +249 -0
- package/dist/esm/validators/email.js +98 -0
- package/dist/esm/validators/password.js +160 -0
- package/dist/esm/validators/schema.js +74 -0
- package/dist/index.d.ts +238 -0
- package/package.json +41 -4
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { isNonEmptyString } = require('../utils/helpers.js');
|
|
4
|
+
const { isNumber } = require('./characters.js');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Checks whether a value is empty (null, undefined, empty string, empty array, or empty object).
|
|
8
|
+
*
|
|
9
|
+
* @param {unknown} value
|
|
10
|
+
* @param {object} [options]
|
|
11
|
+
* @param {boolean} [options.ignoreWhitespace=true] - Treat whitespace-only strings as empty.
|
|
12
|
+
* @returns {boolean}
|
|
13
|
+
*/
|
|
14
|
+
function isEmpty(value, options = {}) {
|
|
15
|
+
const { ignoreWhitespace = true } = options;
|
|
16
|
+
|
|
17
|
+
if (value === null || value === undefined) {
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
if (typeof value === 'string') {
|
|
21
|
+
return (ignoreWhitespace ? value.trim() : value).length === 0;
|
|
22
|
+
}
|
|
23
|
+
if (Array.isArray(value)) {
|
|
24
|
+
return value.length === 0;
|
|
25
|
+
}
|
|
26
|
+
if (typeof value === 'object') {
|
|
27
|
+
return Object.keys(value).length === 0;
|
|
28
|
+
}
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Checks whether a value is present (not empty).
|
|
34
|
+
*
|
|
35
|
+
* @param {unknown} value
|
|
36
|
+
* @param {object} [options]
|
|
37
|
+
* @returns {boolean}
|
|
38
|
+
*/
|
|
39
|
+
function isRequired(value, options) {
|
|
40
|
+
return !isEmpty(value, options);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Checks whether a string's length falls within [min, max].
|
|
45
|
+
*
|
|
46
|
+
* @param {unknown} value
|
|
47
|
+
* @param {object} [options]
|
|
48
|
+
* @param {number} [options.min=0]
|
|
49
|
+
* @param {number} [options.max=Infinity]
|
|
50
|
+
* @returns {boolean}
|
|
51
|
+
*/
|
|
52
|
+
function isLength(value, options = {}) {
|
|
53
|
+
if (typeof value !== 'string') {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
const { min = 0, max = Infinity } = options;
|
|
57
|
+
return value.length >= min && value.length <= max;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Checks whether a numeric value falls within [min, max].
|
|
62
|
+
*
|
|
63
|
+
* @param {unknown} value
|
|
64
|
+
* @param {object} [options]
|
|
65
|
+
* @param {number} [options.min=-Infinity]
|
|
66
|
+
* @param {number} [options.max=Infinity]
|
|
67
|
+
* @returns {boolean}
|
|
68
|
+
*/
|
|
69
|
+
function isRange(value, options = {}) {
|
|
70
|
+
if (!isNumber(value)) {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
const { min = -Infinity, max = Infinity } = options;
|
|
74
|
+
const num = Number(value);
|
|
75
|
+
return num >= min && num <= max;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Checks whether a value is a valid phone number.
|
|
80
|
+
*
|
|
81
|
+
* @param {unknown} value
|
|
82
|
+
* @param {object} [options]
|
|
83
|
+
* @param {number} [options.minDigits=7]
|
|
84
|
+
* @param {number} [options.maxDigits=15]
|
|
85
|
+
* @param {boolean} [options.allowFormatting=true] - Allow spaces, hyphens, parentheses, and dots.
|
|
86
|
+
* @returns {boolean}
|
|
87
|
+
*/
|
|
88
|
+
function isPhone(value, options = {}) {
|
|
89
|
+
if (!isNonEmptyString(value)) {
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
const { minDigits = 7, maxDigits = 15, allowFormatting = true } = options;
|
|
93
|
+
const trimmed = value.trim();
|
|
94
|
+
|
|
95
|
+
if (!allowFormatting) {
|
|
96
|
+
const digitsOnly = trimmed.startsWith('+') ? trimmed.slice(1) : trimmed;
|
|
97
|
+
return /^\d+$/.test(digitsOnly) && digitsOnly.length >= minDigits && digitsOnly.length <= maxDigits;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (!/^\+?[\d\s().-]+$/.test(trimmed)) {
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Reject unbalanced parentheses
|
|
105
|
+
let openCount = 0;
|
|
106
|
+
for (const ch of trimmed) {
|
|
107
|
+
if (ch === '(') openCount += 1;
|
|
108
|
+
else if (ch === ')') openCount -= 1;
|
|
109
|
+
if (openCount < 0) return false;
|
|
110
|
+
}
|
|
111
|
+
if (openCount !== 0) {
|
|
112
|
+
return false;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const digits = trimmed.replace(/\D/g, '');
|
|
116
|
+
return digits.length >= minDigits && digits.length <= maxDigits;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Checks whether a value is a valid URL.
|
|
121
|
+
*
|
|
122
|
+
* @param {unknown} value
|
|
123
|
+
* @param {object} [options]
|
|
124
|
+
* @param {boolean} [options.requireProtocol=true] - Require protocol prefix (e.g. https://).
|
|
125
|
+
* @param {string[]} [options.protocols=['http:', 'https:']] - Allowed URL protocols.
|
|
126
|
+
* @param {boolean} [options.allowLocalhost=false] - Allow "localhost" hostnames.
|
|
127
|
+
* @returns {boolean}
|
|
128
|
+
*/
|
|
129
|
+
function isURL(value, options = {}) {
|
|
130
|
+
if (!isNonEmptyString(value) || /\s/.test(value)) {
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
const {
|
|
134
|
+
requireProtocol = true,
|
|
135
|
+
protocols = ['http:', 'https:'],
|
|
136
|
+
allowLocalhost = false,
|
|
137
|
+
} = options;
|
|
138
|
+
|
|
139
|
+
let candidate = value;
|
|
140
|
+
const hasProtocol = /^[a-zA-Z][a-zA-Z\d+\-.]*:\/\//.test(value);
|
|
141
|
+
|
|
142
|
+
if (!hasProtocol) {
|
|
143
|
+
if (requireProtocol) {
|
|
144
|
+
return false;
|
|
145
|
+
}
|
|
146
|
+
candidate = `https://${value}`;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
try {
|
|
150
|
+
const parsed = new URL(candidate);
|
|
151
|
+
if (!protocols.includes(parsed.protocol)) {
|
|
152
|
+
return false;
|
|
153
|
+
}
|
|
154
|
+
if (!parsed.hostname) {
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
if (parsed.hostname === 'localhost') {
|
|
158
|
+
return allowLocalhost;
|
|
159
|
+
}
|
|
160
|
+
return parsed.hostname.includes('.') && !parsed.hostname.startsWith('.') && !parsed.hostname.endsWith('.');
|
|
161
|
+
} catch {
|
|
162
|
+
return false;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Checks whether a value is a valid username.
|
|
168
|
+
*
|
|
169
|
+
* @param {unknown} value
|
|
170
|
+
* @param {object} [options]
|
|
171
|
+
* @param {number} [options.minLength=3]
|
|
172
|
+
* @param {number} [options.maxLength=20]
|
|
173
|
+
* @param {boolean} [options.allowUnderscore=true]
|
|
174
|
+
* @param {boolean} [options.allowHyphen=true]
|
|
175
|
+
* @param {boolean} [options.allowDot=false]
|
|
176
|
+
* @param {boolean} [options.mustStartWithLetter=true]
|
|
177
|
+
* @returns {boolean}
|
|
178
|
+
*/
|
|
179
|
+
function isUsername(value, options = {}) {
|
|
180
|
+
if (!isNonEmptyString(value)) {
|
|
181
|
+
return false;
|
|
182
|
+
}
|
|
183
|
+
const {
|
|
184
|
+
minLength = 3,
|
|
185
|
+
maxLength = 20,
|
|
186
|
+
allowUnderscore = true,
|
|
187
|
+
allowHyphen = true,
|
|
188
|
+
allowDot = false,
|
|
189
|
+
mustStartWithLetter = true,
|
|
190
|
+
} = options;
|
|
191
|
+
|
|
192
|
+
if (value.length < minLength || value.length > maxLength) {
|
|
193
|
+
return false;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
if (mustStartWithLetter && !/^[A-Za-z]/.test(value)) {
|
|
197
|
+
return false;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
let allowed = 'A-Za-z0-9';
|
|
201
|
+
if (allowUnderscore) allowed += '_';
|
|
202
|
+
if (allowHyphen) allowed += '\\-';
|
|
203
|
+
if (allowDot) allowed += '.';
|
|
204
|
+
|
|
205
|
+
return new RegExp(`^[${allowed}]+$`).test(value);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const UUID_REGEX_BY_VERSION = {
|
|
209
|
+
1: /^[0-9a-f]{8}-[0-9a-f]{4}-1[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,
|
|
210
|
+
2: /^[0-9a-f]{8}-[0-9a-f]{4}-2[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,
|
|
211
|
+
3: /^[0-9a-f]{8}-[0-9a-f]{4}-3[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,
|
|
212
|
+
4: /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,
|
|
213
|
+
5: /^[0-9a-f]{8}-[0-9a-f]{4}-5[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,
|
|
214
|
+
all: /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i,
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Checks whether a value is a valid UUID string.
|
|
219
|
+
*
|
|
220
|
+
* @param {unknown} value
|
|
221
|
+
* @param {1 | 2 | 3 | 4 | 5 | 'all'} [version='all']
|
|
222
|
+
* @returns {boolean}
|
|
223
|
+
*/
|
|
224
|
+
function isUUID(value, version = 'all') {
|
|
225
|
+
if (!isNonEmptyString(value)) {
|
|
226
|
+
return false;
|
|
227
|
+
}
|
|
228
|
+
const regex = UUID_REGEX_BY_VERSION[version] || UUID_REGEX_BY_VERSION.all;
|
|
229
|
+
return regex.test(value);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Checks whether a string matches a given regular expression or pattern string.
|
|
234
|
+
*
|
|
235
|
+
* @param {unknown} value
|
|
236
|
+
* @param {RegExp | string} pattern
|
|
237
|
+
* @param {string} [flags]
|
|
238
|
+
* @returns {boolean}
|
|
239
|
+
*/
|
|
240
|
+
function matches(value, pattern, flags) {
|
|
241
|
+
if (typeof value !== 'string') {
|
|
242
|
+
return false;
|
|
243
|
+
}
|
|
244
|
+
if (pattern instanceof RegExp) {
|
|
245
|
+
return pattern.test(value);
|
|
246
|
+
}
|
|
247
|
+
if (typeof pattern === 'string') {
|
|
248
|
+
return new RegExp(pattern, flags).test(value);
|
|
249
|
+
}
|
|
250
|
+
return false;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
exports.isEmpty = isEmpty;
|
|
254
|
+
exports.isRequired = isRequired;
|
|
255
|
+
exports.isLength = isLength;
|
|
256
|
+
exports.isRange = isRange;
|
|
257
|
+
exports.isPhone = isPhone;
|
|
258
|
+
exports.isURL = isURL;
|
|
259
|
+
exports.isUsername = isUsername;
|
|
260
|
+
exports.isUUID = isUUID;
|
|
261
|
+
exports.matches = matches;
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const LOCAL_PART_REGEX = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+$/;
|
|
4
|
+
const DOMAIN_LABEL_REGEX = /^[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?$/;
|
|
5
|
+
const TLD_REGEX = /^[a-zA-Z]{2,}$/;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Validates an email address and returns detailed validation errors.
|
|
9
|
+
*
|
|
10
|
+
* @param {unknown} value - The email value to validate.
|
|
11
|
+
* @param {object} [options]
|
|
12
|
+
* @param {boolean} [options.trim=false] - Whether to trim leading/trailing whitespace before validating.
|
|
13
|
+
* @param {string[]} [options.allowedDomains] - Whitelist of allowed email domains (case-insensitive).
|
|
14
|
+
* @param {string[]} [options.blockedDomains] - Blacklist of blocked email domains (case-insensitive).
|
|
15
|
+
* @returns {{ valid: boolean, errors: string[] }}
|
|
16
|
+
*/
|
|
17
|
+
function validateEmail(value, options = {}) {
|
|
18
|
+
const { trim = false, allowedDomains, blockedDomains } = options;
|
|
19
|
+
const errors = [];
|
|
20
|
+
|
|
21
|
+
if (typeof value !== 'string') {
|
|
22
|
+
return { valid: false, errors: ['Email must be a string.'] };
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const email = trim ? value.trim() : value;
|
|
26
|
+
|
|
27
|
+
if (email.length === 0) {
|
|
28
|
+
return { valid: false, errors: ['Email is required.'] };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (email.length > 254) {
|
|
32
|
+
errors.push('Email must not exceed 254 characters.');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (/\s/.test(email)) {
|
|
36
|
+
errors.push('Email must not contain whitespace.');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const parts = email.split('@');
|
|
40
|
+
if (parts.length !== 2) {
|
|
41
|
+
return { valid: false, errors: [...errors, 'Email must contain a single "@" symbol.'] };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const [localPart, domain] = parts;
|
|
45
|
+
|
|
46
|
+
if (localPart.length === 0 || localPart.length > 64) {
|
|
47
|
+
errors.push('Email local part must be between 1 and 64 characters.');
|
|
48
|
+
} else if (
|
|
49
|
+
localPart.startsWith('.') ||
|
|
50
|
+
localPart.endsWith('.') ||
|
|
51
|
+
localPart.includes('..') ||
|
|
52
|
+
!LOCAL_PART_REGEX.test(localPart)
|
|
53
|
+
) {
|
|
54
|
+
errors.push('Email local part contains invalid characters or dot placement.');
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (domain.length === 0 || domain.length > 255) {
|
|
58
|
+
errors.push('Email domain must be between 1 and 255 characters.');
|
|
59
|
+
} else {
|
|
60
|
+
const labels = domain.split('.');
|
|
61
|
+
if (labels.length < 2) {
|
|
62
|
+
errors.push('Email domain must include a valid top-level domain.');
|
|
63
|
+
} else {
|
|
64
|
+
const tld = labels[labels.length - 1];
|
|
65
|
+
const allLabelsValid = labels.every((label) => DOMAIN_LABEL_REGEX.test(label));
|
|
66
|
+
if (!allLabelsValid || !TLD_REGEX.test(tld)) {
|
|
67
|
+
errors.push('Email domain format is invalid.');
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const normalizedDomain = domain.toLowerCase();
|
|
73
|
+
|
|
74
|
+
if (Array.isArray(allowedDomains) && allowedDomains.length > 0) {
|
|
75
|
+
const allowed = allowedDomains.map((d) => String(d).toLowerCase());
|
|
76
|
+
if (!allowed.includes(normalizedDomain)) {
|
|
77
|
+
errors.push(`Email domain "${domain}" is not allowed.`);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (Array.isArray(blockedDomains) && blockedDomains.length > 0) {
|
|
82
|
+
const blocked = blockedDomains.map((d) => String(d).toLowerCase());
|
|
83
|
+
if (blocked.includes(normalizedDomain)) {
|
|
84
|
+
errors.push(`Email domain "${domain}" is blocked.`);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return { valid: errors.length === 0, errors };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Checks whether a value is a valid email address.
|
|
93
|
+
*
|
|
94
|
+
* @param {unknown} value
|
|
95
|
+
* @param {object} [options]
|
|
96
|
+
* @returns {boolean}
|
|
97
|
+
*/
|
|
98
|
+
function isEmail(value, options) {
|
|
99
|
+
return validateEmail(value, options).valid;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
exports.validateEmail = validateEmail;
|
|
103
|
+
exports.isEmail = isEmail;
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { hasUppercase, hasLowercase, hasDigits, hasSpecialChars, hasSpaces } = require('./characters.js');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Evaluates password strength independent of custom rule overrides.
|
|
7
|
+
*
|
|
8
|
+
* @param {unknown} value
|
|
9
|
+
* @returns {'weak' | 'medium' | 'strong'}
|
|
10
|
+
*/
|
|
11
|
+
function getPasswordStrength(value) {
|
|
12
|
+
if (typeof value !== 'string' || value.length < 6) {
|
|
13
|
+
return 'weak';
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
let score = 0;
|
|
17
|
+
if (value.length >= 8) score += 1;
|
|
18
|
+
if (value.length >= 12) score += 1;
|
|
19
|
+
if (hasUppercase(value)) score += 1;
|
|
20
|
+
if (hasLowercase(value)) score += 1;
|
|
21
|
+
if (hasDigits(value)) score += 1;
|
|
22
|
+
if (hasSpecialChars(value)) score += 1;
|
|
23
|
+
|
|
24
|
+
if (score >= 5 && value.length >= 8) {
|
|
25
|
+
return 'strong';
|
|
26
|
+
}
|
|
27
|
+
if (score >= 3) {
|
|
28
|
+
return 'medium';
|
|
29
|
+
}
|
|
30
|
+
return 'weak';
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Validates a password against configurable complexity rules and returns detailed results.
|
|
35
|
+
*
|
|
36
|
+
* @param {unknown} value - The password string to validate.
|
|
37
|
+
* @param {object} [options]
|
|
38
|
+
* @param {number} [options.minLength=8] - Minimum password length.
|
|
39
|
+
* @param {number} [options.maxLength=128] - Maximum password length.
|
|
40
|
+
* @param {boolean} [options.requireUppercase=true] - Require at least one uppercase letter.
|
|
41
|
+
* @param {boolean} [options.requireLowercase=true] - Require at least one lowercase letter.
|
|
42
|
+
* @param {boolean} [options.requireDigits=true] - Require at least one digit (0-9).
|
|
43
|
+
* @param {boolean} [options.requireNumbers] - Alias for requireDigits.
|
|
44
|
+
* @param {boolean} [options.requireSpecialChars=true] - Require at least one special character.
|
|
45
|
+
* @param {string} [options.specialChars] - Optional custom set of allowed special characters.
|
|
46
|
+
* @param {boolean} [options.allowSpaces=false] - Whether whitespace characters are allowed.
|
|
47
|
+
* @returns {{
|
|
48
|
+
* valid: boolean,
|
|
49
|
+
* errors: string[],
|
|
50
|
+
* strength: 'weak' | 'medium' | 'strong',
|
|
51
|
+
* checks: {
|
|
52
|
+
* minLength: boolean,
|
|
53
|
+
* maxLength: boolean,
|
|
54
|
+
* uppercase: boolean,
|
|
55
|
+
* lowercase: boolean,
|
|
56
|
+
* digits: boolean,
|
|
57
|
+
* specialChars: boolean,
|
|
58
|
+
* noSpaces: boolean
|
|
59
|
+
* }
|
|
60
|
+
* }}
|
|
61
|
+
*/
|
|
62
|
+
function validatePassword(value, options = {}) {
|
|
63
|
+
const {
|
|
64
|
+
minLength = 8,
|
|
65
|
+
maxLength = 128,
|
|
66
|
+
requireUppercase = true,
|
|
67
|
+
requireLowercase = true,
|
|
68
|
+
requireDigits,
|
|
69
|
+
requireNumbers,
|
|
70
|
+
requireSpecialChars = true,
|
|
71
|
+
specialChars,
|
|
72
|
+
allowSpaces = false,
|
|
73
|
+
} = options;
|
|
74
|
+
|
|
75
|
+
const mustHaveDigits = requireDigits ?? requireNumbers ?? true;
|
|
76
|
+
|
|
77
|
+
if (typeof value !== 'string') {
|
|
78
|
+
return {
|
|
79
|
+
valid: false,
|
|
80
|
+
errors: ['Password must be a string.'],
|
|
81
|
+
strength: 'weak',
|
|
82
|
+
checks: {
|
|
83
|
+
minLength: false,
|
|
84
|
+
maxLength: false,
|
|
85
|
+
uppercase: false,
|
|
86
|
+
lowercase: false,
|
|
87
|
+
digits: false,
|
|
88
|
+
specialChars: false,
|
|
89
|
+
noSpaces: false,
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const checks = {
|
|
95
|
+
minLength: value.length >= minLength,
|
|
96
|
+
maxLength: value.length <= maxLength,
|
|
97
|
+
uppercase: hasUppercase(value),
|
|
98
|
+
lowercase: hasLowercase(value),
|
|
99
|
+
digits: hasDigits(value),
|
|
100
|
+
specialChars: hasSpecialChars(value, specialChars),
|
|
101
|
+
noSpaces: !hasSpaces(value),
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const errors = [];
|
|
105
|
+
|
|
106
|
+
if (!checks.minLength) {
|
|
107
|
+
errors.push(`Password must be at least ${minLength} characters long.`);
|
|
108
|
+
}
|
|
109
|
+
if (!checks.maxLength) {
|
|
110
|
+
errors.push(`Password must not exceed ${maxLength} characters.`);
|
|
111
|
+
}
|
|
112
|
+
if (requireUppercase && !checks.uppercase) {
|
|
113
|
+
errors.push('Password must contain at least one uppercase letter.');
|
|
114
|
+
}
|
|
115
|
+
if (requireLowercase && !checks.lowercase) {
|
|
116
|
+
errors.push('Password must contain at least one lowercase letter.');
|
|
117
|
+
}
|
|
118
|
+
if (mustHaveDigits && !checks.digits) {
|
|
119
|
+
errors.push('Password must contain at least one digit.');
|
|
120
|
+
}
|
|
121
|
+
if (requireSpecialChars && !checks.specialChars) {
|
|
122
|
+
errors.push('Password must contain at least one special character.');
|
|
123
|
+
}
|
|
124
|
+
if (!allowSpaces && !checks.noSpaces) {
|
|
125
|
+
errors.push('Password must not contain spaces.');
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return {
|
|
129
|
+
valid: errors.length === 0,
|
|
130
|
+
errors,
|
|
131
|
+
strength: getPasswordStrength(value),
|
|
132
|
+
checks,
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Checks whether a password satisfies the configured validation rules.
|
|
138
|
+
*
|
|
139
|
+
* @param {unknown} value
|
|
140
|
+
* @param {object} [options]
|
|
141
|
+
* @returns {boolean}
|
|
142
|
+
*/
|
|
143
|
+
function isPassword(value, options) {
|
|
144
|
+
return validatePassword(value, options).valid;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Alias for {@link isPassword}.
|
|
149
|
+
*
|
|
150
|
+
* @param {unknown} value
|
|
151
|
+
* @param {object} [options]
|
|
152
|
+
* @returns {boolean}
|
|
153
|
+
*/
|
|
154
|
+
function isStrongPassword(value, options) {
|
|
155
|
+
return isPassword(value, options);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
exports.getPasswordStrength = getPasswordStrength;
|
|
159
|
+
exports.validatePassword = validatePassword;
|
|
160
|
+
exports.isPassword = isPassword;
|
|
161
|
+
exports.isStrongPassword = isStrongPassword;
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Validates an object of fields against a schema of validation rules.
|
|
5
|
+
*
|
|
6
|
+
* Each schema entry is an array of rules. A rule can be:
|
|
7
|
+
* - A function `(value, data) => boolean | { valid: boolean, errors?: string[] }`
|
|
8
|
+
* - An object `{ validator, message?, options? }` (or `{ rule, message?, options? }`)
|
|
9
|
+
*
|
|
10
|
+
* @param {Record<string, unknown>} data - Key-value object to validate.
|
|
11
|
+
* @param {Record<string, Array<Function | { validator?: Function, rule?: Function, message?: string, options?: object }>>} schema
|
|
12
|
+
* @returns {{ valid: boolean, errors: Record<string, string[]> }}
|
|
13
|
+
*/
|
|
14
|
+
function validate(data, schema) {
|
|
15
|
+
if (!data || typeof data !== 'object') {
|
|
16
|
+
return { valid: false, errors: { _root: ['Input data must be an object.'] } };
|
|
17
|
+
}
|
|
18
|
+
if (!schema || typeof schema !== 'object') {
|
|
19
|
+
return { valid: true, errors: {} };
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const errors = {};
|
|
23
|
+
|
|
24
|
+
for (const [field, rules] of Object.entries(schema)) {
|
|
25
|
+
if (!Array.isArray(rules)) {
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
const value = data[field];
|
|
29
|
+
const fieldErrors = [];
|
|
30
|
+
|
|
31
|
+
for (const entry of rules) {
|
|
32
|
+
let fn;
|
|
33
|
+
let message;
|
|
34
|
+
let options;
|
|
35
|
+
|
|
36
|
+
if (typeof entry === 'function') {
|
|
37
|
+
fn = entry;
|
|
38
|
+
} else if (entry && typeof entry === 'object') {
|
|
39
|
+
fn = entry.validator || entry.rule;
|
|
40
|
+
message = entry.message;
|
|
41
|
+
options = entry.options;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (typeof fn !== 'function') {
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const result = options !== undefined ? fn(value, options) : fn(value, data);
|
|
49
|
+
|
|
50
|
+
if (typeof result === 'boolean') {
|
|
51
|
+
if (!result) {
|
|
52
|
+
fieldErrors.push(message || `Field "${field}" is invalid.`);
|
|
53
|
+
}
|
|
54
|
+
} else if (result && typeof result === 'object' && 'valid' in result) {
|
|
55
|
+
if (!result.valid) {
|
|
56
|
+
if (message) {
|
|
57
|
+
fieldErrors.push(message);
|
|
58
|
+
} else if (Array.isArray(result.errors) && result.errors.length > 0) {
|
|
59
|
+
fieldErrors.push(...result.errors);
|
|
60
|
+
} else {
|
|
61
|
+
fieldErrors.push(`Field "${field}" is invalid.`);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (fieldErrors.length > 0) {
|
|
68
|
+
errors[field] = fieldErrors;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return {
|
|
73
|
+
valid: Object.keys(errors).length === 0,
|
|
74
|
+
errors,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
exports.validate = validate;
|