validators

import validators "ganigeorgiev.com/pocketbase/pocketbase/core/validators"

Package validators implements some common custom PocketBase validators.

Source: core/validators.

Variables

var ErrUnsupportedValueType = validation.NewError("validation_unsupported_value_type", "Invalid or unsupported value type.")

Functions

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)))

IPOrSubnet

func IPOrSubnet(value any) error

IPOrSubnet checks whether the validated value is an individual IPv4/IPv6 or CIDR subnet.

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))

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.

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

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"))

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)))

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)))