public
Created

Erlang parameter validation

  • Download Gist
t.erl
Erlang
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
% Post/Query string parameter validation.
%
% Mark Allen, mrallen1@yahoo.com
%
% Specs [{{name, "param_name"}, {required, true}, {type, function}}, ... ]
% Input [{"name", "value}, ...] just you'd get from post or query string parameters
 
-module(t).
-export([test/0, test1/0, test2/0]).
 
find_spec(P, S) ->
case S of
{{name, P}, R, V} ->
{{name, P}, R, V};
{{name, P}, V} ->
{{name, P}, {required, false}, V};
{_, _, _} ->
skip;
{_, _} ->
skip
end.
 
validate_required(I, S) ->
InputParams = [ Name || {Name, _Value} <- I ],
R = [ case Bool of
true ->
ParamName;
false ->
false
end || {{_, ParamName}, {required, Bool}, {_, _}} <- S],
ReqParams = lists:filter(fun(A) -> A =/= false end, R),
V = [ lists:member(N, InputParams) || N <- ReqParams ],
[ case Bool of
true ->
{ok, Name};
false ->
{missingreq, Name}
end || {Name, Bool} <- lists:zip(ReqParams, V) ].
 
validate_specification(S, Acc) ->
case S of
{{name, Name}, {required, Bool}, {Type, F}} ->
Member = lists:member(Type, [int, string, atom, list, tuple]),
if
is_list(Name), is_boolean(Bool), Member == true, is_function(F) ->
{ok, Name};
true ->
{badspec, Name}
end;
{{name, Name}, {Type, F}} ->
validate_specification({{name, Name}, {required, false}, {Type, F}}, Acc);
[H|T] ->
validate_specification(T, [validate_specification(H, Acc) | Acc]);
[] ->
lists:filter(fun(A) -> is_tuple(A) end, Acc)
end.
validate_params(Input, Spec) ->
[ case lists:filter(fun(A) -> A =/= skip end, [ find_spec(Name, S) || S <- Spec ]) of
[H|_T] ->
{{name, Name}, {required, _Bool}, {Type, F}} = H,
try
{ok, Coerce} = coerce_type(Type, Value),
case F(Coerce) of
true ->
{ok, Name, Coerce};
false ->
{badparam, Name, Value}
end
catch
_:_ ->
{badparam, Name, Value}
end;
[] ->
{missingspec, Name}
end || {Name, Value} <- Input ].
 
coerce_type(Type, Value) ->
case Type of
int when is_list(Value) ->
try
V = list_to_integer(Value),
{ok, V}
catch
_:_ ->
{badparam, Value}
end;
int when is_integer(Value) ->
{ok, Value};
string when is_list(Value) ->
{ok, Value};
binary when is_list(Value) ->
try
V = list_to_binary(Value),
{ok, V}
catch
_:_ ->
{badparam, Value}
end;
binary when is_binary(Value) ->
{ok, Value};
tuple when is_list(Value) ->
try
V = list_to_tuple(Value),
{ok, V}
catch
_:_ ->
{badparam, Value}
end;
tuple when is_tuple(Value) ->
{ok, Value};
list when is_list(Value) ->
{ok, Value}
end.
 
test() ->
Spec = [
{{name, "foo"}, {required, true}, {int, fun(A) -> A > 50 end}},
{{name, "bar"}, {required, false}, {string, fun(A) -> A =:= "quux" end}},
{{name, "guz"}, {required, crap}, {string, fun(A) -> A =:= "quux" end}},
{{name, gurz}, {required, crap}, {string, fun(A) -> A =:= "quux" end}},
{{name, "baz"}, {int, fun(A) -> A =:= 0 end}},
{{name, "bak"}, {crap, fun(A) -> A =:= 0 end}},
{{name, "bez"}, {string, crap}},
{{name, "vap"}, {int, fun(A) -> A < 0 end}}
% {{name, "zod"}},
% {{name, "zux"}, {int}},
% {{name, "faz"}, {fun(_A) -> true end}}
],
validate_specification(Spec, []).
 
test1() ->
Spec = [
{{name, "foo"}, {required, true}, {int, fun(A) -> A > 50 end}},
{{name, "bar"}, {required, false}, {string, fun(A) -> A =:= "quux" end}},
{{name, "baz"}, {int, fun(A) -> A =:= 0 end}},
{{name, "vap"}, {int, fun(A) -> A < 0 end}}
],
Input = [{"foo", "abc"}, {"bar", "quux"}, {"weasel", "bat"}],
 
validate_params(Input, Spec).
 
test2() ->
Spec = [
{{name, "foo"}, {required, true}, {int, fun(A) -> A > 50 end}},
{{name, "bar"}, {required, true}, {string, fun(A) -> A =:= "quux" end}},
{{name, "baz"}, {int, fun(A) -> A =:= 0 end}},
{{name, "vap"}, {int, fun(A) -> A < 0 end}}
],
Input = [{"foo", "abc"}, {"barf", "quux"}, {"baz", "bat"}],
 
validate_required(Input, Spec).

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.