-
Notifications
You must be signed in to change notification settings - Fork 0
/
is_null_float_validator.go
48 lines (43 loc) · 993 Bytes
/
is_null_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
package validators
import "fmt"
// Checks if the value is a number or null.
//
// Configuration parameters:
// - errorMessage (string): custom error message (optional).
//
// Input value (any): value to be validated.
//
// Usage examples:
//
// value1 := nil
// v.IsNullFloat()(value1) // Output: nil, true
//
// value2 := 1.0
// v.IsNullFloat()(value2) // Output: nil, false
//
// value3 := 1
// v.IsNullFloat()(value3) // Output: [error message], true
//
// value4 := ""
// v.IsNullFloat()(value4) // Output: [error message], true
// v.IsNullFloat("error")(value4) // Output: "error", true
func IsNullFloat(
errorMessage ...string,
) Validator {
message := ""
if len(errorMessage) != 0 {
message = errorMessage[0]
}
return func(value interface{}) (*string, bool) {
if value == nil {
return nil, true
}
if message == "" {
message = fmt.Sprintf(
"The value is not a decimal number or null: value is %T.",
value,
)
}
return IsFloat(message)(value)
}
}