Validation: Move validation into apimachinery package (#111736)
This commit is contained in:
@@ -100,6 +100,9 @@ func (s *Storage) prepareObjectForStorage(ctx context.Context, newObject runtime
|
||||
if obj.GetFolder() != "" && !s.opts.EnableFolderSupport {
|
||||
return v, apierrors.NewBadRequest(fmt.Sprintf("folders are not supported for: %s", s.gr.String()))
|
||||
}
|
||||
if s.opts.MaximumNameLength > 0 && len(obj.GetName()) > s.opts.MaximumNameLength {
|
||||
return v, apierrors.NewBadRequest(fmt.Sprintf("name exceeds maximum length (%d)", s.opts.MaximumNameLength))
|
||||
}
|
||||
|
||||
v.grantPermissions = obj.GetAnnotation(utils.AnnoKeyGrantPermissions)
|
||||
if v.grantPermissions != "" {
|
||||
|
||||
@@ -3,6 +3,7 @@ package apistore
|
||||
import (
|
||||
"context"
|
||||
"math/rand/v2"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -35,6 +36,7 @@ func TestPrepareObjectForStorage(t *testing.T) {
|
||||
opts: StorageOptions{
|
||||
EnableFolderSupport: true,
|
||||
LargeObjectSupport: nil,
|
||||
MaximumNameLength: 100,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -49,10 +51,18 @@ func TestPrepareObjectForStorage(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("Error on missing name", func(t *testing.T) {
|
||||
dashboard := dashv1.Dashboard{}
|
||||
_, err := s.prepareObjectForStorage(ctx, dashboard.DeepCopyObject())
|
||||
dashboard := &dashv1.Dashboard{}
|
||||
_, err := s.prepareObjectForStorage(ctx, dashboard)
|
||||
require.Error(t, err)
|
||||
require.Contains(t, err.Error(), "missing name")
|
||||
require.ErrorContains(t, err, "missing name")
|
||||
})
|
||||
|
||||
t.Run("name is too long", func(t *testing.T) {
|
||||
dashboard := &dashv1.Dashboard{}
|
||||
dashboard.Name = strings.Repeat("a", 120)
|
||||
_, err := s.prepareObjectForStorage(ctx, dashboard)
|
||||
require.Error(t, err)
|
||||
require.ErrorContains(t, err, "name exceeds maximum length")
|
||||
})
|
||||
|
||||
t.Run("Error on non-empty resource version", func(t *testing.T) {
|
||||
|
||||
@@ -59,6 +59,9 @@ type StorageOptions struct {
|
||||
// Allow writing objects with metadata.annotations[grafana.app/folder]
|
||||
EnableFolderSupport bool
|
||||
|
||||
// Some resources should not allow the absolute maximum (254 characters)
|
||||
MaximumNameLength int
|
||||
|
||||
// Add internalID label when missing
|
||||
RequireDeprecatedInternalID bool
|
||||
|
||||
|
||||
Reference in New Issue
Block a user