go-playground / validator

:100:Go Struct and Field validation, including Cross Field, Cross Struct, Map, Slice and Array diving
MIT License
16.14k stars 1.29k forks source link

"A": "gt=0.1,lt=0.9" there is a bad case in `lt` #1144

Closed ahaooahaz closed 11 months ago

ahaooahaz commented 11 months ago

Package version eg. v9, v10: v10

Issue, Question or Enhancement: Issue

I found there is an bad case with lt=float

Code sample, to showcase or reproduce:

package main

import (
    "fmt"

    "github.com/go-playground/validator/v10"
)

type Data struct {
    A float32
}

var validate = validator.New()

func main() {
    data1 := Data{
        A: 0.9,
    }

        data1 := Data{
        A: 0.1,
    }

    rules := map[string]string{
        "A": "gt=0.1,lt=0.9",
    }

    validate.RegisterStructValidationMapRules(rules, Data{})

    err := validate.Struct(data1)
        err := validate.Struct(data2)
    fmt.Println(err)
    fmt.Println()
}
ahaooahaz commented 11 months ago

https://github.com/go-playground/validator/pull/1145

Floating-point numbers lost precision during the conversion process, which caused the equality of lt and gt to fail to take effect. I modified the code to complete the precision of four decimal places after floating-point numbers to support floating-point numbers