Skip to content

Commit

Permalink
Merge pull request #967 from stevebuik/master
Browse files Browse the repository at this point in the history
Add tip for recursively dereferencing registry schemas
  • Loading branch information
ikitommi committed Dec 3, 2023
2 parents c97fa68 + a921a56 commit 54b5cb8
Showing 1 changed file with 30 additions and 0 deletions.
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.

0 comments on commit 54b5cb8

Please sign in to comment.