-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation-utils.js
More file actions
320 lines (266 loc) · 11 KB
/
Copy pathvalidation-utils.js
File metadata and controls
320 lines (266 loc) · 11 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
/**
* Validation Utilities Module
* Centralized input validation and error handling for StepSyncAI
*/
const fs = require('fs');
class ValidationUtils {
/**
* Validates and parses an integer with bounds checking
* @param {string|number} value - The value to parse
* @param {Object} options - Validation options
* @param {number} options.min - Minimum allowed value (inclusive)
* @param {number} options.max - Maximum allowed value (inclusive)
* @param {number} options.default - Default value if parsing fails
* @param {string} options.fieldName - Field name for error messages
* @returns {number|null} Parsed integer or null if invalid
*/
static parseInteger(value, options = {}) {
const { min = -Infinity, max = Infinity, default: defaultValue = null, fieldName = 'value' } = options;
if (value === undefined || value === null || value === '') {
return defaultValue;
}
const parsed = parseInt(value, 10);
if (isNaN(parsed)) {
console.error(`❌ Invalid ${fieldName}: "${value}" is not a valid number`);
return defaultValue;
}
if (parsed < min) {
console.error(`❌ Invalid ${fieldName}: ${parsed} is below minimum allowed value (${min})`);
return defaultValue;
}
if (parsed > max) {
console.error(`❌ Invalid ${fieldName}: ${parsed} exceeds maximum allowed value (${max})`);
return defaultValue;
}
return parsed;
}
/**
* Validates and parses a float with bounds checking
* @param {string|number} value - The value to parse
* @param {Object} options - Validation options
* @returns {number|null} Parsed float or null if invalid
*/
static parseFloat(value, options = {}) {
const { min = -Infinity, max = Infinity, default: defaultValue = null, fieldName = 'value' } = options;
if (value === undefined || value === null || value === '') {
return defaultValue;
}
const parsed = parseFloat(value);
if (isNaN(parsed)) {
console.error(`❌ Invalid ${fieldName}: "${value}" is not a valid number`);
return defaultValue;
}
if (parsed < min) {
console.error(`❌ Invalid ${fieldName}: ${parsed} is below minimum allowed value (${min})`);
return defaultValue;
}
if (parsed > max) {
console.error(`❌ Invalid ${fieldName}: ${parsed} exceeds maximum allowed value (${max})`);
return defaultValue;
}
return parsed;
}
/**
* Validates a date string and returns a Date object
* @param {string} dateString - Date string to validate
* @param {Object} options - Validation options
* @param {Date} options.minDate - Minimum allowed date
* @param {Date} options.maxDate - Maximum allowed date (default: today)
* @param {string} options.fieldName - Field name for error messages
* @returns {Date|null} Valid Date object or null
*/
static parseDate(dateString, options = {}) {
const { minDate = new Date('2020-01-01'), maxDate = new Date(), fieldName = 'date' } = options;
if (!dateString || typeof dateString !== 'string') {
console.error(`❌ Invalid ${fieldName}: Date string is required`);
return null;
}
const date = new Date(dateString);
if (isNaN(date.getTime())) {
console.error(`❌ Invalid ${fieldName}: "${dateString}" is not a valid date format`);
return null;
}
if (date < minDate) {
console.error(`❌ Invalid ${fieldName}: ${dateString} is before ${minDate.toISOString().split('T')[0]}`);
return null;
}
if (date > maxDate) {
console.error(`❌ Invalid ${fieldName}: ${dateString} is in the future`);
return null;
}
return date;
}
/**
* Validates a time string in HH:MM format
* @param {string} timeString - Time string to validate
* @param {string} fieldName - Field name for error messages
* @returns {boolean} True if valid, false otherwise
*/
static validateTime(timeString, fieldName = 'time') {
if (!timeString || typeof timeString !== 'string') {
console.error(`❌ Invalid ${fieldName}: Time string is required`);
return false;
}
const timeRegex = /^([0-1]?[0-9]|2[0-3]):([0-5][0-9])$/;
if (!timeRegex.test(timeString)) {
console.error(`❌ Invalid ${fieldName}: "${timeString}" must be in HH:MM format (e.g., 09:30 or 14:45)`);
return false;
}
return true;
}
/**
* Validates a non-empty string
* @param {string} value - String to validate
* @param {Object} options - Validation options
* @param {number} options.minLength - Minimum length
* @param {number} options.maxLength - Maximum length
* @param {string} options.fieldName - Field name for error messages
* @returns {string|null} Trimmed string or null if invalid
*/
static validateString(value, options = {}) {
const { minLength = 1, maxLength = 10000, fieldName = 'input' } = options;
if (value === undefined || value === null) {
console.error(`❌ Invalid ${fieldName}: Value is required`);
return null;
}
if (typeof value !== 'string') {
value = String(value);
}
const trimmed = value.trim();
if (trimmed.length < minLength) {
console.error(`❌ Invalid ${fieldName}: Must be at least ${minLength} character(s)`);
return null;
}
if (trimmed.length > maxLength) {
console.error(`❌ Invalid ${fieldName}: Must not exceed ${maxLength} characters`);
return null;
}
return trimmed;
}
/**
* Safely reads a JSON file with error handling
* @param {string} filename - Path to JSON file
* @param {Object} options - Options
* @param {number} options.maxSizeMB - Maximum file size in MB
* @returns {Object|null} Parsed JSON or null if error
*/
static readJSONFile(filename, options = {}) {
const { maxSizeMB = 10 } = options;
if (!filename || typeof filename !== 'string') {
console.error('\n❌ Invalid filename: Path is required\n');
return null;
}
// Check if file exists
if (!fs.existsSync(filename)) {
console.error(`\n❌ File not found: ${filename}\n`);
console.log('💡 Tip: Check the file path and try again\n');
return null;
}
try {
// Check file size
const stats = fs.statSync(filename);
const fileSizeMB = stats.size / (1024 * 1024);
if (fileSizeMB > maxSizeMB) {
console.error(`\n❌ File too large: ${fileSizeMB.toFixed(2)}MB (max: ${maxSizeMB}MB)\n`);
return null;
}
// Read file
const rawData = fs.readFileSync(filename, 'utf8');
// Parse JSON
const data = JSON.parse(rawData);
return data;
} catch (error) {
if (error.code === 'EACCES') {
console.error(`\n❌ Permission denied: Cannot read ${filename}\n`);
console.log('💡 Tip: Check file permissions\n');
} else if (error instanceof SyntaxError) {
console.error(`\n❌ Invalid JSON format in ${filename}\n`);
console.log('💡 Tip: Validate your JSON file at jsonlint.com\n');
} else if (error.code === 'ENOENT') {
console.error(`\n❌ File not found: ${filename}\n`);
} else {
console.error(`\n❌ Error reading file: ${error.message}\n`);
}
return null;
}
}
/**
* Safely writes a JSON file with error handling
* @param {string} filename - Path to JSON file
* @param {Object} data - Data to write
* @returns {boolean} True if successful, false otherwise
*/
static writeJSONFile(filename, data) {
if (!filename || typeof filename !== 'string') {
console.error('\n❌ Invalid filename: Path is required\n');
return false;
}
if (!data || typeof data !== 'object') {
console.error('\n❌ Invalid data: Object is required\n');
return false;
}
try {
const jsonString = JSON.stringify(data, null, 2);
fs.writeFileSync(filename, jsonString, 'utf8');
return true;
} catch (error) {
if (error.code === 'EACCES') {
console.error(`\n❌ Permission denied: Cannot write to ${filename}\n`);
console.log('💡 Tip: Check file and directory permissions\n');
} else if (error.code === 'ENOSPC') {
console.error(`\n❌ Disk full: Not enough space to write ${filename}\n`);
console.log('💡 Tip: Free up disk space and try again\n');
} else {
console.error(`\n❌ Error writing file: ${error.message}\n`);
}
return false;
}
}
/**
* Validates imported wellness data structure
* @param {Object} data - Imported data to validate
* @returns {Object} Validation result with isValid and errors array
*/
static validateImportedData(data) {
const errors = [];
if (!data || typeof data !== 'object') {
errors.push('Data must be a valid object');
return { isValid: false, errors };
}
// Check required top-level properties
if (!data.exportInfo) {
errors.push('Missing required field: exportInfo');
} else {
if (!data.exportInfo.version) {
errors.push('Missing exportInfo.version');
}
if (!data.exportInfo.timestamp) {
errors.push('Missing exportInfo.timestamp');
}
}
if (!data.dailyRecords || !Array.isArray(data.dailyRecords)) {
errors.push('Missing or invalid dailyRecords array');
} else {
// Validate sample of records (check first 5)
const sampleSize = Math.min(5, data.dailyRecords.length);
for (let i = 0; i < sampleSize; i++) {
const record = data.dailyRecords[i];
if (!record.date) {
errors.push(`Record ${i}: Missing date field`);
}
// Validate timestamps if present
if (record.mood?.timestamp) {
const date = new Date(record.mood.timestamp);
if (isNaN(date.getTime())) {
errors.push(`Record ${i}: Invalid mood timestamp`);
}
}
}
}
return {
isValid: errors.length === 0,
errors
};
}
}
module.exports = ValidationUtils;