Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
frenchy64 committed Dec 10, 2024
1 parent 76ff1ac commit 3b5d6b4
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions src/flanders/json_schema.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

(defn- -normalize
"normalize to string"
[k]
[k opts]
(prn "-normalize" k)
(cond
(keyword? k) (-> k symbol str)
(symbol? k) (str k)
(string? k) k
:else (throw (ex-info "Cannot normalize" {:k k}))))
:else (throw (ex-info (str "Cannot normalize at " (pr-str (::path opts))) {:k k :opts opts}))))

(defn resolve-id [id {::keys [base-id]}]
(assert (string? base-id))
Expand Down Expand Up @@ -55,11 +55,11 @@
::path ["$defs" "fingerprint"]})
)

(defn normalize-map [m]
(defn normalize-map [m opts]
(assert (map? m))
(let [seen (volatile! #{})]
(update-keys m (fn [k]
(let [n (-normalize k)]
(let [n (-normalize k (conj-path opts k))]
(when (@seen n)
(throw (ex-info "Clash while normalizing" {:m m :k k :n n})))
(vswap! seen conj n)
Expand All @@ -75,7 +75,7 @@
[v opts]
(cond
(map? v) (let [{:strs [description title example
$ref $defs $dynamicAnchor $dynamicRef $id $vocabulary $schema]} (normalize-map v)
$ref $defs $dynamicAnchor $dynamicRef $id $vocabulary $schema]} (normalize-map v opts)
;; TODO
_ (when $schema (assert (= "http://json-schema.org/draft-07/schema#" $schema) (pr-str $schema)))
_ (assert (nil? $dynamicAnchor)) ;; TODO
Expand Down Expand Up @@ -119,17 +119,26 @@
(when-not (= 1 (count conjuncts))
(throw (ex-info "Only a single allOf schema supported" {})))
(->flanders (first conjuncts) (conj-path opts "allOf" "0")))
(case (-normalize (doto (get v "type") prn))
(case (some-> (get v "type")
(-normalize (conj-path opts "type")))
"integer" (if-some [enum (seq (get v "enum"))]
(f/enum (mapv long enum))
(f/int))
"number" (if-some [enum (seq (get v "enum"))]
(f/enum (mapv num enum))
(f/num))
"boolean" (if-some [enum (not-empty (set (get v "enum")))]
(cond
(= #{true false} enum) (f/bool)
(= #{true} enum) (f/bool :equals true)
(= #{false} enum) (f/bool :equals false)
:else (throw (ex-info (str "Unsupported boolean enum: " (pr-str (get v "enum")))
{:schema v})))
(f/bool))
"string" (let [{fmt "format" :strs [enum]} v]
(assert (nil? fmt) (pr-str fmt))
(if (seq enum)
(f/enum (mapv -normalize enum))
(f/enum (into [] (map-indexed #(-normalize %2 (conj-path opts (str %)))) enum))
(f/str)))
"null" (throw (ex-info "Flanders cannot check for nil" {}))
"array" (let [{:strs [items uniqueItems]} v]
Expand All @@ -143,16 +152,16 @@
(throw (ex-info "Cannot combine properties and additionalProperties" {})))
(if properties
(f/map (mapv (fn [[k s]]
(f/entry k (->flanders s (conj-path opts (-normalize k))) :required? (contains? required k)))
(f/entry k (->flanders s (conj-path opts (-normalize k opts))) :required? (contains? required k)))
properties))

(if additionalProperties
(f/map-of {})
(assert nil (str "TODO closed map" (pr-str v))))))
nil)
(when-some [enum (seq (get v "enum"))]
(f/enum (cond-> enum
(some (some-fn ident? string?) enum) (mapv -normalize))))
(f/enum (cond->> enum
(some (some-fn ident? string?) enum) (into [] (map #(-normalize %2 (conj-path opts (str %))))))))
(unknown-schema! v opts))]
(cond-> base
;; TODO unit test
Expand Down

0 comments on commit 3b5d6b4

Please sign in to comment.