@@ -15,6 +15,24 @@ import (
1515 "github.com/open-edge-platform/cluster-manager/v2/pkg/api"
1616)
1717
18+ var (
19+ // validOrderByFields is a map of valid order by fields
20+ validOrderByFields = map [string ]bool {
21+ "name" : true ,
22+ "kubernetesVersion" : true ,
23+ "providerStatus" : true ,
24+ "lifecyclePhase" : true ,
25+ }
26+
27+ // validFilterFields is a map of valid filter fields
28+ validFilterFields = map [string ]bool {
29+ "name" : true ,
30+ "kubernetesVersion" : true ,
31+ "providerStatus" : true ,
32+ "lifecyclePhase" : true ,
33+ }
34+ )
35+
1836type Filter struct {
1937 Name string
2038 Value string
@@ -245,44 +263,30 @@ func ValidateParams(params any) (pageSize, offset *int, orderBy, filter *string,
245263 return nil , nil , nil , nil , fmt .Errorf ("invalid offset: must be non-negative" )
246264 }
247265
248- if orderBy != nil {
249- validOrderByFields := map [string ]bool {
250- "name" : true ,
251- "kubernetesVersion" : true ,
252- "providerStatus" : true ,
253- "lifecyclePhase" : true ,
254- }
255- orderByParts := strings .Split (* orderBy , " " )
256- if len (orderByParts ) == 1 {
257- orderBy = convert .Ptr (orderByParts [0 ] + " asc" )
258- } else if len (orderByParts ) == 2 {
259- if ! validOrderByFields [orderByParts [0 ]] || (orderByParts [1 ] != "asc" && orderByParts [1 ] != "desc" ) {
260- return nil , nil , nil , nil , fmt .Errorf ("invalid orderBy field" )
261- }
262- } else if * orderBy == "" {
266+ if orderBy == nil || * orderBy == "" {
267+ return nil , nil , nil , nil , fmt .Errorf ("invalid orderBy: cannot be empty" )
268+ }
269+
270+ if filter == nil || * filter == "" {
271+ return nil , nil , nil , nil , fmt .Errorf ("invalid filter: cannot be empty" )
272+ }
273+
274+ orderByParts := strings .Split (* orderBy , " " )
275+ if len (orderByParts ) == 1 {
276+ orderBy = convert .Ptr (orderByParts [0 ] + " asc" )
277+ } else if len (orderByParts ) == 2 {
278+ if ! validOrderByFields [orderByParts [0 ]] || (orderByParts [1 ] != "asc" && orderByParts [1 ] != "desc" ) {
263279 return nil , nil , nil , nil , fmt .Errorf ("invalid orderBy field" )
264280 }
265281 }
266282
267- if filter != nil {
268- if * filter == "" {
269- return nil , nil , nil , nil , fmt .Errorf ("invalid filter: cannot be empty" )
270- }
271- validFilterFields := map [string ]bool {
272- "name" : true ,
273- "kubernetesVersion" : true ,
274- "providerStatus" : true ,
275- "lifecyclePhase" : true ,
276- "version" : true ,
277- }
278- filterParts := strings .FieldsFunc (* filter , func (r rune ) bool {
279- return r == ' ' || r == 'O' || r == 'R' || r == 'A' || r == 'N' || r == 'D'
280- })
281- for _ , part := range filterParts {
282- subParts := strings .Split (part , "=" )
283- if len (subParts ) != 2 || ! validFilterFields [subParts [0 ]] {
284- return nil , nil , nil , nil , fmt .Errorf ("invalid filter field" )
285- }
283+ filterParts := strings .FieldsFunc (* filter , func (r rune ) bool {
284+ return r == ' ' || r == 'O' || r == 'R' || r == 'A' || r == 'N' || r == 'D'
285+ })
286+ for _ , part := range filterParts {
287+ subParts := strings .Split (part , "=" )
288+ if len (subParts ) != 2 || ! validFilterFields [subParts [0 ]] {
289+ return nil , nil , nil , nil , fmt .Errorf ("invalid filter field" )
286290 }
287291 }
288292
0 commit comments