mirror of
https://github.com/pocketbase/pocketbase.git
synced 2026-06-16 04:30:11 -04:00
48 lines
1.1 KiB
Go
48 lines
1.1 KiB
Go
// Package validators implements some common custom PocketBase validators.
|
|
package validators
|
|
|
|
import (
|
|
"errors"
|
|
"maps"
|
|
|
|
validation "github.com/go-ozzo/ozzo-validation/v4"
|
|
)
|
|
|
|
var ErrUnsupportedValueType = validation.NewError("validation_unsupported_value_type", "Invalid or unsupported value type.")
|
|
|
|
// JoinValidationErrors attempts to join the provided [validation.Errors] arguments.
|
|
//
|
|
// If only one of the arguments is [validation.Errors], it returns the first non-empty [validation.Errors].
|
|
//
|
|
// If both arguments are not [validation.Errors] then it returns a combined [errors.Join] error.
|
|
func JoinValidationErrors(errA, errB error) error {
|
|
vErrA, okA := errA.(validation.Errors)
|
|
vErrB, okB := errB.(validation.Errors)
|
|
|
|
// merge
|
|
if okA && okB {
|
|
result := maps.Clone(vErrA)
|
|
maps.Copy(result, vErrB)
|
|
if len(result) > 0 {
|
|
return result
|
|
}
|
|
}
|
|
|
|
if okA && len(vErrA) > 0 {
|
|
return vErrA
|
|
}
|
|
|
|
if okB && len(vErrB) > 0 {
|
|
return vErrB
|
|
}
|
|
|
|
return errors.Join(errA, errB)
|
|
}
|
|
|
|
func cutStr(str string, max int) string {
|
|
if len(str) > max {
|
|
return str[:max] + "..."
|
|
}
|
|
return str
|
|
}
|