-
Notifications
You must be signed in to change notification settings - Fork 0
/
starts_with_validator.go
40 lines (36 loc) · 948 Bytes
/
starts_with_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
package validators
import (
"fmt"
"strings"
)
// Checks if the value starts with a given sequence.
//
// Configuration parameters:
// - startWith(string): character sequence that the value must start with.
// - errorMessage (string): custom error message (optional).
//
// Input value (string): value to be validated.
//
// Usage examples:
//
// value := "message"
// v.StartsWith("mes")(value) // Output: nil, false
//
// value := "send message"
// v.StartsWith("end")(value) // Output: [error message], false
// v.StartsWith("end", "error")(value) // Output: "error", false
func StartsWith(
startWith string,
errorMessage ...string,
) Validator {
message := fmt.Sprintf("The value must start with: %s.", startWith)
if len(errorMessage) != 0 {
message = errorMessage[0]
}
return func(value interface{}) (*string, bool) {
if !strings.HasPrefix(value.(string), startWith) {
return &message, false
}
return nil, false
}
}