logingest/utils/validation.js

38 lines
1.1 KiB
JavaScript
Raw Permalink Normal View History

2025-06-15 14:47:15 +00:00
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
2025-06-18 12:05:24 +00:00
if (!data.body || typeof data.body !== 'string') {
errors.push('body field is required and must be a string');
} else if (data.body.trim().length === 0) {
errors.push('body field cannot be empty');
2025-06-15 14:47:15 +00:00
}
// 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
};
}