amelinium.http.middleware.validators
added in 1.0.0
amelinium service, validators.
gen-required
(gen-required {required-cat :required/cat, required-special :required/special} items rng)
(gen-required {required-cat :required/cat, required-special :required/special} items)
(gen-required {required-cat :required/cat, required-special :required/special})
Generates a map with keys :blank
, :some
and :any
containing vectors of randomly selected, unique names which fit into certain categories, accordingly: required keys which must be blank, required keys which must not be blank, required keys which may have any value.
Data for randomization is taken from a map under the key :required/cat
and from a vector under the key :required/special
of the provided configuration argument. This vector is randomized to get a list of items
values marked in validators configuration as special (coming from :required/blank
, :required/some
or :required
keys, without its validation function being overriden by any manually configured validator). When the list is ready its items are grouped by the mentioned categories and returned as a map.
The optional second argument items
is a number of items to generate. The optional third argument rng
should be a random number generator object.
gen-required-blank
(gen-required-blank {required-blank :required/blank})
(gen-required-blank {required-blank :required/blank} items)
(gen-required-blank {required-blank :required/blank} items rng)
Generates a list of randomly selected, unique names from a vector which should be available under the key :required/blank
of the provided map.
The optional second argument items
is a number of items to generate. The optional third argument rng
should be a random number generator object.
gen-required-some
(gen-required-some {required-some :required/some})
(gen-required-some {required-some :required/some} items)
(gen-required-some {required-some :required/some} items rng)
Generates a list of randomly selected, unique names from a vector which should be available under the key :required/some
of the provided map.
The optional second argument items
is a number of items to generate. The optional third argument rng
should be a random number generator object.
prep-validators
(prep-validators {required-mode :required/mode, required :required, :as config, :or {required default-required, default-pass? default-default-pass?, required-some default-required-some, config-key default-config-key, explain-key default-explain-key, enabled? true, required-blank default-required-blank, validators default-validators, explain? default-explain?, result-key default-result-key, check-required? default-check-required?}, default-pass? :default-pass?, required-some :required/some, config-key :config-key, explain-key :explain-key, enabled? :enabled?, required-blank :required/blank, validators :validators, required-source :required/source, explain? :explain?, result-key :result-key, check-required? :check-required?})
Prepares validators configuration.
Sequences of values provided under :required/some
, :required/blank
and :required
keys are cleaned-up and transformed into strings. Map under the key :validators
is also transformed: its keys are converted into strings and values are dereferenced if they are symbols.
Any parameter in a sequence provided under :required/some
or :required/blank
key is removed if it has a custom validator assigned in :validators
map.
Parameter names which exist in both :required/some
and :required/blank
sequences are removed from them and moved to a sequence under the :required/any
key. Marking some required parameter to be blank and non-blank at the same time causes it to fall into a category where is can have any value to be considered valid.
Parameter names which exist in :required
sequence and also exist in :required/some
or :required/blank
sequence are removed from the :required
sequence.
Parameter names which exist in :required/some
, :required/blank
and :required
are removed from :required/some
and :required/blank
, and only exist in :required
. Unless their validator was already specified in :validators
they are also added to :required/any
.
For each parameter name from :required/some
a validator is generated (checking if a given value is not blank). For each parameter name from :required/blank
a validator is generated (checking if a given value is blank). For each parameter name from :required/any
a validator is generated (accepting any value, including blanks).
Manually assigned validators are expressed as a map under :validators
key.
Automatically created validators (for the aforementioned keys: :required/some
, :required/blank
and :required/any
) are associated with their parameter names in a map under the :validators/all
key, which also contains manually assigned validators.
During the operation a map is created under the :required/cat
key. It contains all of the required parameter names as keys with validation categories assigned (expressed as keywords):
:some
– parameter is valid if it has a non-blank value:blank
– parameter is valid if it has a blank value:any
– parameter is valid if it has any value:custom
– parameter is valid if it passes a custom validation (see:validators
).
This function returns a given configuration map with the following keys transformed as described earlier:
Booleans: * :enabled?
(true
if enabled in configuration with :enabled?
) * :default-pass?
(true
if unknown parameters are considered valid) * :check-required?
(true
if required parameters are to be checked)
Vectors: * :required/some
(required parameters having auto-generated validators for non-blanks) * :required/blank
(required parameters having auto-generated validators for blanks) * :required/any
(required parameters having auto-generated validators for any values) * :required/special
(all required parameters having auto-generated validators) * :required/user
(required parameters which were manually configured) * :required
(all required parameters, including those having auto-generated validators)
Maps: * :required/cat
(all required parameters with categories assigned) * :validators
(manually configured validators for parameter names) * :validators/all
(manually and automatically configured validators)
Keywords: * :result-key
(key identifying validation results in a request map) * :config-key
(key identifying configuration in a request map) * :required/source
(required parameters source, a keyword, defaults to :required/user
) * :required/mode
(required parameters checking mode: :all (default), :one or a number).
wrap-validators
(wrap-validators k config)
Validators wrapping middleware initializer. Processes the configuration expressed as a map which may have the following keys specified:
Switches: * :enabled?
(true
if enabled in configuration with :enabled?
) * :default-pass?
(true
if unknown parameters are considered valid) * :check-required?
(true
if required parameters are to be checked)
Sequential collections: * :required/some
(parameters which should have generated validators for non-blanks) * :required/blank
(parameters which should have generated validators for blanks) * :required/any
(parameters which should have generated validators for any values) * :required/user
(parameters associated with the :required
key) * :required/special
(parameters for which validator was automatically generated) * :required/nospecial
(parameters for which validator was not automatically generated) * :required
(all required parameters)
Maps: * :validators
(validators assigned to parameter names)
Keywords: * :result-key
(key identifying validation results in a request map) * :config-key
(key identifying configuration in a request map).
Validator names should be strings or objects which are convertible to strings. Validators can be various objects, see io.randomseed.utils.validators/Validating
protocol.
The result of calling this function is a map intended to be used with Reitit router as a middleware. The handler function itself will add :validators/config
and :validators/params-valid?
entries to a request map. Names of these identifying keys can be changed by setting :result-key
and/or :config-key
configuration options.
The presence of validation function can be controlled by setting :validators/disabled?
in a request context to override the defaults. It is a switch which disables the validation completely (any parameter will be considered valid).
By default, only manually configured required validators (assigned to the :required/user
configuration key) will be checked for presence! It can be changed by setting :required/check
to:
:required/some
(parameters which should have generated validators for non-blanks):required/blank
(parameters which should have generated validators for blanks):required/any
(parameters which should have generated validators for any values):required/user
(parameters associated with the:required
key):required/special
(parameters for which validator was automatically generated):required/nospecial
(parameters for which validator was not automatically generated):required
(all required parameters)
See amelinium.http.middleware.validators/prep-validators
to see the detailed logic behind preparing the configuration.