From ebe1ebb339ae33df866accb4e7ee4b1e9e2aba93 Mon Sep 17 00:00:00 2001 From: alireza Date: Sat, 5 Feb 2022 00:06:58 +0330 Subject: [PATCH] added IsInCSV to validator.go --- validator.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/validator.go b/validator.go index 46ecfc8..b6e5a7e 100644 --- a/validator.go +++ b/validator.go @@ -6,12 +6,14 @@ import ( "crypto/rsa" "crypto/x509" "encoding/base64" + "encoding/csv" "encoding/json" "encoding/pem" "fmt" "io/ioutil" "net" "net/url" + "os" "reflect" "regexp" "sort" @@ -200,6 +202,35 @@ func IsNumeric(str string) bool { return rxNumeric.MatchString(str) } +// checks if the csv file contains the given string +func IsInCSV(str string, address string) bool { + if IsNull(str) { + return true + } + + f, err := os.Open(address) + if err != nil { + return false //could not read file + } + defer f.Close() + + csvReader := csv.NewReader(f) + records, err := csvReader.ReadAll() + if err != nil { + return false //file is not csv + } + + for _, row := range records { + for _, val := range row { + val_str := string(val) + if strings.Contains(val_str, str) { + return true + } + } + } + return false +} + // IsUTFNumeric checks if the string contains only unicode numbers of any kind. // Numbers can be 0-9 but also Fractions ¾,Roman Ⅸ and Hangzhou 〩. Empty string is valid. func IsUTFNumeric(str string) bool {