Skip to content

Commit f560fd4

Browse files
itzjustalanDean Karn
andauthored
Feat: support mongodb objectID validation (#1023)
Co-authored-by: Dean Karn <[email protected]>
1 parent 29d50ba commit f560fd4

File tree

5 files changed

+53
-0
lines changed

5 files changed

+53
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ Baked-in Validations
156156
| btc_addr | Bitcoin Address |
157157
| btc_addr_bech32 | Bitcoin Bech32 Address (segwit) |
158158
| credit_card | Credit Card Number |
159+
| mongodb | MongoDB ObjectID |
159160
| cron | Cron |
160161
| datetime | Datetime |
161162
| e164 | e164 formatted phone number |

baked_in.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ var (
219219
"semver": isSemverFormat,
220220
"dns_rfc1035_label": isDnsRFC1035LabelFormat,
221221
"credit_card": isCreditCard,
222+
"mongodb": isMongoDB,
222223
"cron": isCron,
223224
}
224225
)
@@ -2567,6 +2568,12 @@ func isDnsRFC1035LabelFormat(fl FieldLevel) bool {
25672568
return dnsRegexRFC1035Label.MatchString(val)
25682569
}
25692570

2571+
// isMongoDB is the validation function for validating if the current field's value is valid mongoDB objectID
2572+
func isMongoDB(fl FieldLevel) bool {
2573+
val := fl.Field().String()
2574+
return mongodbRegex.MatchString(val)
2575+
}
2576+
25702577
// isCreditCard is the validation function for validating if the current field's value is a valid credit card number
25712578
func isCreditCard(fl FieldLevel) bool {
25722579
val := fl.Field().String()

doc.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,6 +1332,13 @@ This validates that a string value contains a valid credit card number using Luh
13321332
Usage: credit_card
13331333
13341334
1335+
#MongoDb ObjectID
1336+
1337+
This validates that a string is a valid 24 character hexadecimal string.
1338+
1339+
Usage: mongodb
1340+
1341+
13351342
# Cron
13361343
13371344
This validates that a string value contains a valid cron expression.

regexes.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ const (
6565
bicRegexString = `^[A-Za-z]{6}[A-Za-z0-9]{2}([A-Za-z0-9]{3})?$`
6666
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/
6767
dnsRegexStringRFC1035Label = "^[a-z]([-a-z0-9]*[a-z0-9]){0,62}$"
68+
mongodbRegexString = "^[a-f\\d]{24}$"
6869
cronRegexString = `(@(annually|yearly|monthly|weekly|daily|hourly|reboot))|(@every (\d+(ns|us|µs|ms|s|m|h))+)|((((\d+,)+\d+|(\d+(\/|-)\d+)|\d+|\*) ?){5,7})`
6970
)
7071

@@ -129,5 +130,6 @@ var (
129130
bicRegex = regexp.MustCompile(bicRegexString)
130131
semverRegex = regexp.MustCompile(semverRegexString)
131132
dnsRegexRFC1035Label = regexp.MustCompile(dnsRegexStringRFC1035Label)
133+
mongodbRegex = regexp.MustCompile(mongodbRegexString)
132134
cronRegex = regexp.MustCompile(cronRegexString)
133135
)

validator_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12546,6 +12546,42 @@ func TestValidate_ValidateMapCtx(t *testing.T) {
1254612546
}
1254712547
}
1254812548

12549+
func TestMongoDBObjectIDFormatValidation(t *testing.T) {
12550+
tests := []struct {
12551+
value string `validate:"mongodb"`
12552+
tag string
12553+
expected bool
12554+
}{
12555+
{"507f191e810c19729de860ea", "mongodb", true},
12556+
{"507f191e810c19729de860eG", "mongodb", false},
12557+
{"M07f191e810c19729de860eG", "mongodb", false},
12558+
{"07f191e810c19729de860ea", "mongodb", false},
12559+
{"507f191e810c19729de860e", "mongodb", false},
12560+
{"507f191e810c19729de860ea4", "mongodb", false},
12561+
}
12562+
12563+
validate := New()
12564+
12565+
for i, test := range tests {
12566+
errs := validate.Var(test.value, test.tag)
12567+
12568+
if test.expected {
12569+
if !IsEqual(errs, nil) {
12570+
t.Fatalf("Index: %d mongodb failed Error: %s", i, errs)
12571+
}
12572+
} else {
12573+
if IsEqual(errs, nil) {
12574+
t.Fatalf("Index: %d mongodb failed Error: %s", i, errs)
12575+
} else {
12576+
val := getError(errs, "", "")
12577+
if val.Tag() != "mongodb" {
12578+
t.Fatalf("Index: %d mongodb failed Error: %s", i, errs)
12579+
}
12580+
}
12581+
}
12582+
}
12583+
}
12584+
1254912585
func TestCreditCardFormatValidation(t *testing.T) {
1255012586
tests := []struct {
1255112587
value string `validate:"credit_card"`

0 commit comments

Comments
 (0)