Skip to content

Commit

Permalink
Merge pull request #967 from jackchenjc/issue-966
Browse files Browse the repository at this point in the history
fix: Generate proper error message for 'unique' validation tag
  • Loading branch information
cloudxxx8 authored Jan 6, 2025
2 parents c47232d + be22b17 commit 2aec29e
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
9 changes: 8 additions & 1 deletion common/validator.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//go:build !no_dto_validator

//
// Copyright (C) 2020-2024 IOTech Ltd
// Copyright (C) 2020-2025 IOTech Ltd
//
// SPDX-License-Identifier: Apache-2.0

Expand Down Expand Up @@ -103,6 +103,13 @@ func getErrorMessage(e validator.FieldError) string {
msg = fmt.Sprintf("%s field should be one of %s", fieldName, fieldValue)
case "gt":
msg = fmt.Sprintf("%s field should greater than %s", fieldName, fieldValue)
case "unique":
// If the tag contains a field param, it means that the unique tag is used for a slice of struct
if fieldValue != "" {
msg = fmt.Sprintf("%s field should only contain unique elements with unique '%s' values", fieldName, fieldValue)
} else {
msg = fmt.Sprintf("%s field should only contain unique elements", fieldName)
}
case dtoDurationTag:
msg = fmt.Sprintf("%s field should follows the ISO 8601 Durations format, e.g.,100ms, 24h, or be greater than or equal to the minimum value %s ", fieldName, fieldValue)
case dtoUuidTag:
Expand Down
63 changes: 63 additions & 0 deletions common/validator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//
// Copyright (C) 2025 IOTech Ltd
//
// SPDX-License-Identifier: Apache-2.0

package common

import (
"testing"

"github.com/stretchr/testify/require"
)

type TestItem struct {
Name string
UniqueArray []string `validate:"unique"`
UniqueSliceOfStruct []TestItem `validate:"unique=Name"` // only one field is supported
}

// The test only covers the validation of the "unique" tag at the time of writing this comment.
// TODO: Add more tests to cover other validation tags if needed

// TestValidate tests the Validate function
func TestValidate(t *testing.T) {

validUniqueArray := TestItem{
UniqueArray: []string{"a", "b", "c"},
}
invalidUniqueArray := TestItem{
UniqueArray: []string{"a", "a", "c"},
}
validUniqueSliceOfSturct := TestItem{
UniqueSliceOfStruct: []TestItem{
{Name: "a"},
{Name: "b"},
},
}
invalidUniqueSliceOfSturct := TestItem{
UniqueSliceOfStruct: []TestItem{
{Name: "a"},
{Name: "a"},
},
}

tests := []struct {
name string
input any
expected bool
}{
{"valid, unique tag for arrays and slices", validUniqueArray, false},
{"invalid, unique tag for arrays and slices", invalidUniqueArray, true},
{"valid, unique tag for slices of struct", validUniqueSliceOfSturct, false},
{"invalid, unique tag for slices of struct", invalidUniqueSliceOfSturct, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := Validate(tt.input)
if tt.expected {
require.Error(t, err)
}
})
}
}

0 comments on commit 2aec29e

Please sign in to comment.