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.
@@ -0,0 +1,122 @@
1
+ import { isEmail, validateEmail } from './validators/email.js';
2
+ import {
3
+ isPassword,
4
+ isStrongPassword,
5
+ validatePassword,
6
+ getPasswordStrength,
7
+ } from './validators/password.js';
8
+ import {
9
+ isNumber,
10
+ isNumeric,
11
+ hasNumbers,
12
+ isDigits,
13
+ hasDigits,
14
+ isAlphabets,
15
+ isAlpha,
16
+ hasAlphabets,
17
+ hasAlpha,
18
+ isAlphanumeric,
19
+ isSpecialChars,
20
+ hasSpecialChars,
21
+ hasNoSpecialChars,
22
+ hasUppercase,
23
+ hasLowercase,
24
+ isUppercase,
25
+ isLowercase,
26
+ hasSpaces,
27
+ } from './validators/characters.js';
28
+ import {
29
+ isEmpty,
30
+ isRequired,
31
+ isLength,
32
+ isRange,
33
+ isPhone,
34
+ isURL,
35
+ isUsername,
36
+ isUUID,
37
+ matches,
38
+ } from './validators/common.js';
39
+ import { validate } from './validators/schema.js';
40
+
41
+ export {
42
+ // Email
43
+ isEmail,
44
+ validateEmail,
45
+ // Password
46
+ isPassword,
47
+ isStrongPassword,
48
+ validatePassword,
49
+ getPasswordStrength,
50
+ // Numbers & Digits
51
+ isNumber,
52
+ isNumeric,
53
+ hasNumbers,
54
+ isDigits,
55
+ hasDigits,
56
+ // Alphabets & Alphanumeric
57
+ isAlphabets,
58
+ isAlpha,
59
+ hasAlphabets,
60
+ hasAlpha,
61
+ isAlphanumeric,
62
+ // Special Characters & Case
63
+ isSpecialChars,
64
+ hasSpecialChars,
65
+ hasNoSpecialChars,
66
+ hasUppercase,
67
+ hasLowercase,
68
+ isUppercase,
69
+ isLowercase,
70
+ hasSpaces,
71
+ // Common Validators
72
+ isEmpty,
73
+ isRequired,
74
+ isLength,
75
+ isRange,
76
+ isPhone,
77
+ isURL,
78
+ isUsername,
79
+ isUUID,
80
+ matches,
81
+ // Schema / Object Validation
82
+ validate,
83
+ };
84
+
85
+ export const validation = {
86
+ isEmail,
87
+ validateEmail,
88
+ isPassword,
89
+ isStrongPassword,
90
+ validatePassword,
91
+ getPasswordStrength,
92
+ isNumber,
93
+ isNumeric,
94
+ hasNumbers,
95
+ isDigits,
96
+ hasDigits,
97
+ isAlphabets,
98
+ isAlpha,
99
+ hasAlphabets,
100
+ hasAlpha,
101
+ isAlphanumeric,
102
+ isSpecialChars,
103
+ hasSpecialChars,
104
+ hasNoSpecialChars,
105
+ hasUppercase,
106
+ hasLowercase,
107
+ isUppercase,
108
+ isLowercase,
109
+ hasSpaces,
110
+ isEmpty,
111
+ isRequired,
112
+ isLength,
113
+ isRange,
114
+ isPhone,
115
+ isURL,
116
+ isUsername,
117
+ isUUID,
118
+ matches,
119
+ validate,
120
+ };
121
+
122
+ export default validation;
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "module"
3
+ }
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Checks whether a value is a non-empty string.
3
+ * @param {unknown} value
4
+ * @returns {boolean}
5
+ */
6
+ export function isNonEmptyString(value) {
7
+ return typeof value === 'string' && value.length > 0;
8
+ }
9
+
10
+ /**
11
+ * Escapes special regular expression characters in a string.
12
+ * @param {string} str
13
+ * @returns {string}
14
+ */
15
+ export function escapeRegExp(str) {
16
+ return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
17
+ }
18
+
19
+ /**
20
+ * Normalizes a string or finite number into a string representation.
21
+ * Returns null for all other types (booleans, objects, null, undefined, NaN, Infinity).
22
+ * @param {unknown} value
23
+ * @returns {string | null}
24
+ */
25
+ export function toNumericString(value) {
26
+ if (typeof value === 'number') {
27
+ return Number.isFinite(value) ? String(value) : null;
28
+ }
29
+ if (typeof value === 'string') {
30
+ return value;
31
+ }
32
+ return null;
33
+ }
34
+
35
+ /**
36
+ * Default printable ASCII special characters (non-alphanumeric, non-whitespace).
37
+ */
38
+ export const SPECIAL_CHAR_REGEX = /[!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?`~]/;
39
+ export const ONLY_SPECIAL_CHARS_REGEX = /^[!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?`~]+$/;
@@ -0,0 +1,245 @@
1
+ import {
2
+ isNonEmptyString,
3
+ escapeRegExp,
4
+ toNumericString,
5
+ SPECIAL_CHAR_REGEX,
6
+ ONLY_SPECIAL_CHARS_REGEX,
7
+ } from '../utils/helpers.js';
8
+
9
+ /**
10
+ * Checks whether a value is a valid number (string or finite JS number).
11
+ *
12
+ * @param {unknown} value
13
+ * @param {object} [options]
14
+ * @param {boolean} [options.allowNegative=true] - Allow leading "-" sign.
15
+ * @param {boolean} [options.allowDecimal=true] - Allow decimal point.
16
+ * @param {boolean} [options.allowPlus=true] - Allow leading "+" sign.
17
+ * @returns {boolean}
18
+ */
19
+ export function isNumber(value, options = {}) {
20
+ const { allowNegative = true, allowDecimal = true, allowPlus = true } = options;
21
+ const str = toNumericString(value);
22
+ if (str === null || str.length === 0) {
23
+ return false;
24
+ }
25
+
26
+ let signPattern = '';
27
+ if (allowNegative && allowPlus) {
28
+ signPattern = '[+-]?';
29
+ } else if (allowNegative) {
30
+ signPattern = '-?';
31
+ } else if (allowPlus) {
32
+ signPattern = '\\+?';
33
+ }
34
+
35
+ const bodyPattern = allowDecimal ? '(?:\\d+(?:\\.\\d+)?|\\.\\d+)' : '\\d+';
36
+ const regex = new RegExp(`^${signPattern}${bodyPattern}$`);
37
+ return regex.test(str);
38
+ }
39
+
40
+ /**
41
+ * Alias for {@link isNumber}.
42
+ */
43
+ export function isNumeric(value, options) {
44
+ return isNumber(value, options);
45
+ }
46
+
47
+ /**
48
+ * Checks whether a value contains at least one digit (0-9).
49
+ *
50
+ * @param {unknown} value
51
+ * @returns {boolean}
52
+ */
53
+ export function hasDigits(value) {
54
+ const str = toNumericString(value);
55
+ return str !== null && /\d/.test(str);
56
+ }
57
+
58
+ /**
59
+ * Alias for {@link hasDigits}. Checks whether a value contains at least one numeric digit.
60
+ *
61
+ * @param {unknown} value
62
+ * @returns {boolean}
63
+ */
64
+ export function hasNumbers(value) {
65
+ return hasDigits(value);
66
+ }
67
+
68
+ /**
69
+ * Checks whether a value consists strictly of ASCII digits (0-9).
70
+ * Rejects signs ("+", "-"), decimals ("."), and whitespace.
71
+ *
72
+ * @param {unknown} value
73
+ * @returns {boolean}
74
+ */
75
+ export function isDigits(value) {
76
+ const str = toNumericString(value);
77
+ return str !== null && /^\d+$/.test(str);
78
+ }
79
+
80
+ /**
81
+ * Checks whether a string consists only of English alphabetic characters (A-Z, a-z).
82
+ *
83
+ * @param {unknown} value
84
+ * @param {object} [options]
85
+ * @param {boolean} [options.allowSpaces=false] - Allow spaces between letters.
86
+ * @returns {boolean}
87
+ */
88
+ export function isAlphabets(value, options = {}) {
89
+ if (!isNonEmptyString(value)) {
90
+ return false;
91
+ }
92
+ const { allowSpaces = false } = options;
93
+ if (allowSpaces) {
94
+ return /^[A-Za-z ]+$/.test(value) && /[A-Za-z]/.test(value);
95
+ }
96
+ return /^[A-Za-z]+$/.test(value);
97
+ }
98
+
99
+ /**
100
+ * Alias for {@link isAlphabets}.
101
+ */
102
+ export function isAlpha(value, options) {
103
+ return isAlphabets(value, options);
104
+ }
105
+
106
+ /**
107
+ * Checks whether a string contains at least one alphabetic character (A-Z, a-z).
108
+ *
109
+ * @param {unknown} value
110
+ * @returns {boolean}
111
+ */
112
+ export function hasAlphabets(value) {
113
+ return isNonEmptyString(value) && /[A-Za-z]/.test(value);
114
+ }
115
+
116
+ /**
117
+ * Alias for {@link hasAlphabets}.
118
+ */
119
+ export function hasAlpha(value) {
120
+ return hasAlphabets(value);
121
+ }
122
+
123
+ /**
124
+ * Checks whether a string consists only of alphanumeric characters (A-Z, a-z, 0-9).
125
+ *
126
+ * @param {unknown} value
127
+ * @param {object} [options]
128
+ * @param {boolean} [options.allowSpaces=false] - Allow spaces.
129
+ * @param {boolean} [options.allowUnderscore=false] - Allow underscores ("_").
130
+ * @param {boolean} [options.allowHyphen=false] - Allow hyphens ("-").
131
+ * @returns {boolean}
132
+ */
133
+ export function isAlphanumeric(value, options = {}) {
134
+ if (!isNonEmptyString(value)) {
135
+ return false;
136
+ }
137
+ const { allowSpaces = false, allowUnderscore = false, allowHyphen = false } = options;
138
+ let allowed = 'A-Za-z0-9';
139
+ if (allowSpaces) allowed += ' ';
140
+ if (allowUnderscore) allowed += '_';
141
+ if (allowHyphen) allowed += '\\-';
142
+
143
+ const regex = new RegExp(`^[${allowed}]+$`);
144
+ return regex.test(value) && /[A-Za-z0-9]/.test(value);
145
+ }
146
+
147
+ /**
148
+ * Checks whether a string contains at least one special character.
149
+ *
150
+ * @param {unknown} value
151
+ * @param {string} [customChars] - Optional string of characters to treat as special characters.
152
+ * @returns {boolean}
153
+ */
154
+ export function hasSpecialChars(value, customChars) {
155
+ if (!isNonEmptyString(value)) {
156
+ return false;
157
+ }
158
+ if (typeof customChars === 'string' && customChars.length > 0) {
159
+ const escaped = escapeRegExp(customChars);
160
+ return new RegExp(`[${escaped}]`).test(value);
161
+ }
162
+ return SPECIAL_CHAR_REGEX.test(value);
163
+ }
164
+
165
+ /**
166
+ * Checks whether a string consists exclusively of special characters.
167
+ *
168
+ * @param {unknown} value
169
+ * @param {string} [customChars] - Optional string of characters to treat as special characters.
170
+ * @returns {boolean}
171
+ */
172
+ export function isSpecialChars(value, customChars) {
173
+ if (!isNonEmptyString(value)) {
174
+ return false;
175
+ }
176
+ if (typeof customChars === 'string' && customChars.length > 0) {
177
+ const escaped = escapeRegExp(customChars);
178
+ return new RegExp(`^[${escaped}]+$`).test(value);
179
+ }
180
+ return ONLY_SPECIAL_CHARS_REGEX.test(value);
181
+ }
182
+
183
+ /**
184
+ * Checks whether a string contains no special characters.
185
+ *
186
+ * @param {unknown} value
187
+ * @param {string} [customChars] - Optional string of special characters to disallow.
188
+ * @returns {boolean}
189
+ */
190
+ export function hasNoSpecialChars(value, customChars) {
191
+ if (typeof value !== 'string') {
192
+ return false;
193
+ }
194
+ return !hasSpecialChars(value, customChars);
195
+ }
196
+
197
+ /**
198
+ * Checks whether a string contains at least one uppercase letter (A-Z).
199
+ *
200
+ * @param {unknown} value
201
+ * @returns {boolean}
202
+ */
203
+ export function hasUppercase(value) {
204
+ return isNonEmptyString(value) && /[A-Z]/.test(value);
205
+ }
206
+
207
+ /**
208
+ * Checks whether a string contains at least one lowercase letter (a-z).
209
+ *
210
+ * @param {unknown} value
211
+ * @returns {boolean}
212
+ */
213
+ export function hasLowercase(value) {
214
+ return isNonEmptyString(value) && /[a-z]/.test(value);
215
+ }
216
+
217
+ /**
218
+ * Checks whether all alphabetic characters in a non-empty string are uppercase.
219
+ *
220
+ * @param {unknown} value
221
+ * @returns {boolean}
222
+ */
223
+ export function isUppercase(value) {
224
+ return isNonEmptyString(value) && /[A-Z]/.test(value) && value === value.toUpperCase();
225
+ }
226
+
227
+ /**
228
+ * Checks whether all alphabetic characters in a non-empty string are lowercase.
229
+ *
230
+ * @param {unknown} value
231
+ * @returns {boolean}
232
+ */
233
+ export function isLowercase(value) {
234
+ return isNonEmptyString(value) && /[a-z]/.test(value) && value === value.toLowerCase();
235
+ }
236
+
237
+ /**
238
+ * Checks whether a string contains any whitespace character.
239
+ *
240
+ * @param {unknown} value
241
+ * @returns {boolean}
242
+ */
243
+ export function hasSpaces(value) {
244
+ return typeof value === 'string' && /\s/.test(value);
245
+ }
@@ -0,0 +1,249 @@
1
+ import { isNonEmptyString } from '../utils/helpers.js';
2
+ import { isNumber } from './characters.js';
3
+
4
+ /**
5
+ * Checks whether a value is empty (null, undefined, empty string, empty array, or empty object).
6
+ *
7
+ * @param {unknown} value
8
+ * @param {object} [options]
9
+ * @param {boolean} [options.ignoreWhitespace=true] - Treat whitespace-only strings as empty.
10
+ * @returns {boolean}
11
+ */
12
+ export function isEmpty(value, options = {}) {
13
+ const { ignoreWhitespace = true } = options;
14
+
15
+ if (value === null || value === undefined) {
16
+ return true;
17
+ }
18
+ if (typeof value === 'string') {
19
+ return (ignoreWhitespace ? value.trim() : value).length === 0;
20
+ }
21
+ if (Array.isArray(value)) {
22
+ return value.length === 0;
23
+ }
24
+ if (typeof value === 'object') {
25
+ return Object.keys(value).length === 0;
26
+ }
27
+ return false;
28
+ }
29
+
30
+ /**
31
+ * Checks whether a value is present (not empty).
32
+ *
33
+ * @param {unknown} value
34
+ * @param {object} [options]
35
+ * @returns {boolean}
36
+ */
37
+ export function isRequired(value, options) {
38
+ return !isEmpty(value, options);
39
+ }
40
+
41
+ /**
42
+ * Checks whether a string's length falls within [min, max].
43
+ *
44
+ * @param {unknown} value
45
+ * @param {object} [options]
46
+ * @param {number} [options.min=0]
47
+ * @param {number} [options.max=Infinity]
48
+ * @returns {boolean}
49
+ */
50
+ export function isLength(value, options = {}) {
51
+ if (typeof value !== 'string') {
52
+ return false;
53
+ }
54
+ const { min = 0, max = Infinity } = options;
55
+ return value.length >= min && value.length <= max;
56
+ }
57
+
58
+ /**
59
+ * Checks whether a numeric value falls within [min, max].
60
+ *
61
+ * @param {unknown} value
62
+ * @param {object} [options]
63
+ * @param {number} [options.min=-Infinity]
64
+ * @param {number} [options.max=Infinity]
65
+ * @returns {boolean}
66
+ */
67
+ export function isRange(value, options = {}) {
68
+ if (!isNumber(value)) {
69
+ return false;
70
+ }
71
+ const { min = -Infinity, max = Infinity } = options;
72
+ const num = Number(value);
73
+ return num >= min && num <= max;
74
+ }
75
+
76
+ /**
77
+ * Checks whether a value is a valid phone number.
78
+ *
79
+ * @param {unknown} value
80
+ * @param {object} [options]
81
+ * @param {number} [options.minDigits=7]
82
+ * @param {number} [options.maxDigits=15]
83
+ * @param {boolean} [options.allowFormatting=true] - Allow spaces, hyphens, parentheses, and dots.
84
+ * @returns {boolean}
85
+ */
86
+ export function isPhone(value, options = {}) {
87
+ if (!isNonEmptyString(value)) {
88
+ return false;
89
+ }
90
+ const { minDigits = 7, maxDigits = 15, allowFormatting = true } = options;
91
+ const trimmed = value.trim();
92
+
93
+ if (!allowFormatting) {
94
+ const digitsOnly = trimmed.startsWith('+') ? trimmed.slice(1) : trimmed;
95
+ return /^\d+$/.test(digitsOnly) && digitsOnly.length >= minDigits && digitsOnly.length <= maxDigits;
96
+ }
97
+
98
+ if (!/^\+?[\d\s().-]+$/.test(trimmed)) {
99
+ return false;
100
+ }
101
+
102
+ // Reject unbalanced parentheses
103
+ let openCount = 0;
104
+ for (const ch of trimmed) {
105
+ if (ch === '(') openCount += 1;
106
+ else if (ch === ')') openCount -= 1;
107
+ if (openCount < 0) return false;
108
+ }
109
+ if (openCount !== 0) {
110
+ return false;
111
+ }
112
+
113
+ const digits = trimmed.replace(/\D/g, '');
114
+ return digits.length >= minDigits && digits.length <= maxDigits;
115
+ }
116
+
117
+ /**
118
+ * Checks whether a value is a valid URL.
119
+ *
120
+ * @param {unknown} value
121
+ * @param {object} [options]
122
+ * @param {boolean} [options.requireProtocol=true] - Require protocol prefix (e.g. https://).
123
+ * @param {string[]} [options.protocols=['http:', 'https:']] - Allowed URL protocols.
124
+ * @param {boolean} [options.allowLocalhost=false] - Allow "localhost" hostnames.
125
+ * @returns {boolean}
126
+ */
127
+ export function isURL(value, options = {}) {
128
+ if (!isNonEmptyString(value) || /\s/.test(value)) {
129
+ return false;
130
+ }
131
+ const {
132
+ requireProtocol = true,
133
+ protocols = ['http:', 'https:'],
134
+ allowLocalhost = false,
135
+ } = options;
136
+
137
+ let candidate = value;
138
+ const hasProtocol = /^[a-zA-Z][a-zA-Z\d+\-.]*:\/\//.test(value);
139
+
140
+ if (!hasProtocol) {
141
+ if (requireProtocol) {
142
+ return false;
143
+ }
144
+ candidate = `https://${value}`;
145
+ }
146
+
147
+ try {
148
+ const parsed = new URL(candidate);
149
+ if (!protocols.includes(parsed.protocol)) {
150
+ return false;
151
+ }
152
+ if (!parsed.hostname) {
153
+ return false;
154
+ }
155
+ if (parsed.hostname === 'localhost') {
156
+ return allowLocalhost;
157
+ }
158
+ return parsed.hostname.includes('.') && !parsed.hostname.startsWith('.') && !parsed.hostname.endsWith('.');
159
+ } catch {
160
+ return false;
161
+ }
162
+ }
163
+
164
+ /**
165
+ * Checks whether a value is a valid username.
166
+ *
167
+ * @param {unknown} value
168
+ * @param {object} [options]
169
+ * @param {number} [options.minLength=3]
170
+ * @param {number} [options.maxLength=20]
171
+ * @param {boolean} [options.allowUnderscore=true]
172
+ * @param {boolean} [options.allowHyphen=true]
173
+ * @param {boolean} [options.allowDot=false]
174
+ * @param {boolean} [options.mustStartWithLetter=true]
175
+ * @returns {boolean}
176
+ */
177
+ export function isUsername(value, options = {}) {
178
+ if (!isNonEmptyString(value)) {
179
+ return false;
180
+ }
181
+ const {
182
+ minLength = 3,
183
+ maxLength = 20,
184
+ allowUnderscore = true,
185
+ allowHyphen = true,
186
+ allowDot = false,
187
+ mustStartWithLetter = true,
188
+ } = options;
189
+
190
+ if (value.length < minLength || value.length > maxLength) {
191
+ return false;
192
+ }
193
+
194
+ if (mustStartWithLetter && !/^[A-Za-z]/.test(value)) {
195
+ return false;
196
+ }
197
+
198
+ let allowed = 'A-Za-z0-9';
199
+ if (allowUnderscore) allowed += '_';
200
+ if (allowHyphen) allowed += '\\-';
201
+ if (allowDot) allowed += '.';
202
+
203
+ return new RegExp(`^[${allowed}]+$`).test(value);
204
+ }
205
+
206
+ const UUID_REGEX_BY_VERSION = {
207
+ 1: /^[0-9a-f]{8}-[0-9a-f]{4}-1[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,
208
+ 2: /^[0-9a-f]{8}-[0-9a-f]{4}-2[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,
209
+ 3: /^[0-9a-f]{8}-[0-9a-f]{4}-3[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,
210
+ 4: /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,
211
+ 5: /^[0-9a-f]{8}-[0-9a-f]{4}-5[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,
212
+ all: /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i,
213
+ };
214
+
215
+ /**
216
+ * Checks whether a value is a valid UUID string.
217
+ *
218
+ * @param {unknown} value
219
+ * @param {1 | 2 | 3 | 4 | 5 | 'all'} [version='all']
220
+ * @returns {boolean}
221
+ */
222
+ export function isUUID(value, version = 'all') {
223
+ if (!isNonEmptyString(value)) {
224
+ return false;
225
+ }
226
+ const regex = UUID_REGEX_BY_VERSION[version] || UUID_REGEX_BY_VERSION.all;
227
+ return regex.test(value);
228
+ }
229
+
230
+ /**
231
+ * Checks whether a string matches a given regular expression or pattern string.
232
+ *
233
+ * @param {unknown} value
234
+ * @param {RegExp | string} pattern
235
+ * @param {string} [flags]
236
+ * @returns {boolean}
237
+ */
238
+ export function matches(value, pattern, flags) {
239
+ if (typeof value !== 'string') {
240
+ return false;
241
+ }
242
+ if (pattern instanceof RegExp) {
243
+ return pattern.test(value);
244
+ }
245
+ if (typeof pattern === 'string') {
246
+ return new RegExp(pattern, flags).test(value);
247
+ }
248
+ return false;
249
+ }