Skip to content

Commit

Permalink
fix(fe): validate cron format before send save request
Browse files Browse the repository at this point in the history
  • Loading branch information
fiftin committed Sep 6, 2021
1 parent c236737 commit 28cea35
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 12 deletions.
35 changes: 23 additions & 12 deletions api/projects/schedules.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,25 @@ func GetTemplateSchedules(w http.ResponseWriter, r *http.Request) {
helpers.WriteJSON(w, http.StatusOK, tplSchedules)
}

func validateCronFormat(cronFormat string, w http.ResponseWriter) bool {
err := schedules.ValidateCronFormat(cronFormat)
if err == nil {
return true
}
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
"error": "Cron: " + err.Error(),
})
return false
}

func ValidateScheduleCronFormat(w http.ResponseWriter, r *http.Request) {
var schedule db.Schedule
if !helpers.Bind(w, r, &schedule) {
return
}

_ = validateCronFormat(schedule.CronFormat, w)
}

// AddSchedule adds a template to the database
func AddSchedule(w http.ResponseWriter, r *http.Request) {
Expand All @@ -71,16 +90,12 @@ func AddSchedule(w http.ResponseWriter, r *http.Request) {
return
}

err := schedules.ValidateCronFormat(schedule.CronFormat)
if err != nil {
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
"error": "Cron: " + err.Error(),
})
if !validateCronFormat(schedule.CronFormat, w) {
return
}

schedule.ProjectID = project.ID
schedule, err = helpers.Store(r).CreateSchedule(schedule)
schedule, err := helpers.Store(r).CreateSchedule(schedule)
if err != nil {
helpers.WriteError(w, err)
return
Expand Down Expand Up @@ -130,15 +145,11 @@ func UpdateSchedule(w http.ResponseWriter, r *http.Request) {
return
}

err := schedules.ValidateCronFormat(schedule.CronFormat)
if err != nil {
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
"error": "Cron: " + err.Error(),
})
if !validateCronFormat(schedule.CronFormat, w) {
return
}

err = helpers.Store(r).UpdateSchedule(schedule)
err := helpers.Store(r).UpdateSchedule(schedule)
if err != nil {
helpers.WriteError(w, err)
return
Expand Down
1 change: 1 addition & 0 deletions api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ func Route() *mux.Router {
projectUserAPI.Path("/templates").HandlerFunc(projects.AddTemplate).Methods("POST")

projectUserAPI.Path("/schedules").HandlerFunc(projects.AddSchedule).Methods("POST")
projectUserAPI.Path("/schedules/validate").HandlerFunc(projects.ValidateScheduleCronFormat).Methods("POST")

projectAdminAPI := authenticatedAPI.Path("/project/{project_id}").Subrouter()
projectAdminAPI.Use(projects.ProjectMiddleware, projects.MustBeAdmin)
Expand Down
7 changes: 7 additions & 0 deletions web2/src/components/ItemFormBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export default {
methods: {
async reset() {
this.item = null;
this.formError = null;
if (this.$refs.form) {
this.$refs.form.resetValidation();
}
Expand All @@ -79,6 +80,10 @@ export default {
throw new Error('Not implemented'); // must me implemented in template
},

beforeSave() {

},

afterSave() {

},
Expand Down Expand Up @@ -139,6 +144,8 @@ export default {
let item;

try {
await this.beforeSave();

item = (await axios({
method: this.isNew ? 'post' : 'put',
url: this.isNew
Expand Down
15 changes: 15 additions & 0 deletions web2/src/components/TemplateForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,21 @@ export default {
return `/api/project/${this.projectId}/templates/${this.itemId}`;
},
async beforeSave() {
if (this.cronFormat === '') {
return;
}
await axios({
method: 'post',
url: `/api/project/${this.projectId}/schedules/validate`,
responseType: 'json',
data: {
cron_format: this.cronFormat,
},
});
},
async afterSave(newItem) {
if (newItem || this.schedules.length === 0) {
if (this.cronFormat != null && this.cronFormat !== '') {
Expand Down

0 comments on commit 28cea35

Please sign in to comment.