File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11(import ./helper :as helper )
2+ (import uri )
23
34(defn- max-length? [len val ]
45 (> (length val ) len ))
2324 (not (empty? result )))))
2425
2526
27+ (defn- uri? [val ]
28+ (when (string? val )
29+ (->> (uri/parse val )
30+ (helper/contains? :path ))))
31+
32+
2633(defn- invalid-keys [ks dict pred ]
2734 (filter |(pred (get dict $ ))
2835 ks ))
4956 :min-length min-length
5057 :max-length max-length
5158 :email email
52- :matches matches } validator
59+ :matches matches
60+ :uri uri } validator
5361 msg (cond
5462 (true? required ) " is required"
5563 (number? min-length ) (string " needs to be more than " min-length " characters" )
5664 (number? max-length ) (string " needs to be less than " max-length " characters" )
5765 (not (nil? email )) " needs to be an email"
5866 (not (nil? matches )) (string " needs to match " (string/format " %q" matches ))
67+ (not (nil? uri )) (string " needs to be a valid uri " (string/format " %q" uri ))
5968 :else " " )
6069 predicate (cond
6170 (true? required ) blank?
6271 (number? min-length ) (partial min-length? min-length )
6372 (number? max-length ) (partial max-length? max-length )
6473 (not (nil? email )) (comp not email? )
6574 (not (nil? matches )) (partial (comp not matches-peg? ) matches )
75+ (not (nil? uri )) (comp not uri? )
6676 :else identity )]
6777 (let [invalid-ks (invalid-keys ks body predicate )]
6878 (if (empty? invalid-ks )
Original file line number Diff line number Diff line change 5757 (helper/rescue )
5858 (first )
5959 (freeze ))
60- {:name " name can't be blank" }))))
60+ {:name " name can't be blank" })))
61+
62+ (let [test-params (params
63+ (validates :website :uri true ))]
64+ (test " uri validator"
65+ (= (-> (test-params {:body {:website " example.com" }})
66+ (protect )
67+ (last ))))))
You can’t perform that action at this time.
0 commit comments