2023-06-19 17:42:47 -04:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2021-01-04 00:20:21 -05:00
|
|
|
|
2023-08-10 22:46:45 -04:00
|
|
|
package validator // import "miniflux.app/v2/internal/validator"
|
2021-01-04 00:20:21 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2021-01-04 18:32:32 -05:00
|
|
|
"fmt"
|
2021-01-04 16:49:28 -05:00
|
|
|
"net/url"
|
2021-02-07 21:38:45 -05:00
|
|
|
"regexp"
|
2021-01-04 00:20:21 -05:00
|
|
|
|
2023-08-10 22:46:45 -04:00
|
|
|
"miniflux.app/v2/internal/locale"
|
2021-01-04 00:20:21 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// ValidationError represents a validation error.
|
|
|
|
type ValidationError struct {
|
|
|
|
TranslationKey string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewValidationError initializes a validation error.
|
|
|
|
func NewValidationError(translationKey string) *ValidationError {
|
|
|
|
return &ValidationError{TranslationKey: translationKey}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *ValidationError) String() string {
|
|
|
|
return locale.NewPrinter("en_US").Printf(v.TranslationKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *ValidationError) Error() error {
|
|
|
|
return errors.New(v.String())
|
|
|
|
}
|
2021-01-04 16:49:28 -05:00
|
|
|
|
2021-01-04 18:32:32 -05: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"`)
|
|
|
|
}
|
|
|
|
|
2021-02-07 21:38:45 -05: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 16:49:28 -05:00
|
|
|
_, err := url.ParseRequestURI(absoluteURL)
|
|
|
|
return err == nil
|
|
|
|
}
|