Last active
February 23, 2023 02:39
-
-
Save aasumitro/67e16bb1b1fc8bb7562028d2364e91fd to your computer and use it in GitHub Desktop.
Geography
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type GeographyDetailForm struct { | |
TenantID string `json:"-" form:"-"` | |
Name string `form:"name" json:"name"` | |
Type string `form:"type" json:"type" default:"region"` | |
Countries []struct { | |
ID string `form:"id" json:"id"` | |
Code string `form:"code" json:"code"` | |
Name string `form:"name" json:"name"` | |
Type string `form:"type" json:"type" default:"country"` | |
Currency struct { | |
ID string `form:"id" json:"id"` | |
Code string `form:"code" json:"code"` | |
Name string `form:"name" json:"name"` | |
} `form:"currency" json:"currency"` | |
Locations []struct { | |
Name string `form:"name" json:"name"` | |
Type string `form:"type" json:"type" default:"location"` | |
} `form:"locations" json:"locations"` | |
} `form:"countries" json:"countries"` | |
} | |
func (gdf *GeographyDetailForm) Validate() error { | |
if err := validation.ValidateStruct(gdf, | |
validation.Field(&gdf.Name, validation.Required), | |
validation.Field(&gdf.Type, validation.Required), | |
validation.Field(&gdf.Countries, validation.Required), | |
); err != nil { | |
return err | |
} | |
for i, country := range gdf.Countries { | |
if err := validation.ValidateStruct(&country, | |
validation.Field(&country.ID, validation.Required), | |
validation.Field(&country.Code, validation.Required), | |
validation.Field(&country.Name, validation.Required), | |
validation.Field(&country.Type, validation.Required), | |
validation.Field(&country.Currency, validation.Required), | |
validation.Field(&country.Locations, validation.Required), | |
); err != nil { | |
return wrapValidationError(fmt.Sprintf("country[%d]", i), err) | |
} | |
if err := validation.ValidateStruct(&country.Currency, | |
validation.Field(&country.Currency.ID, validation.Required), | |
validation.Field(&country.Currency.Name, validation.Required), | |
validation.Field(&country.Currency.Code, validation.Required), | |
); err != nil { | |
return wrapValidationError(fmt.Sprintf("country[%d].currency", i), err) | |
} | |
for j, location := range country.Locations { | |
if err := validation.ValidateStruct(&location, | |
validation.Field(&location.Name, validation.Required), | |
validation.Field(&location.Type, validation.Required), | |
); err != nil { | |
return wrapValidationError(fmt.Sprintf("country[%d].location[%d]", i, j), err) | |
} | |
} | |
} | |
return nil | |
} |
Author
aasumitro
commented
Feb 23, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment