Update company form validation to make fields optional and enhance service logic for registration number and tax ID checks
continuous-integration/drone/push Build is passing

- Modified CompanyForm fields (RegistrationNumber, TaxID, Industry) to be optional, allowing for more flexible company creation.
- Updated the Create method in company service to check for existing companies by registration number and tax ID only if provided, improving error handling and user experience during company creation.

This update enhances the company creation process by allowing optional fields while ensuring that existing company checks are performed only when necessary.
This commit is contained in:
Mazyar
2026-07-01 19:53:35 +03:30
parent 492f9ba3c8
commit 506ac01cda
2 changed files with 13 additions and 11 deletions
+3 -3
View File
@@ -7,9 +7,9 @@ type (
CompanyForm struct { CompanyForm struct {
Name string `json:"name" valid:"required,length(2|200)" example:"Opplens"` Name string `json:"name" valid:"required,length(2|200)" example:"Opplens"`
Type string `json:"type" valid:"required,in(private|public|government|ngo|startup)" example:"private"` Type string `json:"type" valid:"required,in(private|public|government|ngo|startup)" example:"private"`
RegistrationNumber string `json:"registration_number" valid:"required,length(5|50)" example:"1234567890"` RegistrationNumber string `json:"registration_number" valid:"optional,length(5|50)" example:"1234567890"`
TaxID string `json:"tax_id" valid:"required,length(5|50)" example:"1234567890"` TaxID string `json:"tax_id" valid:"optional,length(5|50)" example:"1234567890"`
Industry string `json:"industry" valid:"required,length(2|100)" example:"Technology"` Industry string `json:"industry" valid:"optional,length(2|100)" example:"Technology"`
Description *string `json:"description,omitempty" valid:"optional,length(10|1000)" example:"Opplens is a technology company"` Description *string `json:"description,omitempty" valid:"optional,length(10|1000)" example:"Opplens is a technology company"`
Website *string `json:"website,omitempty" valid:"optional,url" example:"https://opplens.com"` Website *string `json:"website,omitempty" valid:"optional,url" example:"https://opplens.com"`
+10 -8
View File
@@ -103,16 +103,18 @@ func (s *companyService) Create(ctx context.Context, form *CompanyForm) (*Compan
return nil, errors.New("company with this name already exists") return nil, errors.New("company with this name already exists")
} }
// Check if registration number already exists if form.RegistrationNumber != "" {
existingCompany, _ = s.repository.GetByRegistrationNumber(ctx, form.RegistrationNumber) existingCompany, _ = s.repository.GetByRegistrationNumber(ctx, form.RegistrationNumber)
if existingCompany != nil { if existingCompany != nil {
return nil, errors.New("company with this registration number already exists") return nil, errors.New("company with this registration number already exists")
}
} }
// Check if tax ID already exists if form.TaxID != "" {
existingCompany, _ = s.repository.GetByTaxID(ctx, form.TaxID) existingCompany, _ = s.repository.GetByTaxID(ctx, form.TaxID)
if existingCompany != nil { if existingCompany != nil {
return nil, errors.New("company with this tax ID already exists") return nil, errors.New("company with this tax ID already exists")
}
} }
// Create company // Create company