I have a budget entity in my domain which can be closed by the user if the budget is open. This means if the budget opens today and closes in seven days, in between this time I can close it, not before (I'd have to delete it), not after (can't do any action).

This time constraint raised a doubt: should I add a validation in my domain code for that, like the one below, or should the validation be in the controller that will access a service which will access the domain?

class Budget {

  public close(): void {
    if (this.isOnPeriod()) {
      this.closed = true

  private isOnPeriod() {
    if (new Date() < this.closing_date) {
      throw new BudgetIsClosedError()
    if (this.opening_date > new Date()) {
      throw new BudgetIsNotYetOpen()

    return true


I don't know how to explain it, but it this seem like this is business logic, although it also seems like this case (of not being on the period) will never even arrive to the domain because I am going to have validation for the data that arrives in the controller (but have in mind I don't know all the business rules and requirements yet).

  • 1
    "because I am going to have validation for the data that arrives in the controller" - why don't you make isOnPeriod public and allow to reuse it for the validation? BTW, don't use new Date() directly in this class, this makes it untestable, and calling it twice makes it also unreliable. Instead, I would pass a helper object (i.e. "calendar") into the class at construction time.
    – Doc Brown
    Nov 8, 2023 at 5:56
  • I can make it public, but then I should have the validation in both places: the domain and the controller? Also, I didn't get why I shouldn't use new Date(), if you can clarify, please... Nov 8, 2023 at 11:10
  • No, when you keep it private, you need to implement the same validation in two places. When you make it public, you can reuse the logic (maybe it is called twice, but that is probably not an issue, and since the call may depend on the point in time, it could be necessary). When you hardcode "new Date()" directly into the business logic, the outcome of a unit tests might depend on when you run the test, today, tomorrow or next week.
    – Doc Brown
    Nov 8, 2023 at 11:19
  • @DocBrown About the date ok, got it. But about the validation method, you're saying I should have the method defined inside the domain but called outside it or that it should be called both outside (controller) and in the domain (e.g. the code above)? Nov 8, 2023 at 12:57
  • I don't know where you need to call it and if you need to call it once or twice or three times - if you don't know your requirements, how do you expect it from an outsider like me? But from what you wrote, it appeared to me you had a requirement of making the same validations in different places, which makes me wondering why you don't implement in one place and reuse it at the other.
    – Doc Brown
    Nov 8, 2023 at 16:55

1 Answer 1


Validating data and logic outside the domain is a crucial step in ensuring data integrity and security. By checking information against predefined rules and standards, errors, anomalies, or malicious inputs can be identified and prevented. This practice enhances system reliability, reduces vulnerabilities, and safeguards the integrity of data and processes, a vital component in software development and cybersecurity.

  • 2
    This is all correct but does not answer the question.
    – Doc Brown
    Nov 8, 2023 at 5:57
  • I do get that, but my problem is more about where to place it. Nov 8, 2023 at 11:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.