Skip to content

Commit 9b7bd7d

Browse files
committed
eliminate copy/paste in validators
Change-Id: I187d90fc702dc4924f5f971d0a8f538b09a230e6 Reviewed-on: http://review.couchbase.org/c/ns_server/+/160745 Tested-by: Artem Stemkovski <[email protected]> Reviewed-by: Abhijeeth Nuthan <[email protected]>
1 parent 70941ba commit 9b7bd7d

File tree

1 file changed

+11
-17
lines changed

1 file changed

+11
-17
lines changed

src/validator.erl

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -187,30 +187,24 @@ with_decoded_object({KVList}, Validators) ->
187187
with_decoded_object(_, _) ->
188188
#state{errors = [{<<"_">>, <<"Unexpected Json">>}]}.
189189

190+
validate_decoded_object(DecodedObject, Validators) ->
191+
St = with_decoded_object(DecodedObject, Validators),
192+
case St#state.errors of
193+
[] ->
194+
{value, prepare_params(St)};
195+
_ ->
196+
{error, {json, jsonify_errors(St#state.errors)}}
197+
end.
198+
190199
decoded_json(Name, Validators, State) ->
191-
validate(
192-
fun (DecodedObject) ->
193-
St = with_decoded_object(DecodedObject, Validators),
194-
case St#state.errors of
195-
[] ->
196-
{value, prepare_params(St)};
197-
_ ->
198-
{error, {json, jsonify_errors(St#state.errors)}}
199-
end
200-
end, Name, State).
200+
validate(validate_decoded_object(_, Validators), Name, State).
201201

202202
json(Name, Validators, State) ->
203203
validate(
204204
fun (BinJson) ->
205205
try ejson:decode(BinJson) of
206206
Object ->
207-
St = with_decoded_object(Object, Validators),
208-
case St#state.errors of
209-
[] ->
210-
{value, prepare_params(St)};
211-
_ ->
212-
{error, {json, jsonify_errors(St#state.errors)}}
213-
end
207+
validate_decoded_object(Object, Validators)
214208
catch _:_ ->
215209
{error, {json, {[{<<"_">>, <<"Invalid Json">>}]}}}
216210
end

0 commit comments

Comments
 (0)