MCPcopy Create free account
hub / github.com/github/gh-aw / TestExtractJSONPathFromValidationError

Function TestExtractJSONPathFromValidationError

pkg/parser/json_path_locator_test.go:101–186  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

99}
100
101func TestExtractJSONPathFromValidationError(t *testing.T) {
102 // Create a schema with validation errors
103 schemaJSON := `{
104 "type": "object",
105 "properties": {
106 "name": {"type": "string"},
107 "age": {"type": "number"},
108 "tools": {
109 "type": "array",
110 "items": {
111 "type": "object",
112 "properties": {
113 "name": {"type": "string"}
114 },
115 "required": ["name"]
116 }
117 }
118 },
119 "additionalProperties": false
120 }`
121
122 // Create invalid data
123 invalidData := map[string]any{
124 "name": "John",
125 "age": "not-a-number", // Should be number
126 "invalid_key": "value", // Additional property not allowed
127 "tools": []any{
128 map[string]any{
129 "name": "tool1",
130 },
131 map[string]any{
132 // Missing required "name" field
133 "description": "tool without name",
134 },
135 },
136 }
137
138 // Compile schema and validate
139 compiler := jsonschema.NewCompiler()
140 var schemaDoc any
141 if err := json.Unmarshal([]byte(schemaJSON), &schemaDoc); err != nil {
142 t.Fatalf("json.Unmarshal error: %v", err)
143 }
144
145 schemaURL := "http://example.com/schema.json"
146 compiler.AddResource(schemaURL, schemaDoc)
147 schema, err := compiler.Compile(schemaURL)
148 if err != nil {
149 t.Fatalf("Schema compilation error: %v", err)
150 }
151
152 err = schema.Validate(invalidData)
153 if err == nil {
154 t.Fatal("Expected validation error, got nil")
155 }
156
157 // Extract JSON path information
158 paths := ExtractJSONPathFromValidationError(err)

Callers

nothing calls this directly

Calls 4

CompileMethod · 0.80
ValidateMethod · 0.45
ErrorfMethod · 0.45

Tested by

no test coverage detected