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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ validate := validator.New(validator.WithRequiredStructEnabled())
| hexcolor | Hexcolor String |
| hsl | HSL String |
| hsla | HSLA String |
| cmyk | CMYK String |
| html | HTML Tags |
| html_encoded | HTML Encoded |
| isbn | International Standard Book Number |
Expand Down Expand Up @@ -274,7 +275,7 @@ validate := validator.New(validator.WithRequiredStructEnabled())
#### Aliases:
| Tag | Description |
| - | - |
| iscolor | hexcolor\|rgb\|rgba\|hsl\|hsla |
| iscolor | hexcolor\|rgb\|rgba\|hsl\|hsla\|cmyk |
| country_code | iso3166_1_alpha2\|iso3166_1_alpha3\|iso3166_1_alpha_numeric |

Benchmarks
Expand Down
8 changes: 7 additions & 1 deletion baked_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ var (
// defines a common or complex set of validation(s) to simplify
// adding validation to structs.
bakedInAliases = map[string]string{
"iscolor": "hexcolor|rgb|rgba|hsl|hsla",
"iscolor": "hexcolor|rgb|rgba|hsl|hsla|cmyk",
"country_code": "iso3166_1_alpha2|iso3166_1_alpha3|iso3166_1_alpha_numeric",
"eu_country_code": "iso3166_1_alpha2_eu|iso3166_1_alpha3_eu|iso3166_1_alpha_numeric_eu",
}
Expand Down Expand Up @@ -134,6 +134,7 @@ var (
"rgba": isRGBA,
"hsl": isHSL,
"hsla": isHSLA,
"cmyk": isCMYK,
"e164": isE164,
"email": isEmail,
"url": isURL,
Expand Down Expand Up @@ -1721,6 +1722,11 @@ func isHSLA(fl FieldLevel) bool {
return hslaRegex().MatchString(fl.Field().String())
}

// isCMYK is the validation function for validating if the current field's value is a valid CMYK color.
func isCMYK(fl FieldLevel) bool {
return cmykRegex().MatchString(fl.Field().String())
}

// isHSL is the validation function for validating if the current field's value is a valid HSL color.
func isHSL(fl FieldLevel) bool {
return hslRegex().MatchString(fl.Field().String())
Expand Down
6 changes: 6 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,12 @@ This validates that a string value contains a valid hsla color

Usage: hsla

# CMYK String

This validates that a string value contains a valid cmyk color

Usage: cmyk

# E.164 Phone Number String

This validates that a string value contains a valid E.164 Phone number
Expand Down
2 changes: 2 additions & 0 deletions regexes.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const (
rgbaRegexString = "^rgba\\(\\s*(?:(?:0|[1-9]\\d?|1\\d\\d?|2[0-4]\\d|25[0-5])\\s*,\\s*(?:0|[1-9]\\d?|1\\d\\d?|2[0-4]\\d|25[0-5])\\s*,\\s*(?:0|[1-9]\\d?|1\\d\\d?|2[0-4]\\d|25[0-5])|(?:0|[1-9]\\d?|1\\d\\d?|2[0-4]\\d|25[0-5])%\\s*,\\s*(?:0|[1-9]\\d?|1\\d\\d?|2[0-4]\\d|25[0-5])%\\s*,\\s*(?:0|[1-9]\\d?|1\\d\\d?|2[0-4]\\d|25[0-5])%)\\s*,\\s*(?:(?:0.[1-9]*)|[01])\\s*\\)$"
hslRegexString = "^hsl\\(\\s*(?:0|[1-9]\\d?|[12]\\d\\d|3[0-5]\\d|360)\\s*,\\s*(?:(?:0|[1-9]\\d?|100)%)\\s*,\\s*(?:(?:0|[1-9]\\d?|100)%)\\s*\\)$"
hslaRegexString = "^hsla\\(\\s*(?:0|[1-9]\\d?|[12]\\d\\d|3[0-5]\\d|360)\\s*,\\s*(?:(?:0|[1-9]\\d?|100)%)\\s*,\\s*(?:(?:0|[1-9]\\d?|100)%)\\s*,\\s*(?:(?:0.[1-9]*)|[01])\\s*\\)$"
cmykRegexString = "^cmyk\\((100|[1-9]?\\d)%\\s*,\\s*(100|[1-9]?\\d)%\\s*,\\s*(100|[1-9]?\\d)%\\s*,\\s*(100|[1-9]?\\d)%\\)$"
emailR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F}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])(?:[a-zA-Z]|\\d|-|\\.|~|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])*(?:[a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])))\\.?$"
e164RegexString = "^\\+?[1-9]\\d{7,14}$"
base32RegexString = "^(?:[A-Z2-7]{8})*(?:[A-Z2-7]{2}={6}|[A-Z2-7]{4}={4}|[A-Z2-7]{5}={3}|[A-Z2-7]{7}=|[A-Z2-7]{8})$"
Expand Down Expand Up @@ -109,6 +110,7 @@ var (
rgbaRegex = lazyRegexCompile(rgbaRegexString)
hslRegex = lazyRegexCompile(hslRegexString)
hslaRegex = lazyRegexCompile(hslaRegexString)
cmykRegex = lazyRegexCompile(cmykRegexString)
e164Regex = lazyRegexCompile(e164RegexString)
emailRegex = lazyRegexCompile(emailRegexString)
base32Regex = lazyRegexCompile(base32RegexString)
Expand Down
45 changes: 43 additions & 2 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ func TestStructLevelValidations(t *testing.T) {

func TestAliasTags(t *testing.T) {
validate := New()
validate.RegisterAlias("iscoloralias", "hexcolor|rgb|rgba|hsl|hsla")
validate.RegisterAlias("iscoloralias", "hexcolor|rgb|rgba|hsl|hsla|cmyk")

s := "rgb(255,255,255)"
errs := validate.Var(s, "iscoloralias")
Expand Down Expand Up @@ -686,7 +686,7 @@ func TestAliasTags(t *testing.T) {

fe := getError(errs, "Test.Color", "Test.Color")
NotEqual(t, fe, nil)
Equal(t, fe.ActualTag(), "hexcolor|rgb|rgba|hsl|hsla")
Equal(t, fe.ActualTag(), "hexcolor|rgb|rgba|hsl|hsla|cmyk")

validate.RegisterAlias("req", "required,dive,iscoloralias")
arr := []string{"val1", "#fff", "#000"}
Expand Down Expand Up @@ -8729,6 +8729,47 @@ func TestOrTag(t *testing.T) {
NotEqual(t, fe, nil)
}

func TestCmyk(t *testing.T) {
validate := New()

s := "cmyk(0%, 100%, 50%, 0%)"
errs := validate.Var(s, "cmyk")
Equal(t, errs, nil)

s = "cmyk(0%, 0%, 0%, 0%)"
errs = validate.Var(s, "cmyk")
Equal(t, errs, nil)

s = "cmyk(0%, 0.1%, 0%, 0%)"
errs = validate.Var(s, "cmyk")
NotEqual(t, errs, nil)
AssertError(t, errs, "", "", "", "", "cmyk")

s = "cmyk(0, 100%, 50%, 0%)"
errs = validate.Var(s, "cmyk")
NotEqual(t, errs, nil)
AssertError(t, errs, "", "", "", "", "cmyk")

s = "cmyk(0%, 101%, 50%)"
errs = validate.Var(s, "cmyk")
NotEqual(t, errs, nil)
AssertError(t, errs, "", "", "", "", "cmyk")

s = "cmyk(0%, 100%, 50%)"
errs = validate.Var(s, "cmyk")
NotEqual(t, errs, nil)
AssertError(t, errs, "", "", "", "", "cmyk")

s = "cmyk(0%, 0%, 0%)"
errs = validate.Var(s, "cmyk")
NotEqual(t, errs, nil)
AssertError(t, errs, "", "", "", "", "cmyk")

i := 1
errs = validate.Var(i, "cmyk")
NotEqual(t, errs, nil)
AssertError(t, errs, "", "", "", "", "cmyk")
}
func TestHsla(t *testing.T) {
validate := New()

Expand Down