/modules/mod_base/validators/validator_base_format.erl

https://code.google.com/p/zotonic/ · Erlang · 78 lines · 51 code · 8 blank · 19 comment · 0 complexity · 58a06f8e3a6f497d33e77cc1debad9f5 MD5 · raw file

  1. %% @author Marc Worrell <marc@worrell.nl>
  2. %% @copyright 2009 Marc Worrell
  3. %% @doc Validator for checking if an input value matches a regular expression
  4. %% Copyright 2009 Marc Worrell
  5. %%
  6. %% Licensed under the Apache License, Version 2.0 (the "License");
  7. %% you may not use this file except in compliance with the License.
  8. %% You may obtain a copy of the License at
  9. %%
  10. %% http://www.apache.org/licenses/LICENSE-2.0
  11. %%
  12. %% Unless required by applicable law or agreed to in writing, software
  13. %% distributed under the License is distributed on an "AS IS" BASIS,
  14. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. %% See the License for the specific language governing permissions and
  16. %% limitations under the License.
  17. -module(validator_base_format).
  18. -include("zotonic.hrl").
  19. -export([render_validator/5, validate/5]).
  20. render_validator(format, TriggerId, _TargetId, Args, Context) ->
  21. Pattern = proplists:get_value(pattern, Args),
  22. Negate = proplists:get_value(negate, Args, false),
  23. JsObject = z_utils:js_object(z_validation:rename_args(Args)),
  24. Script = [<<"z_add_validator(\"">>,TriggerId,<<"\", \"format\", ">>, JsObject, <<");\n">>],
  25. {[z_utils:is_true(Negate),Pattern], Script, Context}.
  26. %% @spec validate(Type, TriggerId, Value, Args, Context) -> {ok,AcceptedValue} | {error,Id,Error}
  27. %% Error = invalid | novalue | {script, Script}
  28. validate(format, _Id, [], _, Context) ->
  29. {{ok, []}, Context};
  30. validate(format, Id, Value, [Negate,Pattern], Context) ->
  31. {Re,Options} = pattern_to_re(Pattern),
  32. Ok = not Negate,
  33. Match = case re:run(Value, Re, Options) of
  34. {match, _} -> true;
  35. nomatch -> false
  36. end,
  37. case Match of
  38. Ok -> {{ok, Value}, Context};
  39. _ -> {{error, Id, invalid}, Context}
  40. end.
  41. %% @doc Translate a regular expression in javascript format to erlang re module format
  42. pattern_to_re([$/|Rest]=Pattern) ->
  43. case string:rchr(Rest, $/) of
  44. 0 ->
  45. {Pattern,[]};
  46. N ->
  47. {Re, [$/|Options]} = lists:split(N-1,Rest),
  48. ReOptions = [anycrlf|trans_options(Options, [])],
  49. {Re, ReOptions}
  50. end;
  51. pattern_to_re(Pattern) ->
  52. {Pattern, []}.
  53. trans_options([], Acc) ->
  54. Acc;
  55. trans_options([$i|T], Acc) ->
  56. trans_options(T, [caseless|Acc]);
  57. trans_options([$m|T], Acc) ->
  58. trans_options(T, [multiline|Acc]);
  59. trans_options([$s|T], Acc) ->
  60. trans_options(T, [dotall|Acc]);
  61. trans_options([$x|T], Acc) ->
  62. trans_options(T, [extended|Acc]);
  63. trans_options([$A|T], Acc) ->
  64. trans_options(T, [anchored|Acc]);
  65. trans_options([$D|T], Acc) ->
  66. trans_options(T, [dollar_endonly|Acc]);
  67. trans_options([$U|T], Acc) ->
  68. trans_options(T, [ungreedy|Acc]);
  69. trans_options([_|T], Acc) ->
  70. trans_options(T, Acc).