43

sums it up!✌💨

Comments
  • 0
    That looks a little like PHP, can't tell due to font size, but if it is, turns out js isn't the only language with the pyramid of doom!
  • 0
    May ba a dumb question but is there a way to do validity testing without a pyramid if doom?
  • 0
    A function containing if statements which return false at the first error.
  • 0
    But then how do you get the individual error messages?
  • 0
    Then you make an array at the start, pass over a collection of if statements, the failed ones populating the array and return that.
    Empty array = valid.
  • 0
    Well, there are some frameworks that support easier validation like Laravel for PHP and Play for Java, but if you don't use them, this will probably be your only option in every language. multiple errors could of course be combined by using arrays.
  • 0
    @NAlignak depending on language, write a validation interface, write a class for each check and build a list of the ones you need for each case. loop the list passing your data for verification
  • 0
    @NAlignak return a message instead of boolean. put them in a list if populated.

    if your list is bigger than zero at the end then something failed validation
  • 0
    @brim4brim by populated i mean the message object is populated. it can have two properties, hasFailedValidation and messageText.
  • 0
    Scratch my comment, just else if's
  • 2
    Else if's and depth>3 is bad to use and look at. Validation interface with classes is useful, but if you want something quick and clean, why not create a validity map with errors? e.g. our check is input is > 3.
    ```
    [
    function($input){ return $input > 3 } => 'It should be at least three!',
    // other similar checks follow.
    ]
    ```
    then loop over them
    ```
    foreach ($validators as $f => $e) {
    if($f($input) == false){
    throw $e;
    }
    }
    ```
  • 0
    The problem I see with a map from experience is it is hard to extend if you want to add classification to the error messages.

    Once you build your validation using maps it can become hard to change it later.
Add Comment