Skip to content

Commit

Permalink
Feat: support mongodb objectID validation (#1023)
Browse files Browse the repository at this point in the history
Co-authored-by: Dean Karn <Dean.Karn@gmail.com>
  • Loading branch information
itzjustalan and deankarn committed Mar 19, 2023
1 parent 29d50ba commit f560fd4
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -156,6 +156,7 @@ Baked-in Validations
| btc_addr | Bitcoin Address |
| btc_addr_bech32 | Bitcoin Bech32 Address (segwit) |
| credit_card | Credit Card Number |
| mongodb | MongoDB ObjectID |
| cron | Cron |
| datetime | Datetime |
| e164 | e164 formatted phone number |
Expand Down
7 changes: 7 additions & 0 deletions baked_in.go
Expand Up @@ -219,6 +219,7 @@ var (
"semver": isSemverFormat,
"dns_rfc1035_label": isDnsRFC1035LabelFormat,
"credit_card": isCreditCard,
"mongodb": isMongoDB,
"cron": isCron,
}
)
Expand Down Expand Up @@ -2567,6 +2568,12 @@ func isDnsRFC1035LabelFormat(fl FieldLevel) bool {
return dnsRegexRFC1035Label.MatchString(val)
}

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

// isCreditCard is the validation function for validating if the current field's value is a valid credit card number
func isCreditCard(fl FieldLevel) bool {
val := fl.Field().String()
Expand Down
7 changes: 7 additions & 0 deletions doc.go
Expand Up @@ -1332,6 +1332,13 @@ This validates that a string value contains a valid credit card number using Luh
Usage: credit_card
#MongoDb ObjectID
This validates that a string is a valid 24 character hexadecimal string.
Usage: mongodb
# Cron
This validates that a string value contains a valid cron expression.
Expand Down
2 changes: 2 additions & 0 deletions regexes.go
Expand Up @@ -65,6 +65,7 @@ const (
bicRegexString = `^[A-Za-z]{6}[A-Za-z0-9]{2}([A-Za-z0-9]{3})?$`
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}$"
mongodbRegexString = "^[a-f\\d]{24}$"
cronRegexString = `(@(annually|yearly|monthly|weekly|daily|hourly|reboot))|(@every (\d+(ns|us|µs|ms|s|m|h))+)|((((\d+,)+\d+|(\d+(\/|-)\d+)|\d+|\*) ?){5,7})`
)

Expand Down Expand Up @@ -129,5 +130,6 @@ var (
bicRegex = regexp.MustCompile(bicRegexString)
semverRegex = regexp.MustCompile(semverRegexString)
dnsRegexRFC1035Label = regexp.MustCompile(dnsRegexStringRFC1035Label)
mongodbRegex = regexp.MustCompile(mongodbRegexString)
cronRegex = regexp.MustCompile(cronRegexString)
)
36 changes: 36 additions & 0 deletions validator_test.go
Expand Up @@ -12546,6 +12546,42 @@ func TestValidate_ValidateMapCtx(t *testing.T) {
}
}

func TestMongoDBObjectIDFormatValidation(t *testing.T) {
tests := []struct {
value string `validate:"mongodb"`
tag string
expected bool
}{
{"507f191e810c19729de860ea", "mongodb", true},
{"507f191e810c19729de860eG", "mongodb", false},
{"M07f191e810c19729de860eG", "mongodb", false},
{"07f191e810c19729de860ea", "mongodb", false},
{"507f191e810c19729de860e", "mongodb", false},
{"507f191e810c19729de860ea4", "mongodb", false},
}

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 failed Error: %s", i, errs)
}
} else {
if IsEqual(errs, nil) {
t.Fatalf("Index: %d mongodb failed Error: %s", i, errs)
} else {
val := getError(errs, "", "")
if val.Tag() != "mongodb" {
t.Fatalf("Index: %d mongodb failed Error: %s", i, errs)
}
}
}
}
}

func TestCreditCardFormatValidation(t *testing.T) {
tests := []struct {
value string `validate:"credit_card"`
Expand Down

0 comments on commit f560fd4

Please sign in to comment.