mirror of
https://github.com/yannh/kubeconform.git
synced 2026-02-11 14:09:21 +00:00
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:
parent
e65429b1e5
commit
5582d069a8
1 changed files with 27 additions and 1 deletions
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue