Skip to content

Commit a7cc53a

Browse files
Dean KarnDean Karn
authored andcommitted
documentation, examples and template updates.
1 parent 9ba3297 commit a7cc53a

File tree

6 files changed

+98
-6
lines changed

6 files changed

+98
-6
lines changed

.github/CONTRIBUTING.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Contribution Guidelines
2+
3+
## Quality Standard
4+
5+
To ensure the continued stability of this package tests are required to be written or alreaady exist in order for a pull request to be merged.
6+
7+
## Reporting issues
8+
9+
Please open an issue or join the gitter chat [![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) for any issues, questions or possible enhancements to the package.

.github/ISSUE_TEMPLATE.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
### Package version eg. v8, v9:
2+
3+
4+
5+
### Issue, Question or Enhancement:
6+
7+
8+
9+
### Code sample, to showcase or reproduce:
10+
11+
```go
12+
13+
```

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Fixes Or Enhances # .
2+
3+
**Make sure that you've checked the boxes below before you submit PR:**
4+
- [ ] Tests exist or have been written that cover this particular change.
5+
6+
Change Details:
7+
8+
-
9+
-
10+
-
11+
12+
13+
@go-playground/admins

README.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
Package validator
22
================
33
<img align="right" src="https://raw.githubusercontent.com/go-playground/validator/v9/logo.png">
4-
[![Join the chat at https://gitter.im/bluesuncorp/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/RC1-9.0.0-yellow.svg)
4+
[![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)
66
[![Build Status](https://semaphoreci.com/api/v1/projects/ec20115f-ef1b-4c7d-9393-cc76aba74eb4/530054/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)
@@ -19,6 +19,7 @@ It has the following **unique** features:
1919
- Handles custom field types such as sql driver Valuer see [Valuer](https://golang.org/src/database/sql/driver/types.go?s=1210:1293#L29)
2020
- Alias validation tags, which allows for mapping of several validations to a single tag for easier defining of validations on structs
2121
- Extraction of custom defined Field Name e.g. can specify to extract the JSON name while validating and have it available in the resulting FieldError
22+
- Default validator for the [gin](https://github.com/gin-gonic/gin) web framework; upgrading from v8 to v9 in gin see [here](https://github.com/go-playground/validator/tree/v9/examples/gin-upgrading-overriding)
2223

2324
Installation
2425
------------
@@ -27,10 +28,6 @@ Use go get.
2728

2829
go get gopkg.in/go-playground/validator.v9
2930

30-
or to update
31-
32-
go get -u gopkg.in/go-playground/validator.v9
33-
3431
Then import the validator package into your own code.
3532

3633
import "gopkg.in/go-playground/validator.v9"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package main
2+
3+
import "github.com/gin-gonic/gin/binding"
4+
5+
func main() {
6+
7+
binding.Validator = new(defaultValidator)
8+
9+
// regular gin logic
10+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package main
2+
3+
import (
4+
"reflect"
5+
"sync"
6+
7+
"github.com/gin-gonic/gin/binding"
8+
"gopkg.in/go-playground/validator.v9"
9+
)
10+
11+
type defaultValidator struct {
12+
once sync.Once
13+
validate *validator.Validate
14+
}
15+
16+
var _ binding.StructValidator = &defaultValidator{}
17+
18+
func (v *defaultValidator) ValidateStruct(obj interface{}) error {
19+
20+
if kindOfData(obj) == reflect.Struct {
21+
22+
v.lazyinit()
23+
24+
if err := v.validate.Struct(obj); err != nil {
25+
return error(err)
26+
}
27+
}
28+
29+
return nil
30+
}
31+
32+
func (v *defaultValidator) lazyinit() {
33+
v.once.Do(func() {
34+
v.validate = validator.New()
35+
v.validate.SetTagName("binding")
36+
37+
// add any custom validations etc. here
38+
})
39+
}
40+
41+
func kindOfData(data interface{}) reflect.Kind {
42+
43+
value := reflect.ValueOf(data)
44+
valueType := value.Kind()
45+
46+
if valueType == reflect.Ptr {
47+
valueType = value.Elem().Kind()
48+
}
49+
return valueType
50+
}

0 commit comments

Comments
 (0)