Skip to content

Commit

Permalink
Refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
english committed Oct 3, 2015
1 parent 9411e6a commit ef24ceb
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 23 deletions.
2 changes: 1 addition & 1 deletion resources/public/css/base.css
Original file line number Diff line number Diff line change
Expand Up @@ -332,5 +332,5 @@ code {
}

.error {
border: 1px solid #ff5555;
color: #ff5555;
}
28 changes: 21 additions & 7 deletions src/cljs/gc_api_browser/json_schema.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,27 @@
false)))

(defn error->map [error]
{:message (.-message error)})
{
:message (.-message error)
:schema-path (.-schemaPath error)
:data-path (.-dataPath error)})

(defn json-error [string]
(try
(js/JSON.parse string)
(catch js/Object e
{:valid false
:errors [{:message (str (.-name e) ": " (.-message e))
:schema-path ""
:data-path ""}]})))

(defn validate-against-schema [schema resource action request-string]
(let [action-schema (:schema (schema->action-node schema resource action))
body-object (-> request-string gjson/parse gobject/getValues first)
result (.validateMultiple js/tv4 body-object (clj->js action-schema) false true)]
(update (js->clj result :keywordize-keys true) :errors #(map error->map %))))

(defn validate-request [schema resource action request-string]
(if (valid-json? request-string)
(let [action-schema (:schema (schema->action-node schema resource action))
body-object (-> request-string gjson/parse gobject/getValues first)
result (.validateMultiple js/tv4 body-object (clj->js action-schema) false true)]
(update (js->clj result :keywordize-keys true) :errors #(map error->map %)))
{:valid false
:errors [{:message "invalid json"}]}))
(validate-against-schema schema resource action request-string)
(json-error request-string)))
33 changes: 18 additions & 15 deletions src/cljs/gc_api_browser/tabbed_request.cljs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns gc-api-browser.tabbed-request
(:require [om.core :as om :include-macros true]
[om.dom :as dom :include-macros true]
[clojure.string :as str]
[goog.json :as gjson]
[gc-api-browser.utils :refer [log]]
[gc-api-browser.json-schema :as json-schema]
Expand All @@ -24,19 +25,10 @@
(let [html (.-innerHTML (om/get-node owner))]
(om/transact! cursor :body (fn [_] html))))}))))

(defn valid-json? [string]
(try
(gjson/parse string)
true
(catch :default e
false)))

(defn validate-request [cursor]
(let [request-string (get-in cursor [:request :body])]
(json-schema/validate-request (:schema cursor)
(:selected-resource cursor)
(:selected-action cursor)
request-string)))
(let [{:keys [schema selected-resource selected-action request]} cursor
request-string (:body request)]
(json-schema/validate-request schema selected-resource selected-action request-string)))

(defn edit-body [cursor]
(let [validation-result (validate-request cursor)
Expand All @@ -46,9 +38,20 @@
(dom/pre #js {:className class-name}
(om/build content-editable (:request cursor)))
(when (seq (:errors validation-result))
(dom/ul nil
(for [error (:errors validation-result)]
(dom/li nil (:message error))))))))
(dom/div
nil
"Errors:"
(dom/ul nil
(for [error (:errors validation-result)]
(dom/li
nil
(dom/ul
nil
(dom/li nil (str "message: " (:message error)))
(when-not (str/blank? (:data-path error))
(dom/li nil (str "data path: " (:data-path error))))
(when-not (str/blank? (:schema-path error))
(dom/li nil (str "schema path: " (:schema-path error)))))))))))))

(defn get? [request]
(= "GET" (:method request)))
Expand Down

0 comments on commit ef24ceb

Please sign in to comment.