-
Notifications
You must be signed in to change notification settings - Fork 1
/
regexp.go
47 lines (37 loc) · 1.26 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
// 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 v2
import (
"reflect"
"regexp"
)
// nameRegexp is the name of the regexp check.
const nameRegexp = "regexp"
// ErrNotMatch indicates that the given string does not match the regexp pattern.
var ErrNotMatch = NewCheckError("REGEXP")
// IsRegexp checks if the given string matches the given regexp expression.
func IsRegexp(expression, value string) (string, error) {
if !regexp.MustCompile(expression).MatchString(value) {
return value, ErrNotMatch
}
return value, nil
}
// MakeRegexpChecker makes a regexp checker for the given regexp expression with the given invalid result.
func MakeRegexpChecker(expression string, invalidError error) CheckFunc[reflect.Value] {
return func(value reflect.Value) (reflect.Value, error) {
if value.Kind() != reflect.String {
panic("string expected")
}
_, err := IsRegexp(expression, value.String())
if err != nil {
return value, invalidError
}
return value, nil
}
}
// makeRegexp makes a checker function for the regexp.
func makeRegexp(config string) CheckFunc[reflect.Value] {
return MakeRegexpChecker(config, ErrNotMatch)
}