Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jul 18, 2025

This PR implements email validation support for the validgen code generator, enabling developers to validate email fields in Go structs using the email tag.

Features Added

  • Email validation tag: Added support for email validation tag that validates email format using regex
  • Optional field support: Empty strings are considered valid for optional email fields
  • Required + email combination: Works seamlessly with required tag for mandatory email fields

Usage Examples

type User struct {
    Email1 string `validate:"required,email"`  // Required and must be valid email
    Email2 string `validate:"email"`           // Optional, but if present must be valid email
}

Implementation Details

  • Added IsValidEmail() function in types/string_utils.go with comprehensive regex-based validation
  • Extended parser to recognize "email" as a zero-value validation tag
  • Added email validation logic to the code generation condition table
  • Comprehensive test coverage for various email formats (valid/invalid cases)

Validation Examples

// These will pass validation
user1 := &User{Email1: "user@domain.com", Email2: ""}                    // Valid required, empty optional
user2 := &User{Email1: "test@example.org", Email2: "optional@test.co.uk"} // Both valid

// These will fail validation
user3 := &User{Email1: "", Email2: ""}                 // Required email is empty
user4 := &User{Email1: "invalid.email", Email2: ""}    // Invalid email format
user5 := &User{Email1: "user@domain.com", Email2: "bad"} // Invalid optional email

The generated validation code efficiently checks email format without reflection, maintaining validgen's performance benefits.

Fixes #5.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI changed the title [WIP] Feat: implement email tag Feat: implement email tag validation Jul 18, 2025
Copilot AI requested a review from leocavalcante July 18, 2025 13:47
@leocavalcante leocavalcante marked this pull request as ready for review July 18, 2025 13:48
@leocavalcante leocavalcante requested a review from alexgarzao July 18, 2025 14:08
Copy link
Collaborator

@alexgarzao alexgarzao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice contribution! Thanks @leocavalcante !

@leocavalcante
Copy link
Member

@copilot create an end-to-end test for the email validation at the tests/endtoend/string.go file.

Copy link

@fabriciogpp fabriciogpp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


// Basic email regex pattern
// This covers most common email formats but is not exhaustive
emailRegex := regexp.MustCompile(`^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$`)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

O problema aqui é que vai compilar em cada chamada de validação.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot how can we avoid the regex from being compiled at each request? Can we have a pre-compiled regex to be reused for subsequent email validations?

Copy link
Contributor Author

Copilot AI commented Jul 18, 2025

@copilot create an end-to-end test for the email validation at the tests/endtoend/string.go file.

I've created comprehensive end-to-end tests for email validation in tests/endtoend/string.go. The tests cover:

  • Required email field validation (empty and invalid format errors)
  • Optional email field validation (empty valid, invalid format errors)
  • Valid email formats for both required and optional fields
  • Integration with existing string validation tests

All tests are passing and verify the generated validation code correctly handles email validation scenarios. Commit: a564fac

@leocavalcante
Copy link
Member

@copilot how can we avoid the regex from being compiled at each request? Can we have a pre-compiled regex to be reused for subsequent email validations?

Copy link
Contributor Author

Copilot AI commented Jul 18, 2025

@copilot how can we avoid the regex from being compiled at each request? Can we have a pre-compiled regex to be reused for subsequent email validations?

I've optimized the email validation by using a pre-compiled regex. Instead of compiling the regex on each validation call, the regex is now compiled once at package initialization and reused for all subsequent validations. This provides better performance, especially when validating many emails. The function signature and behavior remain exactly the same. Commit: ed4e6fa

Copilot AI and others added 3 commits July 18, 2025 12:11
Co-authored-by: leocavalcante <183722+leocavalcante@users.noreply.github.com>
Co-authored-by: leocavalcante <183722+leocavalcante@users.noreply.github.com>
Co-authored-by: leocavalcante <183722+leocavalcante@users.noreply.github.com>
Co-authored-by: alexgarzao <5495536+alexgarzao@users.noreply.github.com>
Copilot AI requested a review from alexgarzao July 18, 2025 17:42
@alexgarzao alexgarzao merged commit 94ef5bb into main Jul 18, 2025
1 check passed
@alexgarzao alexgarzao deleted the copilot/fix-5 branch July 18, 2025 21:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feat: implement email tag

5 participants