60 lines
1.3 KiB
JavaScript
60 lines
1.3 KiB
JavaScript
|
|
const Joi = require('joi');
|
||
|
|
|
||
|
|
const createTokenSchema = Joi.object({
|
||
|
|
company_id: Joi.number()
|
||
|
|
.integer()
|
||
|
|
.positive()
|
||
|
|
.required()
|
||
|
|
.messages({
|
||
|
|
'number.base': 'Company ID must be a number',
|
||
|
|
'any.required': 'Company ID is required',
|
||
|
|
}),
|
||
|
|
target_email: Joi.string()
|
||
|
|
.email()
|
||
|
|
.required()
|
||
|
|
.messages({
|
||
|
|
'string.email': 'Valid email is required',
|
||
|
|
'any.required': 'Target email is required',
|
||
|
|
}),
|
||
|
|
employee_name: Joi.string()
|
||
|
|
.max(255)
|
||
|
|
.allow(null, '')
|
||
|
|
.optional(),
|
||
|
|
template_type: Joi.string()
|
||
|
|
.max(50)
|
||
|
|
.default('bank')
|
||
|
|
.required()
|
||
|
|
.messages({
|
||
|
|
'any.required': 'Template type is required',
|
||
|
|
}),
|
||
|
|
});
|
||
|
|
|
||
|
|
const updateTokenSchema = Joi.object({
|
||
|
|
notes: Joi.string()
|
||
|
|
.max(1000)
|
||
|
|
.allow(null, '')
|
||
|
|
.optional(),
|
||
|
|
});
|
||
|
|
|
||
|
|
const validate = (schema) => {
|
||
|
|
return (req, res, next) => {
|
||
|
|
const { error } = schema.validate(req.body, { abortEarly: false });
|
||
|
|
|
||
|
|
if (error) {
|
||
|
|
return res.status(400).json({
|
||
|
|
success: false,
|
||
|
|
error: 'Validation error',
|
||
|
|
details: error.details.map(d => d.message),
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
next();
|
||
|
|
};
|
||
|
|
};
|
||
|
|
|
||
|
|
module.exports = {
|
||
|
|
validateCreateToken: validate(createTokenSchema),
|
||
|
|
validateUpdateToken: validate(updateTokenSchema),
|
||
|
|
};
|
||
|
|
|