Skip to content

Commit

Permalink
Add annotations as malli properties
Browse files Browse the repository at this point in the history
  • Loading branch information
hkupty committed Jan 6, 2021
1 parent 404e664 commit fc5059b
Showing 1 changed file with 34 additions and 27 deletions.
61 changes: 34 additions & 27 deletions src/malli/json_schema/parse.cljc
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
(ns malli.json-schema.parse
(:require [malli.core :as m]
[malli.util :as mu]
[clojure.set :as set]
[clojure.string :as str]))

(def annotations #{:title :description :default :examples})

(defn annotations->properties [js-schema]
(-> js-schema
(select-keys annotations)
(set/rename-keys {:examples :json-schema/examples})))

;; Utility Functions
(defn- map-values
([-fn] (map (fn [[k v]] [k (-fn v)])))
Expand All @@ -17,41 +25,40 @@

(defn schema->malli [js-schema]
(let [-keys (set (keys js-schema))]
(cond
(-keys :type) (type->malli js-schema)
(mu/update-properties
(cond
(-keys :type) (type->malli js-schema)

(-keys :enum) (into [:enum]
(:enum js-schema))
(-keys :enum) (into [:enum]
(:enum js-schema))

(-keys :const) [:enum (:const js-schema)]
(-keys :const) [:enum (:const js-schema)]

;; Aggregates
(-keys :oneOf)
(into
;; TODO Figure out how to make it exclusively select o schema
[:or]
(map schema->malli)
(:oneOf js-schema))
;; Aggregates
(-keys :oneOf) (into
;; TODO Figure out how to make it exclusively select o schema
[:or]
(map schema->malli)
(:oneOf js-schema))

(-keys :anyOf)
(into
[:or]
(map schema->malli)
(:anyOf js-schema))
(-keys :anyOf) (into
[:or]
(map schema->malli)
(:anyOf js-schema))

(-keys :allOf)
(into
[:and]
(map schema->malli)
(:allOf js-schema))
(-keys :allOf) (into
[:and]
(map schema->malli)
(:allOf js-schema))

(-keys :not) [:not (schema->malli (:not js-schema))]
(-keys :not) [:not (schema->malli (:not js-schema))]

(-keys :$ref) ($ref (:$ref js-schema))
(-keys :$ref) ($ref (:$ref js-schema))

:else
(throw (ex-info "Not supported" {:json-schema js-schema
:reason ::schema-type})))))
:else (throw (ex-info "Not supported" {:json-schema js-schema
:reason ::schema-type})))
merge
(annotations->properties js-schema))))

(defn properties->malli [{:keys [required]} [k v]]
(cond-> [k]
Expand Down

0 comments on commit fc5059b

Please sign in to comment.