[erlang-questions] Idiomatically handling multiple validation checks

qp <>
Tue Dec 6 00:16:16 CET 2016


Hi, I am new to Erlang and wrote this code for validating that the Name,
Action & Target atoms passed in to validRequest are all valid.

validRequest(valid, valid, Target) ->
  case validName(Target) of
      true -> true;
      false -> false
  end;
validRequest(valid, Action, Target) ->
  case validAction(Action) of
      true -> validRequest(valid, valid, Target);
      false -> false
  end;
validRequest(Name, Action, Target) ->
  case validName(Name) of
      true -> validRequest(valid, Action, Target);
      false -> false
  end.

I've since refactored into

validRequest(Name, Action, Target) ->
  validName(Name) and validAction(Action) and validName(Target).

I'm curious what a more idiomatic way of writing this would be? I've seen a
mix of styles using booleans for return values and tuples, with errors
listed. I don't think that's needed here but I'm just curious how you would
handle multiple validation checks, especially if they were more complicated
than this example. Thank you!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20161205/77075ede/attachment-0001.html>


More information about the erlang-questions mailing list