validate

package
v0.0.0-...-d8b85fd Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package validate contains custom validation functions that can be used with the validator package to perform complex or application-specific validations.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrFailedValidation = errors.New("payload failed validation")

	TagErrorsEmail = TagErrorMessages{"email": "Invalid email address provided"}

	TagErrorsPassword = TagErrorMessages{
		"min":         "Password must have at least 10 characters",
		"max":         "Password must have at most 72 characters",
		"is_password": "Password must contain a number, lower case character, upper case character and one of the special symbols(including space) !@#$%^&*()-_+=,.?|\\/<>[]{}",
	}

	TagErrorsDOB = TagErrorMessages{"is_date": "Invalid date format. mm/dd/yyyy"}

	TagErrorsUsername = TagErrorMessages{
		"min": "Username must have at least 3 characters",
		"max": "Username must have at most 100 characters",
	}

	TagErrorsURL = TagErrorMessages{"http_url": "Invalid URL format"}
)
View Source
var (
	Validate *validator.Validate
)

Functions

func Get

func Get() *validator.Validate

func ValidatePayload

func ValidatePayload(payload any, errorMessages FieldErrorMessages) (map[string]string, error)

ValidatePayload validates the provided payload using custom validation rules. It returns a map of field names to error messages for any fields that fail validation, along with an error if validation fails or if there's an issue processing the payload.

Parameters:

  • payload: The data to be validated, typically a struct or map that contains the fields to be checked according to custom validation rules.
  • errorMessages: A map of custom error messages for specific fields that fail validation. This map will be used to generate specific error messages for the fields that don't meet the validation criteria.

Returns:

  • A map where keys are the names of fields that failed validation and the values are the corresponding error messages. This map is empty if no fields fail validation.
  • An error if there is an issue during validation (e.g., if the payload is malformed, or if a validation function fails).

Example:

payload := struct {
  Name string `validate:"required"`
  Age  int    `validate:"min=18"`
}
errorMessages := map[string]string{"Name": "Name is required", "Age": "Age must be at least 18"}
result, err := ValidatePayload(payload, errorMessages)

Types

type FieldErrorMessages

type FieldErrorMessages map[string]TagErrorMessages

type TagErrorMessages

type TagErrorMessages map[string]string

Jump to

Keyboard shortcuts

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