Select a subset of a malli schema.
Select a subset of a malli schema.
Default for the :verify-selection option of select/selector.
See select for accepted values.
Rebind with binding, or set app-wide via alter-var-root
(Clojure) / set! (ClojureScript).
Default for the `:verify-selection` option of `select`/`selector`. See `select` for accepted values. Rebind with `binding`, or set app-wide via `alter-var-root` (Clojure) / `set!` (ClojureScript).
(select schema)(select schema selection)(select schema
selection
{:as options
:keys [verify-selection prune-optionals]
:or {verify-selection *verify-selection*}})selection examples:
[] - everything (deep) optional[:name :age] - required attributes['*] - everything (non-recursive) required[{:address [:street]}] - if :address provided then only :street is required.Combinations:
[:address {:address [:street]}] - require :address but only its :street is required.[:address {:address [] :friends [:name]}] - require :address and optionally :friends.[{:friends [:name]} {:friends [:age]}] - require :name and :age of friends if :friends provided (selections merge).:multi schemas are selected with a map: keys are dispatch values, values
are sub-selections. Unmentioned branches become all-optional. A keyword
:dispatch key stays required in every explicit map-branch:
{:human [:name]} - require :name in branch :human.{'* [:type]} - require :type in every branch (fails verification when a branch lacks it).{'* [:type] :human [:name]} - '* merges with explicit branches.{::m/default [:x]} - address the default branch.{} - everything optional (like []).[:id {:pet {:dog [:breed]}}] - nested: :pet is a :multi with a :dog branch.
With prune-optionals: branches stay when mentioned (or when none is mentioned); other branches are dropped.options:
verify-selection - what to do when selection contains paths not in schema. Defaults to *verify-selection* (initially :throw):
:throw (:assert works as well) - throw an ExceptionInfo with {:type ::unknown-paths :data {:paths ... :available ...}} as ex-data.:log - print a warning (stderr on Clojure, console.warn on ClojureScript) and continue.{:paths ... :available ...}, result ignored, selection continues.:skip, false, nil - don't verify.prune-optionals (false (default), true) - whether all fully optional subtrees should be removed from the resulting schema. Alternatively via metadata of selection: ^:only [:name] (flag takes precedence over metadata). See also select-only, which forces this option.
Typically used when the selected schema is used for data generation.Examples:
(select Person) ;; all optional
(select Person []) ;; all optional
(select Person ['*]) ;; all root attributes of Person required
(select Person [:name :handle]) ;; Require specific root attributes.
(select Person [{:address ['*]}]) ;; Require the full address if provided.
(select Person [:foo]) ;; Throws ExceptionInfo about non existing path, ex-data contains all possible paths.
`selection` examples:
- `[]` - everything (deep) optional
- `[:name :age]` - required attributes
- `['*]` - everything (non-recursive) required
- `[{:address [:street]}]` - if `:address` provided then only `:street` is required.
Combinations:
- `[:address {:address [:street]}]` - require `:address` but only its `:street` is required.
- `[:address {:address [] :friends [:name]}]` - require `:address` and optionally `:friends`.
- `[{:friends [:name]} {:friends [:age]}]` - require `:name` and `:age` of friends if `:friends` provided (selections merge).
`:multi` schemas are selected with a *map*: keys are dispatch values, values
are sub-selections. Unmentioned branches become all-optional. A keyword
`:dispatch` key stays required in every explicit map-branch:
- `{:human [:name]}` - require `:name` in branch `:human`.
- `{'* [:type]}` - require `:type` in *every* branch (fails verification when a branch lacks it).
- `{'* [:type] :human [:name]}` - `'*` merges with explicit branches.
- `{::m/default [:x]}` - address the default branch.
- `{}` - everything optional (like `[]`).
- `[:id {:pet {:dog [:breed]}}]` - nested: `:pet` is a `:multi` with a `:dog` branch.
With `prune-optionals`: branches stay when mentioned (or when none is mentioned); other branches are dropped.
`options`:
- `verify-selection` - what to do when `selection` contains paths not in `schema`. Defaults to `*verify-selection*` (initially `:throw`):
- `:throw` (`:assert` works as well) - throw an `ExceptionInfo` with `{:type ::unknown-paths :data {:paths ... :available ...}}` as `ex-data`.
- `:log` - print a warning (stderr on Clojure, `console.warn` on ClojureScript) and continue.
- a function - called with `{:paths ... :available ...}`, result ignored, selection continues.
- `:skip`, `false`, `nil` - don't verify.
- `prune-optionals` (`false` (default), `true`) - whether all fully optional subtrees should be removed from the resulting schema. Alternatively via metadata of selection: `^:only [:name]` (flag takes precedence over metadata). See also `select-only`, which forces this option.
Typically used when the selected schema is used for data generation.
Examples:
```
(select Person) ;; all optional
(select Person []) ;; all optional
(select Person ['*]) ;; all root attributes of Person required
(select Person [:name :handle]) ;; Require specific root attributes.
(select Person [{:address ['*]}]) ;; Require the full address if provided.
(select Person [:foo]) ;; Throws ExceptionInfo about non existing path, ex-data contains all possible paths.
```
(select-only schema selection)(select-only schema selection options)Like select, but the result contains only the selected attributes:
:prune-optionals is forced to true (a caller-supplied
:prune-optionals false is overridden). All other options (e.g.
:verify-selection) pass through unchanged.
Typically used to generate specific data:
(mg/generate (select-only Person [:name])) ;; => {:name "..."}
For :multi schemas, branches the selection does not mention are dropped.
Like `select`, but the result contains only the selected attributes:
`:prune-optionals` is forced to `true` (a caller-supplied
`:prune-optionals false` is overridden). All other options (e.g.
`:verify-selection`) pass through unchanged.
Typically used to generate specific data:
```
(mg/generate (select-only Person [:name])) ;; => {:name "..."}
```
For `:multi` schemas, branches the selection does not mention are dropped.(selectable-paths schema)Yield set of selectable paths. Branches of a :multi show up as
{:branch dispatch-value} segments.
Examples:
(selectable-paths
[:maybe
[:map
[:addresses [:vector [:map
[:street string?]]]]]])
;;=> #{[:addresses] [:addresses :street]}
Yield set of selectable paths. Branches of a `:multi` show up as
`{:branch dispatch-value}` segments.
Examples:
```
(selectable-paths
[:maybe
[:map
[:addresses [:vector [:map
[:street string?]]]]]])
;;=> #{[:addresses] [:addresses :street]}
```
(selector schema)Yields a function similar to (partial ms/select schema).
A selector is faster when doing multiple selections from a schema as the schema is optionalized once.
Examples:
(let [person-selector (selector Person)]
(person-selector [:name] {:prune-optionals true})) ;; what `select-only` does
Yields a function similar to `(partial ms/select schema)`.
A selector is faster when doing multiple selections from a schema as the schema is optionalized once.
Examples:
```
(let [person-selector (selector Person)]
(person-selector [:name] {:prune-optionals true})) ;; what `select-only` does
```
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 |