-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcrossSearch.ts
More file actions
executable file
·67 lines (62 loc) · 2.3 KB
/
Copy pathcrossSearch.ts
File metadata and controls
executable file
·67 lines (62 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { ParsedCrossSearchRouteParameters } from '../types'
import { Validators, validator, ValidationError } from './validator'
const columnFilterCheck = (columnFilters: unknown) => {
if (!Array.isArray(columnFilters)) return 'Column filters is not an array'
for (const filter of columnFilters as Record<string, unknown>[]) {
if (!('id' in filter) || typeof filter.id !== 'string' || filter.id === '') {
return 'Invalid or missing id field in filter'
}
if (!('value' in filter) || typeof filter.value !== 'string' || filter.value === '') {
return 'Invalid or missing value field in filter'
}
}
return null as ValidationError
}
const sortingCheck = (sorting: unknown) => {
if (!Array.isArray(sorting)) return 'Sorting is not an array'
for (const sort of sorting as Record<string, unknown>[]) {
if (!('id' in sort) || typeof sort.id !== 'string' || sort.id === '') {
return 'Invalid or missing id field in sort object'
}
if (!('desc' in sort)) {
return `Invalid or missing desc field in sort object`
}
if (typeof sort.desc !== 'boolean') {
return 'desc field in sort object must be either true or false (boolean)'
}
}
return null as ValidationError
}
export const validateCrossSearchRouteParams = (
parameters: ParsedCrossSearchRouteParameters,
fieldName: keyof ParsedCrossSearchRouteParameters
) => {
const validators: Validators<Partial<ParsedCrossSearchRouteParameters>> = {
limit: {
name: 'Limit',
required: () => {
// TODO: change this and offset to not use the parameters object, might not be possible without changing the way validators work
if (typeof parameters.limit !== 'undefined' && typeof parameters.limit !== 'number')
return 'Limit must be undefined or a number'
return
},
},
offset: {
name: 'Offset',
required: () => {
if (typeof parameters.offset !== 'undefined' && typeof parameters.offset !== 'number')
return 'Offset must be undefined or a number'
return
},
},
columnFilters: {
name: 'Column Filters',
miscArray: columnFilterCheck,
},
sorting: {
name: 'Sorting',
miscArray: sortingCheck,
},
}
return validator<ParsedCrossSearchRouteParameters>(validators, parameters, fieldName)
}