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.
- Validator 1: Checks whether user exists
- Validator 2: Checks whether requested order exists in the user's order history
- Validator 3: Checks whether that order has a cancel option
Each validator in this list will throw an error if the previous condition is not met. I'm currently using this pattern:
function validator1() { ... }
function validator2() { validator1(); ... }
function validator3() { validator2(); ... }
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.
I also considered doing this:
function validator1() { ... }
function validator2() { ... }
function validator3() { ... }
function someTask() { validator1(); validator2(); validator3(); ... }
But this is unsafe because there is nothing stopping me from running validator3() and throwing an error if condition 2 hasn't been met.
Is there a way to do this that is both safe and explicit?
Aucun commentaire:
Enregistrer un commentaire