1
0
mirror of https://github.com/alice-lg/alice-lg.git synced 2024-05-11 05:55:03 +00:00
alice-lg-alice-lg/pkg/http/api_validators.go

45 lines
908 B
Go
Raw Normal View History

2021-10-22 22:40:03 +02:00
package http
2017-06-28 12:42:28 +02:00
import (
"fmt"
"net/http"
)
// Helper: Validate source Id
2021-03-22 16:50:08 +01:00
func validateSourceID(id string) (string, error) {
2019-01-28 09:19:32 +01:00
if len(id) > 42 {
2021-11-15 21:43:22 +01:00
return "unknown", fmt.Errorf("source ID too long with length: %d", len(id))
2017-06-28 12:42:28 +02:00
}
2018-12-09 17:31:02 +01:00
return id, nil
2017-06-28 12:42:28 +02:00
}
// Helper: Validate query string
func validateQueryString(req *http.Request, key string) (string, error) {
query := req.URL.Query()
values, ok := query[key]
if !ok {
2021-03-22 16:50:08 +01:00
return "", fmt.Errorf("query param %s is missing", key)
2017-06-28 12:42:28 +02:00
}
if len(values) != 1 {
2021-03-22 16:50:08 +01:00
return "", fmt.Errorf("query param %s is ambigous", key)
2017-06-28 12:42:28 +02:00
}
value := values[0]
if value == "" {
2021-03-22 16:50:08 +01:00
return "", fmt.Errorf("query param %s may not be empty", key)
2017-06-28 12:42:28 +02:00
}
return value, nil
}
// Helper: Validate prefix query
func validatePrefixQuery(value string) (string, error) {
// We should at least provide 2 chars
if len(value) < 2 {
2021-11-15 21:43:22 +01:00
return "", fmt.Errorf("query too short")
2017-06-28 12:42:28 +02:00
}
return value, nil
}