Skip to content

Commit

Permalink
Use KubernetesResourceName helper for validation
Browse files Browse the repository at this point in the history
  • Loading branch information
nikita-vanyasin committed Nov 27, 2023
1 parent 5eb454a commit 4d66673
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
9 changes: 3 additions & 6 deletions pkg/apis/shared/v1/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
meta "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/arangodb/kube-arangodb/pkg/apis/shared"
"github.com/arangodb/kube-arangodb/pkg/util/errors"
)

type Object struct {
Expand Down Expand Up @@ -64,11 +63,9 @@ func (o *Object) Validate() error {
}

var errs []error
if o.Name == "" {
errs = append(errs, shared.PrefixResourceErrors("name", errors.New("must be not empty")))
}
if o.Namespace != nil && *o.Namespace == "" {
errs = append(errs, shared.PrefixResourceErrors("namespace", errors.New("must be nil or non-empty string")))
errs = append(errs, shared.PrefixResourceErrors("name", AsKubernetesResourceName(&o.Name).Validate()))
if o.Namespace != nil {
errs = append(errs, shared.PrefixResourceErrors("namespace", AsKubernetesResourceName(o.Namespace).Validate()))
}

return shared.WithErrors(errs...)
Expand Down
52 changes: 52 additions & 0 deletions pkg/apis/shared/v1/object_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//
// DISCLAIMER
//
// Copyright 2023 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//

package v1

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/arangodb/kube-arangodb/pkg/util"
)

func Test_Object_Validate(t *testing.T) {
var o *Object
require.Error(t, o.Validate())

o = &Object{}
require.Error(t, o.Validate())

o.Name = "#invalid"
require.Error(t, o.Validate())

o.Name = "valid"
require.NoError(t, o.Validate())

o.Namespace = util.NewType("")
require.Error(t, o.Validate())

o.Namespace = util.NewType("#invalid")
require.Error(t, o.Validate())

o.Namespace = util.NewType("valid")
require.NoError(t, o.Validate())
}

0 comments on commit 4d66673

Please sign in to comment.