Skip to content

Instantly share code, notes, and snippets.

@ikitommi
Last active September 5, 2020 15:56
Show Gist options
  • Select an option

  • Save ikitommi/06143540e6259323b95253e87e1ef710 to your computer and use it in GitHub Desktop.

Select an option

Save ikitommi/06143540e6259323b95253e87e1ef710 to your computer and use it in GitHub Desktop.
Malli with lazy providers & lazy :multi
(require '[malli.core :as m])
(require '[malli.registry :as mr])
;; - cljs: :closure-defines {malli.registry/type "custom"}
;; - clj: :jvm-opts ["-Dmalli.registry/type=custom"]
(mr/set-default-registry!
(mr/lazy-registry
(mr/composite-registry
;; the defaults
(m/default-schemas)
;; new lazy :multi variant
{:lazy-multi (m/-multi-schema
{:type :lazy-multi
:lazy-refs true
:naked-keys true})})
;; dummy dynamic schema provider
(fn [type registry]
(let [lookup {"AWS::ApiGateway::UsagePlan" [:map {:closed true}
[:Type [:= "AWS::ApiGateway::UsagePlan"]]
[:Description {:optional true} string?]
[:UsagePlanName {:optional true} string?]]
"AWS::AppSync::ApiKey" [:map {:closed true}
[:Type [:= "AWS::AppSync::ApiKey"]]
[:ApiId string?]
[:Description {:optional true} string?]]}
schema (some-> type lookup (m/schema {:registry registry}))]
(println "loaded" (pr-str type))
schema))))
(m/validate
"AWS::ApiGateway::UsagePlan"
{:Type "AWS::ApiGateway::UsagePlan"
:Description "kikka"
:UsagePlanName "kukka"})
; loaded "AWS::ApiGateway::UsagePlan"
; => true
(def Schema
[:lazy-multi {:dispatch :Type}
"AWS::ApiGateway::Stage"
"AWS::ApiGateway::UsagePlan"
"AWS::AppSync::ApiKey"])
(m/form Schema)
; => [:lazy-multi {:dispatch :Type} "AWS::ApiGateway::Stage" "AWS::ApiGateway::UsagePlan" "AWS::AppSync::ApiKey"]
(require '[malli.error :as me])
(-> (m/explain
Schema
{:Type "AWS::AppSync::ApiKey"
:Descriptionz "kikka"})
(me/with-spell-checking)
(me/humanize))
; loaded "AWS::AppSync::ApiKey"
; => {:ApiId ["missing required key"]
; :Descriptionz ["should be spelled :Description"]}
(-> (m/explain
Schema
{:Type "AWS::ApiGatway::UsagePlan"
:Description "kikka"
:UsagePlanName "kukka"})
(me/with-spell-checking)
(me/humanize))
; => {:Type ["did you mean AWS::ApiGateway::UsagePlan"]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment