-
Notifications
You must be signed in to change notification settings - Fork 212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Initial implementation of json-schema import #211
Open
hkupty
wants to merge
9
commits into
metosin:master
Choose a base branch
from
hkupty:js-schema-to-malli
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e099544
Initial implementation of json-schema import
hkupty 74f62cf
Simplify functions, rename and make code simpler
hkupty 9690651
Add support for anyOf and allOf
hkupty b3f6a9a
Rename import->parse
hkupty 0ad4ca5
Implement properties for strings, some int checks
hkupty 404e664
Fix missing `:else` clause
hkupty fc5059b
Add annotations as malli properties
hkupty 18332a6
Add min/max properties
hkupty 9f380a7
add min/max properties to object
hkupty File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
(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)]))) | ||
([-fn coll] (sequence (map-values -fn) coll))) | ||
|
||
;; Parsing | ||
(defmulti type->malli :type) | ||
|
||
(defn $ref [v] | ||
;; TODO to be improved | ||
(keyword (last (str/split v #"/")))) | ||
|
||
(defn schema->malli [js-schema] | ||
(let [-keys (set (keys js-schema))] | ||
(mu/update-properties | ||
(cond | ||
(-keys :type) (type->malli js-schema) | ||
|
||
(-keys :enum) (into [:enum] | ||
(:enum 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)) | ||
|
||
(-keys :anyOf) (into | ||
[:or] | ||
(map schema->malli) | ||
(:anyOf js-schema)) | ||
|
||
(-keys :allOf) (into | ||
[:and] | ||
(map schema->malli) | ||
(:allOf js-schema)) | ||
|
||
(-keys :not) [:not (schema->malli (:not js-schema))] | ||
|
||
(-keys :$ref) ($ref (:$ref js-schema)) | ||
|
||
:else (throw (ex-info "Not supported" {:json-schema js-schema | ||
:reason ::schema-type}))) | ||
merge | ||
(annotations->properties js-schema)))) | ||
|
||
(defn properties->malli [required [k v]] | ||
(cond-> [k] | ||
(nil? (required k)) (conj {:optional true}) | ||
true (conj (schema->malli v)))) | ||
|
||
(defn- prop-size [pred?] (fn [-map] (pred? (count (keys -map))))) | ||
(defn- min-properties [-min] (prop-size (partial <= -min))) | ||
(defn- max-properties [-max] (prop-size (partial >= -max))) | ||
|
||
(defn with-min-max-poperties-size [malli v] | ||
(let [predicates [(some->> v | ||
(:minProperties) | ||
(min-properties) | ||
(conj [:fn])) | ||
(some->> v | ||
(:maxProperties) | ||
(max-properties) | ||
(conj [:fn]))]] | ||
(cond->> malli | ||
(some some? predicates) | ||
(conj (into [:and] | ||
(filter some?) | ||
predicates))))) | ||
|
||
(defn object->malli [v] | ||
(let [required (into #{} | ||
;; TODO Should use the same fn as $ref | ||
(map keyword) | ||
(:required v)) | ||
closed? (false? (:additionalProperties v))] | ||
(m/schema (-> [:map] | ||
(cond-> closed? (conj {:closed :true})) | ||
(into | ||
(map (partial properties->malli required)) | ||
(:properties v)) | ||
(with-min-max-poperties-size v))))) | ||
|
||
(defmethod type->malli "string" [{:keys [pattern minLength maxLength enum]}] | ||
;; `format` metadata is deliberately not considered. | ||
;; String enums are stricter, so they're also implemented here. | ||
(cond | ||
pattern [:re pattern] | ||
enum [:and | ||
:string | ||
(into [:enum] enum)] | ||
:else [:string (cond-> {} | ||
minLength (assoc :min minLength) | ||
maxLength (assoc :max maxLength))])) | ||
|
||
(defmethod type->malli "integer" [{:keys [minimum maximum exclusiveMinimum exclusiveMaximum multipleOf] | ||
:or {minimum Integer/MIN_VALUE | ||
maximum Integer/MAX_VALUE}}] | ||
;; On draft 4, exclusive{Minimum,Maximum} is a boolean. | ||
;; TODO Decide on whether draft 4 will be supported | ||
;; TODO Implement exclusive{Minimum,Maximum} support | ||
;; TODO Implement multipleOf support | ||
;; TODO Wrap, when it makes sense, the values below with range checkers, i.e. [:< maximum] | ||
;; TODO extract ranges logic and reuse with number | ||
(cond | ||
(pos? minimum) pos-int? | ||
(neg? maximum) neg-int? | ||
:else int?)) | ||
|
||
(defmethod type->malli "number" [p] number?) | ||
(defmethod type->malli "boolean" [p] boolean?) | ||
(defmethod type->malli "null" [p] nil?) | ||
(defmethod type->malli "object" [p] (object->malli p)) | ||
(defmethod type->malli "array" [p] (let [items (:items p)] | ||
(cond | ||
(vector? items) (into [:tuple] | ||
(map schema->malli) | ||
items) | ||
(map? items) [:vector (schema->malli items)] | ||
:else (throw (ex-info "Not Supported" {:json-schema p | ||
:reason ::array-items}))))) | ||
|
||
(defn json-schema-document->malli [obj] | ||
[:schema {:registry (into {} | ||
(map-values schema->malli) | ||
(:definitions obj))} | ||
(schema->malli obj)]) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can do this by combining
and
,or
, andnot
.It would be nice if malli just added a xor schema though (which could do the same expansion under the hood).
@ikitommi have you considered adding
:xor
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
xor != oneOf
https://en.wikipedia.org/wiki/Exclusive_or