Skip to content

example: demonstrate the process of validating a signup request #1352

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions _examples/simple/main.go
Original file line number Diff line number Diff line change
@@ -25,6 +25,12 @@ type Address struct {
Phone string `validate:"required"`
}

type SignupRequest struct {
Email string `validate:"required,email"`
Password string `validate:"required"`
PasswordConfrim string `validate:"eqfield=Password"`
}

// use a single instance of Validate, it caches struct info
var validate *validator.Validate

@@ -34,6 +40,7 @@ func main() {

validateStruct()
validateVariable()
validateSignup()
}

func validateStruct() {
@@ -101,3 +108,42 @@ func validateVariable() {

// email ok, move on
}

// validateSignup demonstrates the process of validating a signup request
// using a predefined struct validation. It creates a sample request with
// invalid data to showcase error handling and a second request with valid
// data to ensure successful validation.
func validateSignup() {
fmt.Println("Validate Signup request")

// Create a SignupRequest instance with invalid data
req := &SignupRequest{
Email: "test@test.com",
Password: "Password123!",
PasswordConfrim: "badpassword",
}

// Validate the SignupRequest instance against defined struct validation rules
err := validate.Struct(req)
if err != nil {
// Log the validation failure and the specific error details
fmt.Printf("Signup request validation failed: %v\n", err)
}

// Create a new SignupRequest instance with corrected data
req = &SignupRequest{
Email: "test@test.com",
Password: "Password123!",
PasswordConfrim: "Password123!",
}

// Revalidate the corrected SignupRequest instance
err = validate.Struct(req)
if err != nil {
// If this code path executes, there is an unexpected error, so panic
panic(0) // Should not reach here in normal circumstances
}

// Log successful validation of the signup request
fmt.Println("Signup request has been validated")
}