thi-ng / validate

Spec based validation & correction for nested data structures, wildcard support, no macros

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Wildcard does not work with sets

martinklepsch opened this issue · comments

(v/validate
 {:x #{:a :b :c}}
 {:x {:* (v/keyword)}})
;; returns
[{:x #{:c :b :a}} 
 {:x {0 ("must be a keyword"), 1 ("must be a keyword"), 2 ("must be a keyword")}}]

I found this unexpected but given the current implementation it makes sense.

I guess properly reading the README could have saved me that surprise:

This library can be used to validate and/or conform a nested associative collection (map or vector) via given validation specs.

Instead of wildcards you could define a new validator, smth like this:

(defn all* [pred msg] (v/validator (fn [_ v] (every? pred (seq v))) msg))

(def all-keywords (all* keyword? "coll can only contain keywords"))

(v/validate
  {:x #{:a :b :c}}
  {:x [(v/required) (all-keywords)]})
;; [{:x #{:c :b :a}} nil]

(v/validate
  {:x #{"a" "b" "c"}}
  {:x [(v/required) (all-keywords)]})
;; [{:x #{"a" "b" "c"}} {:x ("coll can only contain keywords")}]

I will soon cut a new release (w/o CLJX) and will add some more things like this... unless someone beats me to it :)

Nice, that makes sense now that I have it in front of me :)