validation

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetValidatedData

func GetValidatedData[T any](c *fiber.Ctx) (T, error)

GetValidatedData retrieves validated data from context

func ValidateRequest

func ValidateRequest[T any](validator *Validator) fiber.Handler

Middleware function for request validation

Types

type CreateProductRequest

type CreateProductRequest struct {
	Name        string  `json:"name" validate:"required,min=2,max=200" example:"iPhone 15" description:"Product name"`
	Description string  `json:"description" validate:"max=1000" example:"Latest iPhone model" description:"Product description"`
	Price       float64 `json:"price" validate:"required,min=0" example:"999.99" description:"Product price"`
	SKU         string  `json:"sku" validate:"required,alphanum" example:"IPH15-128-BLK" description:"Product SKU"`
	Category    string  `json:"category" validate:"required" example:"Electronics" description:"Product category"`
	InStock     bool    `json:"in_stock" example:"true" description:"Stock availability"`
}

CreateProductRequest represents a product creation request

type CreateUserRequest

type CreateUserRequest struct {
	Name     string `json:"name" validate:"required,min=2,max=100" example:"John Doe" description:"Full name of the user"`
	Email    string `json:"email" validate:"required,email" example:"john@example.com" description:"Email address"`
	Password string `json:"password" validate:"required,password" example:"SecurePass123!" description:"Password with at least 8 characters"`
	Phone    string `json:"phone" validate:"phone" example:"+1234567890" description:"Phone number"`
	Age      int    `json:"age" validate:"min=18,max=120" example:"25" description:"Age of the user"`
}

CreateUserRequest represents a user creation request

type ErrorResponse

type ErrorResponse struct {
	Error   string `json:"error" example:"validation_error" description:"Error type"`
	Message string `json:"message" example:"Invalid input data" description:"Error message"`
	Details string `json:"details,omitempty" example:"Email is required" description:"Additional error details"`
}

ErrorResponse represents an error response

type LoginRequest

type LoginRequest struct {
	Email    string `json:"email" validate:"required,email" example:"user@example.com" description:"Email address"`
	Password string `json:"password" validate:"required" example:"password123" description:"Password"`
}

LoginRequest represents a login request

type LoginResponse

type LoginResponse struct {
	User        UserResponse `json:"user" description:"User information"`
	Tokens      TokenPair    `json:"tokens" description:"Access and refresh tokens"`
	Requires2FA bool         `json:"requires_2fa" example:"false" description:"Whether 2FA is required"`
	RequiresOTP bool         `json:"requires_otp" example:"false" description:"Whether OTP is required"`
}

LoginResponse represents a login response

type PaginationRequest

type PaginationRequest struct {
	Page     int `json:"page" validate:"min=1" example:"1" description:"Page number (starts from 1)"`
	PageSize int `json:"page_size" validate:"min=1,max=100" example:"10" description:"Number of items per page"`
}

PaginationRequest represents pagination parameters

type PaginationResponse

type PaginationResponse struct {
	Page       int   `json:"page" example:"1" description:"Current page number"`
	PageSize   int   `json:"page_size" example:"10" description:"Items per page"`
	Total      int64 `json:"total" example:"100" description:"Total number of items"`
	TotalPages int   `json:"total_pages" example:"10" description:"Total number of pages"`
}

PaginationResponse represents pagination metadata

type ProductResponse

type ProductResponse struct {
	ID          string    `json:"id" example:"550e8400-e29b-41d4-a716-446655440000" description:"Unique identifier"`
	Name        string    `json:"name" example:"iPhone 15" description:"Product name"`
	Description string    `json:"description" example:"Latest iPhone model" description:"Product description"`
	Price       float64   `json:"price" example:"999.99" description:"Product price"`
	SKU         string    `json:"sku" example:"IPH15-128-BLK" description:"Product SKU"`
	Category    string    `json:"category" example:"Electronics" description:"Product category"`
	InStock     bool      `json:"in_stock" example:"true" description:"Stock availability"`
	CreatedAt   time.Time `json:"created_at" example:"2023-10-20T10:30:00Z" description:"Creation timestamp"`
	UpdatedAt   time.Time `json:"updated_at" example:"2023-10-20T10:30:00Z" description:"Last update timestamp"`
}

