Skip to content

Commit d8cc0df

Browse files
committed
error handling coverage
1 parent 564f49e commit d8cc0df

1 file changed

Lines changed: 80 additions & 0 deletions

File tree

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Copyright 2026 Princess B33f Heavy Industries / Dave Shanley
2+
// SPDX-License-Identifier: MIT
3+
4+
package schema_validation
5+
6+
import (
7+
"testing"
8+
9+
"github.com/santhosh-tekuri/jsonschema/v6"
10+
"github.com/stretchr/testify/assert"
11+
"go.yaml.in/yaml/v4"
12+
"golang.org/x/text/message"
13+
)
14+
15+
type stubErrorKind struct {
16+
msg string
17+
}
18+
19+
func (s stubErrorKind) KeywordPath() []string {
20+
return nil
21+
}
22+
23+
func (s stubErrorKind) LocalizedString(_ *message.Printer) string {
24+
return s.msg
25+
}
26+
27+
func adjustedLine(node *yaml.Node) int {
28+
line := node.Line
29+
if (node.Kind == yaml.MappingNode || node.Kind == yaml.SequenceNode) && line > 0 {
30+
line--
31+
}
32+
return line
33+
}
34+
35+
func TestExtractBasicErrors_FallbackRenderedSchema_AdjustsLines(t *testing.T) {
36+
renderedSchema := []byte(`type: object
37+
required:
38+
- item
39+
properties:
40+
item:
41+
type: object`)
42+
payload := []byte(`{"item":{}}`)
43+
44+
flatErrors := []jsonschema.OutputUnit{
45+
{
46+
KeywordLocation: "/properties/item",
47+
AbsoluteKeywordLocation: "#/properties/item",
48+
InstanceLocation: "/item",
49+
Error: &jsonschema.OutputError{
50+
Kind: stubErrorKind{msg: "item is invalid"},
51+
},
52+
},
53+
{
54+
KeywordLocation: "/required",
55+
AbsoluteKeywordLocation: "#/required",
56+
InstanceLocation: "/item",
57+
Error: &jsonschema.OutputError{
58+
Kind: stubErrorKind{msg: "required is invalid"},
59+
},
60+
},
61+
}
62+
63+
failures := extractBasicErrors(flatErrors, renderedSchema, nil, map[string]any{"item": map[string]any{}}, payload, nil, nil)
64+
assert.Len(t, failures, 2)
65+
66+
var docNode yaml.Node
67+
err := yaml.Unmarshal(renderedSchema, &docNode)
68+
assert.NoError(t, err)
69+
assert.NotEmpty(t, docNode.Content)
70+
71+
mappingNode := LocateSchemaPropertyNodeByJSONPath(docNode.Content[0], "/properties/item")
72+
sequenceNode := LocateSchemaPropertyNodeByJSONPath(docNode.Content[0], "/required")
73+
74+
assert.NotNil(t, mappingNode)
75+
assert.NotNil(t, sequenceNode)
76+
assert.Equal(t, adjustedLine(mappingNode), failures[0].Line)
77+
assert.Equal(t, mappingNode.Column, failures[0].Column)
78+
assert.Equal(t, adjustedLine(sequenceNode), failures[1].Line)
79+
assert.Equal(t, sequenceNode.Column, failures[1].Column)
80+
}

0 commit comments

Comments
 (0)