Merge pull request #262 from go-playground/unicode-alphas

Add alphaunicode & aplhanumericunicode
pull/267/head v9.3.0
Dean Karn 8 years ago committed by GitHub
commit 941ce2cabc
  1. 2
      README.md
  2. 158
      baked_in.go
  3. 16
      doc.go
  4. 104
      regexes.go
  5. 89
      validator_test.go

@ -2,7 +2,7 @@ Package validator
================ ================
<img align="right" src="https://raw.githubusercontent.com/go-playground/validator/v9/logo.png"> <img align="right" src="https://raw.githubusercontent.com/go-playground/validator/v9/logo.png">
[![Join the chat at https://gitter.im/go-playground/validator](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/go-playground/validator?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) [![Join the chat at https://gitter.im/go-playground/validator](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/go-playground/validator?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
![Project status](https://img.shields.io/badge/version-9.2.2-green.svg) ![Project status](https://img.shields.io/badge/version-9.3.0-green.svg)
[![Build Status](https://semaphoreci.com/api/v1/joeybloggs/validator/branches/v9/badge.svg)](https://semaphoreci.com/joeybloggs/validator) [![Build Status](https://semaphoreci.com/api/v1/joeybloggs/validator/branches/v9/badge.svg)](https://semaphoreci.com/joeybloggs/validator)
[![Coverage Status](https://coveralls.io/repos/go-playground/validator/badge.svg?branch=v9&service=github)](https://coveralls.io/github/go-playground/validator?branch=v9) [![Coverage Status](https://coveralls.io/repos/go-playground/validator/badge.svg?branch=v9&service=github)](https://coveralls.io/github/go-playground/validator?branch=v9)
[![Go Report Card](https://goreportcard.com/badge/github.com/go-playground/validator)](https://goreportcard.com/report/github.com/go-playground/validator) [![Go Report Card](https://goreportcard.com/badge/github.com/go-playground/validator)](https://goreportcard.com/report/github.com/go-playground/validator)

@ -40,79 +40,81 @@ var (
// you can add, remove or even replace items to suite your needs, // you can add, remove or even replace items to suite your needs,
// or even disregard and use your own map if so desired. // or even disregard and use your own map if so desired.
bakedInValidators = map[string]Func{ bakedInValidators = map[string]Func{
"required": hasValue, "required": hasValue,
"len": hasLengthOf, "len": hasLengthOf,
"min": hasMinOf, "min": hasMinOf,
"max": hasMaxOf, "max": hasMaxOf,
"eq": isEq, "eq": isEq,
"ne": isNe, "ne": isNe,
"lt": isLt, "lt": isLt,
"lte": isLte, "lte": isLte,
"gt": isGt, "gt": isGt,
"gte": isGte, "gte": isGte,
"eqfield": isEqField, "eqfield": isEqField,
"eqcsfield": isEqCrossStructField, "eqcsfield": isEqCrossStructField,
"necsfield": isNeCrossStructField, "necsfield": isNeCrossStructField,
"gtcsfield": isGtCrossStructField, "gtcsfield": isGtCrossStructField,
"gtecsfield": isGteCrossStructField, "gtecsfield": isGteCrossStructField,
"ltcsfield": isLtCrossStructField, "ltcsfield": isLtCrossStructField,
"ltecsfield": isLteCrossStructField, "ltecsfield": isLteCrossStructField,
"nefield": isNeField, "nefield": isNeField,
"gtefield": isGteField, "gtefield": isGteField,
"gtfield": isGtField, "gtfield": isGtField,
"ltefield": isLteField, "ltefield": isLteField,
"ltfield": isLtField, "ltfield": isLtField,
"alpha": isAlpha, "alpha": isAlpha,
"alphanum": isAlphanum, "alphanum": isAlphanum,
"numeric": isNumeric, "alphaunicode": isAlphaUnicode,
"number": isNumber, "alphanumunicode": isAlphanumUnicode,
"hexadecimal": isHexadecimal, "numeric": isNumeric,
"hexcolor": isHEXColor, "number": isNumber,
"rgb": isRGB, "hexadecimal": isHexadecimal,
"rgba": isRGBA, "hexcolor": isHEXColor,
"hsl": isHSL, "rgb": isRGB,
"hsla": isHSLA, "rgba": isRGBA,
"email": isEmail, "hsl": isHSL,
"url": isURL, "hsla": isHSLA,
"uri": isURI, "email": isEmail,
"base64": isBase64, "url": isURL,
"contains": contains, "uri": isURI,
"containsany": containsAny, "base64": isBase64,
"containsrune": containsRune, "contains": contains,
"excludes": excludes, "containsany": containsAny,
"excludesall": excludesAll, "containsrune": containsRune,
"excludesrune": excludesRune, "excludes": excludes,
"isbn": isISBN, "excludesall": excludesAll,
"isbn10": isISBN10, "excludesrune": excludesRune,
"isbn13": isISBN13, "isbn": isISBN,
"uuid": isUUID, "isbn10": isISBN10,
"uuid3": isUUID3, "isbn13": isISBN13,
"uuid4": isUUID4, "uuid": isUUID,
"uuid5": isUUID5, "uuid3": isUUID3,
"ascii": isASCII, "uuid4": isUUID4,
"printascii": isPrintableASCII, "uuid5": isUUID5,
"multibyte": hasMultiByteCharacter, "ascii": isASCII,
"datauri": isDataURI, "printascii": isPrintableASCII,
"latitude": isLatitude, "multibyte": hasMultiByteCharacter,
"longitude": isLongitude, "datauri": isDataURI,
"ssn": isSSN, "latitude": isLatitude,
"ipv4": isIPv4, "longitude": isLongitude,
"ipv6": isIPv6, "ssn": isSSN,
"ip": isIP, "ipv4": isIPv4,
"cidrv4": isCIDRv4, "ipv6": isIPv6,
"cidrv6": isCIDRv6, "ip": isIP,
"cidr": isCIDR, "cidrv4": isCIDRv4,
"tcp4_addr": isTCP4AddrResolvable, "cidrv6": isCIDRv6,
"tcp6_addr": isTCP6AddrResolvable, "cidr": isCIDR,
"tcp_addr": isTCPAddrResolvable, "tcp4_addr": isTCP4AddrResolvable,
"udp4_addr": isUDP4AddrResolvable, "tcp6_addr": isTCP6AddrResolvable,
"udp6_addr": isUDP6AddrResolvable, "tcp_addr": isTCPAddrResolvable,
"udp_addr": isUDPAddrResolvable, "udp4_addr": isUDP4AddrResolvable,
"ip4_addr": isIP4AddrResolvable, "udp6_addr": isUDP6AddrResolvable,
"ip6_addr": isIP6AddrResolvable, "udp_addr": isUDPAddrResolvable,
"ip_addr": isIPAddrResolvable, "ip4_addr": isIP4AddrResolvable,
"unix_addr": isUnixAddrResolvable, "ip6_addr": isIP6AddrResolvable,
"mac": isMAC, "ip_addr": isIPAddrResolvable,
"unix_addr": isUnixAddrResolvable,
"mac": isMAC,
} }
) )
@ -879,6 +881,16 @@ func isAlpha(fl FieldLevel) bool {
return alphaRegex.MatchString(fl.Field().String()) return alphaRegex.MatchString(fl.Field().String())
} }
// IsAlphanumUnicode is the validation function for validating if the current field's value is a valid alphanumeric unicode value.
func isAlphanumUnicode(fl FieldLevel) bool {
return alphaUnicodeNumericRegex.MatchString(fl.Field().String())
}
// IsAlphaUnicode is the validation function for validating if the current field's value is a valid alpha unicode value.
func isAlphaUnicode(fl FieldLevel) bool {
return alphaUnicodeRegex.MatchString(fl.Field().String())
}
// HasValue is the validation function for validating if the current field's value is not the default static value. // HasValue is the validation function for validating if the current field's value is not the default static value.
func hasValue(fl FieldLevel) bool { func hasValue(fl FieldLevel) bool {

@ -463,16 +463,28 @@ to the top level struct.
Alpha Only Alpha Only
This validates that a string value contains alpha characters only This validates that a string value contains ASCII alpha characters only
Usage: alpha Usage: alpha
Alphanumeric Alphanumeric
This validates that a string value contains alphanumeric characters only This validates that a string value contains ASCII alphanumeric characters only
Usage: alphanum Usage: alphanum
Alpha Unicode
This validates that a string value contains unicode alpha characters only
Usage: alphaunicode
Alphanumeric Unicode
This validates that a string value contains unicode alphanumeric characters only
Usage: alphanumunicode
Numeric Numeric
This validates that a string value contains a basic numeric value. This validates that a string value contains a basic numeric value.

@ -3,57 +3,61 @@ package validator
import "regexp" import "regexp"
const ( const (
alphaRegexString = "^[a-zA-Z]+$" alphaRegexString = "^[a-zA-Z]+$"
alphaNumericRegexString = "^[a-zA-Z0-9]+$" alphaNumericRegexString = "^[a-zA-Z0-9]+$"
numericRegexString = "^[-+]?[0-9]+(?:\\.[0-9]+)?$" alphaUnicodeRegexString = "^[\\p{L}]+$"
numberRegexString = "^[0-9]+$" alphaUnicodeNumericRegexString = "^[\\p{L}\\p{N}]+$"
hexadecimalRegexString = "^[0-9a-fA-F]+$" numericRegexString = "^[-+]?[0-9]+(?:\\.[0-9]+)?$"
hexcolorRegexString = "^#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6})$" numberRegexString = "^[0-9]+$"
rgbRegexString = "^rgb\\(\\s*(?:(?:0|[1-9]\\d?|1\\d\\d?|2[0-4]\\d|25[0-5])\\s*,\\s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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}])|(?:(?:[a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\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}])))\\.?$"
iSBN13RegexString = "^(?:(?:97(?:8|9))[0-9]{10})$" base64RegexString = "^(?:[A-Za-z0-9+\\/]{4})*(?:[A-Za-z0-9+\\/]{2}==|[A-Za-z0-9+\\/]{3}=|[A-Za-z0-9+\\/]{4})$"
uUID3RegexString = "^[0-9a-f]{8}-[0-9a-f]{4}-3[0-9a-f]{3}-[0-9a-f]{4}-[0-9a-f]{12}$" iSBN10RegexString = "^(?:[0-9]{9}X|[0-9]{10})$"
uUID4RegexString = "^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$" iSBN13RegexString = "^(?:(?:97(?:8|9))[0-9]{10})$"
uUID5RegexString = "^[0-9a-f]{8}-[0-9a-f]{4}-5[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$" uUID3RegexString = "^[0-9a-f]{8}-[0-9a-f]{4}-3[0-9a-f]{3}-[0-9a-f]{4}-[0-9a-f]{12}$"
uUIDRegexString = "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$" uUID4RegexString = "^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"
aSCIIRegexString = "^[\x00-\x7F]*$" uUID5RegexString = "^[0-9a-f]{8}-[0-9a-f]{4}-5[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"
printableASCIIRegexString = "^[\x20-\x7E]*$" uUIDRegexString = "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"
multibyteRegexString = "[^\x00-\x7F]" aSCIIRegexString = "^[\x00-\x7F]*$"
dataURIRegexString = "^data:.+\\/(.+);base64$" printableASCIIRegexString = "^[\x20-\x7E]*$"
latitudeRegexString = "^[-+]?([1-8]?\\d(\\.\\d+)?|90(\\.0+)?)$" multibyteRegexString = "[^\x00-\x7F]"
longitudeRegexString = "^[-+]?(180(\\.0+)?|((1[0-7]\\d)|([1-9]?\\d))(\\.\\d+)?)$" dataURIRegexString = "^data:.+\\/(.+);base64$"
sSNRegexString = `^\d{3}[- ]?\d{2}[- ]?\d{4}$` latitudeRegexString = "^[-+]?([1-8]?\\d(\\.\\d+)?|90(\\.0+)?)$"
longitudeRegexString = "^[-+]?(180(\\.0+)?|((1[0-7]\\d)|([1-9]?\\d))(\\.\\d+)?)$"
sSNRegexString = `^\d{3}[- ]?\d{2}[- ]?\d{4}$`
) )
var ( var (
alphaRegex = regexp.MustCompile(alphaRegexString) alphaRegex = regexp.MustCompile(alphaRegexString)
alphaNumericRegex = regexp.MustCompile(alphaNumericRegexString) alphaNumericRegex = regexp.MustCompile(alphaNumericRegexString)
numericRegex = regexp.MustCompile(numericRegexString) alphaUnicodeRegex = regexp.MustCompile(alphaUnicodeRegexString)
numberRegex = regexp.MustCompile(numberRegexString) alphaUnicodeNumericRegex = regexp.MustCompile(alphaUnicodeNumericRegexString)
hexadecimalRegex = regexp.MustCompile(hexadecimalRegexString) numericRegex = regexp.MustCompile(numericRegexString)
hexcolorRegex = regexp.MustCompile(hexcolorRegexString) numberRegex = regexp.MustCompile(numberRegexString)
rgbRegex = regexp.MustCompile(rgbRegexString) hexadecimalRegex = regexp.MustCompile(hexadecimalRegexString)
rgbaRegex = regexp.MustCompile(rgbaRegexString) hexcolorRegex = regexp.MustCompile(hexcolorRegexString)
hslRegex = regexp.MustCompile(hslRegexString) rgbRegex = regexp.MustCompile(rgbRegexString)
hslaRegex = regexp.MustCompile(hslaRegexString) rgbaRegex = regexp.MustCompile(rgbaRegexString)
emailRegex = regexp.MustCompile(emailRegexString) hslRegex = regexp.MustCompile(hslRegexString)
base64Regex = regexp.MustCompile(base64RegexString) hslaRegex = regexp.MustCompile(hslaRegexString)
iSBN10Regex = regexp.MustCompile(iSBN10RegexString) emailRegex = regexp.MustCompile(emailRegexString)
iSBN13Regex = regexp.MustCompile(iSBN13RegexString) base64Regex = regexp.MustCompile(base64RegexString)
uUID3Regex = regexp.MustCompile(uUID3RegexString) iSBN10Regex = regexp.MustCompile(iSBN10RegexString)
uUID4Regex = regexp.MustCompile(uUID4RegexString) iSBN13Regex = regexp.MustCompile(iSBN13RegexString)
uUID5Regex = regexp.MustCompile(uUID5RegexString) uUID3Regex = regexp.MustCompile(uUID3RegexString)
uUIDRegex = regexp.MustCompile(uUIDRegexString) uUID4Regex = regexp.MustCompile(uUID4RegexString)
aSCIIRegex = regexp.MustCompile(aSCIIRegexString) uUID5Regex = regexp.MustCompile(uUID5RegexString)
printableASCIIRegex = regexp.MustCompile(printableASCIIRegexString) uUIDRegex = regexp.MustCompile(uUIDRegexString)
multibyteRegex = regexp.MustCompile(multibyteRegexString) aSCIIRegex = regexp.MustCompile(aSCIIRegexString)
dataURIRegex = regexp.MustCompile(dataURIRegexString) printableASCIIRegex = regexp.MustCompile(printableASCIIRegexString)
latitudeRegex = regexp.MustCompile(latitudeRegexString) multibyteRegex = regexp.MustCompile(multibyteRegexString)
longitudeRegex = regexp.MustCompile(longitudeRegexString) dataURIRegex = regexp.MustCompile(dataURIRegexString)
sSNRegex = regexp.MustCompile(sSNRegexString) latitudeRegex = regexp.MustCompile(latitudeRegexString)
longitudeRegex = regexp.MustCompile(longitudeRegexString)
sSNRegex = regexp.MustCompile(sSNRegexString)
) )

@ -5954,6 +5954,11 @@ func TestAlpha(t *testing.T) {
NotEqual(t, errs, nil) NotEqual(t, errs, nil)
AssertError(t, errs, "", "", "", "", "alpha") AssertError(t, errs, "", "", "", "", "alpha")
s = "this is a test string"
errs = validate.Var(s, "alpha")
NotEqual(t, errs, nil)
AssertError(t, errs, "", "", "", "", "alpha")
errs = validate.Var(1, "alpha") errs = validate.Var(1, "alpha")
NotEqual(t, errs, nil) NotEqual(t, errs, nil)
AssertError(t, errs, "", "", "", "", "alpha") AssertError(t, errs, "", "", "", "", "alpha")
@ -6824,3 +6829,87 @@ func TestRequiredPtr(t *testing.T) {
err = validate.Struct(test4) err = validate.Struct(test4)
Equal(t, err, nil) Equal(t, err, nil)
} }
func TestAlphaUnicodeValidation(t *testing.T) {
tests := []struct {
param string
expected bool
}{
{"", false},
{"abc", true},
{"this is a test string", false},
{"这是一个测试字符串", true},
{"123", false},
{"<>@;.-=", false},
{"ひらがな・カタカナ、.漢字", false},
{"あいうえおfoobar", true},
{"test@example.com", false},
{"1234abcDE", false},
{"カタカナ", true},
}
validate := New()
for i, test := range tests {
errs := validate.Var(test.param, "alphaunicode")
if test.expected {
if !IsEqual(errs, nil) {
t.Fatalf("Index: %d Alpha Unicode failed Error: %s", i, errs)
}
} else {
if IsEqual(errs, nil) {
t.Fatalf("Index: %d Alpha Unicode failed Error: %s", i, errs)
} else {
val := getError(errs, "", "")
if val.Tag() != "alphaunicode" {
t.Fatalf("Index: %d Alpha Unicode failed Error: %s", i, errs)
}
}
}
}
}
func TestAlphanumericUnicodeValidation(t *testing.T) {
tests := []struct {
param string
expected bool
}{
{"", false},
{"abc", true},
{"this is a test string", false},
{"这是一个测试字符串", true},
{"\u0031\u0032\u0033", true}, // unicode 5
{"123", true},
{"<>@;.-=", false},
{"ひらがな・カタカナ、.漢字", false},
{"あいうえおfoobar", true},
{"test@example.com", false},
{"1234abcDE", true},
{"カタカナ", true},
}
validate := New()
for i, test := range tests {
errs := validate.Var(test.param, "alphanumunicode")
if test.expected {
if !IsEqual(errs, nil) {
t.Fatalf("Index: %d Alphanum Unicode failed Error: %s", i, errs)
}
} else {
if IsEqual(errs, nil) {
t.Fatalf("Index: %d Alphanum Unicode failed Error: %s", i, errs)
} else {
val := getError(errs, "", "")
if val.Tag() != "alphanumunicode" {
t.Fatalf("Index: %d Alphanum Unicode failed Error: %s", i, errs)
}
}
}
}
}

Loading…
Cancel
Save