Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions pkg/vin/vin_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ func (v VIN) IsValidVIN() bool {
if len(v) != 17 {
return false
}
if AreAllCharactersTheSameAlt(string(v)) {
return false
}

// Check if the string matches the VIN pattern
// Excluding I, O, Q as per standard
Expand All @@ -55,6 +58,9 @@ func (v VIN) IsValidVIN() bool {
}

func (v VIN) IsValidJapanChassis() bool {
if AreAllCharactersTheSameAlt(string(v)) {
return false
}
re := regexp.MustCompile(`(?)^[A-Z0-9]{2,7}-?\d{5,7}$`)
return re.MatchString(string(v))
}
Expand Down Expand Up @@ -122,3 +128,18 @@ func (v VIN) SerialNumber() string {
}
return string(v[11:17])
}

// AreAllCharactersTheSameAlt check not getting bogus VIN
func AreAllCharactersTheSameAlt(s string) bool {
if len(s) <= 1 {
return true
}

firstChar := rune(s[0])
for _, char := range s[1:] {
if char != firstChar {
return false
}
}
return true
}
10 changes: 10 additions & 0 deletions pkg/vin/vin_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@ func TestVIN_IsValidJapanChassis(t *testing.T) {
v: "ZWR90-8000186",
want: true,
},
{
name: "Invalid japan chassis number all the same",
v: "AAAAAAAAAAAAA",
want: false,
},
{
name: "Japan chassis without dash",
v: "DJLAS203662",
Expand All @@ -187,6 +192,11 @@ func TestVIN_IsValidJapanChassis(t *testing.T) {
v: "1FTFX1E57JKE37092",
want: false,
},
{
name: "Invalid VIN with all chars the same",
v: "AAAAAAAAAAAAAAAAA",
want: false,
},
{
name: "Short VIN without dash",
v: "WBMWD",
Expand Down
Loading