Select a subset of a malli schema.
Select a subset of a malli schema.
(select schema)
(select schema selection)
(select schema
selection
{:as options
:keys [verify-selection prune-optionals]
:or {verify-selection :assert}})
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
(:assert
(default), :skip
, false
, nil
) - what to do when selection
contains paths not in schema
.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]) ;; Assert exception about non existing path, showing 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` (`:assert` (default), `:skip`, `false`, `nil`) - what to do when `selection` contains paths not in `schema`. - `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]) ;; Assert exception about non existing path, showing all possible paths. ```
(selectable-paths schema)
Examples: (selectable-paths [:maybe [:map [:addresses [:vector [:map [:street string?]]]]]]) ;;=> #{[:addresses] [:addresses :street]}
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 is a website building & hosting documentation for Clojure/Script libraries
× close