gpc

package module
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 22, 2024 License: MIT Imports: 12 Imported by: 50

README

Go Playground Converter

GitHub go.mod Go version Go Report Card PRs Welcome

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, 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.

Installation

$ go get -u github.com/restuwahyu13/go-playground-converter

Example Usage Without GPC Tags

  package main

  import (
  "fmt"
   gpc "github.com/restuwahyu13/go-playground-converter"
  )

  type Login struct {
  	Email    string `validate:"required"`
  	Password string `validate:"required"`
  }

  func main() {
     payload := Login{Email: "", Password: ""}
     res, err := gpc.Validator(payload)

      if err != nil {
        panic(err)
      }

      fmt.Println(res) // if not errors, validator return nil value
  }

  // {
  //   "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

  package main

  import (
  "fmt"
   gpc "github.com/restuwahyu13/go-playground-converter"
  )

  type Login struct {
  	Email    string `validate:"required" gpc:"required=Email tidak boleh kosong"`
  	Password string `validate:"required" gpc:"required=Password tidak boleh kosong"`
  }

  func main() {
     payload := Login{Email: "", Password: ""}
     res, err := gpc.Validator(payload)

      if err != nil {
        panic(err)
      }

      fmt.Println(res) // if not errors, validator return nil value
  }

  // {
  //   "errors": [
  //     {
  //       "msg": "Email tidak boleh kosong",
  //       "param": "Email",
  //       "tag": "required"
  //     },
  //     {
  //       "msg": "Password tidak boleh kosong",
  //       "param": "Password",
  //       "tag": "required"
  //     }
  //   ]
  // }

Testing

  • Testing Via Local

    go test .
    
  • Testing Via Docker

    docker build -t go-playground-converter --compress . && docker run go-playground-converter go test --cover -v --failfast .
    

Bugs

For information on bugs related to package libraries, please visit here

Contributing

Want to make Go Playground Converter more perfect ? Let's contribute and follow the contribution guide.

License

BACK TO TOP

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GoValidator added in v1.0.2

func GoValidator() *validator.Validate

Core module validator from https://github.com/go-playground/validator

Types

type FormatError added in v1.0.2

type FormatError struct {
	Errors []FormatErrorMetadata
}

func Validator added in v0.0.8

func Validator(s interface{}) (*FormatError, error)

Validation request from struct field

type FormatErrorMetadata added in v1.0.2

type FormatErrorMetadata struct {
	Msg   string `json:"msg"`
	Param string `json:"param"`
	Tag   string `json:"tag"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL