Skip to content
This repository has been archived by the owner on Feb 12, 2022. It is now read-only.

Commit

Permalink
Fixes #15, dealing correctly with required facet and object optional …
Browse files Browse the repository at this point in the history
…properties
  • Loading branch information
antoniogarrote committed Sep 12, 2016
1 parent 56cbcc8 commit 0353e51
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
[clojure.string]
[datatype-expansion.utils :refer [clear-node error]]
[instaparse.core :as insta]
[clojure.string :as string]
[clojure.walk :refer [stringify-keys]]))

#?(:cljs (enable-console-print!))
Expand Down Expand Up @@ -147,7 +148,15 @@
(= type "object")) (-> {:type "object"}
(process-constraints type-node)
(assoc :properties (->> (:properties type-node)
(mapv (fn [[prop-name type]] [(name prop-name) (expanded-form-inner type context)]))
(mapv (fn [[prop-name type]]
(let [prop-name (name prop-name)
prop-expanded (expanded-form-inner type context)
explicit-required (and (map? type) (:required type))
optional (string/ends-with? prop-name "?")
prop-expanded (if (and optional (not explicit-required))
(assoc prop-expanded :required false)
prop-expanded)]
[prop-name prop-expanded])))
(into {})))
clear-node)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,20 @@
:type "object",
:required true}
canonical))))

(deftest optional-property-error
(let [input {:properties {"name1?" {:type "string" :required true}
"name2?" "string"
"name3?" {:type "string" :required false}}
:additionalProperties true,
:type "object"
:required true}
expanded (expanded-form input {})
canonical (canonical-form expanded)]
(is (= {:properties {"name1?" {:type "string", :required true},
"name2?" {:type "string", :required false},
"name3?" {:type "string", :required false}},
:additionalProperties true,
:type "object",
:required true}
canonical))))

0 comments on commit 0353e51

Please sign in to comment.