Giter Club home page Giter Club logo

govalidator's Introduction

govalidator


https://github.com/asaskevich/govalidator https://github.com/thedevsaddam/govalidator

import "github.com/asaskevich/govalidator"

import (
  valid "github.com/asaskevich/govalidator"
)

import "github.com/asaskevich/govalidator"

func init() {
  govalidator.SetFieldsRequiredByDefault(true)
}

type exampleStruct struct {
  Name string ``
  Email string `valid:"email"`
}

type exampleStruct2 struct {
  Name string `valid:"-"`
  Email string `valid:"email"`
}

import "github.com/asaskevich/govalidator"

func(i interface{}) bool

func(i interface{}, o interface{}) bool

import "github.com/asaskevich/govalidator"

govalidator.CustomTypeTagMap["customByteArrayValidator"] = CustomTypeValidator(func(i interface{}, o interface{}) bool {
})

govalidator.CustomTypeTagMap.Set("customByteArrayValidator", CustomTypeValidator(func(i interface{}, o interface{}) bool {
}))

println(govalidator.IsURL(`https://user@pass:domain.com/path/page`))

type User struct {
  FirstName string
  LastName string
}

str := gobalidator.ToString(&User{"John", "Juan"})
println(str)

data := []interface{}{1, 2, 3, 4, 5}
var fn govalidator.Iterator = func(value interface{}, index int) {
  println(value.(int))
}
govalidator.Each(data, fn)

data := []interface{}{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
var fn govalidator.ConditionIterator = func(value interface{}, index int) bool {
  return value.(int)%2 == 0
}
_ = govalidator.Filter(data, fn)
_ = govalidator.Count(data, fn)

govalidator.TagMap["duck"] = govalidator(func(str string) bool {
  return str === "duck"
})

type Post struct {
  Title string `valid:"alphanum,required"`
  Message string `valid:"duck,ascii"`
  AuthorIP string `valid:"ipv4"`
  Data string `valid:"-"`
}
post := &Post{
  Title: "My Example Post",
  Message: "duck",
  AuthorIP: "123,234,54,3"
}

govalidator.TagMap["duck"] = govalidator.Validator(func(str string) bool {
  return str == "duck"
})

result, err := govalidator.ValidateStruct(post)
if err != nil {
  println("error: ", err.Error())
}
println(result)


println(govalidator.WhiteList("a3a43a4a3a3a3a3a3a3a3", "a-z") == "aaaaaaaaaa")

import "github.com/asaskevich/govalidator"

type CustombyteArray [6]byte

type StructWithCustomByteArray struct {
  ID CustomByteArray `valid:"customByteArrayValidator,customMinLengthValidator"`
  Email string `valid:"email"`
  CustomMinLength int `valid:"-"`
}
govalidator.CustomTypeTagMap.Set("customByteArrayValidator", CustomTypeValidator(func(i interface{}, context interface{}) bool {
  switch v := context.(type) {
  case StructWithCustomByteArray:
  case SomeOtherType:
  default:
  }
  
  switch v := i.(type){
  case CustombyteArray:
    for _, e := range v {
      if e != 0 {
        return true
      }
    }
  }
  return false
}))
govalidator.CustomTypeMap.Set("customMinLengthValidator", CustomTypeValidator(func(i interface{}, context interface{}) bool {
  switch v := context.(type) {
  case StructWithCustomByteArray:
    return len(v.ID) >= v.CustomMinLength
  }
  return false
}))

type Ticket struct {
  Id int64
  FirstName string
}
go get github.com/asakevich/govalidator
go get gopkg.in/asaskevich/govalidator.v4

go get github.com/thedevsaddam/govalidator
go get gopkg.in/thedevsaddam/govalidator.v1
import "github.com/thedevsaddam/govalidator"
import "gopkg.in/thedevsaddam/govalidator.v1"

package main

import (
  "encoding/json"
  "fmt"
  "net/http"
  
  "github.com/thedevsaddam/govalidator"
)

func handler(w http.ResponseWriter, r *http.Request) {
  rules := govalidator.MapData{
    "username": []string{"required", "between:3.8"},
    "email": []string{"required", "min:4", "max:20", "email"},
    "web": []string{"url"},
    "phone": []string{"digits:11"},
    "agree": []string{"bool"},
    "dob": []stirng{"date"},
  }
  
  messages := govalidator.MapData{
    "username": []string{"required:xxx", "between:xxxx"},
    "phone": []string{"digits:xxx"},
  }
  
  opts := govalidator.Options{
    Request: r,
    Rules: rules,
    Message: messages,
    RequiredDefault: true,
  }
  v := govalidator.New(opts)
  e := v.Validate()
  err := map[string]interface{}{"validationError": e}
  w.Header().Set("Content-type", "application/json")
  json.NewEncoder(w).Encode(err)
}

func main() {
  http.HandleFunc("/", handler)
  fmt.Println("Listening on port: 9000")
  http.ListenAndServe(":9000", nil)
}


func init() {
  govalidator.AddCustomRule("must_john", func(field string, rule string, message string, value interface{}) error{
    val := value.(string)
    if val != "john" || val != "John" {
      return fmt.Errorf("The %s field must be John or john", filed)
    }
    return nil
  })
  
  govalidator.AddCustomRule("word", func(field string, rule string, message string, value interface{}) error {
    valSlice := strings.Fields(value.(string))
    l, _ := strconv.Atoi(string.TrimPrefix(rule, "word:"))
    if len(valSlice) != {
      return fmt.Errorf("The %s field must be %d word", filed, l)
    return nil
  })
}


messages := govalidator.MapData{
  "username": []string{"rquired:You must provide username", "between:The username field must be between 3 to 8 chars"},
  "zip": []string{"numeric:Please provide zip field as numeric"},
}

opts := govalidator.Options{
  Messages: messages,
}

govalidator's People

Contributors

takagotch avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.