Search code examples
validationclojureschemamalli

How do I validate a big decimal value in malli?


I have a variable of data type big decimal. How to validate that in Malli?

Looking at the predicate types in malli, there isn't one for big decimal. https://github.com/metosin/malli#built-in-schemas

So how do can I validate for this?

And what do we use number? for in Malli. Can someone provide an example for this?


Solution

  • (require
            '[malli.core :as m]
            '[malli.registry :as mr])
    (mr/set-default-registry!
        (merge
            (m/default-schemas)
            {:bigdec? (m/-simple-schema {:type :bigdec?
                                         :pred #(instance? BigDecimal %)})}))
    (let [schema [:map [:n :bigdec?]]]
        {:invalid (m/explain schema {:n 1})
         :valid   (m/explain schema {:n (BigDecimal. 1)})})