https://github.com/restuwahyu13/go-playground-converter
Formatter error response inspiration like express-validator in nodejs build on top go-playground-validator
https://github.com/restuwahyu13/go-playground-converter
Last synced: 5 days ago
JSON representation
Formatter error response inspiration like express-validator in nodejs build on top go-playground-validator
- Host: GitHub
- URL: https://github.com/restuwahyu13/go-playground-converter
- Owner: restuwahyu13
- License: mit
- Created: 2021-04-28T13:33:45.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2024-03-02T08:33:11.000Z (about 1 year ago)
- Last Synced: 2024-06-18T21:50:21.913Z (10 months ago)
- Language: Go
- Homepage:
- Size: 102 KB
- Stars: 14
- Watchers: 1
- Forks: 7
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
## Go Playground Converter

[](https://goreportcard.com/report/github.com/restuwahyu13/go-playground-converter) [](https://github.com/restuwahyu13/go-playground-converter/blob/master/CONTRIBUTING.md)**go-playground-converter** is formatter error response inspiration like express-validator in Node.js. `go-playground-converter` builds on top of
`go-playground/validator`, see more about struct references and follow this [documentation](https://github.com/go-playground/validator), And for the new version of `go-playground-converter`, you can use custom messages using `gpc` struct tags, and you need the core `go-playground/validator` you can access use `GoValidator`.- [Go Playground Converter](#go-playground-converter)
- [Installation](#installation)
- [Example Usage Without GPC Tags](#example-usage-without-gpc-tags)
- [Example Usage With GPC Tags](#example-usage-with-gpc-tags)
- [Example With Options](#example-with-options)
- [Example Custom Validation](#example-custom-validation)
- [Example Custom Validation With Gorutine](#example-custom-validation-with-gorutine)
- [Testing](#testing)
- [Bugs](#bugs)
- [Contributing](#contributing)
- [License](#license)### Installation
```sh
$ go get -u github.com/restuwahyu13/go-playground-converter
```### Example Usage Without GPC Tags
```go
package mainimport (
"fmt"
gpc "github.com/restuwahyu13/go-playground-converter"
)type Login struct {
Email string `validate:"required" json:"email"`
Password string `validate:"required" json:"password"`
}func main() {
payload := Login{Email: "", Password: ""}
res, err := gpc.Validator(payload) // if not errors, validator return res & err nil valueif err != nil {
panic(err)
}http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(&res)
})http.ListenAndServe(":3000", nil)
}// {
// "errors": [
// {
// "msg": "Email is a required field",
// "param": "Email",
// "tag": "required"
// },
// {
// "msg": "Password is a required field",
// "param": "Password",
// "tag": "required"
// }
// ]
// }
```### Example Usage With GPC Tags
```go
package mainimport (
"fmt"
gpc "github.com/restuwahyu13/go-playground-converter"
)type Login struct {
Email string `validate:"required" gpc:"required=Email tidak boleh kosong" json:"email"`
Password string `validate:"required" gpc:"required=Password tidak boleh kosong" json:"password"`
}func main() {
payload := Login{Email: "", Password: ""}
res, err := gpc.Validator(payload) // if not errors, validator return res & err nil valueif err != nil {
panic(err)
}http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(&res)
})http.ListenAndServe(":3000", nil)
}// {
// "errors": [
// {
// "msg": "Email tidak boleh kosong",
// "param": "Email",
// "tag": "required"
// },
// {
// "msg": "Password tidak boleh kosong",
// "param": "Password",
// "tag": "required"
// }
// ]
// }
```### Example With Options
```go
package mainimport (
"encoding/json"
"net/http""github.com/go-playground/validator/v10"
gpc "github.com/restuwahyu13/go-playground-converter"
)type Login struct {
Email string `validate:"required" json:"email"`
Password string `validate:"required" json:"password"`
}func main() {
payload := Login{Email: "", Password: ""}
res, err := gpc.Validator(payload, validator.WithRequiredStructEnabled()) // if not errors, validator return res & err nil valueif err != nil {
panic(err)
}http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(&res)
})http.ListenAndServe(":3000", nil)
}// {
// "errors": [
// {
// "msg": "Email is a required field",
// "param": "Email",
// "tag": "required"
// },
// {
// "msg": "Password is a required field",
// "param": "Password",
// "tag": "required"
// }
// ]
// }
```### Example Custom Validation
```go
func GoValidator(s interface{}) (*gpc.FormatError, error) {
res, err := gpc.Validator(s) // <- pass your struct from param in hereif err != nil {
return nil, err
}return res, nil
}
```### Example Custom Validation With Gorutine
```go
func GoValidator(s interface{}) (*gpc.FormatError, error) {
var (
wg *sync.WaitGroup = new(sync.WaitGroup)
errChan chan error = make(chan error, 1)
resChan chan *gpc.FormatError = make(chan *gpc.FormatError, 1)
)wg.Add(1)
go func() {
defer wg.Done()res, err := gpc.Validator(s) // <- pass your struct from param in here
if err != nil {
errChan <- err
resChan <- nil
}if res != nil {
errChan <- nil
resChan <- res
}
}()defer close(errChan)
defer close(resChan)wg.Wait()
if err := <-errChan; err != nil {
return nil, err
}return <-resChan, nil
}
```## Testing
- Testing Via Local
```sh
go test --race -v --failfast .
```- Testing Via Docker
```sh
docker build -t go-playground-converter --compress . && docker run go-playground-converter go test --race -v --failfast .
```### Bugs
For information on bugs related to package libraries, please visit
[here](https://github.com/restuwahyu13/go-playground-converter/issues)### Contributing
Want to make **Go Playground Converter** more perfect ? Let's contribute and follow the
[contribution guide.](https://github.com/restuwahyu13/go-playground-converter/blob/master/CONTRIBUTING.md)### License
- [MIT License](https://github.com/restuwahyu13/go-playground-converter/blob/master/LICENSE.md)