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]}] - only require :age of friends if :friends provided (last selection wins).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).
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]}]` - only require `:age` of friends if `:friends` provided (last selection wins).
`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).
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.
```
(selectable-paths schema)Yield set of selectable paths.
Examples:
(selectable-paths
[:maybe
[:map
[:addresses [:vector [:map
[:street string?]]]]]])
;;=> #{[:addresses] [:addresses :street]}
Yield set of selectable paths.
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 ^:only [:name]))
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 ^:only [:name])) ```
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 |