PageRenderTime 15ms CodeModel.GetById 1ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 1ms

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