(jsonData: any, allowedSorts: string[], defaultSort: string)
| 217 | } |
| 218 | |
| 219 | export function validateSort(jsonData: any, allowedSorts: string[], defaultSort: string): string | null { |
| 220 | let sort = jsonData.sort ?? defaultSort; |
| 221 | |
| 222 | if (sort === 'no_sort' || isEmpty(sort)) { |
| 223 | sort = null; |
| 224 | } else if (isNotNullish(sort)) { |
| 225 | if (!isStringOfMinLength(sort)) { |
| 226 | throw new JsonHTTPResponseWithMessage('Sort must be a string with at least one character'); |
| 227 | } |
| 228 | |
| 229 | sort = sort.toLowerCase(); |
| 230 | if (!allowedSorts.includes(sort)) { |
| 231 | throw new JsonHTTPResponseWithMessage(`Invalid sort. Must be one of: ${allowedSorts.join(', ')}.`); |
| 232 | } |
| 233 | } else { |
| 234 | sort = defaultSort; |
| 235 | } |
| 236 | return sort; |
| 237 | } |
| 238 | |
| 239 | export function validateResultsRequest(jsonData: any, allowedSorts: string[], allowedViews: string[], defaultSort: string): [any, string | null, string | undefined, number, number | undefined] { |
| 240 | ensureJsonBodyIsDict(jsonData); |
no test coverage detected