@@ -836,19 +836,28 @@ The first argument to `:error/fn` is a map with keys:
836
836
- `:schema`, the schema to explain
837
837
- `:value` (optional ), the value to explain
838
838
- `:negated` (optional ), a function returning the explanation of `(m/explain [:not schema] value)`.
839
- If provided, then this call explains `(m/explain [:not schema] value)`.
840
- Rarely needed but useful in some cases .
841
- Note in this scenario, `(m/validate schema value)` is true and
842
- the resulting error message will be negated by the `:error/fn` caller in the same way as `:error/message`
843
- in service of a final explanation of `(m/explain [ :not schema] value)`:
839
+ If provided, then we are explaining the failure of negating this schema via `(m/explain [:not schema] value)`.
840
+ Note in this scenario, `(m/validate schema value)` is true .
841
+ If returning a string,
842
+ the resulting error message will be negated by the `:error/fn` caller in the same way as `:error/message`.
843
+ Returning `(negated string)` disables this behavior and `string` is used as the negated error message.
844
844
```clojure
845
- ; ; e.g.,
845
+ ; ; automatic negation
846
846
(me/humanize
847
847
(m/explain
848
848
[:not [:fn {:error/fn {:en (fn [_ _] " should not be a multiple of 3" )}}
849
849
#(not= 0 (mod % 3 ))]]
850
850
1 ))
851
851
; => ["should be a multiple of 3"]
852
+
853
+ ; ; manual negation
854
+ (me/humanize
855
+ (m/explain [:not [:fn {:error/fn {:en (fn [{:keys [negated]} _]
856
+ (if negated
857
+ (negated " should not avoid being a multiple of 3" )
858
+ " should not be a multiple of 3" ))}}
859
+ #(not= 0 (mod % 3 ))]] 1 ))
860
+ ; => ["should not avoid being a multiple of 3"]
852
861
```
853
862
854
863
Here are some basic examples of `:error/message` and `:error/fn`:
0 commit comments