From f4c28634c95b7226a4e3bfad2e90ce67c6e8921e Mon Sep 17 00:00:00 2001 From: Ambrose Bonnaire-Sergeant Date: Tue, 26 Nov 2024 18:53:08 -0600 Subject: [PATCH] readme --- README.md | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 1d592367b..cd9d685ab 100644 --- a/README.md +++ b/README.md @@ -836,19 +836,28 @@ The first argument to `:error/fn` is a map with keys: - `:schema`, the schema to explain - `:value` (optional), the value to explain - `:negated` (optional), a function returning the explanation of `(m/explain [:not schema] value)`. - If provided, then this call explains `(m/explain [:not schema] value)`. - Rarely needed but useful in some cases. - Note in this scenario, `(m/validate schema value)` is true and - the resulting error message will be negated by the `:error/fn` caller in the same way as `:error/message` - in service of a final explanation of `(m/explain [:not schema] value)`: + If provided, then we are explaining the failure of negating this schema via `(m/explain [:not schema] value)`. + Note in this scenario, `(m/validate schema value)` is true. + If returning a string, + the resulting error message will be negated by the `:error/fn` caller in the same way as `:error/message`. + Returning `(negated string)` disables this behavior and `string` is used as the negated error message. ```clojure -;; e.g., +;; automatic negation (me/humanize (m/explain [:not [:fn {:error/fn {:en (fn [_ _] "should not be a multiple of 3")}} #(not= 0 (mod % 3))]] 1)) ; => ["should be a multiple of 3"] + +;; manual negation +(me/humanize + (m/explain [:not [:fn {:error/fn {:en (fn [{:keys [negated]} _] + (if negated + (negated "should not avoid being a multiple of 3") + "should not be a multiple of 3"))}} + #(not= 0 (mod % 3))]] 1)) +; => ["should not avoid being a multiple of 3"] ``` Here are some basic examples of `:error/message` and `:error/fn`: