Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 22 additions & 21 deletions pkg/parser/import_schema_validation_input_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ import (
"testing"

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

// TestValidateImportInputType_String covers valid and invalid string inputs.
func TestValidateImportInputType_String(t *testing.T) {
err := validateImportInputType("name", "hello", "string", nil, "owner/repo/import.md")
assert.NoError(t, err)
require.NoError(t, err)

err = validateImportInputType("name", 42, "string", nil, "owner/repo/import.md")
assert.Error(t, err)
require.Error(t, err)
assert.Contains(t, err.Error(), "must be a string")
}

Expand All @@ -26,21 +27,21 @@ func TestValidateImportInputType_NumberAllTypes(t *testing.T) {
}
for _, v := range validValues {
err := validateImportInputType("count", v, "number", nil, "owner/repo/import.md")
assert.NoErrorf(t, err, "expected %T to be accepted as number", v)
require.NoErrorf(t, err, "expected %T to be accepted as number", v)
}

err := validateImportInputType("count", "not-a-number", "number", nil, "owner/repo/import.md")
assert.Error(t, err)
require.Error(t, err)
assert.Contains(t, err.Error(), "must be a number")
}

// TestValidateImportInputType_Boolean covers valid and invalid boolean inputs.
func TestValidateImportInputType_Boolean(t *testing.T) {
err := validateImportInputType("flag", true, "boolean", nil, "owner/repo/import.md")
assert.NoError(t, err)
require.NoError(t, err)

err = validateImportInputType("flag", "true", "boolean", nil, "owner/repo/import.md")
assert.Error(t, err)
require.Error(t, err)
assert.Contains(t, err.Error(), "must be a boolean")
}

Expand All @@ -49,62 +50,62 @@ func TestValidateImportInputType_Boolean(t *testing.T) {
func TestValidateImportInputType_Choice(t *testing.T) {
// Non-string value is rejected.
err := validateImportInputType("level", 5, "choice", nil, "owner/repo/import.md")
assert.Error(t, err)
require.Error(t, err)
assert.Contains(t, err.Error(), "must be a string for choice type")

// No "options" key declared: falls through without error.
err = validateImportInputType("level", "low", "choice", map[string]any{}, "owner/repo/import.md")
assert.NoError(t, err)
require.NoError(t, err)

// Options declared but not a []any: falls through the type switch (options ignored) -> no match -> error.
err = validateImportInputType("level", "low", "choice", map[string]any{"options": "not-a-list"}, "owner/repo/import.md")
assert.NoError(t, err)
require.NoError(t, err)

// Matching option.
paramDef := map[string]any{"options": []any{"low", "medium", "high"}}
err = validateImportInputType("level", "medium", "choice", paramDef, "owner/repo/import.md")
assert.NoError(t, err)
require.NoError(t, err)

// Non-matching option.
err = validateImportInputType("level", "extreme", "choice", paramDef, "owner/repo/import.md")
assert.Error(t, err)
require.Error(t, err)
assert.Contains(t, err.Error(), "is not in the allowed options")

// Options list containing a non-string entry is skipped without panicking.
paramDefMixed := map[string]any{"options": []any{42, "low"}}
err = validateImportInputType("level", "low", "choice", paramDefMixed, "owner/repo/import.md")
assert.NoError(t, err)
require.NoError(t, err)
}

// TestValidateImportInputType_Array covers non-array rejection, no items schema,
// items schema without a type, matching item types, and recursive rejection.
func TestValidateImportInputType_Array(t *testing.T) {
// Non-array value is rejected.
err := validateImportInputType("tags", "not-an-array", "array", nil, "owner/repo/import.md")
assert.Error(t, err)
require.Error(t, err)
assert.Contains(t, err.Error(), "must be an array")

// No "items" schema declared: any array is accepted.
err = validateImportInputType("tags", []any{"a", "b"}, "array", map[string]any{}, "owner/repo/import.md")
assert.NoError(t, err)
require.NoError(t, err)

// Items schema present but not a map: no item type -> accepted.
err = validateImportInputType("tags", []any{"a"}, "array", map[string]any{"items": "not-a-map"}, "owner/repo/import.md")
assert.NoError(t, err)
require.NoError(t, err)

// Items schema with empty type: accepted without recursing.
err = validateImportInputType("tags", []any{"a"}, "array", map[string]any{"items": map[string]any{}}, "owner/repo/import.md")
assert.NoError(t, err)
require.NoError(t, err)

// Items schema with matching string type: all items validate successfully.
arrParamDef := map[string]any{"items": map[string]any{"type": "string"}}
err = validateImportInputType("tags", []any{"a", "b", "c"}, "array", arrParamDef, "owner/repo/import.md")
assert.NoError(t, err)
require.NoError(t, err)

// Items schema with string type but an invalid item: recursive call surfaces the error
// with an indexed item name.
err = validateImportInputType("tags", []any{"a", 5}, "array", arrParamDef, "owner/repo/import.md")
assert.Error(t, err)
require.Error(t, err)
assert.Contains(t, err.Error(), "tags[1]")
assert.Contains(t, err.Error(), "must be a string")
}
Expand All @@ -115,16 +116,16 @@ func TestValidateImportInputType_Object(t *testing.T) {
// A nil paramDef / no "properties" key: validateObjectInput should not error for
// a map value regardless of properties (delegation smoke test).
err := validateImportInputType("config", map[string]any{"a": 1}, "object", map[string]any{}, "owner/repo/import.md")
assert.NoError(t, err)
require.NoError(t, err)

// Non-map value for an object type should produce an error via delegation.
err = validateImportInputType("config", "not-an-object", "object", map[string]any{}, "owner/repo/import.md")
assert.Error(t, err)
require.Error(t, err)
}

// TestValidateImportInputType_UnknownDeclaredType verifies an unrecognized declared
// type falls through the switch without validation and returns nil.
func TestValidateImportInputType_UnknownDeclaredType(t *testing.T) {
err := validateImportInputType("field", "anything", "unknown-type", nil, "owner/repo/import.md")
assert.NoError(t, err)
require.NoError(t, err)
}