index/functions/validateSortOrderDirection.md
validateSortOrderDirection(
value,fieldName):void
Validates sort direction enum value (ASC or DESC)
Consolidates 7 instances of sort direction validation found in:
- GSwapPoolQueryService.ts (pool listing sort)
- ChatService.ts (message sorting)
- CommentsService.ts (comment thread sorting)
- OverseersService.ts (list sorting)
- BanService.ts (ban list sorting)
- ModeratorInviteService.ts (invite list sorting)
- common.ts (type definitions)
field name
Replaces Pattern
Before (Duplicate across services):
if (sortDir !== 'ASC' && sortDir !== 'DESC') {
throw new ValidationError(`Invalid sort direction: ${sortDir}`);
}
After (Single call):
validateSortOrderDirection(sortDir, 'sortDir');
Parameters
| Parameter | Type | Description |
|---|---|---|
value | string | The sort direction value to validate ('ASC' or 'DESC') |
fieldName | string | Field name for error messages (e.g., 'sortDir', 'sortOrder') |
field name
Returns
void
Throws
If value is not 'ASC' or 'DESC'
Examples
Basic sort direction validation
import { validateSortOrderDirection } from '../utils/validation-patterns';
function listComments(options: { sortDir?: string }) {
if (options.sortDir) {
validateSortOrderDirection(options.sortDir, 'sortDir');
}
// Validation passed - sortDir is either 'ASC' or 'DESC'
}
With service method
import { validateSortOrderDirection } from '../utils/validation-patterns';
async getMessages(tokenName: string, sortDir: string = 'DESC') {
validateSortOrderDirection(sortDir, 'sortDir');
// Fetch and return messages sorted in the specified direction
}
Since
6.25.0