Files
adopt-a-street/backend/middleware/validators/rewardValidator.js
William Valentin 9ac21fca72 feat: migrate Event and Reward models from MongoDB to CouchDB
- Replace Event model with CouchDB version using couchdbService
- Replace Reward model with CouchDB version using couchdbService
- Update event and reward routes to use new model interfaces
- Handle participant management with embedded user data
- Maintain status transitions for events (upcoming, ongoing, completed, cancelled)
- Preserve catalog functionality and premium vs regular rewards
- Update validators to accept CouchDB document IDs
- Add rewards design document to couchdbService
- Update test helpers for new model structure
- Initialize CouchDB alongside MongoDB in server.js for backward compatibility
- Fix linting issues in migrated routes

🤖 Generated with [AI Assistant]

Co-Authored-By: AI Assistant <noreply@ai-assistant.com>
2025-11-01 13:26:00 -07:00

62 lines
1.4 KiB
JavaScript

const { body, param, validationResult } = require("express-validator");
const validate = (req, res, next) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({
success: false,
errors: errors.array().map((err) => ({
field: err.path,
message: err.msg,
})),
});
}
next();
};
/**
* Create reward validation
*/
const createRewardValidation = [
body("name")
.trim()
.notEmpty()
.withMessage("Reward name is required")
.isLength({ min: 2, max: 100 })
.withMessage("Name must be between 2 and 100 characters"),
body("description")
.trim()
.notEmpty()
.withMessage("Reward description is required")
.isLength({ min: 10, max: 500 })
.withMessage("Description must be between 10 and 500 characters"),
body("cost")
.notEmpty()
.withMessage("Cost is required")
.isInt({ min: 1, max: 100000 })
.withMessage("Cost must be a positive integer between 1 and 100000"),
body("isPremium")
.optional()
.isBoolean()
.withMessage("isPremium must be a boolean"),
validate,
];
/**
* Reward ID validation
*/
const rewardIdValidation = [
param("id")
.notEmpty()
.withMessage("Reward ID is required")
.matches(/^(reward_[a-zA-Z0-9]+|[0-9a-fA-F]{24})$/)
.withMessage("Invalid reward ID format"),
validate,
];
module.exports = {
createRewardValidation,
rewardIdValidation,
validate,
};