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

Robust Humanize Errors #502

Merged
merged 4 commits into from
Aug 6, 2021
Merged

Robust Humanize Errors #502

merged 4 commits into from
Aug 6, 2021

Conversation

ikitommi
Copy link
Member

@ikitommi ikitommi commented Aug 6, 2021

Simplified humanization with proper tests. Fixes #80, #428 & #499.

(deftest robust-humanize-form
  (let [f (fn [s] [:fn {:error/message s} (constantly false)])
        => ::irrelevant]
    (are [schema value _ expected]
      (is (= expected (-> (m/explain schema value) (me/humanize))))

      ;; simple cases
      :any :any => nil
      [:and :any :any] :any => nil
      [:and (f "1") :any] :any => ["1"]
      [:and (f "1") (f "1") :any] :any => ["1" "1"]
      [:and (f "1") (f "2")] {:a :map} => ["1" "2"]

      ;; accumulate into maps if error shape is already a map
      [:map [:x [:and [:map [:y :any]] seq?]]] 123 => ["invalid type"]
      [:map [:x [:and [:map [:y :any]] seq?]]] {} => {:x ["missing required key"]}
      [:map [:x [:and [:map [:y :any]] seq?]]] {:x 123} => {:x ["invalid type" "should be a seq"]}
      [:map [:x [:and [:map [:y :any]] seq? (f "kosh")]]] {:x {}} => {:x {:y ["missing required key"]
                                                                          :malli/error ["should be a seq" "kosh"]}}
      [:map [:x [:and [:map [:y :any]] seq?]]] {:x {:y 123}} => {:x ["should be a seq"]}

      ;; don't derive error form from value in case of top-level error
      [:map [:x [:and seq? [:map [:y :any]]]]] 123 => ["invalid type"]
      [:map [:x [:and seq? [:map [:y :any]]]]] {} => {:x ["missing required key"]}
      [:map [:x [:and seq? [:map [:y :any]]]]] {:x 123} => {:x ["should be a seq" "invalid type"]}
      [:map [:x [:and seq? [:map [:y :any]]]]] {:x {}} => {:x ["should be a seq"]}

      ;; tuple sizes
      [:map [:x [:tuple :int :int :int]]] {} => {:x ["missing required key"]}
      [:map [:x [:tuple :int :int :int]]] {:x []} => {:x ["invalid tuple size 0, expected 3"]}
      [:map [:x [:tuple :int :int :int]]] {:x [1 "2" 3]} => {:x [nil ["should be an integer"]]}
      [:map [:x [:tuple :int :int :int]]] {:x [1 "2" "3"]} => {:x [nil ["should be an integer"] ["should be an integer"]]}
      [:map [:x [:tuple :int [:and :int (f "fails")] :int]]] {:x [1 "2" "3"]} => {:x [nil ["should be an integer" "fails"] ["should be an integer"]]}
      [:map [:x [:tuple :int :int :int]]] {:x [1 2 3]} => nil

      ;; sequences
      [:and [:sequential :int] (f "1") (f "2")] [1 "2"] => [nil ["should be an integer"]]
      [:and [:sequential :int] (f "1") (f "2")] [1 2] => ["1" "2"])))

@ikitommi ikitommi merged commit cf588bc into master Aug 6, 2021
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

Successfully merging this pull request may close these issues.

More Robust Humanized Errors
1 participant