Skip to content

Commit a7ca4a1

Browse files
Dean KarnDean Karn
authored andcommitted
add default translations for "en"
- need help creating more.
1 parent 532878b commit a7ca4a1

File tree

8 files changed

+2066
-77
lines changed

8 files changed

+2066
-77
lines changed

README.md

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Package validator
22
================
33
<img align="right" src="https://raw.githubusercontent.com/go-playground/validator/v9/logo.png">
44
[![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)
5-
![Project status](https://img.shields.io/badge/version-9.0.0-green.svg)
5+
![Project status](https://img.shields.io/badge/version-9.1.0-green.svg)
66
[![Build Status](https://semaphoreci.com/api/v1/joeybloggs/validator/branches/v9/badge.svg)](https://semaphoreci.com/joeybloggs/validator)
77
[![Coverage Status](https://coveralls.io/repos/go-playground/validator/badge.svg?branch=v9&service=github)](https://coveralls.io/github/go-playground/validator?branch=v9)
88
[![Go Report Card](https://goreportcard.com/badge/github.com/go-playground/validator)](https://goreportcard.com/report/github.com/go-playground/validator)
@@ -333,54 +333,54 @@ Benchmarks
333333
------
334334
###### Run on MacBook Pro (Retina, 15-inch, Late 2013) 2.6 GHz Intel Core i7 16 GB 1600 MHz DDR3 using Go version go1.7 darwin/amd64
335335
```go
336-
BenchmarkFieldSuccess-8 20000000 108 ns/op 0 B/op 0 allocs/op
337-
BenchmarkFieldSuccessParallel-8 50000000 35.7 ns/op 0 B/op 0 allocs/op
338-
BenchmarkFieldFailure-8 5000000 320 ns/op 192 B/op 4 allocs/op
339-
BenchmarkFieldFailureParallel-8 20000000 113 ns/op 192 B/op 4 allocs/op
340-
BenchmarkFieldDiveSuccess-8 2000000 726 ns/op 201 B/op 11 allocs/op
341-
BenchmarkFieldDiveSuccessParallel-8 10000000 263 ns/op 201 B/op 11 allocs/op
342-
BenchmarkFieldDiveFailure-8 2000000 939 ns/op 396 B/op 16 allocs/op
343-
BenchmarkFieldDiveFailureParallel-8 5000000 382 ns/op 397 B/op 16 allocs/op
344-
BenchmarkFieldCustomTypeSuccess-8 5000000 268 ns/op 32 B/op 2 allocs/op
345-
BenchmarkFieldCustomTypeSuccessParallel-8 20000000 87.8 ns/op 32 B/op 2 allocs/op
346-
BenchmarkFieldCustomTypeFailure-8 5000000 310 ns/op 192 B/op 4 allocs/op
347-
BenchmarkFieldCustomTypeFailureParallel-8 20000000 131 ns/op 192 B/op 4 allocs/op
348-
BenchmarkFieldOrTagSuccess-8 2000000 889 ns/op 16 B/op 1 allocs/op
349-
BenchmarkFieldOrTagSuccessParallel-8 5000000 418 ns/op 16 B/op 1 allocs/op
350-
BenchmarkFieldOrTagFailure-8 3000000 546 ns/op 208 B/op 5 allocs/op
351-
BenchmarkFieldOrTagFailureParallel-8 3000000 450 ns/op 208 B/op 5 allocs/op
352-
BenchmarkStructLevelValidationSuccess-8 5000000 336 ns/op 32 B/op 2 allocs/op
353-
BenchmarkStructLevelValidationSuccessParallel-8 20000000 123 ns/op 32 B/op 2 allocs/op
354-
BenchmarkStructLevelValidationFailure-8 2000000 611 ns/op 288 B/op 8 allocs/op
355-
BenchmarkStructLevelValidationFailureParallel-8 5000000 298 ns/op 288 B/op 8 allocs/op
356-
BenchmarkStructSimpleCustomTypeSuccess-8 2000000 555 ns/op 32 B/op 2 allocs/op
357-
BenchmarkStructSimpleCustomTypeSuccessParallel-8 10000000 197 ns/op 32 B/op 2 allocs/op
358-
BenchmarkStructSimpleCustomTypeFailure-8 2000000 811 ns/op 392 B/op 9 allocs/op
359-
BenchmarkStructSimpleCustomTypeFailureParallel-8 5000000 370 ns/op 408 B/op 10 allocs/op
360-
BenchmarkStructPartialSuccess-8 2000000 676 ns/op 256 B/op 6 allocs/op
361-
BenchmarkStructPartialSuccessParallel-8 5000000 301 ns/op 256 B/op 6 allocs/op
362-
BenchmarkStructPartialFailure-8 1000000 1001 ns/op 464 B/op 11 allocs/op
363-
BenchmarkStructPartialFailureParallel-8 3000000 436 ns/op 464 B/op 11 allocs/op
364-
BenchmarkStructExceptSuccess-8 1000000 1038 ns/op 480 B/op 12 allocs/op
365-
BenchmarkStructExceptSuccessParallel-8 10000000 281 ns/op 240 B/op 5 allocs/op
366-
BenchmarkStructExceptFailure-8 2000000 863 ns/op 448 B/op 10 allocs/op
367-
BenchmarkStructExceptFailureParallel-8 3000000 379 ns/op 448 B/op 10 allocs/op
368-
BenchmarkStructSimpleCrossFieldSuccess-8 3000000 549 ns/op 72 B/op 3 allocs/op
369-
BenchmarkStructSimpleCrossFieldSuccessParallel-8 10000000 192 ns/op 72 B/op 3 allocs/op
370-
BenchmarkStructSimpleCrossFieldFailure-8 2000000 783 ns/op 288 B/op 8 allocs/op
371-
BenchmarkStructSimpleCrossFieldFailureParallel-8 5000000 296 ns/op 288 B/op 8 allocs/op
372-
BenchmarkStructSimpleCrossStructCrossFieldSuccess-8 2000000 837 ns/op 80 B/op 4 allocs/op
373-
BenchmarkStructSimpleCrossStructCrossFieldSuccessParallel-8 5000000 284 ns/op 80 B/op 4 allocs/op
374-
BenchmarkStructSimpleCrossStructCrossFieldFailure-8 1000000 1110 ns/op 304 B/op 9 allocs/op
375-
BenchmarkStructSimpleCrossStructCrossFieldFailureParallel-8 3000000 416 ns/op 304 B/op 9 allocs/op
376-
BenchmarkStructSimpleSuccess-8 5000000 380 ns/op 0 B/op 0 allocs/op
377-
BenchmarkStructSimpleSuccessParallel-8 20000000 114 ns/op 0 B/op 0 allocs/op
378-
BenchmarkStructSimpleFailure-8 2000000 760 ns/op 392 B/op 9 allocs/op
379-
BenchmarkStructSimpleFailureParallel-8 5000000 353 ns/op 392 B/op 9 allocs/op
380-
BenchmarkStructComplexSuccess-8 1000000 2100 ns/op 128 B/op 8 allocs/op
381-
BenchmarkStructComplexSuccessParallel-8 2000000 662 ns/op 128 B/op 8 allocs/op
382-
BenchmarkStructComplexFailure-8 200000 5080 ns/op 2833 B/op 53 allocs/op
383-
BenchmarkStructComplexFailureParallel-8 1000000 2159 ns/op 2833 B/op 53 allocs/op
336+
BenchmarkFieldSuccess-8 20000000 105 ns/op 0 B/op 0 allocs/op
337+
BenchmarkFieldSuccessParallel-8 50000000 35.1 ns/op 0 B/op 0 allocs/op
338+
BenchmarkFieldFailure-8 5000000 337 ns/op 208 B/op 4 allocs/op
339+
BenchmarkFieldFailureParallel-8 20000000 120 ns/op 208 B/op 4 allocs/op
340+
BenchmarkFieldDiveSuccess-8 2000000 716 ns/op 201 B/op 11 allocs/op
341+
BenchmarkFieldDiveSuccessParallel-8 10000000 253 ns/op 201 B/op 11 allocs/op
342+
BenchmarkFieldDiveFailure-8 1000000 1060 ns/op 412 B/op 16 allocs/op
343+
BenchmarkFieldDiveFailureParallel-8 5000000 360 ns/op 413 B/op 16 allocs/op
344+
BenchmarkFieldCustomTypeSuccess-8 5000000 299 ns/op 32 B/op 2 allocs/op
345+
BenchmarkFieldCustomTypeSuccessParallel-8 20000000 86.0 ns/op 32 B/op 2 allocs/op
346+
BenchmarkFieldCustomTypeFailure-8 5000000 341 ns/op 208 B/op 4 allocs/op
347+
BenchmarkFieldCustomTypeFailureParallel-8 20000000 140 ns/op 208 B/op 4 allocs/op
348+
BenchmarkFieldOrTagSuccess-8 2000000 893 ns/op 16 B/op 1 allocs/op
349+
BenchmarkFieldOrTagSuccessParallel-8 5000000 431 ns/op 16 B/op 1 allocs/op
350+
BenchmarkFieldOrTagFailure-8 2000000 563 ns/op 224 B/op 5 allocs/op
351+
BenchmarkFieldOrTagFailureParallel-8 5000000 417 ns/op 224 B/op 5 allocs/op
352+
BenchmarkStructLevelValidationSuccess-8 5000000 339 ns/op 32 B/op 2 allocs/op
353+
BenchmarkStructLevelValidationSuccessParallel-8 20000000 114 ns/op 32 B/op 2 allocs/op
354+
BenchmarkStructLevelValidationFailure-8 2000000 630 ns/op 304 B/op 8 allocs/op
355+
BenchmarkStructLevelValidationFailureParallel-8 5000000 291 ns/op 304 B/op 8 allocs/op
356+
BenchmarkStructSimpleCustomTypeSuccess-8 3000000 540 ns/op 32 B/op 2 allocs/op
357+
BenchmarkStructSimpleCustomTypeSuccessParallel-8 10000000 176 ns/op 32 B/op 2 allocs/op
358+
BenchmarkStructSimpleCustomTypeFailure-8 2000000 821 ns/op 424 B/op 9 allocs/op
359+
BenchmarkStructSimpleCustomTypeFailureParallel-8 5000000 336 ns/op 440 B/op 10 allocs/op
360+
BenchmarkStructPartialSuccess-8 2000000 686 ns/op 256 B/op 6 allocs/op
361+
BenchmarkStructPartialSuccessParallel-8 5000000 282 ns/op 256 B/op 6 allocs/op
362+
BenchmarkStructPartialFailure-8 2000000 931 ns/op 480 B/op 11 allocs/op
363+
BenchmarkStructPartialFailureParallel-8 5000000 394 ns/op 480 B/op 11 allocs/op
364+
BenchmarkStructExceptSuccess-8 1000000 1017 ns/op 496 B/op 12 allocs/op
365+
BenchmarkStructExceptSuccessParallel-8 10000000 233 ns/op 240 B/op 5 allocs/op
366+
BenchmarkStructExceptFailure-8 2000000 864 ns/op 464 B/op 10 allocs/op
367+
BenchmarkStructExceptFailureParallel-8 5000000 393 ns/op 464 B/op 10 allocs/op
368+
BenchmarkStructSimpleCrossFieldSuccess-8 3000000 552 ns/op 72 B/op 3 allocs/op
369+
BenchmarkStructSimpleCrossFieldSuccessParallel-8 10000000 202 ns/op 72 B/op 3 allocs/op
370+
BenchmarkStructSimpleCrossFieldFailure-8 2000000 798 ns/op 304 B/op 8 allocs/op
371+
BenchmarkStructSimpleCrossFieldFailureParallel-8 5000000 356 ns/op 304 B/op 8 allocs/op
372+
BenchmarkStructSimpleCrossStructCrossFieldSuccess-8 2000000 825 ns/op 80 B/op 4 allocs/op
373+
BenchmarkStructSimpleCrossStructCrossFieldSuccessParallel-8 5000000 300 ns/op 80 B/op 4 allocs/op
374+
BenchmarkStructSimpleCrossStructCrossFieldFailure-8 2000000 1103 ns/op 320 B/op 9 allocs/op
375+
BenchmarkStructSimpleCrossStructCrossFieldFailureParallel-8 3000000 433 ns/op 320 B/op 9 allocs/op
376+
BenchmarkStructSimpleSuccess-8 5000000 360 ns/op 0 B/op 0 allocs/op
377+
BenchmarkStructSimpleSuccessParallel-8 20000000 110 ns/op 0 B/op 0 allocs/op
378+
BenchmarkStructSimpleFailure-8 2000000 783 ns/op 424 B/op 9 allocs/op
379+
BenchmarkStructSimpleFailureParallel-8 5000000 358 ns/op 424 B/op 9 allocs/op
380+
BenchmarkStructComplexSuccess-8 1000000 2120 ns/op 128 B/op 8 allocs/op
381+
BenchmarkStructComplexSuccessParallel-8 2000000 659 ns/op 128 B/op 8 allocs/op
382+
BenchmarkStructComplexFailure-8 300000 5126 ns/op 3041 B/op 53 allocs/op
383+
BenchmarkStructComplexFailureParallel-8 1000000 2261 ns/op 3041 B/op 53 allocs/op
384384
```
385385

386386
Complimentary Software

errors.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ func (ve ValidationErrors) Translate(ut ut.Translator) ValidationErrorsTranslati
6363

6464
for i := 0; i < len(ve); i++ {
6565
fe = ve[i].(*fieldError)
66+
67+
// // in case an Anonymous struct was used, ensure that the key
68+
// // would be 'Username' instead of ".Username"
69+
// if len(fe.ns) > 0 && fe.ns[:1] == "." {
70+
// trans[fe.ns[1:]] = fe.Translate(ut)
71+
// continue
72+
// }
73+
6674
trans[fe.ns] = fe.Translate(ut)
6775
}
6876

@@ -192,8 +200,18 @@ func (fe *fieldError) StructNamespace() string {
192200
// Field returns the fields name with the tag name taking precedence over the
193201
// fields actual name.
194202
func (fe *fieldError) Field() string {
195-
// return fe.field
203+
196204
return fe.ns[len(fe.ns)-int(fe.fieldLen):]
205+
// // return fe.field
206+
// fld := fe.ns[len(fe.ns)-int(fe.fieldLen):]
207+
208+
// log.Println("FLD:", fld)
209+
210+
// if len(fld) > 0 && fld[:1] == "." {
211+
// return fld[1:]
212+
// }
213+
214+
// return fld
197215
}
198216

199217
// returns the fields actual name from the struct, when able to determine.
@@ -236,7 +254,7 @@ func (fe *fieldError) Error() string {
236254
// as calling fe.Error()
237255
func (fe *fieldError) Translate(ut ut.Translator) string {
238256

239-
m, ok := fe.v.transTagFunc[ut.Locale()]
257+
m, ok := fe.v.transTagFunc[ut]
240258
if !ok {
241259
return fe.Error()
242260
}

examples/translations/main.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"gopkg.in/go-playground/validator.v9"
7+
)
8+
9+
// User contains user information
10+
type User struct {
11+
FirstName string `validate:"required"`
12+
LastName string `validate:"required"`
13+
Age uint8 `validate:"gte=0,lte=130"`
14+
Email string `validate:"required,email"`
15+
FavouriteColor string `validate:"iscolor"` // alias for 'hexcolor|rgb|rgba|hsl|hsla'
16+
Addresses []*Address `validate:"required,dive,required"` // a person can have a home and cottage...
17+
}
18+
19+
// Address houses a users address information
20+
type Address struct {
21+
Street string `validate:"required"`
22+
City string `validate:"required"`
23+
Planet string `validate:"required"`
24+
Phone string `validate:"required"`
25+
}
26+
27+
// use a single instance of Validate, it caches struct info
28+
var validate *validator.Validate
29+
30+
func main() {
31+
32+
validate = validator.New()
33+
34+
validateStruct()
35+
validateVariable()
36+
}
37+
38+
func validateStruct() {
39+
40+
address := &Address{
41+
Street: "Eavesdown Docks",
42+
Planet: "Persphone",
43+
Phone: "none",
44+
}
45+
46+
user := &User{
47+
FirstName: "Badger",
48+
LastName: "Smith",
49+
Age: 135,
50+
51+
FavouriteColor: "#000-",
52+
Addresses: []*Address{address},
53+
}
54+
55+
// returns nil or ValidationErrors ( map[string]*FieldError )
56+
err := validate.Struct(user)
57+
if err != nil {
58+
59+
// this check is only needed when your code could produce
60+
// an invalid value for validation such as interface with nil
61+
// value most including myself do not usually have code like this.
62+
if _, ok := err.(*validator.InvalidValidationError); ok {
63+
fmt.Println(err)
64+
return
65+
}
66+
67+
for _, err := range err.(validator.ValidationErrors) {
68+
69+
fmt.Println(err.Namespace())
70+
fmt.Println(err.Field())
71+
fmt.Println(err.StructNamespace()) // can differ when a custom TagNameFunc is registered or
72+
fmt.Println(err.StructField()) // by passing alt name to ReportError like below
73+
fmt.Println(err.Tag())
74+
fmt.Println(err.ActualTag())
75+
fmt.Println(err.Kind())
76+
fmt.Println(err.Type())
77+
fmt.Println(err.Value())
78+
fmt.Println(err.Param())
79+
fmt.Println()
80+
}
81+
82+
// from here you can create your own error messages in whatever language you wish
83+
return
84+
}
85+
86+
// save user to database
87+
}
88+
89+
func validateVariable() {
90+
91+
myEmail := "joeybloggs.gmail.com"
92+
93+
errs := validate.Var(myEmail, "required,email")
94+
95+
if errs != nil {
96+
fmt.Println(errs) // output: Key: "" Error:Field validation for "" failed on the "email" tag
97+
return
98+
}
99+
100+
// email ok, move on
101+
}

0 commit comments

Comments
 (0)