validators

package
v0.23.0-rc4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 9, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package validators implements some common custom PocketBase validators.

Index

Constants

This section is empty.

Variables

View Source
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

func IsRegex(value any) error

IsRegex checks whether the validated value is a valid regular expression pattern.

Example:

validation.Field(&form.Pattern, validation.By(validators.IsRegex))

func JoinValidationErrors

func JoinValidationErrors(errA, errB error) error

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

func NormalizeUniqueIndexError(err error, tableOrAlias string, fieldNames []string) error

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL