Skip to content

Commit

Permalink
Support more comprehensive checks when adding a task
Browse files Browse the repository at this point in the history
  • Loading branch information
djmitche committed Oct 13, 2024
1 parent 64f73b4 commit 463e426
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/TDB2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ void TDB2::open_replica(const std::string& location, bool create_if_missing) {
////////////////////////////////////////////////////////////////////////////////
// Add the new task to the replica.
void TDB2::add(Task& task) {
// Validate a task for addition. This is stricter than `task.validate`, as any
// inconsistency is probably user error.
task.validate_add();

// Ensure the task is consistent, and provide defaults if necessary.
// bool argument to validate() is "applyDefault", to apply default values for
// properties not otherwise given.
Expand Down
18 changes: 16 additions & 2 deletions src/Task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1408,6 +1408,21 @@ void Task::substitute(const std::string& from, const std::string& to, const std:
}
#endif

////////////////////////////////////////////////////////////////////////////////
// Validate a task for addition, raising user-visible errors for inconsistent or
// incorrect inputs. This is called before `Task::validate`.
void Task::validate_add() {
// There is no fixing a missing description.
if (!has("description"))
throw std::string("A task must have a description.");
else if (get("description") == "")
throw std::string("Cannot add a task that is blank.");

// Cannot have an old-style recur frequency with no due date - when would it recur?
if (has("recur") && (!has("due") || get("due") == ""))
throw std::string("A recurring task must also have a 'due' date.");
}

////////////////////////////////////////////////////////////////////////////////
// The purpose of Task::validate is three-fold:
// 1) To provide missing attributes where possible
Expand All @@ -1431,8 +1446,7 @@ void Task::validate(bool applyDefault /* = true */) {
Lexer::Type type;
// `uuid` is not a property in the TaskChampion model, so an invalid UUID is
// actually an error.
if (!lex.isUUID(token, type, true))
throw format("Not a valid UUID '{1}'.", uid);
if (!lex.isUUID(token, type, true)) throw format("Not a valid UUID '{1}'.", uid);
} else
set("uuid", uuid());

Expand Down
1 change: 1 addition & 0 deletions src/Task.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ class Task {
void substitute(const std::string&, const std::string&, const std::string&);
#endif

void validate_add();
void validate(bool applyDefault = true);

float urgency_c() const;
Expand Down

0 comments on commit 463e426

Please sign in to comment.