(defprotocol protocol-sym & specs)Defines a native Clojure protocol whose methods carry Malli schemas.
This is the core malt API entrypoint.
A method spec takes param/schema pairs followed by a return schema and an
optional (throws [...]) clause for checked exceptions. Methods with
multiple fixed arities wrap each complete spec in a list.
Methods without a throws clause are expected not to throw.
(defprotocol UserStore
(create-user [name :string age :int] :string)
(find-user
([id :string] ?User)
([id :string options ?Options] [:maybe ?User]))
(suspend-user! [id :string]
:nil
(throws [not-found Exception])))
All provided data is made available on the produced protocol root value:
(malt/defprotocol Foo
(foo [name :string]
:string))
Foo
;; =>
{:malt/protocol true
:sigs {:foo
{:malt/specs
[{:params [name]
:param-schemas {:name :string}
:arguments-schema [:cat :string]
:return-schema :string
:arguments-validator #object[...]
:return-validator #object[...]}]
...}}}
The resolved schema data is also attached to the metadata of each generated method var, so a method's signature can be read from the method itself:
(meta #'foo)
;; =>
{:malt/specs
[{:params [name]
:param-schemas {:name :string}
:arguments-schema [:cat :string]
:return-schema :string
:arguments-validator #object[...]
:return-validator #object[...]}]
...}
Also defines a ?Name var containing a Malli schema which can be used to
assert a value implements the protocol. This is done through satisfies?.
Defines a native Clojure protocol whose methods carry Malli schemas.
This is the core malt API entrypoint.
A method spec takes param/schema pairs followed by a return schema and an
optional `(throws [...])` clause for checked exceptions. Methods with
multiple fixed arities wrap each complete spec in a list.
Methods without a `throws` clause are expected not to throw.
```clojure
(defprotocol UserStore
(create-user [name :string age :int] :string)
(find-user
([id :string] ?User)
([id :string options ?Options] [:maybe ?User]))
(suspend-user! [id :string]
:nil
(throws [not-found Exception])))
```
All provided data is made available on the produced protocol root value:
```clojure
(malt/defprotocol Foo
(foo [name :string]
:string))
Foo
;; =>
{:malt/protocol true
:sigs {:foo
{:malt/specs
[{:params [name]
:param-schemas {:name :string}
:arguments-schema [:cat :string]
:return-schema :string
:arguments-validator #object[...]
:return-validator #object[...]}]
...}}}
```
The resolved schema data is also attached to the metadata of each generated
method var, so a method's signature can be read from the method itself:
```clojure
(meta #'foo)
;; =>
{:malt/specs
[{:params [name]
:param-schemas {:name :string}
:arguments-schema [:cat :string]
:return-schema :string
:arguments-validator #object[...]
:return-validator #object[...]}]
...}
```
Also defines a `?Name` var containing a Malli schema which can be used to
assert a value implements the protocol. This is done through `satisfies?`.(defrecord record-sym & specs)Defines a native Clojure record with schema-validated constructors.
Fields are given as field/schema pairs. Inline implementations of malt protocols are wrapped with input/output validation:
(defrecord UserStoreImpl
[db ?DataSource]
UserStore
(create-user [_ name age]
(persist-user db name age)))
Overrides the generated ->Name and map->Name constructors to validate
their inputs, and additionally defines ?NameSchema (a Malli :map schema of
the fields) and ?Name (an instance? check).
Defines a native Clojure record with schema-validated constructors.
Fields are given as field/schema pairs. Inline implementations of malt
protocols are wrapped with input/output validation:
```clojure
(defrecord UserStoreImpl
[db ?DataSource]
UserStore
(create-user [_ name age]
(persist-user db name age)))
```
Overrides the generated `->Name` and `map->Name` constructors to validate
their inputs, and additionally defines `?NameSchema` (a Malli :map schema of
the fields) and `?Name` (an `instance?` check).(extend-type type-sym & protocol+method-forms)Like [clojure.core/extend-type], but methods of malt protocols are wrapped with input/output validation.
Additionally supports passing a namespace-qualified record symbol (for
example other.ns/SomeRecord) which is resolved to the underlying class.
Like [clojure.core/extend-type], but methods of malt protocols are wrapped with input/output validation. Additionally supports passing a namespace-qualified record symbol (for example `other.ns/SomeRecord`) which is resolved to the underlying class.
(reify & protocol+method-forms)Like [clojure.core/reify], but methods of malt protocols are wrapped with input/output validation.
Like [clojure.core/reify], but methods of malt protocols are wrapped with input/output validation.
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |