title: Design Patterns for dependent sequential validation
body: <p>I have three validators each sequentially dependent on the previous. I might use all three or only the first two or only the first one. E.g.</p>
  <ol>
  <li>Validator 1: Checks whether user exists</li>
  <li>Validator 2: Checks whether requested order exists in the user's order history</li>
  <li>Validator 3: Checks whether that order has a cancel option</li>
  </ol>
  <p>Each validator in this list will throw an error if the previous condition is not met. I'm currently using this pattern:</p>
  <pre><code>function validator1() { ... }
  
  function validator2() { validator1(); ... }
  
  function validator3() { validator2(); ... }
  </code></pre>
  <p>But this feels like an anti-pattern to me because all of the prerequisite validation is done implicitly so when I run validtor2(), it is not clear that validator1() is also being run.</p>
  <p>I also considered doing this:</p>
  <pre><code>function validator1() { ... }
  
  function validator2() { ... }
  
  function validator3() { ... }
  
  function someTask() { validator1(); validator2(); validator3(); ... }
  </code></pre>
  <p>But this is unsafe because there is nothing stopping me from running validator3() and throwing an error if condition 2 hasn't been met.</p>
  <p>Is there a way to do this that is both safe and explicit?</p>
  <p><strong>Edit</strong></p>
  <p>This is a great reference for the design pattern mentioned in the accepted answer: <a href="https://refactoring.guru/design-patterns/chain-of-responsibility" rel="nofollow noreferrer">https://refactoring.guru/design-patterns/chain-of-responsibility</a></p>