diff --git a/README.md b/README.md index 23df9f2..39a7559 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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.")), ) ``` @@ -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.")), ) ``` @@ -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 diff --git a/is/rules.go b/is/rules.go index b16083f..55e8d21 100644 --- a/is/rules.go +++ b/is/rules.go @@ -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. @@ -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") @@ -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)