From 5582d069a8d15af77b3aff4f31fed89d5ef0744d Mon Sep 17 00:00:00 2001 From: Emil Thorenfeldt Date: Sat, 12 Jul 2025 14:13:15 +0200 Subject: [PATCH] Fix schema matching when using multiple schema locations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When multiple schema locations are provided, ensure each resource is validated against the correct schema by checking if the schema's kind constraints match the expected resource type. Fixes #299 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- pkg/validator/validator.go | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/pkg/validator/validator.go b/pkg/validator/validator.go index 2412ac8..71b5252 100644 --- a/pkg/validator/validator.go +++ b/pkg/validator/validator.go @@ -384,7 +384,10 @@ func downloadSchema(registries []registry.Registry, l jsonschema.SchemeURLLoader if err != nil { continue } - return schema, nil + if schema != nil && isSchemaForKind(schema, kind) { + return schema, nil + } + continue } if _, notfound := err.(*loader.NotFoundError); notfound { @@ -399,3 +402,26 @@ func downloadSchema(registries []registry.Registry, l jsonschema.SchemeURLLoader return nil, nil // No schema found - we don't consider it an error, resource will be skipped } + +func isSchemaForKind(schema *jsonschema.Schema, expectedKind string) bool { + if schema == nil { + return true + } + + if schemaMap, ok := schema.Meta.(map[string]interface{}); ok { + if properties, ok := schemaMap["properties"].(map[string]interface{}); ok { + if kindProp, ok := properties["kind"].(map[string]interface{}); ok { + if enum, ok := kindProp["enum"].([]interface{}); ok && len(enum) > 0 { + if enumKind, ok := enum[0].(string); ok { + return enumKind == expectedKind + } + } + if constVal, ok := kindProp["const"].(string); ok { + return constVal == expectedKind + } + } + } + } + + return true +}