Skip to content

Commit 917465e

Browse files
fix: enhance validations in loadRawScoreFiles to check for missing metadata (#498)
Signed-off-by: Prashantkumar Khatri <prashantkhatri202@gmail.com> Signed-off-by: Prashantkumar Khatri <khatri2105104@st.jmi.ac.in> Co-authored-by: Mathieu Benoit <mathieu-benoit@hotmail.fr>
1 parent c8ce11f commit 917465e

2 files changed

Lines changed: 20 additions & 7 deletions

File tree

internal/command/generate.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,7 @@ func parseResourceUid(raw string) framework.ResourceUid {
453453
func loadRawScoreFiles(fileNames []string) ([]string, map[string]map[string]interface{}, error) {
454454
workloadNames := make([]string, 0, len(fileNames))
455455
workloadToRawScore := make(map[string]map[string]interface{}, len(fileNames))
456+
var validationErrors []string
456457

457458
for _, fileName := range fileNames {
458459
var out map[string]interface{}
@@ -464,15 +465,28 @@ func loadRawScoreFiles(fileNames []string) ([]string, map[string]map[string]inte
464465
}
465466

466467
var workloadName string
467-
if meta, ok := out["metadata"].(map[string]interface{}); ok {
468-
workloadName, _ = meta["name"].(string)
469-
if _, ok := workloadToRawScore[workloadName]; ok {
470-
return nil, nil, fmt.Errorf("workload name '%s' in file '%s' is used more than once", workloadName, fileName)
471-
}
468+
meta, ok := out["metadata"].(map[string]interface{})
469+
if !ok {
470+
validationErrors = append(validationErrors, fmt.Sprintf("workload in file '%s' is missing required metadata", fileName))
471+
continue
472+
}
473+
workloadName, _ = meta["name"].(string)
474+
if len(workloadName) == 0 {
475+
validationErrors = append(validationErrors, fmt.Sprintf("workload in file '%s' has empty metadata.name", fileName))
476+
continue
477+
}
478+
if _, ok := workloadToRawScore[workloadName]; ok {
479+
validationErrors = append(validationErrors, fmt.Sprintf("workload name '%s' in file '%s' is used more than once", workloadName, fileName))
480+
continue
472481
}
473482
workloadNames = append(workloadNames, workloadName)
474483
workloadToRawScore[workloadName] = out
475484
}
485+
486+
if len(validationErrors) > 0 {
487+
return nil, nil, fmt.Errorf("validation failed:\n - %s", strings.Join(validationErrors, "\n - "))
488+
}
489+
476490
return workloadNames, workloadToRawScore, nil
477491
}
478492

@@ -561,4 +575,3 @@ func injectWaitService(p *types.Project) (string, bool) {
561575
p.Services[newService.Name] = newService
562576
return newService.Name, true
563577
}
564-

internal/command/generate_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func TestInitAndGenerateWithBadScore(t *testing.T) {
134134
assert.NoError(t, os.WriteFile(filepath.Join(td, "thing"), []byte(`{}`), 0644))
135135

136136
stdout, _, err = executeAndResetCommand(context.Background(), rootCmd, []string{"generate", "thing"})
137-
assert.EqualError(t, err, "validation errors in workload '': jsonschema: '' does not validate with https://score.dev/schemas/score#/required: missing properties: 'apiVersion', 'metadata', 'containers'")
137+
assert.EqualError(t, err, "validation failed:\n - workload in file 'thing' is missing required metadata")
138138
assert.Equal(t, "", stdout)
139139
}
140140

0 commit comments

Comments
 (0)