-
Notifications
You must be signed in to change notification settings - Fork 0
/
minlenght.go
42 lines (34 loc) · 1.25 KB
/
minlenght.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
// Copyright (c) 2023-2024 Onur Cinar.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
// https://github.com/cinar/checker
package checker
import (
"fmt"
"reflect"
"strconv"
)
// tagMinLength is the tag of the checker.
const tagMinLength = "min-length"
// IsMinLength checks if the length of the given value is greather than the given minimum length.
func IsMinLength(value interface{}, minLength int) error {
return checkMinLength(reflect.Indirect(reflect.ValueOf(value)), reflect.ValueOf(nil), minLength)
}
// makeMinLength makes a checker function for the min length checker.
func makeMinLength(config string) CheckFunc {
minLength, err := strconv.Atoi(config)
if err != nil {
panic("unable to parse min length value")
}
return func(value, parent reflect.Value) error {
return checkMinLength(value, parent, minLength)
}
}
// checkMinLength checks if the length of the given value is greather than the given minimum length.
// The function uses the reflect.Value.Len() function to determine the length of the value.
func checkMinLength(value, _ reflect.Value, minLength int) error {
if value.Len() < minLength {
return fmt.Errorf("please enter at least %d characters", minLength)
}
return nil
}