From a7cc53a707581c655fefb3e5dfd6576bf14fa995 Mon Sep 17 00:00:00 2001 From: Dean Karn Date: Thu, 1 Sep 2016 08:36:55 -0400 Subject: [PATCH] documentation, examples and template updates. --- .github/CONTRIBUTING.md | 9 ++++ .github/ISSUE_TEMPLATE.md | 13 +++++ .github/PULL_REQUEST_TEMPLATE.md | 13 +++++ README.md | 9 ++-- examples/gin-upgrading-overriding/main.go | 10 ++++ examples/gin-upgrading-overriding/v8_to_v9.go | 50 +++++++++++++++++++ 6 files changed, 98 insertions(+), 6 deletions(-) create mode 100644 .github/CONTRIBUTING.md create mode 100644 .github/ISSUE_TEMPLATE.md create mode 100644 .github/PULL_REQUEST_TEMPLATE.md create mode 100644 examples/gin-upgrading-overriding/main.go create mode 100644 examples/gin-upgrading-overriding/v8_to_v9.go diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md new file mode 100644 index 0000000..9b9450e --- /dev/null +++ b/.github/CONTRIBUTING.md @@ -0,0 +1,9 @@ +# Contribution Guidelines + +## Quality Standard + +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. + +## Reporting issues + +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. \ No newline at end of file diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md new file mode 100644 index 0000000..8afb259 --- /dev/null +++ b/.github/ISSUE_TEMPLATE.md @@ -0,0 +1,13 @@ +### Package version eg. v8, v9: + + + +### Issue, Question or Enhancement: + + + +### Code sample, to showcase or reproduce: + +```go + +``` diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 0000000..7bebde3 --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,13 @@ +Fixes Or Enhances # . + +**Make sure that you've checked the boxes below before you submit PR:** +- [ ] Tests exist or have been written that cover this particular change. + +Change Details: + +- +- +- + + +@go-playground/admins \ No newline at end of file diff --git a/README.md b/README.md index 192730c..262af8e 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ Package validator ================ -[![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) -![Project status](https://img.shields.io/badge/RC1-9.0.0-yellow.svg) +[![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.0.0-green.svg) [![Build Status](https://semaphoreci.com/api/v1/projects/ec20115f-ef1b-4c7d-9393-cc76aba74eb4/530054/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) @@ -19,6 +19,7 @@ It has the following **unique** features: - Handles custom field types such as sql driver Valuer see [Valuer](https://golang.org/src/database/sql/driver/types.go?s=1210:1293#L29) - Alias validation tags, which allows for mapping of several validations to a single tag for easier defining of validations on structs - 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 +- 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) Installation ------------ @@ -27,10 +28,6 @@ Use go get. go get gopkg.in/go-playground/validator.v9 -or to update - - go get -u gopkg.in/go-playground/validator.v9 - Then import the validator package into your own code. import "gopkg.in/go-playground/validator.v9" diff --git a/examples/gin-upgrading-overriding/main.go b/examples/gin-upgrading-overriding/main.go new file mode 100644 index 0000000..46fdf2a --- /dev/null +++ b/examples/gin-upgrading-overriding/main.go @@ -0,0 +1,10 @@ +package main + +import "github.com/gin-gonic/gin/binding" + +func main() { + + binding.Validator = new(defaultValidator) + + // regular gin logic +} diff --git a/examples/gin-upgrading-overriding/v8_to_v9.go b/examples/gin-upgrading-overriding/v8_to_v9.go new file mode 100644 index 0000000..272b670 --- /dev/null +++ b/examples/gin-upgrading-overriding/v8_to_v9.go @@ -0,0 +1,50 @@ +package main + +import ( + "reflect" + "sync" + + "github.com/gin-gonic/gin/binding" + "gopkg.in/go-playground/validator.v9" +) + +type defaultValidator struct { + once sync.Once + validate *validator.Validate +} + +var _ binding.StructValidator = &defaultValidator{} + +func (v *defaultValidator) ValidateStruct(obj interface{}) error { + + if kindOfData(obj) == reflect.Struct { + + v.lazyinit() + + if err := v.validate.Struct(obj); err != nil { + return error(err) + } + } + + return nil +} + +func (v *defaultValidator) lazyinit() { + v.once.Do(func() { + v.validate = validator.New() + v.validate.SetTagName("binding") + + // add any custom validations etc. here + }) +} + +func kindOfData(data interface{}) reflect.Kind { + + value := reflect.ValueOf(data) + valueType := value.Kind() + + if valueType == reflect.Ptr { + valueType = value.Elem().Kind() + } + return valueType +}