1
0
Fork 0
mirror of https://github.com/alice-lg/birdwatcher.git synced 2025-03-09 00:00:05 +01:00
birdwatcher/endpoints/filter.go

56 lines
1.2 KiB
Go
Raw Permalink Normal View History

2016-12-06 13:02:12 +01:00
package endpoints
import (
"fmt"
)
/*
* Parameter / Request Validation
*/
// Check if the value is not longer than a given length
func ValidateLength(value string, maxLength int) error {
if len(value) > maxLength {
return fmt.Errorf("Provided param value is too long.")
}
return nil
}
func ValidateCharset(value string, alphabet string) error {
2016-12-06 14:26:47 +01:00
for _, check := range value {
2016-12-06 13:02:12 +01:00
ok := false
2016-12-06 14:26:47 +01:00
for _, char := range alphabet {
if char == check {
2016-12-06 13:02:12 +01:00
ok = true
break
}
}
if !ok {
return fmt.Errorf("Invalid character in param value")
}
}
return nil
}
2016-12-08 11:09:25 +01:00
func ValidateLengthAndCharset(value string, maxLength int, alphabet string) (string, error) {
2016-12-06 13:02:12 +01:00
// Check length
2016-12-08 11:09:25 +01:00
if err := ValidateLength(value, maxLength); err != nil {
2016-12-06 13:02:12 +01:00
return "", err
}
// Check input
2016-12-08 11:09:25 +01:00
if err := ValidateCharset(value, alphabet); err != nil {
2016-12-06 13:02:12 +01:00
return "", err
}
return value, nil
}
2016-12-08 11:09:25 +01:00
func ValidateProtocolParam(value string) (string, error) {
2017-02-23 12:10:39 +01:00
return ValidateLengthAndCharset(value, 80, "ABCDEFGHIJKLMNOPQRSTUVWXYZ_:.abcdefghijklmnopqrstuvwxyz1234567890")
2016-12-08 11:09:25 +01:00
}
func ValidatePrefixParam(value string) (string, error) {
return ValidateLengthAndCharset(value, 80, "1234567890abcdef.:/")
}