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
8 changes: 8 additions & 0 deletions baked_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ var (
"json": isJSON,
"jwt": isJWT,
"hostname_port": isHostnamePort,
"port": isPort,
"lowercase": isLowercase,
"uppercase": isUppercase,
"datetime": isDatetime,
Expand Down Expand Up @@ -2704,6 +2705,13 @@ func isHostnamePort(fl FieldLevel) bool {
return true
}

// IsPort validates if the current field's value represents a valid port
func isPort(fl FieldLevel) bool {
val := fl.Field().Uint()

return val >= 1 && val <= 65535
}

// isLowercase is the validation function for validating if the current field's value is a lowercase string.
func isLowercase(fl FieldLevel) bool {
field := fl.Field()
Expand Down
26 changes: 26 additions & 0 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12377,6 +12377,32 @@ func Test_hostnameport_validator(t *testing.T) {
}
}

func Test_port_validator(t *testing.T) {
type Host struct {
Port uint32 `validate:"port"`
}

type testInput struct {
data uint32
expected bool
}
testData := []testInput{
{0, false},
{1, true},
{65535, true},
{65536, false},
{65538, false},
}
for _, td := range testData {
h := Host{Port: td.data}
v := New()
err := v.Struct(h)
if td.expected != (err == nil) {
t.Fatalf("Test failed for data: %v Error: %v", td.data, err)
}
}
}

func TestLowercaseValidation(t *testing.T) {
tests := []struct {
param string
Expand Down