-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathis_string_validator.go
More file actions
49 lines (44 loc) · 946 Bytes
/
is_string_validator.go
File metadata and controls
49 lines (44 loc) · 946 Bytes
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
package validators
import (
"errors"
"fmt"
)
// Checks if the value is a string.
//
// Configuration parameters:
//
// 1. errorMessage (string): custom error message (optional).
//
// Input value (string): value to be validated.
//
// Usage examples:
//
// value1 := "Name"
// v.IsString()(value1) // Output: nil, false
//
// value2 := nil
// v.IsString()(value2) // Output: [error message], true
//
// value3 := 0
// v.IsString()(value3) // Output: [error message], true
// v.IsString("error")(value3) // Output: "error", true
func IsString(
errorMessage ...string,
) Validator {
message := ""
if len(errorMessage) != 0 {
message = errorMessage[0]
}
return func(value any) (error, bool) {
if _, ok := value.(string); !ok {
if message == "" {
message = fmt.Sprintf(
"The value is not a string: value is %T.",
value,
)
}
return errors.New(message), true
}
return nil, false
}
}