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

feat: pkg-util-validator Enhancement #860

Merged
merged 1 commit into from
Jul 18, 2022
Merged
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
63 changes: 63 additions & 0 deletions pkg/util/validator/validator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package validator

import (
"reflect"
"testing"

"github.com/go-playground/validator/v10"
)

type Tool struct {
Name string `yaml:"name" validate:"required"`
InstanceID string `yaml:"instanceID" validate:"required,dns1123subdomain"`
DependsOn []string `yaml:"dependsOn"`
Options map[string]interface{} `yaml:"options"`
}

func TestStruct(t *testing.T) {
tests := []struct {
name string
s interface{}
wantErrCount int
}{
// TODO: Add test cases.
{"base", struct{}{}, 0},
{"base Tool instance", Tool{}, 2},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Struct(tt.s); len(got) != tt.wantErrCount {
t.Errorf("Struct() = %v\n, got err count: %d\n, want err count:%d", got, len(got), tt.wantErrCount)
}
})
}
}

type FakerFieldLeveler struct {
validator.FieldLevel
field string
}

func (fl *FakerFieldLeveler) Field() reflect.Value {
return reflect.ValueOf(fl.field)
}

func Test_dns1123SubDomain(t *testing.T) {
goodValues, badValues := "a", ""
tests := []struct {
name string
fl validator.FieldLevel
want bool
}{
// TODO: Add test cases.
{"base", &FakerFieldLeveler{field: goodValues}, true},
{"base", &FakerFieldLeveler{field: badValues}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := dns1123SubDomain(tt.fl); got != tt.want {
t.Errorf("dns1123SubDomain() = %v, want %v", got, tt.want)
}
})
}
}