-
Notifications
You must be signed in to change notification settings - Fork 0
/
is_float_validator.go
51 lines (46 loc) · 1.01 KB
/
is_float_validator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package validators
import (
"errors"
"fmt"
)
// Checks if the value is a number or null.
//
// Configuration parameters:
// - errorMessage (string): custom error message (optional).
//
// Input value (float): value to be validated.
//
// Usage examples:
//
// value2 := 1.0
// v.IsFloat()(value2) // Output: nil, false
//
// value1 := 1
// v.IsFloat()(value1) // Output: [error message], true
//
// value3 := nil
// v.IsFloat()(value3) // Output: [error message], true
//
// value4 := ""
// v.IsFloat()(value4) // Output: [error message], true
// v.IsFloat("error")(value4) // Output: "error", true
func IsFloat(
errorMessage ...string,
) Validator {
message := ""
if len(errorMessage) != 0 {
message = errorMessage[0]
}
return func(value any) (error, bool) {
if _, ok := value.(float64); !ok {
if message == "" {
message = fmt.Sprintf(
"The value is not a decimal number: value is %T.",
value,
)
}
return errors.New(message), true
}
return nil, false
}
}