Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tip for recursively dereferencing registry schemas #967

Merged
merged 1 commit into from
Dec 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions docs/tips.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.