Skip to content

Commit

Permalink
Replace 'Specs' with 'Asserting' in README
Browse files Browse the repository at this point in the history
  • Loading branch information
weavejester committed Apr 29, 2024
1 parent e9f651e commit f495b6b
Showing 1 changed file with 18 additions and 31 deletions.
49 changes: 18 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -470,47 +470,34 @@ values.
(fn [_] (resp/response (str "Hello " (clojure.string/join ", " names))))
```

### Specs
### Asserting

It would be incorrect to write specs directly against the keys used by
Integrant, as the same key will be used in the configuration, during
initiation, and in the resulting system. All will likely have
different values.

To resolve this, Integrant has an `pre-init-spec` multimethod that can
be extended to provide Integrant with a spec to test the value after
the references are resolved, but before they are initiated. The
resulting spec is checked directly before `init-key`, and an exception
is raised if it fails.

Here's how our two example keys would be specced out:
It's often useful to add assertions to ensure that the system has been
initiated correctly. This can be done via `assert-key`:

```clojure
(require '[clojure.spec.alpha :as s])

(s/def ::port pos-int?)
(s/def ::handler fn?)

(defmethod ig/pre-init-spec :adapter/jetty [_]
(s/keys :req-un [::port ::handler]))

(s/def ::name string?)

(defmethod ig/pre-init-spec :handler/greet [_]
(s/keys :req-un [::name]))
(defmethod ig/assert-key :adapter/jetty [_ {:keys [port]}]
(assert (nat-int? port) ":port should be a valid port number"))
```

If we try to `init` an invalid configuration:
If we try to `init` an invalid configuration, then an `AssertionError`
is thrown explaining the error:

```clojure
(ig/init {:adapter/jetty {:port 3000} :handler/greet {:name "foo"}})
```
user=> (ig/init {:adapter/jetty {:port "3000"}})
AssertionError Assert failed: :port should be a valid port number
(nat-int? port) user/eval3088/fn--3090 (form-init14815382800832764134.clj:2
```

Then an `ExceptionInfo` is thrown explaining the error:
This error is wrapped in an `clojure.lang.ExceptionInfo` that contains
additional information:

```
ExceptionInfo Spec failed on key :adapter/jetty when building system
val: {:port 3000} fails predicate: (contains? % :handler)
user=> (ex-data *e)
{:reason :integrant.core/build-failed-spec
:system {}
:key :adapter/jetty
:value {:port "3000"}}
```

### Loading namespaces
Expand Down

0 comments on commit f495b6b

Please sign in to comment.