Home > Net >  How do I validate a big decimal value in malli?
How do I validate a big decimal value in malli?

Time:08-29

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?

CodePudding user response:

(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)})})
  • Related