go-ozzo / ozzo-validation

An idiomatic Go (golang) validation package. Supports configurable and extensible validation rules (validators) using normal language constructs instead of error-prone struct tags.
MIT License
3.73k stars 224 forks source link

[SOLVED] Not working with a float64, don't know why #68

Closed Daavidaviid closed 5 years ago

Daavidaviid commented 5 years ago

Hi, I have the following code :

I defined a structure called Users (I'm using it to query users) :


type Users struct {
  RegisteredAfter  *time.Time `json:"registeredAfter"`
  RegisteredBefore *time.Time `json:"registeredBefore"`

  RangeInKm   *float64      `json:"rangeInKm"`
  Geolocation *common.Point `json:"geolocation"`

  Number int  `json:"number"`
  From   *int `json:"from"`
}

func (u Users) Validate() error {
  validate := validation.ValidateStruct(&u,
    validation.Field(&u.RegisteredAfter, validation.Max(time.Now())),
    validation.Field(&u.RegisteredBefore, validation.Max(time.Now())),

    validation.Field(&u.Number, validation.Required, validation.Max(60)),

    validation.Field(&u.RangeInKm, validation.Min(0), validation.Max(1000)),
    validation.Field(&u.Geolocation),
  )

  return validate
}

And this is the following error I get when I set rangeInKm which is a *float64 :

2018/12/11 12:06:06 VALIDATION rangeInKm: cannot convert float64 to int64.

I don't know why tho.

autobotbengg commented 5 years ago

The threshold value is passed in as an int but the value being checked against is a float.

From the docs

// Min is a validation rule that checks if a value is greater or equal than the specified value.
// By calling Exclusive, the rule will check if the value is strictly greater than the specified value.
// Note that the value being checked and the threshold value must be of the same type.
// Only int, uint, float and time.Time types are supported.
// An empty value is considered valid. Please use the Required rule to make sure a value is not empty.

Try validation.Field(&u.RangeInKm, validation.Min(float64(0)), validation.Max(float64(1000))),

Daavidaviid commented 5 years ago

Thanks, I missed that part !

smithaitufe commented 5 years ago

Shouldn't this issue be closed?