Validation with Fiber
This example demonstrates how to use go-playground/validator for input validation in a Go Fiber application.
This project provides a basic setup for validating request data in a Go Fiber application using the go-playground/validator
package. It includes the necessary configuration and code to perform validation on incoming requests.
main.go
: The main application entry point.config/env.go
: Configuration file for environment variables.go.mod
: The Go module file..env
: Environment variables file.
-
Clone the repository:
git clone https://github.com/gofiber/recipes.git cd recipes/validation
-
Install the dependencies:
go mod download
-
Create a
.env
file in the root directory with the following content:PORT=":8080"
-
Run the application:
go run main.go
The application should now be running on http://localhost:8080
.
-
Send a POST request to
http://localhost:8080/validate
with a JSON payload:{ "name": "John Doe", "email": "john.doe@example.com", "age": 30 }
-
The server will validate the request data and respond with a success message if the data is valid, or an error message if the data is invalid.
The main Go file sets up the Fiber application, handles HTTP requests, and performs validation using the go-playground/validator
package.
The configuration file for loading environment variables.
package config
import "os"
// Config func to get env value
func Config(key string) string {
return os.Getenv(key)
}
This example provides a basic setup for validating request data in a Go Fiber application using the go-playground/validator
package. It can be extended and customized further to fit the needs of more complex applications.