feat: validate expand api parameter

This commit is contained in:
kolaente 2025-01-24 10:09:36 +01:00
parent 0a9f3cf41b
commit bc0c0b103f
No known key found for this signature in database
GPG Key ID: F40E70337AB24C9B
2 changed files with 20 additions and 1 deletions

View File

@ -111,11 +111,15 @@ func (err ValidationHTTPError) Error() string {
}
func InvalidFieldError(fields []string) error {
return InvalidFieldErrorWithMessage(fields, "Invalid Data")
}
func InvalidFieldErrorWithMessage(fields []string, message string) error {
return ValidationHTTPError{
HTTPError: web.HTTPError{
HTTPCode: http.StatusPreconditionFailed,
Code: ErrCodeInvalidData,
Message: "Invalid Data",
Message: message,
},
InvalidFields: fields,
}

View File

@ -64,6 +64,16 @@ type TaskCollectionExpandable string
const TaskCollectionExpandSubtasks TaskCollectionExpandable = `subtasks`
// Validate validates if the TaskCollectionExpandable value is valid.
func (t TaskCollectionExpandable) Validate() error {
switch t {
case TaskCollectionExpandSubtasks:
return nil
}
return InvalidFieldErrorWithMessage([]string{"expand"}, "Expand must be one of the following values: subtasks")
}
func validateTaskField(fieldName string) error {
switch fieldName {
case
@ -331,6 +341,11 @@ func (tf *TaskCollection) ReadAll(s *xorm.Session, a web.Auth, search string, pa
return nil, 0, 0, err
}
err = tf.Expand.Validate()
if err != nil {
return nil, 0, 0, err
}
opts.search = search
opts.page = page
opts.perPage = perPage