MCPcopy
hub / github.com/C2SP/wycheproof / lintVectorToSchema

Function lintVectorToSchema

tools/vectorlint/main.go:287–340  ·  view source on GitHub ↗
(schemaCompiler *jsonschema.Compiler, vectorData []byte, path string, results *schemaLintResults)

Source from the content-addressed store, hash-verified

285}
286
287func lintVectorToSchema(schemaCompiler *jsonschema.Compiler, vectorData []byte, path string, results *schemaLintResults) error {
288 var vector struct {
289 Schema string `json:"schema"`
290 }
291
292 if err := json.Unmarshal(vectorData, &vector); err != nil {
293 log.Printf("❌ %q: invalid vector JSON data: %s\n", path, err)
294 results.invalid++
295 return nil
296 }
297
298 if vector.Schema == "" {
299 log.Printf("❌ %q: no schema specified\n", path)
300 results.noSchema++
301 return nil
302 }
303
304 if missingSchemas[vector.Schema] {
305 log.Printf("⚠️ %q: ignoring missing schema %q\n", path, vector.Schema)
306 results.ignored++
307 return nil
308 }
309
310 schemaPath := filepath.Join(*schemaDirectory, vector.Schema)
311 if _, err := os.Stat(schemaPath); os.IsNotExist(err) {
312 log.Printf("❌ %q: referenced schema %q not found\n", path, vector.Schema)
313 results.invalid++
314 return nil
315 }
316
317 schema, err := schemaCompiler.Compile(schemaPath)
318 if err != nil {
319 log.Printf("❌ %q: invalid schema %q: %s\n", path, vector.Schema, err)
320 results.invalid++
321 return nil
322 }
323
324 var instance any
325 if err := json.Unmarshal(vectorData, &instance); err != nil {
326 log.Printf("❌ %q: invalid vector JSON data: %s\n", path, err)
327 results.invalid++
328 return nil
329 }
330
331 if err := schema.Validate(instance); err != nil {
332 log.Printf("❌ %q: vector doesn't validate with schema: %s\n", path, err)
333 results.invalid++
334 return nil
335 }
336
337 log.Printf("✅ %q: validates with %q\n", path, vector.Schema)
338 results.valid++
339 return nil
340}
341
342type schemaLintResults struct {
343 total, valid, invalid, noSchema, ignored int

Callers 1

lintVectorDirFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected