Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CAPI-31 Add enum validation and some minor fixes #4

Merged
merged 1 commit into from
Aug 12, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ request_param_info('{{operationId}}', {{^isBodyParam}}'{{baseName}}'{{/isBodyPar
{type, 'boolean'}, {{/isBoolean}}{{#isDate}}
{type, 'date'}, {{/isDate}}{{#isDateTime}}
{type, 'datetime'}, {{/isDateTime}}{{#isEnum}}
{enum, [{{#allowableValues}}{{#values}}{{.}}{{/values}}{{/allowableValues}}] },{{/isEnum}}{{#maximum}}
{enum, [{{#allowableValues}}{{#values}}'{{.}}'{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}] },{{/isEnum}}{{#maximum}}
{max, {{maximum}} }, {{/maximum}}{{#exclusiveMaximum}}
{exclusive_max, {{exclusiveMaximum}} }, {{/exclusiveMaximum}}{{#minimum}}
{min, {{minimum}} }, {{/minimum}}{{#exclusiveMinimum}}
Expand Down Expand Up @@ -117,8 +117,8 @@ validate( Rule = {type, 'integer'}, Name, Value, _ValidatorState) ->
try
{ok, {{packageName}}_utils:to_int(Value)}
catch
_:_ ->
validation_error(Rule, Name)
_:Error ->
validation_error(Rule, {Name, Error})
end;

validate( Rule = {type, 'float'}, Name, Value, _ValidatorState) ->
Expand All @@ -140,9 +140,14 @@ validate( _Rule = {type, 'boolean'}, _Name, Value, _ValidatorState) when is_bool

validate( Rule = {type, 'boolean'}, Name, Value, _ValidatorState) ->
V = binary_to_lower(Value),
case binary_to_existing_atom(V, utf8) of
B when is_boolean(B) -> {ok, B};
_ -> validation_error(Rule, Name)
try
case binary_to_existing_atom(V, utf8) of
B when is_boolean(B) -> {ok, B};
_ -> validation_error(Rule, Name)
end
catch
error:badarg ->
validation_error(Rule, Name)
end;

validate( Rule = {type, 'date'}, Name, Value, _ValidatorState) ->
Expand All @@ -158,9 +163,15 @@ validate( Rule = {type, 'datetime'}, Name, Value, _ValidatorState) ->
end;

validate( Rule = {enum, Values}, Name, Value, _ValidatorState) ->
case lists:member(Value, Values) of
true -> ok;
false -> validation_error(Rule, Name)
try
FormattedValue = erlang:binary_to_existing_atom(Value, utf8),
case lists:member(FormattedValue, Values) of
true -> {ok, FormattedValue};
false -> validation_error(Rule, Name)
end
catch
error:badarg ->
validation_error(Rule, Name)
end;

validate( Rule = {max, Max}, Name, Value, _ValidatorState) ->
Expand Down