Skip to content

rafaelbreno/go-request-validator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Request Validator

PROJECT IN A BREAK

About

TODO List

  • Define Folder Structure
  • Create a Package
  • Call one Function
    • Maybe like this:
    •     func Validate(reqStruct interface{}, req *http.Request) {
              // so it would only be called and sended those 
              // params
              dec := json.NewDecoder(req.Body)
              dec.Decode(reqStruct)
              /* ... something like this ... */
          }
  • POST Validation
    • Simple
      • Required
      • Type
      • Length
    • Medium Level
      • Unique
      • Regex
  • Database Connection

Define Folder Structure

Structure

  • go-request-validator/ - Package Root Dir
    • test/ - Folder for tests
    • tools/ - Helpers, global func
    • pkg/ - Packages that can be used by everything
    • config/ - App config files
    • cmd/ - Main Applications

Code Snippets

  • Receiving json data
  •     package main
    
        import (
            "log"
            "net/http"
            "encoding/json"
        )
    
        type test_struc struct {
        //  varName varType `json:"keyName"`
            Name    string  `json:"name"`
            Email   string  `json:"email"`
            Age     int     `json:"age"`
        }
    
        // Func Handler
        func getReq(w http.ResponseWriter, req *http.Request) {
            dec := json.NewDecoder(req.Body)
            var t test_struc
            err := dec.Decode(&t)
            if(err != nil) {
                panic(err)
            }
            log.Println(t)
        }
    
        func main()  {
        //  http.HandleFunc("/endpoint", func)
            http.HandleFunc("/", getReq)
    
        //  http.ListenAndServe(":port", nil)
            http.ListenAndServe(":8080", nil)
        }
  • Sending:
  •     {
            "name": "Rafael Breno de Vasconcellos Santos",
            "email": "[email protected]",
            "age": 20
        }
  • The struct t will be:

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages