From a921a564180bf08b00e9b3ad4d38989ae4a2ce85 Mon Sep 17 00:00:00 2001 From: Steve Date: Sat, 14 Oct 2023 12:38:52 +1100 Subject: [PATCH] Add tip for recursively dereferencing registry schemas --- docs/tips.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/docs/tips.md b/docs/tips.md index ec28167c9..342ddecf0 100644 --- a/docs/tips.md +++ b/docs/tips.md @@ -539,3 +539,33 @@ Example utility to convert schemas recursively: ; [:int {:gen/elements [1 2 3]}] ; :string]]]]] ``` + +## De-reference schemas from a registry + +When transforming schemas, if they are provided by a registry, it's useful to dereference them first. + +```clojure +(defn deref-recursive + "deref all schemas at all levels so the resulting schema has no registry references" + [schema] + (m/walk schema + (m/schema-walker m/deref-all) + {::m/walk-schema-refs true + ::m/walk-refs true})) + +(def smart-phone + (m/schema "smart-phone" + {:registry (merge (m/default-schemas) + {"smart-phone" [:map + [:type [:enum :android :iphone]] + [:connector "connector"]] + "connector" [:enum :usb-c :micro-usb :lightning]})})) + +(deref-recursive smart-phone) +=> [:map + [:type [:enum :android :iphone]] + [:connector [:enum :usb-c :micro-usb :lightning]]] +``` + +The fully de-referenced schema is plain data which is much easier to transform. +