ValidateAndExtractName validates and extracts a name from either an explicit name parameter OR from metadata.name in the file content. Ensures exactly one source is provided. Returns error when: - Neither explicit name nor metadata.name is provided - Both explicit name and metadata.name are provided
(explicitName, filePath string)
| 58 | // - Neither explicit name nor metadata.name is provided |
| 59 | // - Both explicit name and metadata.name are provided (ambiguous) |
| 60 | func ValidateAndExtractName(explicitName, filePath string) (string, error) { |
| 61 | // Load file content if provided |
| 62 | var content []byte |
| 63 | var err error |
| 64 | if filePath != "" { |
| 65 | content, err = LoadFileOrURL(filePath) |
| 66 | if err != nil { |
| 67 | return "", fmt.Errorf("load file: %w", err) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // Extract name from v2 metadata (if present) |
| 72 | metadataName, err := extractNameFromMetadata(content) |
| 73 | if err != nil { |
| 74 | return "", fmt.Errorf("parse content: %w", err) |
| 75 | } |
| 76 | |
| 77 | // Both provided - ambiguous |
| 78 | if explicitName != "" && metadataName != "" { |
| 79 | return "", fmt.Errorf("conflicting names: explicit name (%q) and metadata.name (%q) both provided", explicitName, metadataName) |
| 80 | } |
| 81 | |
| 82 | // Neither provided - missing required name |
| 83 | if explicitName == "" && metadataName == "" { |
| 84 | if len(content) == 0 { |
| 85 | return "", errors.New("name is required when no file is provided") |
| 86 | } |
| 87 | return "", errors.New("name is required: either provide explicit name or include metadata.name in the schema") |
| 88 | } |
| 89 | |
| 90 | // Return whichever name was provided |
| 91 | if explicitName != "" { |
| 92 | return explicitName, nil |
| 93 | } |
| 94 | return metadataName, nil |
| 95 | } |
| 96 | |
| 97 | // metadataWithName represents a partial structure to extract metadata.name field |
| 98 | type metadataWithName struct { |
no test coverage detected