logingest/utils/validation.js

38 lines
1.1 KiB
JavaScript

export function validateLogData(data) {
const errors = [];
// Check if data exists
if (!data || typeof data !== 'object') {
errors.push('Request body must be a valid JSON object');
return { isValid: false, errors };
}
// Validate required message field
if (!data.message || typeof data.message !== 'string') {
errors.push('message field is required and must be a string');
} else if (data.message.trim().length === 0) {
errors.push('message field cannot be empty');
}
// Validate optional fields if provided
if (data.project !== undefined && typeof data.project !== 'string') {
errors.push('project field must be a string if provided');
}
if (data.type !== undefined && typeof data.type !== 'string') {
errors.push('type field must be a string if provided');
}
if (data.owner !== undefined && typeof data.owner !== 'string') {
errors.push('owner field must be a string if provided');
}
if (data.avatar_src !== undefined && typeof data.avatar_src !== 'string') {
errors.push('avatar_src field must be a string if provided');
}
return {
isValid: errors.length === 0,
errors
};
}