Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added IsInCSV to validator.go, issue #468 #471

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 31 additions & 0 deletions validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down