Skip to content

Commit

Permalink
Merge branch 'master' into add_persian
Browse files Browse the repository at this point in the history
  • Loading branch information
deankarn committed Jul 8, 2021
2 parents e573ee7 + af8b8d1 commit c1c416e
Show file tree
Hide file tree
Showing 7 changed files with 1,025 additions and 5 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,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-10.6.1-green.svg)
![Project status](https://img.shields.io/badge/version-10.6.2-green.svg)
[![Build Status](https://travis-ci.org/go-playground/validator.svg?branch=master)](https://travis-ci.org/go-playground/validator)
[![Coverage Status](https://coveralls.io/repos/go-playground/validator/badge.svg?branch=master&service=github)](https://coveralls.io/github/go-playground/validator?branch=master)
[![Go Report Card](https://goreportcard.com/badge/github.com/go-playground/validator)](https://goreportcard.com/report/github.com/go-playground/validator)
Expand Down Expand Up @@ -43,7 +43,7 @@ They return type error to avoid the issue discussed in the following, where err
* http://stackoverflow.com/a/29138676/3158232
* https://github.com/go-playground/validator/issues/134

Validator only InvalidValidationError for bad validation input, nil or ValidationErrors as type error; so, in your code all you need to do is check if the error returned is not nil, and if it's not check if error is InvalidValidationError ( if necessary, most of the time it isn't ) type cast it to type ValidationErrors like so:
Validator returns only InvalidValidationError for bad validation input, nil or ValidationErrors as type error; so, in your code all you need to do is check if the error returned is not nil, and if it's not check if error is InvalidValidationError ( if necessary, most of the time it isn't ) type cast it to type ValidationErrors like so:

```go
err := validate.Struct(mystruct)
Expand Down
7 changes: 7 additions & 0 deletions baked_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ var (
"iso3166_1_alpha2": isIso3166Alpha2,
"iso3166_1_alpha3": isIso3166Alpha3,
"iso3166_1_alpha_numeric": isIso3166AlphaNumeric,
"iso3166_2": isIso31662,
"bcp47_language_tag": isBCP47LanguageTag,
"postcode_iso3166_alpha2": isPostcodeByIso3166Alpha2,
"postcode_iso3166_alpha2_field": isPostcodeByIso3166Alpha2Field,
Expand Down Expand Up @@ -2345,6 +2346,12 @@ func isIso3166AlphaNumeric(fl FieldLevel) bool {
return iso3166_1_alpha_numeric[code]
}

// isIso31662 is the validation function for validating if the current field's value is a valid iso3166-2 code.
func isIso31662(fl FieldLevel) bool {
val := fl.Field().String()
return iso3166_2[val]
}

// isBCP47LanguageTag is the validation function for validating if the current field's value is a valid BCP 47 language tag, as parsed by language.Parse
func isBCP47LanguageTag(fl FieldLevel) bool {
field := fl.Field()
Expand Down
970 changes: 970 additions & 0 deletions country_codes.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion translations/ja/ja.go
Original file line number Diff line number Diff line change
Expand Up @@ -1226,7 +1226,7 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
},
{
tag: "uuid5",
translation: "{0}はバージョンが4の正しいUUIDでなければなりません",
translation: "{0}はバージョンが5の正しいUUIDでなければなりません",
override: false,
},
{
Expand Down
2 changes: 1 addition & 1 deletion translations/ja/ja_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ func TestTranslations(t *testing.T) {
},
{
ns: "Test.UUID5",
expected: "UUID5はバージョンが4の正しいUUIDでなければなりません",
expected: "UUID5はバージョンが5の正しいUUIDでなければなりません",
},
{
ns: "Test.ISBN",
Expand Down
2 changes: 1 addition & 1 deletion validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func (v *validate) traverseField(ctx context.Context, parent reflect.Value, curr
}
}

if !ct.hasTag {
if ct == nil || !ct.hasTag {
return
}

Expand Down
43 changes: 43 additions & 0 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3285,6 +3285,21 @@ func TestMapDiveValidation(t *testing.T) {
s := fmt.Sprint(errs.Error())
NotEqual(t, s, "")

type TestMapInterface struct {
Errs map[int]interface{} `validate:"dive"`
}

mit := map[int]interface{}{0: Inner{"ok"}, 1: Inner{""}, 3: nil, 5: "string", 6: 33}

msi := &TestMapInterface{
Errs: mit,
}

errs = validate.Struct(msi)
NotEqual(t, errs, nil)
Equal(t, len(errs.(ValidationErrors)), 1)
AssertError(t, errs, "TestMapInterface.Errs[1].Name", "TestMapInterface.Errs[1].Name", "Name", "Name", "required")

type TestMapTimeStruct struct {
Errs map[int]*time.Time `validate:"gt=0,dive,required"`
}
Expand Down Expand Up @@ -10941,6 +10956,34 @@ func TestIsIso3166Alpha2Validation(t *testing.T) {
}
}

func TestIsIso31662Validation(t *testing.T) {
tests := []struct {
value string `validate:"iso3166_2"`
expected bool
}{
{"US-FL", true},
{"US-F", false},
{"US", false},
}

validate := New()

for i, test := range tests {

errs := validate.Var(test.value, "iso3166_2")

if test.expected {
if !IsEqual(errs, nil) {
t.Fatalf("Index: %d iso3166_2 failed Error: %s", i, errs)
}
} else {
if IsEqual(errs, nil) {
t.Fatalf("Index: %d iso3166_2 failed Error: %s", i, errs)
}
}
}
}

func TestIsIso3166Alpha3Validation(t *testing.T) {
tests := []struct {
value string `validate:"iso3166_1_alpha3"`
Expand Down

0 comments on commit c1c416e

Please sign in to comment.