-
Notifications
You must be signed in to change notification settings - Fork 0
/
regexp.go
52 lines (41 loc) · 1.48 KB
/
regexp.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
52
// 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 (
"errors"
"reflect"
"regexp"
)
// tagRegexp is the tag of the checker.
const tagRegexp = "regexp"
// ErrNotMatch indicates that the given string does not match the regexp pattern.
var ErrNotMatch = errors.New("please enter a valid input")
// MakeRegexpMaker makes a regexp checker maker for the given regexp expression with the given invalid result.
func MakeRegexpMaker(expression string, invalidError error) MakeFunc {
return func(_ string) CheckFunc {
return MakeRegexpChecker(expression, invalidError)
}
}
// MakeRegexpChecker makes a regexp checker for the given regexp expression with the given invalid result.
func MakeRegexpChecker(expression string, invalidError error) CheckFunc {
pattern := regexp.MustCompile(expression)
return func(value, parent reflect.Value) error {
return checkRegexp(value, pattern, invalidError)
}
}
// makeRegexp makes a checker function for the regexp.
func makeRegexp(config string) CheckFunc {
return MakeRegexpChecker(config, ErrNotMatch)
}
// checkRegexp checks if the given string matches the regexp pattern.
func checkRegexp(value reflect.Value, pattern *regexp.Regexp, invalidError error) error {
if value.Kind() != reflect.String {
panic("string expected")
}
if !pattern.MatchString(value.String()) {
return invalidError
}
return nil
}