diff --git a/examples/list_verification.yaml b/examples/list_verification.yaml new file mode 100644 index 00000000..ebada1d6 --- /dev/null +++ b/examples/list_verification.yaml @@ -0,0 +1,4 @@ +- name: Verify no duplicates in list + type: list + itemsPath: $.someList + noDuplicates: true diff --git a/internal/verify/config.go b/internal/verify/config.go new file mode 100644 index 00000000..9d1a0a31 --- /dev/null +++ b/internal/verify/config.go @@ -0,0 +1 @@ + NoDuplicates bool `yaml:"noDuplicates,omitempty"` diff --git a/internal/verify/duplicate_check.go b/internal/verify/duplicate_check.go new file mode 100644 index 00000000..1dcb139f --- /dev/null +++ b/internal/verify/duplicate_check.go @@ -0,0 +1,36 @@ +// +// Licensed to Apache Software Foundation (ASF) under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Apache Software Foundation (ASF) licenses this file to you 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. + +package verify + +import ( + "fmt" +) + +// CheckForDuplicates verifies if there are duplicate entries in a list. +// It returns an error if any duplicates are found. +func CheckForDuplicates(list []string) error { + seen := make(map[string]bool) + for _, item := range list { + if seen[item] { + return fmt.Errorf("duplicate data found: %s", item) + } + seen[item] = true + } + return nil +} diff --git a/internal/verify/duplicate_check_test.go b/internal/verify/duplicate_check_test.go new file mode 100644 index 00000000..9bab9f65 --- /dev/null +++ b/internal/verify/duplicate_check_test.go @@ -0,0 +1,56 @@ +// +// Licensed to Apache Software Foundation (ASF) under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Apache Software Foundation (ASF) licenses this file to you 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. + +package verify + +import "testing" + +func TestCheckForDuplicates(t *testing.T) { + tests := []struct { + name string + input []string + wantErr bool + }{ + {"no duplicates", []string{"a", "b", "c"}, false}, + {"has duplicates", []string{"a", "b", "a"}, true}, + } + + for _, tt := range tests { + err := CheckForDuplicates(tt.input) + if (err != nil) != tt.wantErr { + t.Errorf("%s: expected error=%v, got %v", tt.name, tt.wantErr, err != nil) + } + } +} + +func TestRunListVerification_NoDuplicatesFails(t *testing.T) { + cfg := ListVerification{NoDuplicates: true} + items := []string{"a", "b", "a"} + err := (&Verifier{}).RunListVerification(cfg, items) + if err == nil { + t.Fatalf("expected duplicate check to fail but got nil") + } +} + +func TestRunListVerification_NoDuplicatesPassesForUnique(t *testing.T) { + cfg := ListVerification{NoDuplicates: true} + items := []string{"a", "b", "c"} + if err := (&Verifier{}).RunListVerification(cfg, items); err != nil { + t.Fatalf("unexpected error: %v", err) + } +} diff --git a/internal/verify/runner.go b/internal/verify/runner.go new file mode 100644 index 00000000..f144440d --- /dev/null +++ b/internal/verify/runner.go @@ -0,0 +1,5 @@ +if cfg.NoDuplicates { + if err := CheckForDuplicates(items); err != nil { + return fmt.Errorf("duplicate check failed: %w", err) + } +}