Skip to content
Merged
Show file tree
Hide file tree
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
25 changes: 13 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ type Address struct {

func (a Address) Validate() error {
return validation.ValidateStruct(&a,
// Street cannot be empty, and the length must between 5 and 50
validation.Field(&a.Street, validation.Required, validation.Length(5, 50)),
// City cannot be empty, and the length must between 5 and 50
validation.Field(&a.City, validation.Required, validation.Length(5, 50)),
// Street cannot be empty, and the length must between 2 and 50
validation.Field(&a.Street, validation.Required, validation.Length(2, 50)),
// City cannot be empty, and the length must between 2 and 50
validation.Field(&a.City, validation.Required, validation.Length(2, 50)),
// State cannot be empty, and must be a string consisting of two letters in upper case
validation.Field(&a.State, validation.Required, validation.Match(regexp.MustCompile("^[A-Z]{2}$"))),
// State cannot be empty, and must be a string consisting of five digits
Expand Down Expand Up @@ -153,10 +153,10 @@ err := validation.Validate(c,
validation.Key("Email", validation.Required, is.Email),
// Validate Address using its own validation rules
validation.Key("Address", validation.Map(
// Street cannot be empty, and the length must between 5 and 50
validation.Key("Street", validation.Required, validation.Length(5, 50)),
// City cannot be empty, and the length must between 5 and 50
validation.Key("City", validation.Required, validation.Length(5, 50)),
// Street cannot be empty, and the length must between 2 and 50
validation.Key("Street", validation.Required, validation.Length(2, 50)),
// City cannot be empty, and the length must between 2 and 50
validation.Key("City", validation.Required, validation.Length(2, 50)),
// State cannot be empty, and must be a string consisting of two letters in upper case
validation.Key("State", validation.Required, validation.Match(regexp.MustCompile("^[A-Z]{2}$"))),
// State cannot be empty, and must be a string consisting of five digits
Expand Down Expand Up @@ -451,8 +451,8 @@ The following code implements the aforementioned examples:
```go
result := validation.ValidateStruct(&a,
validation.Field(&a.Unit, validation.When(a.Quantity != "", validation.Required).Else(validation.Nil)),
validation.Field(&a.Phone, validation.When(a.Email == "", validation.Required.Error('Either phone or Email is required.')),
validation.Field(&a.Email, validation.When(a.Phone == "", validation.Required.Error('Either phone or Email is required.')),
validation.Field(&a.Phone, validation.When(a.Email == "", validation.Required.Error("Either phone or Email is required.")),
validation.Field(&a.Email, validation.When(a.Phone == "", validation.Required.Error("Either phone or Email is required.")),
)
```

Expand All @@ -463,8 +463,8 @@ The above code can also be simplified using the shortcut `validation.Required.Wh
```go
result := validation.ValidateStruct(&a,
validation.Field(&a.Unit, validation.Required.When(a.Quantity != ""), validation.Nil.When(a.Quantity == "")),
validation.Field(&a.Phone, validation.Required.When(a.Email == "").Error('Either phone or Email is required.')),
validation.Field(&a.Email, validation.Required.When(a.Phone == "").Error('Either phone or Email is required.')),
validation.Field(&a.Phone, validation.Required.When(a.Email == "").Error("Either phone or Email is required.")),
validation.Field(&a.Email, validation.Required.When(a.Phone == "").Error("Either phone or Email is required.")),
)
```

Expand Down Expand Up @@ -659,6 +659,7 @@ Below is the whole list of the rules provided by the `is` package:
- `UUIDv4`: validates if a string is a valid version 4 UUID
- `UUIDv5`: validates if a string is a valid version 5 UUID
- `UUID`: validates if a string is a valid UUID
- `ULID`: validates if a string is a valid ULID
- `CreditCard`: validates if a string is a valid credit card number
- `ISBN10`: validates if a string is an ISBN version 10
- `ISBN13`: validates if a string is an ISBN version 13
Expand Down
6 changes: 4 additions & 2 deletions is/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ var (
ErrUUIDv5 = validation.NewError("validation_is_uuid_v5", "must be a valid UUID v5")
// ErrUUID is the error that returns in case of an invalid UUID value.
ErrUUID = validation.NewError("validation_is_uuid", "must be a valid UUID")
// ErrULID is the error that returns in case of an invalid ULID value.
ErrULID = validation.NewError("validation_is_ulid", "must be a valid ULID")
// ErrCreditCard is the error that returns in case of an invalid credit card number.
ErrCreditCard = validation.NewError("validation_is_credit_card", "must be a valid credit card number")
// ErrISBN10 is the error that returns in case of an invalid ISBN-10 value.
Expand All @@ -84,7 +86,7 @@ var (
ErrBase64 = validation.NewError("validation_is_base64", "must be encoded in Base64")
// ErrDataURI is the error that returns in case of an invalid data URI.
ErrDataURI = validation.NewError("validation_is_data_uri", "must be a Base64-encoded data URI")
// ErrE164 is the error that returns in case of an invalid e165.
// ErrE164 is the error that returns in case of an invalid e164.
ErrE164 = validation.NewError("validation_is_e164_number", "must be a valid E164 number")
// ErrCountryCode2 is the error that returns in case of an invalid two-letter country code.
ErrCountryCode2 = validation.NewError("validation_is_country_code_2_letter", "must be a valid two-letter country code")
Expand Down Expand Up @@ -197,7 +199,7 @@ var (
Base64 = validation.NewStringRuleWithError(govalidator.IsBase64, ErrBase64)
// DataURI validates if a string is a valid base64-encoded data URI
DataURI = validation.NewStringRuleWithError(govalidator.IsDataURI, ErrDataURI)
// E164 validates if a string is a valid ISO3166 Alpha 2 country code
// E164 validates if a string is a valid E164 telephone number
E164 = validation.NewStringRuleWithError(isE164Number, ErrE164)
// CountryCode2 validates if a string is a valid ISO3166 Alpha 2 country code
CountryCode2 = validation.NewStringRuleWithError(govalidator.IsISO3166Alpha2, ErrCountryCode2)
Expand Down