forked from Movalabs-crew/mova-store
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation.ts
More file actions
425 lines (339 loc) · 11.7 KB
/
Copy pathvalidation.ts
File metadata and controls
425 lines (339 loc) · 11.7 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/**
* Input Validation Utilities
*
* Provides validation functions for user inputs to prevent XSS,
* SQL injection, and other common security vulnerabilities.
*/
// =============================================================================
// TYPE DEFINITIONS
// =============================================================================
export interface ValidationResult {
isValid: boolean;
error?: string;
sanitized?: string;
}
export interface FormErrors {
[key: string]: string | undefined;
}
// =============================================================================
// SANITIZATION
// =============================================================================
/**
* Escapes HTML special characters to prevent XSS.
*/
export function escapeHtml(input: string): string {
const htmlEscapes: Record<string, string> = {
"&": "&",
"<": "<",
">": ">",
'"': """,
"'": "'",
"/": "/",
};
return input.replace(/[&<>"'/]/g, (char) => htmlEscapes[char] || char);
}
/**
* Removes potentially dangerous characters from input.
* Allows alphanumeric, spaces, and common punctuation.
*/
export function sanitizeText(input: string): string {
if (!input) return "";
// Remove null bytes and control characters
return input
.replace(/\0/g, "")
.replace(/[\x00-\x1F\x7F]/g, "")
.trim();
}
/**
* Sanitizes input for safe use in HTML contexts.
*/
export function sanitizeForHtml(input: string): string {
return escapeHtml(sanitizeText(input));
}
// =============================================================================
// EMAIL VALIDATION
// =============================================================================
const EMAIL_REGEX = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
/**
* Validates an email address format.
*/
export function validateEmail(email: string): ValidationResult {
const sanitized = sanitizeText(email).toLowerCase();
if (!sanitized) {
return { isValid: false, error: "Email is required" };
}
if (sanitized.length > 254) {
return { isValid: false, error: "Email is too long" };
}
if (!EMAIL_REGEX.test(sanitized)) {
return { isValid: false, error: "Please enter a valid email address" };
}
return { isValid: true, sanitized };
}
// =============================================================================
// NAME VALIDATION
// =============================================================================
/**
* Validates a person's name.
*/
export function validateName(name: string, fieldName = "Name"): ValidationResult {
const sanitized = sanitizeText(name);
if (!sanitized) {
return { isValid: false, error: `${fieldName} is required` };
}
if (sanitized.length < 2) {
return { isValid: false, error: `${fieldName} must be at least 2 characters` };
}
if (sanitized.length > 100) {
return { isValid: false, error: `${fieldName} is too long` };
}
// Allow letters, spaces, hyphens, apostrophes (for names like O'Brien, Mary-Jane)
const nameRegex = /^[a-zA-Z\s'-]+$/;
if (!nameRegex.test(sanitized)) {
return {
isValid: false,
error: `${fieldName} can only contain letters, spaces, hyphens, and apostrophes`,
};
}
return { isValid: true, sanitized };
}
// =============================================================================
// ADDRESS VALIDATION
// =============================================================================
/**
* Validates a street address.
*/
export function validateAddress(address: string): ValidationResult {
const sanitized = sanitizeText(address);
if (!sanitized) {
return { isValid: false, error: "Address is required" };
}
if (sanitized.length < 5) {
return { isValid: false, error: "Please enter a complete address" };
}
if (sanitized.length > 200) {
return { isValid: false, error: "Address is too long" };
}
return { isValid: true, sanitized };
}
/**
* Validates a city name.
*/
export function validateCity(city: string): ValidationResult {
const sanitized = sanitizeText(city);
if (!sanitized) {
return { isValid: false, error: "City is required" };
}
if (sanitized.length < 2) {
return { isValid: false, error: "Please enter a valid city" };
}
if (sanitized.length > 100) {
return { isValid: false, error: "City name is too long" };
}
return { isValid: true, sanitized };
}
/**
* Validates a postal/zip code.
*/
export function validatePostalCode(code: string): ValidationResult {
const sanitized = sanitizeText(code).toUpperCase();
if (!sanitized) {
return { isValid: false, error: "Postal code is required" };
}
// Allow various formats (US, UK, Canada, etc.)
const postalRegex = /^[A-Z0-9\s-]{3,10}$/;
if (!postalRegex.test(sanitized)) {
return { isValid: false, error: "Please enter a valid postal code" };
}
return { isValid: true, sanitized };
}
// =============================================================================
// PHONE VALIDATION
// =============================================================================
/**
* Validates a phone number.
*/
export function validatePhone(phone: string): ValidationResult {
// Remove common formatting characters
const sanitized = sanitizeText(phone).replace(/[\s\-().]/g, "");
if (!sanitized) {
return { isValid: false, error: "Phone number is required" };
}
// Allow + for international prefix
const phoneRegex = /^\+?[0-9]{7,15}$/;
if (!phoneRegex.test(sanitized)) {
return { isValid: false, error: "Please enter a valid phone number" };
}
return { isValid: true, sanitized };
}
// =============================================================================
// PRODUCT VALIDATION
// =============================================================================
/**
* Validates a product name.
*/
export function validateProductName(name: string): ValidationResult {
const sanitized = sanitizeText(name);
if (!sanitized) {
return { isValid: false, error: "Product name is required" };
}
if (sanitized.length < 2) {
return { isValid: false, error: "Product name must be at least 2 characters" };
}
if (sanitized.length > 200) {
return { isValid: false, error: "Product name is too long" };
}
return { isValid: true, sanitized };
}
/**
* Validates a product price.
*/
export function validatePrice(price: string | number): ValidationResult {
const numPrice = typeof price === "string" ? parseFloat(price) : price;
if (isNaN(numPrice)) {
return { isValid: false, error: "Please enter a valid price" };
}
if (numPrice < 0) {
return { isValid: false, error: "Price cannot be negative" };
}
if (numPrice > 1000000) {
return { isValid: false, error: "Price is too high" };
}
// Round to 2 decimal places
const sanitized = numPrice.toFixed(2);
return { isValid: true, sanitized };
}
// =============================================================================
// OTP VALIDATION
// =============================================================================
/**
* Validates a 6-digit OTP code.
*/
export function validateOTP(otp: string): ValidationResult {
const sanitized = sanitizeText(otp).replace(/\D/g, "");
if (!sanitized) {
return { isValid: false, error: "OTP code is required" };
}
if (sanitized.length !== 6) {
return { isValid: false, error: "OTP must be 6 digits" };
}
return { isValid: true, sanitized };
}
// =============================================================================
// STELLAR ADDRESS VALIDATION
// =============================================================================
/**
* Validates a Stellar public key (G address).
*/
export function validateStellarAddress(address: string): ValidationResult {
const sanitized = sanitizeText(address).toUpperCase();
if (!sanitized) {
return { isValid: false, error: "Stellar address is required" };
}
// Stellar public keys start with G and are 56 characters (base32 encoded)
const stellarRegex = /^G[A-Z2-7]{55}$/;
if (!stellarRegex.test(sanitized)) {
return { isValid: false, error: "Please enter a valid Stellar address" };
}
return { isValid: true, sanitized };
}
// =============================================================================
// CREDIT CARD VALIDATION
// =============================================================================
/**
* Validates a credit card number using the Luhn algorithm.
*/
export function validateCardNumber(cardNumber: string, strictLength = true): ValidationResult {
const sanitized = sanitizeText(cardNumber).replace(/\s+/g, "");
if (!sanitized) {
return { isValid: false, error: "Card number is required" };
}
const lengthRegex = strictLength ? /^\d{13,19}$/ : /^\d{8,19}$/;
if (!lengthRegex.test(sanitized)) {
return { isValid: false, error: "Please enter a valid card number" };
}
// Luhn algorithm check
let sum = 0;
let shouldDouble = false;
for (let i = sanitized.length - 1; i >= 0; i--) {
let digit = parseInt(sanitized.charAt(i), 10);
if (shouldDouble) {
digit *= 2;
if (digit > 9) digit -= 9;
}
sum += digit;
shouldDouble = !shouldDouble;
}
if (sum % 10 !== 0) {
return { isValid: false, error: "Please enter a valid card number" };
}
return { isValid: true, sanitized };
}
/**
* Validates credit card expiry date in MM/YY or MM/YYYY format.
*/
export function validateCardExpiry(expiry: string): ValidationResult {
const sanitized = sanitizeText(expiry).trim();
if (!sanitized) {
return { isValid: false, error: "Expiry date is required" };
}
const match = sanitized.match(/^(0[1-9]|1[0-2])\/(\d{2}|\d{4})$/);
if (!match) {
return { isValid: false, error: "Please enter a valid expiry date (MM/YY)" };
}
const month = parseInt(match[1], 10);
let year = parseInt(match[2], 10);
if (match[2].length === 2) {
year += 2000;
}
const now = new Date();
const currentYear = now.getFullYear();
const currentMonth = now.getMonth() + 1;
if (year < currentYear || (year === currentYear && month < currentMonth)) {
return { isValid: false, error: "Card has expired" };
}
// Cap maximum future expiry (e.g. 20 years)
if (year > currentYear + 20) {
return { isValid: false, error: "Please enter a valid expiry date" };
}
const formattedMonth = month.toString().padStart(2, "0");
const formattedYear = (year % 100).toString().padStart(2, "0");
return { isValid: true, sanitized: `${formattedMonth}/${formattedYear}` };
}
/**
* Validates card CVV/CVC code (3 or 4 digits).
*/
export function validateCardCVV(cvv: string): ValidationResult {
const sanitized = sanitizeText(cvv).trim();
if (!sanitized) {
return { isValid: false, error: "CVV is required" };
}
if (!/^\d{3,4}$/.test(sanitized)) {
return { isValid: false, error: "CVV must be 3 or 4 digits" };
}
return { isValid: true, sanitized };
}
// =============================================================================
// FORM VALIDATION HELPER
// =============================================================================
/**
* Validates multiple fields at once and returns an errors object.
* Useful for form validation.
*/
export function validateForm(
fields: Record<string, { value: string; validator: (v: string) => ValidationResult }>
): { isValid: boolean; errors: FormErrors; sanitized: Record<string, string> } {
const errors: FormErrors = {};
const sanitized: Record<string, string> = {};
let isValid = true;
for (const [fieldName, { value, validator }] of Object.entries(fields)) {
const result = validator(value);
if (!result.isValid) {
errors[fieldName] = result.error;
isValid = false;
} else {
sanitized[fieldName] = result.sanitized || value;
}
}
return { isValid, errors, sanitized };
}