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

defphrase not matching set parameter #29

Open
andywokr opened this issue Feb 7, 2019 · 1 comment
Open

defphrase not matching set parameter #29

andywokr opened this issue Feb 7, 2019 · 1 comment

Comments

@andywokr
Copy link

andywokr commented Feb 7, 2019

This library was exactly what I needed. I am having trouble with the defphrase for the following predicate however.

(defn contains-only?
  [m ks]
  (-> m keys set (set/difference ks) empty?))

Put together the following code to show the problem.

(ns phraser-test
  (:require [phrase.alpha :refer [defphraser] :as phraser]
            [clojure.set :as set]
            [clojure.spec.alpha :as s]))

(defn contains-only?
  [m ks]
  (-> m keys set (set/difference ks) empty?))

(defphraser #(contains-only? % ks)
  [_ _ ks]
  (str ks " are the only allowed keys."))

(s/def ::person #(contains-only? % #{:name :age :gender}))

(defn test []
  (phraser/phrase-first {} ::person {:name "dave" :age 42 :gender :male :hair :brown}))

Despite several attempts I cannot get the phraser dispatching to match. Any help would be appreciated.

@blue0513
Copy link

blue0513 commented Aug 9, 2020

I faced a similar problem.
When I try to phrase with Set parameter, I couldn't show self-defined error message

;; Need :a or :b 
(s/def ::rule #(some #{:a :b} (keys %)))

;; When check {:x 1}, of-course it raises error
(s/explain-data ::rule {:x 1})

;; When phrase
(p/phrase-first {} ::rule {:x 1})

;; It CAN be invoked and show "errro 1"
(p/defphraser #(some #{:a :b} (keys %)) [_ _] "error 1")

;; It CAN NOT be invoked
(p/defphraser #(some my-hash (keys %)) [_ _] "error 2")

;; It CAN NOT be invoked
(p/defphraser #(some my-hash (keys %))  [_ _ my-hash]  "error 3")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants