Skip to content

Commit

Permalink
chore: clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickkabwe committed May 12, 2024
1 parent 9c9c08c commit 41240f3
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 61 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ on:
push:
pull_request:
branches: ["main"]
permissions:
contents: read

jobs:
build:
Expand All @@ -18,6 +20,17 @@ jobs:
with:
go-version: ${{ matrix.go }}

- name: golangci-lint
uses: golangci/golangci-lint-action@v6
with:
version: latest

- name: Lint
run: golangci-lint run ./...

- name: Format
run: go fmt ./...

- name: Build
run: go build -v ./...

Expand Down
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ test:

test-coverage:
@go test -coverprofile=coverage.out ./...
@go tool cover -html=coverage.out
@go tool cover -html=coverage.out

format:
@go fmt ./...
10 changes: 4 additions & 6 deletions errorMap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/stretchr/testify/assert"
)


func TestErrorMap(t *testing.T) {
testCases := []struct {
name string
Expand All @@ -19,13 +18,13 @@ func TestErrorMap(t *testing.T) {
expected: map[string]string{},
},
{
name: "error map with one error",
input: errorMap{"email": ErrEmailNotValid },
name: "error map with one error",
input: errorMap{"email": ErrEmailNotValid},
expected: map[string]string{"email": ErrEmailNotValid.Error()},
},
{
name: "error map with multiple errors",
input: errorMap{"email": ErrEmailNotValid, "phone": ErrNotEmptyField},
name: "error map with multiple errors",
input: errorMap{"email": ErrEmailNotValid, "phone": ErrNotEmptyField},
expected: map[string]string{"email": ErrEmailNotValid.Error(), "phone": ErrNotEmptyField.Error()},
},
}
Expand All @@ -36,4 +35,3 @@ func TestErrorMap(t *testing.T) {
})
}
}

17 changes: 8 additions & 9 deletions validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (v *validator) IsIP(input string) (ok bool, err error) {
return true, nil
}

func (v *validator) isInt(input any,fieldName string) (ok bool, err error) {
func (v *validator) isInt(input any, fieldName string) (ok bool, err error) {
switch input.(type) {
case int:
return true, nil
Expand All @@ -110,7 +110,7 @@ func (v *validator) ValidateStruct(input any) errorMap {
if inputKind == reflect.Ptr {
st = st.Elem()
input = reflect.ValueOf(input).Elem().Interface()
}
}

for i := 0; i < st.NumField(); i++ {
field := st.Field(i)
Expand All @@ -121,21 +121,20 @@ func (v *validator) ValidateStruct(input any) errorMap {

var actualValue any
switch fieldValue.Kind() {
case reflect.Interface:
actualValue = fieldValue.Interface()
default:
actualValue = fieldValue.String()
case reflect.Interface:
actualValue = fieldValue.Interface()
default:
actualValue = fieldValue.String()
}
v.handleStructValidation( validateTags, strings.ToLower(fieldName), actualValue)

v.handleStructValidation(validateTags, strings.ToLower(fieldName), actualValue)

// TODO: handle embedded structs, slices, maps, etc.
}

return v.errors
}


func (v *validator) handleStructValidation(input []string, fieldName string, fieldValue any) {
for _, field := range input {
switch field {
Expand Down
90 changes: 45 additions & 45 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
)

type TestCase[T any] struct {
name string
input T
name string
input T
expectedErr bool
}

Expand All @@ -22,38 +22,38 @@ type StructTest struct {
func TestValidate__isEmail(t *testing.T) {
testCases := []TestCase[string]{
{
name: "not valid email address (Missing @)",
input: "test.com",
name: "not valid email address (Missing @)",
input: "test.com",
expectedErr: true,
},
{
name: "not valid email address (Empty string)",
input: "",
name: "not valid email address (Empty string)",
input: "",
expectedErr: true,
},
{
name: "not valid email address (Missing .)",
input: "test@testcom",
name: "not valid email address (Missing .)",
input: "test@testcom",
expectedErr: true,
},
{
name: "not valid email address (Missing domain)",
input: "[email protected]",
name: "not valid email address (Missing domain)",
input: "[email protected]",
expectedErr: true,
},
{
name: "not valid email address (Missing local part)",
input: "@test.com",
name: "not valid email address (Missing local part)",
input: "@test.com",
expectedErr: true,
},
{
name: "not valid email address (Missing local part and domain)",
input: "@.com",
name: "not valid email address (Missing local part and domain)",
input: "@.com",
expectedErr: true,
},
{
name: "valid email address",
input: "[email protected]",
name: "valid email address",
input: "[email protected]",
expectedErr: false,
},
}
Expand All @@ -69,21 +69,21 @@ func TestValidate__isEmail(t *testing.T) {
assert.NoError(t, err)
assert.True(t, isValid)
}

})
}
}

func TestValidate__isEmpty(t *testing.T) {
testCases := []TestCase[string]{
{
name: "empty string",
input: "",
name: "empty string",
input: "",
expectedErr: true,
},
{
name: "not empty string",
input: "test",
name: "not empty string",
input: "test",
expectedErr: false,
},
}
Expand All @@ -98,7 +98,7 @@ func TestValidate__isEmpty(t *testing.T) {
} else {
assert.NoError(t, err)
assert.True(t, isValid)

}
})
}
Expand All @@ -107,23 +107,23 @@ func TestValidate__isEmpty(t *testing.T) {
func TestValidate__isURL(t *testing.T) {
testCases := []TestCase[string]{
{
name: "not valid url",
input: "",
name: "not valid url",
input: "",
expectedErr: true,
},
{
name: "not valid url",
input: "test.com",
name: "not valid url",
input: "test.com",
expectedErr: true,
},
{
name: "valid url using http protocol",
input: "https://www.google.com",
name: "valid url using http protocol",
input: "https://www.google.com",
expectedErr: false,
},
{
name: "valid url using postgres protocol",
input: "postgres://localhost:5432/testdb",
name: "valid url using postgres protocol",
input: "postgres://localhost:5432/testdb",
expectedErr: false,
},
}
Expand All @@ -146,13 +146,13 @@ func TestValidate__isURL(t *testing.T) {
func TestValidate__isIP(t *testing.T) {
testCases := []TestCase[string]{
{
name: "not valid ip",
input: "192.168.0",
name: "not valid ip",
input: "192.168.0",
expectedErr: true,
},
{
name: "valid ip",
input: "192.168.0.1",
name: "valid ip",
input: "192.168.0.1",
expectedErr: false,
},
}
Expand All @@ -177,32 +177,32 @@ func TestValidate__validateStruct(t *testing.T) {
{
name: "invalid struct - age is missing",
input: StructTest{
Name: "test",
Name: "test",
Phone: 1234567890,
},
expectedErr: true,
},
{
name: "invalid struct - age is not an integer and name is missing",
input: StructTest{
Age: "10",
Age: "10",
Phone: 1234567890,
},
expectedErr: true,
},
{
name: "invalid struct - name is missing",
input: StructTest{
Age: 10,
Age: 10,
Phone: 1234567890,
},
expectedErr: true,
},
{
name: "invalid struct - email is not valid",
input: StructTest{
Name: "test",
Age: 10,
Name: "test",
Age: 10,
Phone: 1234567890,
Email: "test",
},
Expand All @@ -211,8 +211,8 @@ func TestValidate__validateStruct(t *testing.T) {
{
name: "valid struct",
input: StructTest{
Name: "test",
Age: 10,
Name: "test",
Age: 10,
Phone: 1234567890,
Email: "[email protected]",
},
Expand Down Expand Up @@ -241,7 +241,7 @@ func TestValidate__validatePointerStruct(t *testing.T) {
{
name: "invalid pointer struct - age and email is missing",
input: &StructTest{
Name: "test",
Name: "test",
Phone: 1234567890,
},
expectedErr: true,
Expand All @@ -250,8 +250,8 @@ func TestValidate__validatePointerStruct(t *testing.T) {
{
name: "valid pointer struct",
input: &StructTest{
Name: "test",
Age: 10,
Name: "test",
Age: 10,
Phone: 1234567890,
Email: "[email protected]",
},
Expand All @@ -273,4 +273,4 @@ func TestValidate__validatePointerStruct(t *testing.T) {

})
}
}
}

0 comments on commit 41240f3

Please sign in to comment.