Last active
January 9, 2022 08:47
-
-
Save ikitommi/af280065df4cb16a17123558e41463d3 to your computer and use it in GitHub Desktop.
Immutable Malli Schemas for Anna
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; | |
;; 1) using immutable support registry via options | |
;; | |
(ns demo | |
(:require [malli.core :as m] | |
[malli.registry :as mr] | |
[malli.util :as mu])) | |
(def default-registry | |
(mr/composite-registry | |
(m/default-schemas) ;; malli defaults | |
(mu/schemas))) ;; plus the utility schemas | |
(def schema-registry-sample | |
{::payment-authorization [:map | |
[:id :string] | |
[:approvalNumber :string] | |
[:authResultDescription :string] | |
[:authorizationDate :string]] | |
::sales-order-line-item [:map | |
[:id :string] | |
[:authorization [:maybe | |
[:merge | |
::payment-authorization ;; <<<< I want to merge this with the fields below | |
[:map | |
[:type :string] | |
[:activationCode :string] | |
[:productCode :string]]]]] | |
[:offer {:optional true} [:multi {:dispatch :offerProgram} | |
["INSTANTSAVINGS" [:merge | |
::offer [:map [:gtin :string]]]] | |
[::m/default ::offer]]]] | |
::offer [:map] | |
::sales-order [:map | |
[:paymentAuthorization ::payment-authorization] | |
[:salesOrderLineItem [:vector ::sales-order-line-item]]]}) | |
(def sales-order | |
(m/schema | |
[:schema {:registry schema-registry-sample} | |
::sales-order] | |
{:registry default-registry})) | |
(require '[malli.generator :as mg]) | |
(mg/generate sales-order) | |
;{:paymentAuthorization {:id "RuP", :approvalNumber "", :authResultDescription "Iz", :authorizationDate "G"}, | |
; :salesOrderLineItem [{:id "", :authorization nil, :offer {:gtin "2u9"}} | |
; {:id "K70", | |
; :authorization {:id "Rb", | |
; :approvalNumber "6j", | |
; :authResultDescription "KU0xm", | |
; :authorizationDate "Xe1k9", | |
; :type "Hv", | |
; :activationCode "7plP", | |
; :productCode "A"}} | |
; {:id "gOfo", | |
; :authorization {:id "D6L", | |
; :approvalNumber "hcH3", | |
; :authResultDescription "", | |
; :authorizationDate "4", | |
; :type "6", | |
; :activationCode "", | |
; :productCode ""}, | |
; :offer {}}]} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; | |
;; 2) using just immutable registry via options | |
;; | |
(mg/generate | |
(m/schema | |
::sales-order | |
{:registry (mr/composite-registry | |
default-registry | |
schema-registry-sample)})) | |
;{:paymentAuthorization {:id "51CSa6DjART3K5v792gvz00a6bM8j", | |
; :approvalNumber "ZAc", | |
; :authResultDescription "N8J5mA8Pn596Tjc68q330ZTsih", | |
; :authorizationDate "68E85sIPa9j0Y7OJ"}, | |
; :salesOrderLineItem [{:id "mBvR0LKJVwK9XdU9D", | |
; :authorization {:id "TdjJdzaj5N72F2Kh9kn8005jxG", | |
; :approvalNumber "l63c3v50", | |
; :authResultDescription "GiSc", | |
; :authorizationDate "79899TD3Kg7017E7Vkux6kGjTr9g", | |
; :type "0SX", | |
; :activationCode "yTw00LD9c4607Q7FCvcl8Ju", | |
; :productCode "A8fM5FxbbqIu17w5"}} | |
; {:id "1ThHh46WHiFY4I", :authorization nil, :offer {}} | |
; {:id "KKFtSt8KZg63TU6HF9qM2ht", :authorization nil, :offer {:gtin "CcfiE75Rhse197j8X5te1e6UvJ"}} | |
; {:id "j8zqGAx", | |
; :authorization {:id "nh7uB", | |
; :approvalNumber "pNjF7ZQMs1TF19xSBX2uKr5MgNbEBM", | |
; :authResultDescription "ZHQA9o1B7FN6xSct", | |
; :authorizationDate "3v5a2yNY1ouoc", | |
; :type "03IGQ1nox8ToA7MKD344udW2v7I", | |
; :activationCode "Y0LWlVa9t", | |
; :productCode "mkf"}} | |
; {:id "O9VL5gI48BXli9eo42JFg77LwA", | |
; :authorization {:id "wYyzl50pdq", | |
; :approvalNumber "wyWT", | |
; :authResultDescription "sDCJ", | |
; :authorizationDate "CR8P9Cm90", | |
; :type "PNwdOp598e9J47x2h0", | |
; :activationCode "2Q", | |
; :productCode "PoTHB3GqgBSMj9J379o"}, | |
; :offer {}} | |
; {:id "N37du5P8o", :authorization nil, :offer {:gtin "VMhY7V1awLR"}} | |
; {:id "kc1J6B8lsH75kwzaZnp9KVny0aoL5", :authorization nil} | |
; {:id "C68ax0", :authorization nil} | |
; {:id "tUzLR3LPW", :authorization nil} | |
; {:id "5P", | |
; :authorization {:id "4556Wnj", | |
; :approvalNumber "Ly14b34w5j2X", | |
; :authResultDescription "vu5uwNS9G6NlNaFex", | |
; :authorizationDate "0C9tw40I6gFwd948V2", | |
; :type "7Zky2fCtMT2RVWr9tGM0em0K559m7", | |
; :activationCode "835OA2Ep817J2797n5H4Bb", | |
; :productCode "r94EuulQ5jaKXuhr99CV8Jyg2t4mi6"}}]} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; | |
;; 3) registering utility schema via local registry (requires latest code from MASTER) | |
;; | |
(def schema-registry-sample | |
{:merge (mu/-merge) | |
::payment-authorization [:map | |
[:id :string] | |
[:approvalNumber :string] | |
[:authResultDescription :string] | |
[:authorizationDate :string]] | |
::sales-order-line-item [:map | |
[:id :string] | |
[:authorization [:maybe | |
[:merge | |
::payment-authorization ;; <<<< I want to merge this with the fields below | |
[:map | |
[:type :string] | |
[:activationCode :string] | |
[:productCode :string]]]]] | |
[:offer {:optional true} [:multi {:dispatch :offerProgram} | |
["INSTANTSAVINGS" [:merge | |
::offer [:map [:gtin :string]]]] | |
[::m/default ::offer]]]] | |
::offer [:map] | |
::sales-order [:map | |
[:paymentAuthorization ::payment-authorization] | |
[:salesOrderLineItem [:vector ::sales-order-line-item]]]}) | |
(def sales-order | |
(m/schema | |
[:schema {:registry schema-registry-sample} | |
::sales-order])) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; | |
;; 4) using local component directly | |
;; | |
(def Merge (mu/-merge)) | |
(def schema-registry-sample | |
{::payment-authorization [:map | |
[:id :string] | |
[:approvalNumber :string] | |
[:authResultDescription :string] | |
[:authorizationDate :string]] | |
::sales-order-line-item [:map | |
[:id :string] | |
[:authorization [:maybe | |
[Merge | |
::payment-authorization ;; <<<< I want to merge this with the fields below | |
[:map | |
[:type :string] | |
[:activationCode :string] | |
[:productCode :string]]]]] | |
[:offer {:optional true} [:multi {:dispatch :offerProgram} | |
["INSTANTSAVINGS" [Merge | |
::offer [:map [:gtin :string]]]] | |
[::m/default ::offer]]]] | |
::offer [:map] | |
::sales-order [:map | |
[:paymentAuthorization ::payment-authorization] | |
[:salesOrderLineItem [:vector ::sales-order-line-item]]]}) | |
(def sales-order | |
(m/schema | |
[:schema {:registry schema-registry-sample} | |
::sales-order])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment