diff --git a/internal/blueprint/blueprint.go b/internal/blueprint/blueprint.go index 7efc9dfed..1c96edf0a 100644 --- a/internal/blueprint/blueprint.go +++ b/internal/blueprint/blueprint.go @@ -67,6 +67,10 @@ func (b *Blueprint) DeepCopy() Blueprint { // Initialize ensures that the blueprint has sane defaults for any missing fields func (b *Blueprint) Initialize() error { + if len(b.Name) == 0 { + return fmt.Errorf("empty blueprint name not allowed") + } + if b.Packages == nil { b.Packages = []Package{} } diff --git a/internal/store/store.go b/internal/store/store.go index 400ddfd25..bcc37dfee 100644 --- a/internal/store/store.go +++ b/internal/store/store.go @@ -209,17 +209,13 @@ func (s *Store) GetBlueprintChanges(name string) []blueprint.Change { func (s *Store) PushBlueprint(bp blueprint.Blueprint, commitMsg string) error { return s.change(func() error { - if len(bp.Name) == 0 { - return fmt.Errorf("empty blueprint name not allowed") - } - - commit, err := randomSHA1String() + // Make sure the blueprint has default values and that the version is valid + err := bp.Initialize() if err != nil { return err } - // Make sure the blueprint has default values and that the version is valid - err = bp.Initialize() + commit, err := randomSHA1String() if err != nil { return err } diff --git a/internal/weldr/api.go b/internal/weldr/api.go index 73c7dd5f5..aff0364aa 100644 --- a/internal/weldr/api.go +++ b/internal/weldr/api.go @@ -1945,6 +1945,17 @@ func (api *API) blueprintsNewHandler(writer http.ResponseWriter, request *http.R } } + // Make sure the blueprint has default values and that the version is valid + err = blueprint.Initialize() + if err != nil { + errors := responseError{ + ID: "BlueprintsError", + Msg: err.Error(), + } + statusResponseError(writer, http.StatusBadRequest, errors) + return + } + commitMsg := "Recipe " + blueprint.Name + ", version " + blueprint.Version + " saved." err = api.store.PushBlueprint(blueprint, commitMsg) if err != nil {