2

This is my payload.

(m/validate readingDetails-schema 
{:readingCode : "Code1",
  :readingNumber : "twenty two round off",
  :readingCheck : "First",
  :readings : [{
    :readingDate : "2021-05-02",
    :readingType : "first check",
    :readingPrecision : "two decimals check",
    :readingEstimate : "Check"}]})
 

This is my first schema:

(def reading-schema (m/schema            [:map
                                          [:readingDate :re #"\d{4}-\d{2}-\d{2}"]
                                          [:readingType string?]
                                          [:readingPrecision decimal?]
                                          [:readingEstimate string?]]))
=> #'interest-malli-test.core/reading-schema

Now this is the main schema, which must include the above schema.

(def readingDetails-schema                  [:merge
                                               #ref [:reading-schema]
                                               [:map
                                                [:readingCode string?]
                                                [:readingNumber string?]
                                                [:readingCheck string?]
                                                [<how to add a list of reading-schema here?>]
                                                ]])

I'm trying like this.

(def readingDetails-schema                  [:merge
                                                   #ref [:reading-schema]
                                                   [:map
                                                    [:readingCode string?]
                                                    [:readingNumber string?]
                                                    [:readingCheck string?]
                                                    [[:vector #ref [:reading-schema]]
                                                    ]])

1 Answer 1

2

After you convert the payload into Clojure data structures, just use the name of the first schema in the second one (and change decimal? into string?).

Dependencies: [metosin/malli "0.8.9"]

Require: [malli.core :as m]

(def reading-schema
  [:map
   [:readingDate :re #"\d{4}-\d{2}-\d{2}"]
   [:readingType string?]
   [:readingPrecision string?]
   [:readingEstimate string?]])

(def readingDetails-schema
  [:map
   [:readingCode string?]
   [:readingNumber string?]
   [:readingCheck string?]
   [:readings [:vector reading-schema]]])

(m/validate readingDetails-schema
            {:readingCode   "Code1",
             :readingNumber "twenty two round off",
             :readingCheck  "First",
             :readings      [{:readingDate      "2021-05-02",
                              :readingType      "first check",
                              :readingPrecision "two decimals check",
                              :readingEstimate  "Check"}]})

=> true
4
  • "and change decimal? into string?" Right. Done. And this works!
    – Blaze
    Aug 4, 2022 at 4:12
  • A doubt. We don't need #ref at all?
    – Blaze
    Aug 4, 2022 at 5:16
  • 2
    I don't think so, :ref is for recursive schemas. Aug 4, 2022 at 7:09
  • 1
    Is there a tutorial for understanding recursive schemas? I didn't get that entirely from Malli's GitHub page.
    – Blaze
    Aug 4, 2022 at 9:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.