The source headers, LICENSE files, and license metadata had drifted apart. Align the entire project to MIT: - Convert every source-file header to the MIT text across all comment styles (Go, TS, TSX, JS, MJS, SQL, CSS, GraphQL, shell), including SPDX-License-Identifier tags - Set the root and cookie-banner LICENSE files to the MIT text with a "MIT License" title line - Switch the package.json license fields, Docker image label, and cookie-banner README to MIT - Update docs and the genmodels header generator accordingly - Normalize copyright lines to a single format (Copyright (c) <year(s)> Probo Inc <hello@probo.com>.): unify the hello@getprobo.com and hello@probo.inc emails to hello@probo.com and the comma-separated years to a hyphenated range Genuine third-party references are intentionally left untouched: the Lucide icon attributions (Lucide is ISC) and the trivy dependency license allowlist. Signed-off-by: Sacha Al Himdani <sacha@probo.com>
219 lines
5.8 KiB
Go
219 lines
5.8 KiB
Go
// Copyright (c) 2025-2026 Probo Inc <hello@probo.com>.
|
|
//
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
// in the Software without restriction, including without limitation the rights
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
// furnished to do so, subject to the following conditions:
|
|
//
|
|
// The above copyright notice and this permission notice shall be included in
|
|
// all copies or substantial portions of the Software.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
// SOFTWARE.
|
|
|
|
package validator
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
// CustomType simulates types like gid.GID
|
|
type CustomType string
|
|
|
|
func TestCheckEach_EmptyTypedSlice(t *testing.T) {
|
|
v := New()
|
|
|
|
// Simulate what happens with []gid.GID{} (empty slice of custom type)
|
|
emptySlice := []CustomType{}
|
|
|
|
v.CheckEach(emptySlice, "items", func(index int, item any) {
|
|
// This callback should never be called for an empty slice
|
|
t.Error("callback should not be called for empty slice")
|
|
})
|
|
|
|
if v.Error() != nil {
|
|
t.Errorf("unexpected error for empty slice: %v", v.Error())
|
|
}
|
|
}
|
|
|
|
func TestCheckEach_NonEmptyTypedSlice(t *testing.T) {
|
|
v := New()
|
|
|
|
// Simulate what happens with []gid.GID{"abc", "def"}
|
|
slice := []CustomType{"abc", "def"}
|
|
|
|
callCount := 0
|
|
|
|
v.CheckEach(slice, "items", func(index int, item any) {
|
|
callCount++
|
|
// Verify the item is the correct type
|
|
str, ok := item.(CustomType)
|
|
if !ok {
|
|
t.Errorf("expected CustomType, got %T", item)
|
|
}
|
|
|
|
if index == 0 && str != "abc" {
|
|
t.Errorf("expected 'abc', got %s", str)
|
|
}
|
|
|
|
if index == 1 && str != "def" {
|
|
t.Errorf("expected 'def', got %s", str)
|
|
}
|
|
})
|
|
|
|
if callCount != 2 {
|
|
t.Errorf("expected callback to be called 2 times, got %d", callCount)
|
|
}
|
|
|
|
if v.Error() != nil {
|
|
t.Errorf("unexpected error: %v", v.Error())
|
|
}
|
|
}
|
|
|
|
func TestCheckEach_NilTypedSlice(t *testing.T) {
|
|
v := New()
|
|
|
|
// Simulate what happens with var x []gid.GID (nil slice)
|
|
var nilSlice []CustomType
|
|
|
|
v.CheckEach(nilSlice, "items", func(index int, item any) {
|
|
// This callback should never be called for a nil slice
|
|
t.Error("callback should not be called for nil slice")
|
|
})
|
|
|
|
if v.Error() != nil {
|
|
t.Errorf("unexpected error for nil slice: %v", v.Error())
|
|
}
|
|
}
|
|
|
|
func TestCheckEach_PointerToNonEmptySlice(t *testing.T) {
|
|
v := New()
|
|
|
|
// Simulate what happens with *[]gid.GID (pointer to slice)
|
|
slice := []CustomType{"abc", "def", "ghi"}
|
|
ptrToSlice := &slice
|
|
|
|
callCount := 0
|
|
|
|
v.CheckEach(ptrToSlice, "items", func(index int, item any) {
|
|
callCount++
|
|
|
|
str, ok := item.(CustomType)
|
|
if !ok {
|
|
t.Errorf("expected CustomType, got %T", item)
|
|
}
|
|
|
|
expectedValues := []CustomType{"abc", "def", "ghi"}
|
|
if str != expectedValues[index] {
|
|
t.Errorf("at index %d: expected %s, got %s", index, expectedValues[index], str)
|
|
}
|
|
})
|
|
|
|
if callCount != 3 {
|
|
t.Errorf("expected callback to be called 3 times, got %d", callCount)
|
|
}
|
|
|
|
if v.Error() != nil {
|
|
t.Errorf("unexpected error for pointer to slice: %v", v.Error())
|
|
}
|
|
}
|
|
|
|
func TestCheckEach_PointerToEmptySlice(t *testing.T) {
|
|
v := New()
|
|
|
|
// Simulate what happens with *[]gid.GID{} (pointer to empty slice)
|
|
slice := []CustomType{}
|
|
ptrToSlice := &slice
|
|
|
|
v.CheckEach(ptrToSlice, "items", func(index int, item any) {
|
|
t.Error("callback should not be called for empty slice")
|
|
})
|
|
|
|
if v.Error() != nil {
|
|
t.Errorf("unexpected error for pointer to empty slice: %v", v.Error())
|
|
}
|
|
}
|
|
|
|
func TestCheckEach_NilPointerToSlice(t *testing.T) {
|
|
v := New()
|
|
|
|
// Simulate what happens with var x *[]gid.GID (nil pointer to slice)
|
|
var nilPtrToSlice *[]CustomType
|
|
|
|
v.CheckEach(nilPtrToSlice, "items", func(index int, item any) {
|
|
t.Error("callback should not be called for nil pointer to slice")
|
|
})
|
|
|
|
if v.Error() != nil {
|
|
t.Errorf("unexpected error for nil pointer to slice: %v", v.Error())
|
|
}
|
|
}
|
|
|
|
func TestCheckEach_DoublePointerToSlice(t *testing.T) {
|
|
v := New()
|
|
|
|
// Simulate what happens with **[]gid.GID (double pointer to slice)
|
|
slice := []CustomType{"x", "y"}
|
|
ptrToSlice := &slice
|
|
doublePtrToSlice := &ptrToSlice
|
|
|
|
callCount := 0
|
|
|
|
v.CheckEach(doublePtrToSlice, "items", func(index int, item any) {
|
|
callCount++
|
|
|
|
str, ok := item.(CustomType)
|
|
if !ok {
|
|
t.Errorf("expected CustomType, got %T", item)
|
|
}
|
|
|
|
expectedValues := []CustomType{"x", "y"}
|
|
if str != expectedValues[index] {
|
|
t.Errorf("at index %d: expected %s, got %s", index, expectedValues[index], str)
|
|
}
|
|
})
|
|
|
|
if callCount != 2 {
|
|
t.Errorf("expected callback to be called 2 times, got %d", callCount)
|
|
}
|
|
|
|
if v.Error() != nil {
|
|
t.Errorf("unexpected error for double pointer to slice: %v", v.Error())
|
|
}
|
|
}
|
|
|
|
func TestCheckEach_NonSliceValue(t *testing.T) {
|
|
v := New()
|
|
|
|
// Pass a non-slice value
|
|
notASlice := "this is a string"
|
|
|
|
v.CheckEach(notASlice, "items", func(index int, item any) {
|
|
t.Error("callback should not be called for non-slice value")
|
|
})
|
|
|
|
if v.Error() == nil {
|
|
t.Error("expected error for non-slice value")
|
|
}
|
|
|
|
errors := v.Error().(ValidationErrors)
|
|
if len(errors) != 1 {
|
|
t.Errorf("expected 1 error, got %d", len(errors))
|
|
}
|
|
|
|
if errors[0].Code != ErrorCodeInvalidFormat {
|
|
t.Errorf("expected error code %s, got %s", ErrorCodeInvalidFormat, errors[0].Code)
|
|
}
|
|
|
|
if errors[0].Message != "expected a slice" {
|
|
t.Errorf("expected message 'expected a slice', got '%s'", errors[0].Message)
|
|
}
|
|
}
|