Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
nmunro31889yThat 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!
-
NAlignak4149yMay ba a dumb question but is there a way to do validity testing without a pyramid if doom?
-
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. -
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.
-
brim4brim719y@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
-
brim4brim719y@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 -
brim4brim719y@brim4brim by populated i mean the message object is populated. it can have two properties, hasFailedValidation and messageText.
-
tuhajuhan29yElse 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;
}
}
``` -
brim4brim719yThe 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.
sums it up!✌💨
undefined