ProductResponse represents a product response

type Schema

type Schema struct {
	Type        string                 `json:"type"`
	Properties  map[string]interface{} `json:"properties"`
	Required    []string               `json:"required,omitempty"`
	Example     interface{}            `json:"example,omitempty"`
	Description string                 `json:"description,omitempty"`
}

Schema represents a validation schema with metadata

type SearchRequest

type SearchRequest struct {
	Query  string `json:"query" validate:"required,min=1" example:"iPhone" description:"Search query"`
	Limit  int    `json:"limit" validate:"min=1,max=100" example:"20" description:"Maximum number of results"`
	Offset int    `json:"offset" validate:"min=0" example:"0" description:"Number of results to skip"`
}

SearchRequest represents a search request

type SearchResponse

type SearchResponse struct {
	Results []ProductResponse `json:"results" description:"Search results"`
	Total   int64             `json:"total" example:"50" description:"Total number of results"`
	Query   string            `json:"query" example:"iPhone" description:"Search query used"`
}

SearchResponse represents a search response

type SuccessResponse

type SuccessResponse struct {
	Success bool        `json:"success" example:"true" description:"Success status"`
	Message string      `json:"message" example:"Operation completed successfully" description:"Success message"`
	Data    interface{} `json:"data,omitempty" description:"Response data"`
}

SuccessResponse represents a success response

type TokenPair

type TokenPair struct {
	AccessToken  string    `json:"access_token" example:"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..." description:"JWT access token"`
	RefreshToken string    `json:"refresh_token" example:"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..." description:"JWT refresh token"`
	ExpiresAt    time.Time `json:"expires_at" example:"2023-10-20T11:30:00Z" description:"Token expiration time"`
	TokenType    string    `json:"token_type" example:"Bearer" description:"Token type"`
}

TokenPair represents access and refresh tokens

type UpdateUserRequest

type UpdateUserRequest struct {
	Name  *string `json:"name,omitempty" validate:"omitempty,min=2,max=100" example:"Jane Doe" description:"Updated name"`
	Email *string `json:"email,omitempty" validate:"omitempty,email" example:"jane@example.com" description:"Updated email"`
	Phone *string `json:"phone,omitempty" validate:"omitempty,phone" example:"+1987654321" description:"Updated phone number"`
	Age   *int    `json:"age,omitempty" validate:"omitempty,min=18,max=120" example:"30" description:"Updated age"`
}

UpdateUserRequest represents a user update request

type UserResponse

type UserResponse struct {
	ID              string    `json:"id" example:"550e8400-e29b-41d4-a716-446655440000" description:"Unique identifier"`
	Email           string    `json:"email" example:"john@example.com" description:"Email address"`
	IsEmailVerified bool      `json:"is_email_verified" example:"false" description:"Email verification status"`
	Is2FAEnabled    bool      `json:"is_2fa_enabled" example:"false" description:"2FA status"`
	CreatedAt       time.Time `json:"created_at" example:"2023-10-20T10:30:00Z" description:"Creation timestamp"`
}

UserResponse represents a user response

type ValidationError

type ValidationError struct {
	Field   string
	Tag     string
	Value   interface{}
	Message string
}

ValidationError represents a validation error

func (ValidationError) Error

func (e ValidationError) Error() string

Error implements the error interface

type Validator

type Validator struct {
	// contains filtered or unexported fields
}

Validator holds the validation instance

func NewValidator

func NewValidator() *Validator

NewValidator creates a new validator instance

func (*Validator) GenerateSchema

func (v *Validator) GenerateSchema(s interface{}) (*Schema, error)

GenerateSchema generates a JSON schema from a Go struct

func (*Validator) ValidateQuery

func (v *Validator) ValidateQuery(c *fiber.Ctx, dest interface{}) error

ValidateQuery validates query parameters

func (*Validator) ValidateRequest

func (v *Validator) ValidateRequest(c *fiber.Ctx, dest interface{}) error

ValidateRequest validates a request body and binds it to a struct

func (*Validator) ValidateStruct

func (v *Validator) ValidateStruct(s interface{}) error

ValidateStruct validates a struct using struct tags

Jump to

Keyboard shortcuts

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