-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidator.go
More file actions
106 lines (88 loc) · 2.16 KB
/
Copy pathvalidator.go
File metadata and controls
106 lines (88 loc) · 2.16 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package tools
import (
"fmt"
"reflect"
)
func ValidateParameters(params map[string]interface{}, schema map[string]interface{}) error {
if schema == nil {
return nil
}
schemaType, ok := schema["type"].(string)
if !ok || schemaType != "object" {
return nil
}
properties, ok := schema["properties"].(map[string]interface{})
if !ok {
return nil
}
required, _ := schema["required"].([]interface{})
requiredFields := make(map[string]bool)
for _, field := range required {
if fieldName, ok := field.(string); ok {
requiredFields[fieldName] = true
}
}
for fieldName := range requiredFields {
if _, exists := params[fieldName]; !exists {
return fmt.Errorf("required parameter missing: %s", fieldName)
}
}
for paramName, paramValue := range params {
propSchema, exists := properties[paramName]
if !exists {
continue
}
propMap, ok := propSchema.(map[string]interface{})
if !ok {
continue
}
expectedType, ok := propMap["type"].(string)
if !ok {
continue
}
if err := validateType(paramName, paramValue, expectedType); err != nil {
return err
}
}
return nil
}
func validateType(paramName string, value interface{}, expectedType string) error {
if value == nil {
return nil
}
actualType := getJSONType(value)
if expectedType == "integer" && actualType == "number" {
if _, ok := value.(float64); ok {
if float64(int(value.(float64))) == value.(float64) {
return nil
}
}
}
if actualType != expectedType {
return fmt.Errorf("parameter %s: expected type %s, got %s", paramName, expectedType, actualType)
}
return nil
}
func getJSONType(value interface{}) string {
if value == nil {
return "null"
}
switch reflect.TypeOf(value).Kind() {
case reflect.Bool:
return "boolean"
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return "integer"
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return "integer"
case reflect.Float32, reflect.Float64:
return "number"
case reflect.String:
return "string"
case reflect.Slice, reflect.Array:
return "array"
case reflect.Map, reflect.Struct:
return "object"
default:
return "unknown"
}
}