refactor search handler and service into multiple files
This commit is contained in:
parent
b98502284a
commit
1b8ba03c8b
11 changed files with 840 additions and 309 deletions
105
src/services/sigma/utils/search_validation_utils.js
Normal file
105
src/services/sigma/utils/search_validation_utils.js
Normal file
|
@ -0,0 +1,105 @@
|
|||
/**
|
||||
* search_validation_utils.js
|
||||
*
|
||||
* Utility functions for validating search parameters
|
||||
*/
|
||||
const logger = require('../../../utils/logger');
|
||||
const { getFileName } = require('../../../utils/file_utils');
|
||||
const FILE_NAME = getFileName(__filename);
|
||||
|
||||
/**
|
||||
* Validates a search keyword
|
||||
* @param {string} keyword - The keyword to validate
|
||||
* @returns {Object} Validation result object
|
||||
*/
|
||||
function validateKeyword(keyword) {
|
||||
if (!keyword || typeof keyword !== 'string') {
|
||||
logger.warn(`${FILE_NAME}: Cannot search rules: Missing or invalid keyword`);
|
||||
return {
|
||||
isValid: false,
|
||||
message: 'Missing or invalid search keyword'
|
||||
};
|
||||
}
|
||||
|
||||
const trimmedKeyword = keyword.trim();
|
||||
if (trimmedKeyword.length === 0) {
|
||||
logger.warn(`${FILE_NAME}: Cannot search rules: Empty keyword after trimming`);
|
||||
return {
|
||||
isValid: false,
|
||||
message: 'Search keyword cannot be empty'
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
isValid: true,
|
||||
trimmedKeyword
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates query string for complex search
|
||||
* @param {string} queryString - The query string to validate
|
||||
* @returns {Object} Validation result object
|
||||
*/
|
||||
function validateQueryString(queryString) {
|
||||
if (!queryString || typeof queryString !== 'string') {
|
||||
logger.warn(`${FILE_NAME}: Cannot perform complex search: Missing or invalid query string`);
|
||||
return {
|
||||
isValid: false,
|
||||
message: 'Missing or invalid complex query'
|
||||
};
|
||||
}
|
||||
|
||||
const trimmedQuery = queryString.trim();
|
||||
if (trimmedQuery.length === 0) {
|
||||
logger.warn(`${FILE_NAME}: Cannot perform complex search: Empty query after trimming`);
|
||||
return {
|
||||
isValid: false,
|
||||
message: 'Complex query cannot be empty'
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
isValid: true,
|
||||
trimmedQuery
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates pagination parameters
|
||||
* @param {number} page - Page number (1-based)
|
||||
* @param {number} pageSize - Number of results per page
|
||||
* @param {number} maxPageSize - Maximum allowed page size
|
||||
* @returns {Object} Validated pagination parameters
|
||||
*/
|
||||
function validatePagination(page = 1, pageSize = 10, maxPageSize = 100) {
|
||||
let validatedPage = page;
|
||||
let validatedPageSize = pageSize;
|
||||
|
||||
if (typeof validatedPage !== 'number' || validatedPage < 1) {
|
||||
logger.warn(`${FILE_NAME}: Invalid page number: ${page}, defaulting to 1`);
|
||||
validatedPage = 1;
|
||||
}
|
||||
|
||||
if (typeof validatedPageSize !== 'number' || validatedPageSize < 1) {
|
||||
logger.warn(`${FILE_NAME}: Invalid page size: ${pageSize}, defaulting to 10`);
|
||||
validatedPageSize = 10;
|
||||
} else if (validatedPageSize > maxPageSize) {
|
||||
logger.warn(`${FILE_NAME}: Page size ${pageSize} exceeds max ${maxPageSize}, limiting to ${maxPageSize}`);
|
||||
validatedPageSize = maxPageSize;
|
||||
}
|
||||
|
||||
const offset = (validatedPage - 1) * validatedPageSize;
|
||||
|
||||
return {
|
||||
page: validatedPage,
|
||||
pageSize: validatedPageSize,
|
||||
offset
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
validateKeyword,
|
||||
validateQueryString,
|
||||
validatePagination
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue