Fix schema matching when using multiple schema locations

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 <noreply@anthropic.com>
This commit is contained in:
Emil Thorenfeldt 2025-07-12 14:13:15 +02:00
parent e65429b1e5
commit 5582d069a8

View file

@ -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
}