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

MongoDB validator improved #1196

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -163,6 +163,7 @@ validate := validator.New(validator.WithRequiredStructEnabled())
| btc_addr_bech32 | Bitcoin Bech32 Address (segwit) |
| credit_card | Credit Card Number |
| mongodb | MongoDB ObjectID |
| mongodb_connection_string | MongoDB Connection String |
| cron | Cron |
| spicedb | SpiceDb ObjectID/Permission/Type |
| datetime | Datetime |
Expand Down
15 changes: 11 additions & 4 deletions baked_in.go
Expand Up @@ -230,7 +230,8 @@ var (
"credit_card": isCreditCard,
"cve": isCveFormat,
"luhn_checksum": hasLuhnChecksum,
"mongodb": isMongoDB,
"mongodb": isMongoDBObjectId,
"mongodb_connection_string": isMongoDBConnectionString,
"cron": isCron,
"spicedb": isSpiceDB,
}
Expand Down Expand Up @@ -2882,10 +2883,16 @@ func digitsHaveLuhnChecksum(digits []string) bool {
return (sum % 10) == 0
}

// isMongoDB is the validation function for validating if the current field's value is valid mongoDB objectID
func isMongoDB(fl FieldLevel) bool {
// isMongoDBObjectId is the validation function for validating if the current field's value is valid MongoDB ObjectID
func isMongoDBObjectId(fl FieldLevel) bool {
val := fl.Field().String()
return mongodbRegex.MatchString(val)
return mongodbIdRegex.MatchString(val)
}

// isMongoDBConnectionString is the validation function for validating if the current field's value is valid MongoDB Connection String
func isMongoDBConnectionString(fl FieldLevel) bool {
val := fl.Field().String()
return mongodbConnectionRegex.MatchString(val)
}

// isSpiceDB is the validation function for validating if the current field's value is valid for use with Authzed SpiceDB in the indicated way
Expand Down
12 changes: 10 additions & 2 deletions doc.go
Expand Up @@ -1377,11 +1377,19 @@ This validates that a string value contains a valid credit card number using Luh

This validates that a string or (u)int value contains a valid checksum using the Luhn algorithm.

# MongoDb ObjectID
# MongoDB

This validates that a string is a valid 24 character hexadecimal string.
This validates that a string is a valid 24 character hexadecimal string or valid connection string.

Usage: mongodb
mongodb_connection_string

Example:

type Test struct {
ObjectIdField string `validate:"mongodb"`
ConnectionStringField string `validate:"mongodb_connection_string"`
}

# Cron

Expand Down
6 changes: 4 additions & 2 deletions regexes.go
Expand Up @@ -67,7 +67,8 @@ const (
semverRegexString = `^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$` // numbered capture groups https://semver.org/
dnsRegexStringRFC1035Label = "^[a-z]([-a-z0-9]*[a-z0-9]){0,62}$"
cveRegexString = `^CVE-(1999|2\d{3})-(0[^0]\d{2}|0\d[^0]\d{1}|0\d{2}[^0]|[1-9]{1}\d{3,})$` // CVE Format Id https://cve.mitre.org/cve/identifiers/syntaxchange.html
mongodbRegexString = "^[a-f\\d]{24}$"
mongodbIdRegexString = "^[a-f\\d]{24}$"
mongodbConnStringRegexString = "^mongodb(\\+srv)?:\\/\\/(([a-zA-Z\\d]+):([a-zA-Z\\d$:\\/?#\\[\\]@]+)@)?(([a-z\\d.-]+)(:[\\d]+)?)((,(([a-z\\d.-]+)(:(\\d+))?))*)?(\\/[a-zA-Z-_]{1,64})?(\\?(([a-zA-Z]+)=([a-zA-Z\\d]+))(&(([a-zA-Z\\d]+)=([a-zA-Z\\d]+))?)*)?$"
cronRegexString = `(@(annually|yearly|monthly|weekly|daily|hourly|reboot))|(@every (\d+(ns|us|µs|ms|s|m|h))+)|((((\d+,)+\d+|(\d+(\/|-)\d+)|\d+|\*) ?){5,7})`
spicedbIDRegexString = `^(([a-zA-Z0-9/_|\-=+]{1,})|\*)$`
spicedbPermissionRegexString = "^([a-z][a-z0-9_]{1,62}[a-z0-9])?$"
Expand Down Expand Up @@ -137,7 +138,8 @@ var (
semverRegex = regexp.MustCompile(semverRegexString)
dnsRegexRFC1035Label = regexp.MustCompile(dnsRegexStringRFC1035Label)
cveRegex = regexp.MustCompile(cveRegexString)
mongodbRegex = regexp.MustCompile(mongodbRegexString)
mongodbIdRegex = regexp.MustCompile(mongodbIdRegexString)
mongodbConnectionRegex = regexp.MustCompile(mongodbConnStringRegexString)
cronRegex = regexp.MustCompile(cronRegexString)
spicedbIDRegex = regexp.MustCompile(spicedbIDRegexString)
spicedbPermissionRegex = regexp.MustCompile(spicedbPermissionRegexString)
Expand Down
52 changes: 52 additions & 0 deletions validator_test.go
Expand Up @@ -13231,6 +13231,58 @@ func TestMongoDBObjectIDFormatValidation(t *testing.T) {
}
}
}
func TestMongoDBConnectionStringFormatValidation(t *testing.T) {
tests := []struct {
value string `validate:"mongodb_connection_string"`
tag string
expected bool
}{
{"mongodb://username:password@server.example.com:20017/database?replicaSet=test&connectTimeoutMS=300000&ssl=true", "mongodb_connection_string", true},
{"mongodb+srv://username:password@server.example.com:20017/database?replicaSet=test&connectTimeoutMS=300000&ssl=true", "mongodb_connection_string", true},
{"mongodb+srv://username:password@server.example.com:20017,server.example.com,server.example.com:20017/database?replicaSet=test&connectTimeoutMS=300000&ssl=true", "mongodb_connection_string", true},
{"mongodb+srv://username:password@server.example.com:20017,server.example.com,server.example.com:20017?replicaSet=test&connectTimeoutMS=300000&ssl=true", "mongodb_connection_string", true},
{"mongodb://username:password@server.example.com:20017,server.example.com,server.example.com:20017/database?replicaSet=test&connectTimeoutMS=300000&ssl=true", "mongodb_connection_string", true},
{"mongodb://localhost", "mongodb_connection_string", true},
{"mongodb://localhost:27017", "mongodb_connection_string", true},
{"localhost", "mongodb_connection_string", false},
{"mongodb://", "mongodb_connection_string", false},
{"mongodb+srv://", "mongodb_connection_string", false},
{"mongodbsrv://localhost", "mongodb_connection_string", false},
{"mongodb+srv://localhost", "mongodb_connection_string", true},
{"mongodb+srv://localhost:27017", "mongodb_connection_string", true},
{"mongodb+srv://localhost?replicaSet=test", "mongodb_connection_string", true},
{"mongodb+srv://localhost:27017?replicaSet=test", "mongodb_connection_string", true},
{"mongodb+srv://localhost:27017?", "mongodb_connection_string", false},
{"mongodb+srv://localhost:27017?replicaSet", "mongodb_connection_string", false},
{"mongodb+srv://localhost/database", "mongodb_connection_string", true},
{"mongodb+srv://localhost:27017/database", "mongodb_connection_string", true},
{"mongodb+srv://username@localhost", "mongodb_connection_string", false},
{"mongodb+srv://username:password@localhost", "mongodb_connection_string", true},
{"mongodb+srv://username:password@localhost:27017", "mongodb_connection_string", true},
{"mongodb+srv://username:password@localhost:27017,192.0.0.7,192.0.0.9:27018,server.example.com", "mongodb_connection_string", true},
}

validate := New()

for i, test := range tests {
errs := validate.Var(test.value, test.tag)

if test.expected {
if !IsEqual(errs, nil) {
t.Fatalf("Index: %d mongodb_connection_string failed Error: %s", i, errs)
}
} else {
if IsEqual(errs, nil) {
t.Fatalf("Index: %d mongodb_connection_string failed Error: %s", i, errs)
} else {
val := getError(errs, "", "")
if val.Tag() != "mongodb_connection_string" {
t.Fatalf("Index: %d mongodb_connection_string failed Error: %s", i, errs)
}
}
}
}
}

func TestSpiceDBValueFormatValidation(t *testing.T) {
tests := []struct {
Expand Down