2023-06-19 14:42:47 -07:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2021-01-03 21:20:21 -08:00
|
|
|
|
2023-08-10 19:46:45 -07:00
|
|
|
package validator // import "miniflux.app/v2/internal/validator"
|
2021-01-03 21:20:21 -08:00
|
|
|
|
|
|
|
import (
|
2021-01-04 15:32:32 -08:00
|
|
|
"fmt"
|
2021-01-04 13:49:28 -08:00
|
|
|
"net/url"
|
2021-02-07 18:38:45 -08:00
|
|
|
"regexp"
|
2024-10-05 20:37:30 -07:00
|
|
|
"strings"
|
2021-01-03 21:20:21 -08:00
|
|
|
)
|
|
|
|
|
2025-06-10 17:33:00 +02:00
|
|
|
var domainRegex = regexp.MustCompile(`^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z]{2,}$`)
|
2024-10-05 20:37:30 -07:00
|
|
|
|
2021-01-04 15:32:32 -08:00
|
|
|
// ValidateRange makes sure the offset/limit values are valid.
|
|
|
|
func ValidateRange(offset, limit int) error {
|
|
|
|
if offset < 0 {
|
2024-02-24 20:44:40 -08:00
|
|
|
return fmt.Errorf(`offset value should be >= 0`)
|
2021-01-04 15:32:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if limit < 0 {
|
2024-02-24 20:44:40 -08:00
|
|
|
return fmt.Errorf(`limit value should be >= 0`)
|
2021-01-04 15:32:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValidateDirection makes sure the sorting direction is valid.
|
|
|
|
func ValidateDirection(direction string) error {
|
|
|
|
switch direction {
|
|
|
|
case "asc", "desc":
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-24 20:44:40 -08:00
|
|
|
return fmt.Errorf(`invalid direction, valid direction values are: "asc" or "desc"`)
|
2021-01-04 15:32:32 -08:00
|
|
|
}
|
|
|
|
|
2021-02-07 18:38:45 -08:00
|
|
|
// 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 13:49:28 -08:00
|
|
|
_, err := url.ParseRequestURI(absoluteURL)
|
|
|
|
return err == nil
|
|
|
|
}
|
2024-10-05 20:37:30 -07:00
|
|
|
|
|
|
|
func IsValidDomain(domain string) bool {
|
|
|
|
domain = strings.ToLower(domain)
|
|
|
|
|
|
|
|
if len(domain) < 1 || len(domain) > 253 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return domainRegex.MatchString(domain)
|
|
|
|
}
|
|
|
|
|
|
|
|
func IsValidDomainList(value string) bool {
|
|
|
|
domains := strings.Split(strings.TrimSpace(value), " ")
|
|
|
|
for _, domain := range domains {
|
|
|
|
if !IsValidDomain(domain) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|