Skip to content
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

Restrict extra-keys schema in the map spec #415

Merged
merged 1 commit into from
Jun 13, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions src/cljx/schema/core.cljx
Original file line number Diff line number Diff line change
Expand Up @@ -804,22 +804,26 @@
(mapv explain-kspec))]
(macros/assert! (empty? duplicate-keys)
"Schema has multiple variants of the same explicit key: %s" duplicate-keys))
(concat
(for [[k v] (dissoc this extra-keys-schema)]
(let [rk (explicit-schema-key k)
required? (required-key? k)]
(collection/one-element
required? (map-entry (eq rk) v)
(clojure.core/fn [item-fn m]
(let [e (find m rk)]
(cond e (item-fn e)
required? (item-fn (utils/error [rk 'missing-required-key])))
(if e
(dissoc #+clj (if (instance? clojure.lang.PersistentStructMap m) (into {} m) m) #+cljs m
rk)
m))))))
(when extra-keys-schema
[(collection/all-elements (apply map-entry (find this extra-keys-schema)))]))))
(let [without-extra-keys-schema (dissoc this extra-keys-schema)]
(concat
(for [[k v] without-extra-keys-schema]
(let [rk (explicit-schema-key k)
required? (required-key? k)]
(collection/one-element
required? (map-entry (eq rk) v)
(clojure.core/fn [item-fn m]
(let [e (find m rk)]
(cond e (item-fn e)
required? (item-fn (utils/error [rk 'missing-required-key])))
(if e
(dissoc #+clj (if (instance? clojure.lang.PersistentStructMap m) (into {} m) m) #+cljs m
rk)
m))))))
(when extra-keys-schema
(let [specific-keys (set (map explicit-schema-key (keys without-extra-keys-schema)))
[ks vs] (find this extra-keys-schema)
restricted-ks (constrained ks #(not (contains? specific-keys %)))]
[(collection/all-elements (map-entry restricted-ks vs))]))))))

(defn- map-error []
(clojure.core/fn [_ elts extra]
Expand Down