Skip to content

Commit

Permalink
match spec2 :close opt name
Browse files Browse the repository at this point in the history
  • Loading branch information
mpenet committed Dec 25, 2023
1 parent 675e7ef commit b556fdd
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ easily for instance generate open-api definitions using these.

`coax` also allows to *close* maps specced with `s/keys`.

If you call `coerce` using the option `{:closed-keys? true ...}` if a value
If you call `coerce` using the option `{:closed true ...}` if a value
corresponding to a `s/keys` spec is encountered it will effectively remove all
unknown keys from the returned value.

Expand All @@ -90,16 +90,16 @@ unknown keys from the returned value.

(s/def ::m (s/keys :req-un [::foo ::bar]))

(c/coerce ::m {:foo "f" :bar "b" :baz "z"} {:closed-keys? true}) ; baz is not on the spec
(c/coerce ::m {:foo "f" :bar "b" :baz "z"} {:closed true}) ; baz is not on the spec
-> {:foo "f" :bar "b"} ; it gets removed

;; this works in any s/keys matching spec in the data passed:
(coerce `(s/coll-of ::m) [{:foo "f" :bar "b" :baz "x"}] {:closed-keys? true})
(coerce `(s/coll-of ::m) [{:foo "f" :bar "b" :baz "x"}] {:closed true})
-> [{:foo "f" :bar "b"}]

;; also plays nice with s/merge, multi-spec & co
(coerce `(s/merge ::m (s/keys :req-un [::z]))
{:foo "f" :bar "b" :baz "x" :z "z"} {:closed-keys? true})
{:foo "f" :bar "b" :baz "x" :z "z"} {:closed true})

-> {:foo "f" :bar "b" :z "z"}
```
Expand Down Expand Up @@ -279,7 +279,7 @@ during development you might need to be aware of the existence of that
cache (ex if you defined a bugged coercer, or while doing REPL dev).

In any case you can turn off the cache by passing
`:exoscale.coax/cache? false` to the options of
`:exoscale.coax/cache false` to the options of
coerce/conform/coerce-structure, alternatively you can manually fiddle
with the cache under `exoscale.coax/coercer-cache`, for instance via
`(reset! exoscale.coax/coercer-cache {})`.
Expand Down
16 changes: 8 additions & 8 deletions src/exoscale/coax.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@
(map (juxt identity identity))
(flatten (concat req opt)))
keys-mapping (merge keys-mapping-unns keys-mapping-ns)]
(fn [x {:as opts :keys [closed-keys?]}]
(fn [x {:as opts :keys [closed]}]
(if (map? x)
(reduce-kv (fn [m k v]
(let [s-from-mapping (keys-mapping k)
s (or s-from-mapping k)]
(cond
;; if closed and not in mapping dissoc
(and closed-keys? (not s-from-mapping))
(and closed (not s-from-mapping))
(dissoc m k)
;; registered spec -> coerce
(qualified-ident? s)
Expand Down Expand Up @@ -127,13 +127,13 @@

(defn gen-coerce-merge
[[_ & spec-forms]]
(fn [x {:as opts :keys [closed-keys?]}]
(fn [x {:as opts :keys [closed]}]
(if (map? x)
(into (if closed-keys? {} x)
(into (if closed {} x)
(map (fn [spec-form]
(coerce spec-form
x
(assoc opts :closed-keys? true))))
(assoc opts :closed true))))
spec-forms)
:exoscale.coax/invalid)))

Expand Down Expand Up @@ -315,7 +315,7 @@

(defn coerce-fn
[spec opts]
(if (:exoscale.coax/cache? opts true)
(if (:exoscale.coax/cache opts true)
(cached-coerce-fn spec opts)
(coerce-fn* spec opts)))

Expand Down Expand Up @@ -434,7 +434,7 @@

(s/def ::m (s/keys :req-un [::foo ::bar]))

(coerce ::m {:foo "f" :bar "b" :baz "x"} {:closed-keys? true}) ; baz is not on the spec
(coerce ::m {:foo "f" :bar "b" :baz "x"} {:closed true}) ; baz is not on the spec

(coerce `(s/merge ::m (s/keys :req-un [::z]))
{:foo "f" :bar "b" :baz "x" :z "z"} {:closed-keys? true}) ; baz is not on the spec
{:foo "f" :bar "b" :baz "x" :z "z"} {:closed true}) ; baz is not on the spec
4 changes: 2 additions & 2 deletions test/exoscale/coax_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -380,13 +380,13 @@
(s/def ::test-closed-keys (s/keys :req [::bar ::foo]))
(is (= (sc/coerce ::test-closed-keys {::foo 1 ::bar 2 ::zzz 3})
{::foo 1 ::bar "2" ::zzz "3"}))
(is (= (sc/coerce ::test-closed-keys {::foo 1 ::bar 2 ::baz 3} {:closed-keys? true})
(is (= (sc/coerce ::test-closed-keys {::foo 1 ::bar 2 ::baz 3} {:closed true})
{::foo 1 ::bar "2"}))

(s/def ::test-closed-keys2 (s/keys :req-un [::bar ::foo]))
(is (= (sc/coerce ::test-closed-keys2 {:foo 1 :bar 2 :zzz 3})
{:foo 1 :bar "2" :zzz 3}))
(is (= (sc/coerce ::test-closed-keys2 {:foo 1 :bar 2 :baz 3} {:closed-keys? true})
(is (= (sc/coerce ::test-closed-keys2 {:foo 1 :bar 2 :baz 3} {:closed true})
{:foo 1 :bar "2"})))

(s/def ::tuple (s/tuple ::foo ::bar int?))
Expand Down

0 comments on commit b556fdd

Please sign in to comment.