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

Add MAC validation checker and tests to v2. #139

Merged
merged 1 commit into from
Dec 27, 2024
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
41 changes: 41 additions & 0 deletions v2/mac.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) 2023-2024 Onur Cinar.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
// https://github.com/cinar/checker

package v2

import (
"net"
"reflect"
)

const (
// nameMAC is the name of the MAC check.
nameMAC = "mac"
)

var (
// ErrNotMAC indicates that the given value is not a valid MAC address.
ErrNotMAC = NewCheckError("MAC")
)

// IsMAC checks if the value is a valid MAC address.
func IsMAC(value string) (string, error) {
_, err := net.ParseMAC(value)
if err != nil {
return value, ErrNotMAC
}
return value, nil
}

// checkMAC checks if the value is a valid MAC address.
func checkMAC(value reflect.Value) (reflect.Value, error) {
_, err := IsMAC(value.Interface().(string))
return value, err
}

// makeMAC makes a checker function for the MAC checker.
func makeMAC(_ string) CheckFunc[reflect.Value] {
return checkMAC
}
76 changes: 76 additions & 0 deletions v2/mac_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright (c) 2023-2024 Onur Cinar.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
// https://github.com/cinar/checker

package v2_test

import (
"fmt"
"testing"

v2 "github.com/cinar/checker/v2"
)

func ExampleIsMAC() {
_, err := v2.IsMAC("00:1A:2B:3C:4D:5E")
if err != nil {
fmt.Println(err)
}
}

func TestIsMACInvalid(t *testing.T) {
_, err := v2.IsMAC("invalid-mac")
if err == nil {
t.Fatal("expected error")
}
}

func TestIsMACValid(t *testing.T) {
_, err := v2.IsMAC("00:1A:2B:3C:4D:5E")
if err != nil {
t.Fatal(err)
}
}

func TestCheckMACNonString(t *testing.T) {
defer FailIfNoPanic(t, "expected panic")

type Device struct {
MAC int `checkers:"mac"`
}

device := &Device{}

v2.CheckStruct(device)
}

func TestCheckMACInvalid(t *testing.T) {
type Device struct {
MAC string `checkers:"mac"`
}

device := &Device{
MAC: "invalid-mac",
}

_, ok := v2.CheckStruct(device)
if ok {
t.Fatal("expected error")
}
}

func TestCheckMACValid(t *testing.T) {
type Device struct {
MAC string `checkers:"mac"`
}

device := &Device{
MAC: "00:1A:2B:3C:4D:5E",
}

_, ok := v2.CheckStruct(device)
if !ok {
t.Fatal("expected valid")
}
}
1 change: 1 addition & 0 deletions v2/maker.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var makers = map[string]MakeCheckFunc{
nameIPv6: makeIPv6,
nameISBN: makeISBN,
nameLUHN: makeLUHN,
nameMAC: makeMAC,
nameMaxLen: makeMaxLen,
nameMinLen: makeMinLen,
nameRequired: makeRequired,
Expand Down
Loading