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. 12
      baked_in.go
  3. 16
      doc.go
  4. 4
      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">
[![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)
[![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)

@ -64,6 +64,8 @@ var (
"ltfield": isLtField,
"alpha": isAlpha,
"alphanum": isAlphanum,
"alphaunicode": isAlphaUnicode,
"alphanumunicode": isAlphanumUnicode,
"numeric": isNumeric,
"number": isNumber,
"hexadecimal": isHexadecimal,
@ -879,6 +881,16 @@ func isAlpha(fl FieldLevel) bool {
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.
func hasValue(fl FieldLevel) bool {

@ -463,16 +463,28 @@ to the top level struct.
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
Alphanumeric
This validates that a string value contains alphanumeric characters only
This validates that a string value contains ASCII alphanumeric characters only
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
This validates that a string value contains a basic numeric value.

@ -5,6 +5,8 @@ import "regexp"
const (
alphaRegexString = "^[a-zA-Z]+$"
alphaNumericRegexString = "^[a-zA-Z0-9]+$"
alphaUnicodeRegexString = "^[\\p{L}]+$"
alphaUnicodeNumericRegexString = "^[\\p{L}\\p{N}]+$"
numericRegexString = "^[-+]?[0-9]+(?:\\.[0-9]+)?$"
numberRegexString = "^[0-9]+$"
hexadecimalRegexString = "^[0-9a-fA-F]+$"
@ -33,6 +35,8 @@ const (
var (
alphaRegex = regexp.MustCompile(alphaRegexString)
alphaNumericRegex = regexp.MustCompile(alphaNumericRegexString)
alphaUnicodeRegex = regexp.MustCompile(alphaUnicodeRegexString)
alphaUnicodeNumericRegex = regexp.MustCompile(alphaUnicodeNumericRegexString)
numericRegex = regexp.MustCompile(numericRegexString)
numberRegex = regexp.MustCompile(numberRegexString)
hexadecimalRegex = regexp.MustCompile(hexadecimalRegexString)

@ -5954,6 +5954,11 @@ func TestAlpha(t *testing.T) {
NotEqual(t, errs, nil)
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")
NotEqual(t, errs, nil)
AssertError(t, errs, "", "", "", "", "alpha")
@ -6824,3 +6829,87 @@ func TestRequiredPtr(t *testing.T) {
err = validate.Struct(test4)
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