add Block + Component, add TextComponent with min, max and regex validators and early NumberComponent
This commit is contained in:
24
internal/block/text_component/validators/regex.go
Normal file
24
internal/block/text_component/validators/regex.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package text_component_validators
|
||||
|
||||
import (
|
||||
"blocky/internal/block/text_component"
|
||||
"fmt"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
func Regex(pattern string) text_component.TextComponentOption {
|
||||
re := regexp.MustCompile(pattern)
|
||||
|
||||
return func(c *text_component.TextComponentConfig) {
|
||||
c.Validators = append(c.Validators, func(value any) error {
|
||||
s, ok := value.(string)
|
||||
if !ok {
|
||||
return fmt.Errorf("provided value bust be a string: %v", value)
|
||||
}
|
||||
if !re.MatchString(s) {
|
||||
return fmt.Errorf("must match pattern %s", pattern)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user