Home > Enterprise >  How to use :merged for defining schemas in a nested fashion in Malli?
How to use :merged for defining schemas in a nested fashion in Malli?

Time:08-11

This is wrt to the question. How to wrap one schema inside another for Malli? I have two schema:

(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]]])

Now I want to define another schema which wraps these both. The thrird schema :

(def testReading-schema
 [:merge
 #ref [:readingDetails-schema]
  [:map
   [:finalDate :re #"\d{4}-\d{2}-\d{2}"]
   [:claimedBy string?]
   [:sentTo string?]
   [:recievedFrom string?]
   [:readingDetails [how to add here?>]]]])

I'm trying like this

(def testReading-schema
 [:merge
 #ref [:readingDetails-schema]
  [:map
   [:finalDate :re #"\d{4}-\d{2}-\d{2}"]
   [:claimedBy string?]
   [:sentTo string?]
   [:recievedFrom string?]
   [:readingDetails #ref [:readingDetails-schema]]]])

CodePudding user response:

I've tried without any ref tag. You can try this:

(def testReading-schema
   [:map
    [:finalDate :re #"\d{4}-\d{2}-\d{2}"]
    [:claimedBy string?]
    [:sentTo string?]
    [:recievedFrom string?]
    [:readingDetails :readingDetails-schema]])

Note : this will work only in Repl, if you define all schemas as and when needed. If you try this with a call reference to a schema.edn file specified elsewhere & if you try using it in core.clj, this will throw an error. I'm not sure how to resolve that.

  • Related