fulcrologic / guardrails

Efficient, hassle-free function call validation with a concise inline syntax for clojure.spec and Malli
Eclipse Public License 2.0
240 stars 26 forks source link

linear search over malli default registry & custom guardrail registry #35

Closed ikitommi closed 9 months ago

ikitommi commented 9 months ago

Current code pushes all malli default schemas into the atom. This has some drawbacks:

  1. user can accidentally override any default schema, e.g. write :int to (m/-string-schema)
  2. common pattern with malli is to build your mutable registry to application-level, e.g.
(require '[malli.core :as m])
(require '[malli.registry :as mr])

(def registry (atom {}))

(defn register! [type schema]
  (swap! registry assoc type schema))

(mr/set-default-registry!
  ;; linear search
  (mr/composite-registry
    ;; core schemas
    (m/default-schemas)
    ;; to support Var references
    (mr/var-registry)
    ;; mutable (spec-like) registry
    (mr/mutable-registry registry)))

this PR changes the registry to be a composite registry (linear seach) over the following:

  1. malli.core/default-registry -> users can define this into anything, see above. If user has already a mutable registry, those are automatically available with guardrails. Also, in ClojureScript, one can just register few basic schemas for smaller bundle size (malli starts with 2kb gzipped)
  2. the guardrail default registry.

having 1 separated from 2 also means one can't override the base schemas, e.g. :int is always :int.

awkay commented 9 months ago

Excellent. Thanks for the help!