Created
January 31, 2014 01:39
-
-
Save spiegela/8725125 to your computer and use it in GitHub Desktop.
Complex Validation Example in Erlang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% | |
% store all necessary data to pass between validators in | |
% #validator_state struct | |
% | |
run_checks(FunList, Data) -> | |
run_checks(FunList, Data, #validator_state{}). | |
% | |
% evaluate validation chain | |
% | |
run_checks([], Data, State) -> | |
{valid, Data, State}; | |
run_checks([F | FunList], Data, State) -> | |
case ?MODULE:F(Data, State) of | |
{valid, NewData, NewState} -> | |
run_checks(FunList, NewData, NewState); | |
Error = {error, _ErrorMsg, _NewState} -> | |
% you can add to error reporting in WHICH | |
% validator it failed | |
% like | |
% {error, F, _ErrorMsg, _NewState} | |
Error | |
% for ease of programming validators I would add also | |
% shortcut return codes like | |
% valid -> run_checks(FunList, Data, State); | |
% and | |
% {error, Error} -> {error, Error, State}; | |
end. | |
And I will end up with functions like | |
% check functions | |
check_email(PropList, State) -> | |
do something and return values .... | |
check_name(PropList, State) -> | |
do something and return values .... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment