Skip to content

Commit 6e0edc0

Browse files
committed
I think this is as high as I can push it (coverage)
1 parent 19a6145 commit 6e0edc0

File tree

1 file changed

+78
-12
lines changed

1 file changed

+78
-12
lines changed

strict/validator_test.go

Lines changed: 78 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1654,6 +1654,57 @@ components:
16541654
assert.Len(t, result.UndeclaredValues, 2)
16551655
}
16561656

1657+
func TestStrictValidator_PrefixItems_FewerDataElements(t *testing.T) {
1658+
// Covers array_validator.go:41-42 - break when data has fewer elements than prefixItems
1659+
yml := `openapi: "3.1.0"
1660+
info:
1661+
title: Test
1662+
version: "1.0"
1663+
paths: {}
1664+
components:
1665+
schemas:
1666+
Tuple:
1667+
type: array
1668+
prefixItems:
1669+
- type: object
1670+
properties:
1671+
first:
1672+
type: string
1673+
- type: object
1674+
properties:
1675+
second:
1676+
type: string
1677+
- type: object
1678+
properties:
1679+
third:
1680+
type: string
1681+
`
1682+
model := buildSchemaFromYAML(t, yml)
1683+
schema := getSchema(t, model, "Tuple")
1684+
1685+
opts := config.NewValidationOptions(config.WithStrictMode())
1686+
v := NewValidator(opts, 3.1)
1687+
1688+
// Only 1 data element, but 3 prefixItems - should break early at line 42
1689+
data := []any{
1690+
map[string]any{"first": "a", "extra": "undeclared"},
1691+
}
1692+
1693+
result := v.Validate(Input{
1694+
Schema: schema,
1695+
Data: data,
1696+
Direction: DirectionRequest,
1697+
Options: opts,
1698+
BasePath: "$.body",
1699+
Version: 3.1,
1700+
})
1701+
1702+
// Only first element validated, has one undeclared property
1703+
assert.False(t, result.Valid)
1704+
assert.Len(t, result.UndeclaredValues, 1)
1705+
assert.Equal(t, "extra", result.UndeclaredValues[0].Name)
1706+
}
1707+
16571708
func TestStrictValidator_PrefixItemsWithItems(t *testing.T) {
16581709
yml := `openapi: "3.1.0"
16591710
info:
@@ -4954,6 +5005,7 @@ components:
49545005

49555006
func TestStrictValidator_ItemsWithIgnoredPath(t *testing.T) {
49565007
// Covers array_validator.go:71-72 - shouldIgnore in items loop
5008+
// Need to ignore the ITEM PATH itself ($.body[0]) not a nested property
49575009
yml := `openapi: "3.1.0"
49585010
info:
49595011
title: Test
@@ -4972,24 +5024,21 @@ components:
49725024
model := buildSchemaFromYAML(t, yml)
49735025
schema := getSchema(t, model, "ArrayIgnore")
49745026

5027+
// Ignore the first array item entirely ($.body[0])
49755028
opts := config.NewValidationOptions(
49765029
config.WithStrictMode(),
4977-
config.WithStrictIgnorePaths("$.body[*].metadata"),
5030+
config.WithStrictIgnorePaths("$.body[0]"),
49785031
)
49795032
v := NewValidator(opts, 3.1)
49805033

49815034
data := []any{
49825035
map[string]any{
4983-
"name": "item1",
4984-
"metadata": map[string]any{
4985-
"internal": "ignored",
4986-
},
5036+
"name": "item1",
5037+
"extra": "should be ignored because $.body[0] is ignored",
49875038
},
49885039
map[string]any{
4989-
"name": "item2",
4990-
"metadata": map[string]any{
4991-
"secret": "also ignored",
4992-
},
5040+
"name": "item2",
5041+
"extra": "should be flagged",
49935042
},
49945043
}
49955044

@@ -5002,9 +5051,11 @@ components:
50025051
Version: 3.1,
50035052
})
50045053

5005-
// metadata paths are ignored, so no undeclared errors
5006-
assert.True(t, result.Valid)
5007-
assert.Empty(t, result.UndeclaredValues)
5054+
// First item ignored, only second item's extra should be flagged
5055+
assert.False(t, result.Valid)
5056+
assert.Len(t, result.UndeclaredValues, 1)
5057+
assert.Equal(t, "extra", result.UndeclaredValues[0].Name)
5058+
assert.Equal(t, "$.body[1].extra", result.UndeclaredValues[0].Path)
50085059
}
50095060

50105061
func TestValidateRequestHeaders_DeclaredHeaderSkipped(t *testing.T) {
@@ -5195,6 +5246,21 @@ components:
51955246
assert.Empty(t, result.UndeclaredValues)
51965247
}
51975248

5249+
func TestNewValidator_WithCustomLogger(t *testing.T) {
5250+
// Covers types.go:295 - custom logger from options
5251+
customLogger := slog.New(slog.NewTextHandler(nil, nil))
5252+
opts := config.NewValidationOptions(
5253+
config.WithStrictMode(),
5254+
config.WithLogger(customLogger),
5255+
)
5256+
5257+
v := NewValidator(opts, 3.1)
5258+
5259+
// Verify the custom logger is used
5260+
assert.NotNil(t, v)
5261+
assert.Equal(t, customLogger, v.logger)
5262+
}
5263+
51985264
// =============================================================================
51995265
// Phase 3: MEDIUM Priority Tests
52005266
// =============================================================================

0 commit comments

Comments
 (0)