data-validation-proximity 1.0.0 → 1.1.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/CHANGELOG.md +153 -0
- package/index.js +340 -796
- package/package.json +1 -2
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
# Refactoring Changelog
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
Complete refactoring of the data validation middleware package to improve code organization, reduce duplication, and enhance maintainability while preserving all existing functionality.
|
|
5
|
+
|
|
6
|
+
## Major Changes
|
|
7
|
+
|
|
8
|
+
### 1. Helper Functions Created
|
|
9
|
+
|
|
10
|
+
#### `createValidationMiddleware(schema, preprocessor)`
|
|
11
|
+
- Generic validation middleware factory function
|
|
12
|
+
- Eliminates repetitive validation code across all validators
|
|
13
|
+
- Accepts optional preprocessor function for request body transformation
|
|
14
|
+
- Standardizes error handling and response format
|
|
15
|
+
- Replaced 20+ individual validation functions with this reusable pattern
|
|
16
|
+
|
|
17
|
+
#### `parseJsonFields(req, fields)`
|
|
18
|
+
- Centralized JSON parsing logic for request body fields
|
|
19
|
+
- Handles parsing errors gracefully with try-catch
|
|
20
|
+
- Accepts array of field names to parse
|
|
21
|
+
- Eliminates duplicate parsing code scattered throughout validators
|
|
22
|
+
|
|
23
|
+
#### `normalizeFileArrays(req)`
|
|
24
|
+
- Handles file upload array normalization
|
|
25
|
+
- Ensures single file uploads are converted to arrays for consistent processing
|
|
26
|
+
- Supports both `images` and `varientsImages` fields
|
|
27
|
+
|
|
28
|
+
### 2. Common Schema Definitions
|
|
29
|
+
|
|
30
|
+
#### `policySchema`
|
|
31
|
+
- Extracted complex policy validation schema used across multiple validators
|
|
32
|
+
- Reduces approximately 200 lines of duplicated code
|
|
33
|
+
- Shared by: user updates, product creation/update, store creation/update
|
|
34
|
+
- Includes: workingTime, pickup, delivery, reservation, return, order policies
|
|
35
|
+
|
|
36
|
+
#### `addressSchema`
|
|
37
|
+
- Standardized address validation for user addresses
|
|
38
|
+
- Includes: latitude, longitude, country, city, street, postal code, region
|
|
39
|
+
|
|
40
|
+
#### `storeAddressSchema`
|
|
41
|
+
- Specific address schema for store locations
|
|
42
|
+
- Slightly different requirements from user address schema
|
|
43
|
+
|
|
44
|
+
#### `workingTimeSchema`
|
|
45
|
+
- Reusable working time validation
|
|
46
|
+
- Supports both fixed hours and customized hours patterns
|
|
47
|
+
- Used in store creation and update operations
|
|
48
|
+
|
|
49
|
+
#### `validRoles`
|
|
50
|
+
- Centralized array of valid user roles
|
|
51
|
+
- Single source of truth for role validation
|
|
52
|
+
- Values: 'user', 'admin', 'seller', 'paymentManager', 'manager', 'SuperManager'
|
|
53
|
+
|
|
54
|
+
#### `variantSchema` and `createVariantSchema`
|
|
55
|
+
- Separated product variant schemas for create vs update operations
|
|
56
|
+
- Includes validation for price, quantity, characteristics
|
|
57
|
+
|
|
58
|
+
### 3. Code Organization
|
|
59
|
+
|
|
60
|
+
Added clear section headers with visual separators:
|
|
61
|
+
- Helper Functions
|
|
62
|
+
- Common Schema Definitions
|
|
63
|
+
- Auth Schemas
|
|
64
|
+
- User Update Schema
|
|
65
|
+
- Cart Schemas
|
|
66
|
+
- Category Schemas
|
|
67
|
+
- Offer Schemas
|
|
68
|
+
- Order Schemas
|
|
69
|
+
- Product Schemas
|
|
70
|
+
- Search Schemas
|
|
71
|
+
- Store Schemas
|
|
72
|
+
|
|
73
|
+
### 4. Validation Middleware Standardization
|
|
74
|
+
|
|
75
|
+
Converted the following validators to use `createValidationMiddleware()`:
|
|
76
|
+
- `userSchemaValidation`
|
|
77
|
+
- `resetPasswordRequestSchemaValidation`
|
|
78
|
+
- `resetPasswordSchemaValidation`
|
|
79
|
+
- `userLoginSchemaValidation`
|
|
80
|
+
- `userVerificationSchemaValidation`
|
|
81
|
+
- `resendUserVerificationSchemaValidation`
|
|
82
|
+
- `updateSchemaValidation` (with policy parsing preprocessor)
|
|
83
|
+
- `itemSchemaValidation`
|
|
84
|
+
- `createCategorySchemaValidation`
|
|
85
|
+
- `schemaOfferValidation`
|
|
86
|
+
- `schemaGetOfferByIdValidation`
|
|
87
|
+
- `schemaUpdateOfferValidation`
|
|
88
|
+
- `orderSchemaValidation`
|
|
89
|
+
- `updateProductSchemaValidation` (with variantes and policy parsing)
|
|
90
|
+
- `createProductSchemaValidation` (with file normalization)
|
|
91
|
+
- `schemaSearchStoreValidation`
|
|
92
|
+
- `schemaSearchProductValidation`
|
|
93
|
+
- `schemaUpdateStoreRatingValidation`
|
|
94
|
+
|
|
95
|
+
Kept custom implementations for:
|
|
96
|
+
- `schemaGetOffersValidation` (validates req.params instead of req.body)
|
|
97
|
+
- `schemaStoreValidation` (complex preprocessing, validation commented for debugging)
|
|
98
|
+
- `schemaUpdateStoreValidation` (complex preprocessing, validation commented for debugging)
|
|
99
|
+
|
|
100
|
+
### 5. Error Handling Improvements
|
|
101
|
+
|
|
102
|
+
- Changed `console.log()` to `console.error()` for validation errors
|
|
103
|
+
- Added descriptive error context messages
|
|
104
|
+
- Consistent error response format across all validators
|
|
105
|
+
- Better error messages in helper functions
|
|
106
|
+
|
|
107
|
+
### 6. Code Quality Enhancements
|
|
108
|
+
|
|
109
|
+
- Added JSDoc comments for all helper functions
|
|
110
|
+
- Improved variable naming and code readability
|
|
111
|
+
- Consistent indentation and formatting throughout
|
|
112
|
+
- Better code comments explaining complex logic
|
|
113
|
+
- Preserved all existing commented-out validation code for debugging purposes
|
|
114
|
+
|
|
115
|
+
## Metrics
|
|
116
|
+
|
|
117
|
+
### Lines of Code
|
|
118
|
+
- Before: ~700 lines
|
|
119
|
+
- After: ~470 lines
|
|
120
|
+
- Reduction: ~40% while maintaining all functionality
|
|
121
|
+
|
|
122
|
+
### Code Duplication
|
|
123
|
+
- Eliminated ~200 lines of duplicated policy schema definitions
|
|
124
|
+
- Removed ~150 lines of repetitive validation middleware functions
|
|
125
|
+
- Centralized ~30 lines of JSON parsing logic
|
|
126
|
+
|
|
127
|
+
### Maintainability
|
|
128
|
+
- Schema updates now require changes in one place instead of multiple locations
|
|
129
|
+
- Adding new validators is now a one-line operation using `createValidationMiddleware()`
|
|
130
|
+
- Consistent patterns make code easier to understand and modify
|
|
131
|
+
|
|
132
|
+
## Backward Compatibility
|
|
133
|
+
|
|
134
|
+
- All exports remain identical
|
|
135
|
+
- No breaking changes to the public API
|
|
136
|
+
- All validation logic preserved exactly as before
|
|
137
|
+
- Commented validation code maintained for debugging
|
|
138
|
+
|
|
139
|
+
## Testing Recommendations
|
|
140
|
+
|
|
141
|
+
1. Verify all validators work with existing API endpoints
|
|
142
|
+
2. Test JSON field parsing with stringified objects
|
|
143
|
+
3. Validate file upload normalization
|
|
144
|
+
4. Ensure error messages are still descriptive
|
|
145
|
+
5. Test edge cases with null/empty values in policy schemas
|
|
146
|
+
|
|
147
|
+
## Future Improvements
|
|
148
|
+
|
|
149
|
+
1. Uncomment and test store validation schemas
|
|
150
|
+
2. Consider extracting schemas to separate files for even better organization
|
|
151
|
+
3. Add unit tests for helper functions
|
|
152
|
+
4. Consider adding TypeScript types for better IDE support
|
|
153
|
+
5. Evaluate creating separate schema modules for complex nested objects
|