by

Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
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
Julien LEICHER
@YuukanOO
omg, I feel stupid...
YuukanOO @YuukanOO out
Julien LEICHER
@YuukanOO
BTW, thanks for this library @joeybloggs =)
Dean Karn
@deankarn
no problems, be sure to check out v9 :smile: it’s even better
Aleksandar Palic
@codepushr
@joeybloggs hey dean :) i have a question and didn't want to open an issue on github... i wonder if it's possible to only have some fields validated if the struct itself is validated directly? i want to have some constraints ignored if the struct is a relation of another struct
example: POST on /devices should trigger the required tag of the field user_id (as it's the foreign key) but on the other hand when POSTING on /users and sending an array of devices i want the user_id constraint to be ignored as i have no user id at that point -> it has to be persisted first
Dean Karn
@deankarn
Yes there are several options for you, here is a quick list(sorry for the formatting answering from my phone) 1. There is a StructExcept(...) function which a field or fields can be ignored. 2. There is a new StructFiltered(...) Function in which you may specify your own function to filter out fields you do or do not want included/validated (I think this may be your best option) 3. you could make 2 validator instances, with different tag names and have different validations for each tag name.