Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'erlang-json'

  • Loading branch information...
commit 994789adc7d2759065e70a4f34d75b3f07277f6d 2 parents 0af7b21 + ccf73a8
@roidrage roidrage authored
View
1  function-contrib-gollum/_Layout.html
@@ -107,6 +107,7 @@
<h3>Pre/Post Commit Functions</h3>
<ul>
<li><a href="/validate_json.html">Validate JSON Pre-commit (JS)</a></li>
+ <li><a href="/validate_json_erl.html">Validate JSON Pre-commit (Erlang)</a></li>
<li><a href="/pre-and-post-commits.html">More...</a></li>
</ul>
<br />
View
2  function-contrib-gollum/pre-and-post-commits.textile
@@ -11,4 +11,4 @@ For more information on using hooks in your environment please reference the [[R
h2. Contributed Pre- and Post-Commit Functions
[[Validate JSON (Pre)|validate_json]]
-
+[[Validate JSON (Pre) using Erlang|validate_json_erl]]
View
22 function-contrib-gollum/validate_json_erl.textile
@@ -0,0 +1,22 @@
+h1. Validate JSON Pre-Commit Hook (Erlang)
+
+[[Source File on GitHub|https://github.com/basho/riak_function_contrib/blob/master/hooks/pre-commit/erlang/validate_json.erl]]
+
+This function can be used in a pre-commit hook to validate that the input is a valid JSON document.
+
+```js
+-module(validate_json).
+-export([validate/1]).
+
+validate(Object) ->
+ try
+ mochijson2:decode(riak_object:get_value(Object)),
+ Object
+ catch
+ throw:invalid_utf8 ->
+ {fail, "Invalid JSON: Illegal UTF-8 character"};
+ error:Error ->
+ {fail, "Invalid JSON: " ++ binary_to_list(list_to_binary(io_lib:format("~p", [Error])))}
+ end.
+```
+
View
14 hooks/pre-commit/erlang/validate_json.erl
@@ -0,0 +1,14 @@
+-module(validate_json).
+-export([validate/1]).
+
+validate(Object) ->
+ try
+ mochijson2:decode(riak_object:get_value(Object)),
+ Object
+ catch
+ throw:invalid_utf8 ->
+ {fail, "Invalid JSON: Illegal UTF-8 character"};
+ error:Error ->
+ {fail, "Invalid JSON: " ++ binary_to_list(list_to_binary(io_lib:format("~p", [Error])))}
+ end.
+
Please sign in to comment.
Something went wrong with that request. Please try again.