Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gofiber-validator-middleware

License GitHub all releases GitHub issues GitHub commit activity Go Report Card GitHub release GitHub stars GitHub forks

gofiber-validator-middleware is a gofiber middleware to generate a go-playground/validator singleton for use in validating DTOs in handlers (or whatever you need to validate).

Installation

go get github.com/rgglez/gofiber-validator-middleware

Configuration

  • Next defines a function to skip this middleware when returned true.
  • Validator injected instance, if nil a new validator will be created.
  • ContextKey is the key used to store the validator in context. Optional. Default: "validator"
  • CustomValidations allows you to register custom validation functions. Optional. Default: nil

Basic usage

import (
    gofibervalidator "github.com/rgglez/gofiber-validator-middleware/gofibervalidator"
)

// Custom validation function example
func isEvenNumber(fl validator.FieldLevel) bool {
	value := fl.Field().Int()
	return value%2 == 0
}

app.Use(gofibervalidator.New(gofibervalidator.Config{
    ContextKey: "validator",
    CustomValidations: map[string]validator.Func{
        "even": isEvenNumber,
    },
    Next: func(c *fiber.Ctx) bool {
        // Skip validation for specific routes
        return c.Path() == "/health"
    },
}))

Example

An example is included in the example directory. To execute it:

  1. Enter the example directory.
  2. Run the example:
    go run .
  3. You can try 3 use cases:

You can use resting or the plugin of your choice, or tools like curl, to try the endpoints.

Dependencies

Tests

Run the tests with:

go test ./gofibervalidator/... -v

Test cases

Test What it verifies
TestNew_DefaultConfig Middleware stores a *validator.Validate under the "validator" key by default
TestNew_CustomContextKey Custom ContextKey is used; default key is absent
TestNew_EmptyContextKeyFallsBackToDefault Empty ContextKey falls back to "validator"
TestNew_NextSkipsMiddleware When Next returns true, middleware is skipped and no validator is stored
TestNew_CustomValidatorInstance A pre-built *validator.Validate provided via config is used as-is
TestNew_CustomValidations Custom tag functions registered via CustomValidations work correctly
TestGetValidator_DefaultKey GetValidator retrieves the instance using the default key
TestGetValidator_CustomKey GetValidator retrieves via a custom key; wrong key returns nil
TestGetValidator_NotInContext GetValidator returns nil when no middleware is registered
TestGetValidator_ValidatesStruct The retrieved validator correctly validates struct fields

License

Copyright (c) 2026 Rodolfo González González.

Licensed under the Apache 2.0 license. Read the LICENSE file.

About

Middleware to generate a go-playground/validator singleton for use in validating DTOs in handlers.

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages