miniflux/internal/validator/validator.go

46 lines
1.1 KiB
Go
Raw Normal View History

// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package validator // import "miniflux.app/v2/internal/validator"
import (
2021-01-05 00:32:32 +01:00
"fmt"
2021-01-04 22:49:28 +01:00
"net/url"
"regexp"
)
2021-01-05 00:32:32 +01:00
// ValidateRange makes sure the offset/limit values are valid.
func ValidateRange(offset, limit int) error {
if offset < 0 {
return fmt.Errorf(`Offset value should be >= 0`)
}
if limit < 0 {
return fmt.Errorf(`Limit value should be >= 0`)
}
return nil
}
// ValidateDirection makes sure the sorting direction is valid.
func ValidateDirection(direction string) error {
switch direction {
case "asc", "desc":
return nil
}
return fmt.Errorf(`Invalid direction, valid direction values are: "asc" or "desc"`)
}
// IsValidRegex verifies if the regex can be compiled.
func IsValidRegex(expr string) bool {
_, err := regexp.Compile(expr)
return err == nil
}
// IsValidURL verifies if the provided value is a valid absolute URL.
func IsValidURL(absoluteURL string) bool {
2021-01-04 22:49:28 +01:00
_, err := url.ParseRequestURI(absoluteURL)
return err == nil
}