|
| 1 | +/* |
| 2 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | + * file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 5 | + */ |
| 6 | + |
| 7 | +package main |
| 8 | + |
| 9 | +import ( |
| 10 | + "fmt" |
| 11 | + "reflect" |
| 12 | +) |
| 13 | + |
| 14 | +// ValidateActionParams validates that all parameters in ActionParams contain only |
| 15 | +// acceptable JSON types and returns an error if invalid types are found |
| 16 | +func ValidateActionParams(params ActionParams) error { |
| 17 | + for _, param := range params { |
| 18 | + if err := ValidateActionParamValue(param.Field, param.Value); err != nil { |
| 19 | + return err |
| 20 | + } |
| 21 | + } |
| 22 | + return nil |
| 23 | +} |
| 24 | + |
| 25 | +// ValidateActionParamValue validates that a value contains only acceptable JSON types: |
| 26 | +// - Primitives (string, number, boolean, null) |
| 27 | +// - Arrays of any valid JSON values (including nested arrays) |
| 28 | +// - Objects (maps) with any valid JSON values |
| 29 | +func ValidateActionParamValue(field string, value interface{}) error { |
| 30 | + if value == nil { |
| 31 | + return nil |
| 32 | + } |
| 33 | + |
| 34 | + switch v := value.(type) { |
| 35 | + case string, float64, int, int64, float32, bool, uint, uint64, uint32: |
| 36 | + // Basic primitives are valid |
| 37 | + return nil |
| 38 | + |
| 39 | + case []interface{}: |
| 40 | + // Check each element in the array |
| 41 | + for i, element := range v { |
| 42 | + if err := ValidateActionParamValue(fmt.Sprintf("%s[%d]", field, i), element); err != nil { |
| 43 | + return fmt.Errorf("invalid array element at %s: %w", field, err) |
| 44 | + } |
| 45 | + } |
| 46 | + return nil |
| 47 | + |
| 48 | + case map[string]interface{}: |
| 49 | + // Objects (maps) are now valid |
| 50 | + // Check each value in the object |
| 51 | + for key, element := range v { |
| 52 | + if err := ValidateActionParamValue(fmt.Sprintf("%s.%s", field, key), element); err != nil { |
| 53 | + return fmt.Errorf("invalid object property at %s.%s: %w", field, key, err) |
| 54 | + } |
| 55 | + } |
| 56 | + return nil |
| 57 | + |
| 58 | + default: |
| 59 | + // Check for array types and object types using reflection |
| 60 | + val := reflect.ValueOf(value) |
| 61 | + kind := val.Kind() |
| 62 | + |
| 63 | + // Handle arrays/slices |
| 64 | + if kind == reflect.Slice || kind == reflect.Array { |
| 65 | + // For empty arrays, we can't determine element type, so assume it's valid |
| 66 | + if val.Len() == 0 { |
| 67 | + return nil |
| 68 | + } |
| 69 | + |
| 70 | + // Recursively check each element in the array |
| 71 | + for i := 0; i < val.Len(); i++ { |
| 72 | + elemVal := val.Index(i).Interface() |
| 73 | + if err := ValidateActionParamValue(fmt.Sprintf("%s[%d]", field, i), elemVal); err != nil { |
| 74 | + return err |
| 75 | + } |
| 76 | + } |
| 77 | + return nil |
| 78 | + } |
| 79 | + |
| 80 | + // Handle maps/objects |
| 81 | + if kind == reflect.Map { |
| 82 | + // For empty maps, assume it's valid |
| 83 | + if val.Len() == 0 { |
| 84 | + return nil |
| 85 | + } |
| 86 | + |
| 87 | + // Iterate through map keys and validate each value |
| 88 | + for _, key := range val.MapKeys() { |
| 89 | + elemVal := val.MapIndex(key).Interface() |
| 90 | + keyStr := fmt.Sprintf("%v", key.Interface()) |
| 91 | + if err := ValidateActionParamValue(fmt.Sprintf("%s.%s", field, keyStr), elemVal); err != nil { |
| 92 | + return err |
| 93 | + } |
| 94 | + } |
| 95 | + return nil |
| 96 | + } |
| 97 | + |
| 98 | + // If we can't recognize the type as valid, reject it |
| 99 | + return fmt.Errorf("field '%s' has unsupported type %T", field, value) |
| 100 | + } |
| 101 | +} |
0 commit comments