Documentation ¶
Overview ¶
Package validators implements some common custom PocketBase validators.
Index ¶
- Variables
- func Equal[T comparable](valueToCompare T) validation.RuleFunc
- func IsRegex(value any) error
- func JoinValidationErrors(errA, errB error) error
- func NormalizeUniqueIndexError(err error, tableOrAlias string, fieldNames []string) error
- func UniqueId(db dbx.Builder, tableName string) validation.RuleFunc
- func UploadedFileMimeType(validTypes []string) validation.RuleFunc
- func UploadedFileSize(maxBytes int64) validation.RuleFunc
Constants ¶
This section is empty.
Variables ¶
var ErrUnsupportedValueType = validation.NewError("validation_unsupported_value_type", "Invalid or unsupported value type.")
Functions ¶
func Equal ¶
func Equal[T comparable](valueToCompare T) validation.RuleFunc
Equal checks whether the validated value matches another one from the same type.
It expects the compared values to be from the same type and works with booleans, numbers, strings and their pointer variants.
If one of the value is pointer, the comparison is based on its underlying value (when possible to determine).
Note that empty/zero values are also compared (this differ from other validation.RuleFunc).
Example:
validation.Field(&form.PasswordConfirm, validation.By(validators.Equal(form.Password)))
func IsRegex ¶
IsRegex checks whether the validated value is a valid regular expression pattern.
Example:
validation.Field(&form.Pattern, validation.By(validators.IsRegex))
func JoinValidationErrors ¶
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 NormalizeUniqueIndexError ¶
NormalizeUniqueIndexError attempts to convert a "unique constraint failed" error into a validation.Errors.
The provided err is returned as it is without changes if: - err is nil - err is already validation.Errors - err is not "unique constraint failed" error
func UniqueId ¶
func UniqueId(db dbx.Builder, tableName string) validation.RuleFunc
UniqueId checks whether a field string id already exists in the specified table.
Example:
validation.Field(&form.RelId, validation.By(validators.UniqueId(form.app.DB(), "tbl_example"))
func UploadedFileMimeType ¶
func UploadedFileMimeType(validTypes []string) validation.RuleFunc
UploadedFileMimeType checks whether the validated *filesystem.File mimetype is within the provided allowed mime types.
Example:
validMimeTypes := []string{"test/plain","image/jpeg"} validation.Field(&form.File, validation.By(validators.UploadedFileMimeType(validMimeTypes)))
func UploadedFileSize ¶
func UploadedFileSize(maxBytes int64) validation.RuleFunc
UploadedFileSize checks whether the validated *filesystem.File size is no more than the provided maxBytes.
Example:
validation.Field(&form.File, validation.By(validators.UploadedFileSize(1000)))
Types ¶
This section is empty.