/* * Micro Service * Package: gitlab.hexacode.org/go-libs/microservice * Maintainer: Azzis Arswendo * * Copyright (C) 2023 Hexacode Teknologi Indonesia * All Rights Reserved */ package validate import ( "fmt" "regexp" ) func (options *Options) ValidText() error { if !options.Required && options.ValueText == "" { return nil } regexPattern := options.Regex regex, err := regexp.Compile(regexPattern) if err != nil { return err } isValid := regex.MatchString(options.ValueText) if !isValid { return fmt.Errorf("invalid type text: Text must be valid") } if options.Min == 0 && options.Max == 0 { return nil } if len(options.ValueText) < options.Min || len(options.ValueText) > options.Max { return fmt.Errorf("invalid type text: Text must be between %d and %d characters", options.Min, options.Max) } return nil }