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
that line is just a compile time check to ensure “defaultValidator” complies with the “StructValidator” interface
var _ binding.StructValidator = &defaultValidator{}
and imports with become:
import (
    "reflect"
    "regexp"
    "sync"

    "github.com/gin-gonic/gin/binding"

    "gopkg.in/go-playground/validator.v8”
)
Omar Massad
@Massad
Aha go it, but now there's this undefined: defaultValidator
in go binding.Validator = &defaultValidator{}
In main:
binding.Validator = &defaultValidator{}
I have autoimport to cover that ya
Dean Karn
@deankarn
did you put the gin_validator.go in the main pacakge
(define “package main”) at the top
Omar Massad
@Massad
validator.go have package forms
But in main.go it's reading it
it's not reading it*
Dean Karn
@deankarn
oh so gin_validator.go and main are in different packages then, that’s why
Omar Massad
@Massad
ya
main.go and validator.go (custom)
Dean Karn
@deankarn
we defined “defaultValidator” starting with lowercase so it’s not exposed (public) to other pakages
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.