by

Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • Sep 29 19:51
    yaliv commented #617
  • Sep 29 19:51
    yaliv commented #617
  • Sep 29 19:44
    yaliv commented #617
  • Sep 29 19:44
    yaliv commented #617
  • Sep 29 18:57
    yaliv commented #617
  • Sep 29 18:57
    yaliv commented #617
  • Sep 29 18:28
    yaliv commented #617
  • Sep 29 18:28
    yaliv commented #617
  • Sep 29 12:30
    JordyMoos starred go-playground/validator
  • Sep 29 12:14
    andig commented #617
  • Sep 29 12:14
    andig commented #617
  • Sep 29 11:42
    kvii2202 starred go-playground/validator
  • Sep 29 10:49
    yaliv commented #617
  • Sep 29 10:49
    yaliv commented #617
  • Sep 29 09:56
  • Sep 29 08:31
    cobb-tx starred go-playground/validator
  • Sep 29 07:34
    yayaleslie starred go-playground/validator
  • Sep 29 05:54
    deikatsuo edited #671
  • Sep 29 05:54
    deikatsuo edited #671
  • Sep 29 05:33
    deikatsuo opened #671
Omar Massad
@Massad

forms /
form1.go
validator.go

main.go

aha
Dean Karn
@deankarn
ok so you just have to change “defaultValidator” to “DefaultValidator” in your validator.go
Omar Massad
@Massad
ok just as ec
sec*
It's working now! binding.Validator = &forms.DefaultValidator{}
You are amazing, thank you!
Dean Karn
@deankarn
no problems :) glad you got it working
Omar Massad
@Massad
A silly question, doing that move or similar moves in the future would effect the performance in a noticeable way?
The overriding thing
Dean Karn
@deankarn
no not at all
Omar Massad
@Massad
Cool, thank you again :+1:
Dean Karn
@deankarn
gin used StructValidator as an interface to make it’s code pluggable and overriding does not effect performance at all
Omar Massad
@Massad
aha i see
Dean Karn
@deankarn
gin is a fantastic framework, if you ever wanted to start with something a little more low level and build your own framework you can take a look at my router https://github.com/go-playground/lars
Omar Massad
@Massad
I would to do that, since i'm still learning and developing using Gin
Would love*
Dean Karn
@deankarn
well again, glad I could help :) have a great day
Omar Massad
@Massad
Thank you again for your time and have a great day too :smile:
Andrius Solopovas
@asolopovas
Hi everyone.
Dean Karn
@deankarn
hello @asolopovas
muei
@muei
This message was deleted
type Cost struct {
  Unit string
  Fee float64
}

type Payment struct {
  Apple Cost // validate Apple.Fee gt=0
  Water Cost // no need validate
}
Is there some one can help me, how to define the tag that one need validate, the other not need
Dean Karn
@deankarn
type Pament struct {
Apple Cost // validate Apple.Fee gt=0
Water Cost // no need validate
}
Oops sorry didn't mean to send last message, tag should be validate:"gt=0"
muei
@muei
what do you mean?
Dean Karn
@deankarn
and for the Water field validate:"-"
muei
@muei
I want Apple.Cost.Fee great then zero
Dean Karn
@deankarn
the - tells validator to ignore field and the gt=0 should go on the Fee field, I'd write a full example but answering on my phone
muei
@muei
I know the rule. But I don't know how to differentia Apple.Fee and Water.Fee
type Cost struct {
  Unit string
  Fee float64  `validate:"gt=0"`
}

type Payment struct {
  Apple Cost  `validate:"required"`// validate Apple.Fee gt=0
  Water Cost `validate:"-"`// no need validate
}
I tried like this , but it will check all by Cost's define
Dean Karn
@deankarn
Yes , but when validating the Payment Struct if there is an error, the namespace of the error will be Payment.Apple.Fee differentiating itself from just the Cost Struct
muei
@muei
The below, my example , that will not work what I wanted.
I want Apple.Cost.Fee great then zero, but Water.Cost.Fee not validate. So how to define the tag in Payment or Cost
Dean Karn
@deankarn
Oh I see so validation is still happening for Water.Cost.Fee?
muei
@muei
I'm sorry I tried my old testing, It's OK now. @joeybloggs Thank you very much.
Dean Karn
@deankarn
No problem glad I could help
muei
@muei
This message was deleted
muei
@muei
type Demo struct {
   Date time.Time `validate:"gtetoday"`
}

func RegisterGTEToday(validate *validator.Validate) {
    validate.RegisterValidation("gtetoday", gtetoday)
}
func gtetoday(fl validator.FieldLevel) bool {
       var t time.Time
       // todo parse fl.Field()...
      println("Comparison date")
       return time.Now().Before(t)
}

func main() {
   validate := validator.New()
   validators.RegisterGreatThanToday(validate)

tmp:=Demo{}
...
  err := validate.Struct(&tmp)
 ....
}
Can someone help me? Why not print Comparison date. How to config the timetype
Dean Karn
@deankarn
You mean it's not reaching the println code? and shouldn't it be fmt.Println("")
muei
@muei
yeah
Dean Karn
@deankarn
I don't know how this can compile with println() and not fmt.Println() but I'll take a look when I get to a computer
P.S. nice to see someone checking out v9
Dean Karn
@deankarn
ok @ohohco I was able to make your example work for me, I tweaked it a bit to finalize what you wanted, here is the working code:
package main

import (
    "fmt"
    "log"
    "time"

    "gopkg.in/go-playground/validator.v9"
)

// Demo ...
type Demo struct {
    Date time.Time `validate:"gtetoday"`
}

// always a global var as it caches struct data
var validate *validator.Validate

func main() {

    validate = validator.New()

    err := validate.RegisterValidation("gtetoday", gtetoday)
    if err != nil {
        log.Panic(err)
    }

    tmp := Demo{}

    err = validate.Struct(&tmp)
    fmt.Println(err)

    tmp.Date = time.Now().Add(time.Hour * 24)
    err = validate.Struct(&tmp)
    fmt.Println(err)
}

func gtetoday(fl validator.FieldLevel) bool {

    t := fl.Field().Interface().(time.Time)

    fmt.Println("Comparison date")

    now := time.Now()

    return t.After(now) || t.Equal(now)
}
muei
@muei
3ks!
Dean Karn
@deankarn
No problem :)
Julien LEICHER
@YuukanOO
Hi there! I may have missed something but when trying to validate a simple struct, it always returns nil.
type MyStruct struct {
    Email string `validate:"required"`
}

// Something like this
validate := validator.New(&validator.Config{
  TagName: "validator",
})

validate.Struct(&MyStruct{}) // returns nil
Dean Karn
@deankarn
It's because you changed the tag name to validator but your struct tag is validate
and so it sees no tags for validation as they aren't matching up