This namespace provides the utilities for applicative data validation.
Chiefly, it provides the validation
function that defines a data
validation. It also provides predefined data validators for common
use-cases (ints, strings, booleans, ...).
Example:
(defn example-validation
[id name]
(validation (fn [id name] {:id id :name name})
(validate-pos-int id :id)
(validate-string name)))
(example-validation [42 "name"])
;; => active.clojure.validation/ValidationSuccess{:candidate {:id 42, :name "name"}}
(example-validation [42 23])
;; => active.clojure.validation/ValidationFailure{:errors
;; [active.clojure.validation/ValidationError{:candidate 23,
;; :message :active.clojure.validation/string,
;; :label nil}]}
(example-validation ["name" 42])
;; => phoenix.common.validation/ValidationFailure{:errors
;; (active.clojure.validation/ValidationError{:candidate "name",
;; :message :active.clojure.validation/pos-int,
;; :label :id}
;; active.clojure.validation/ValidationError{:candidate 42,
;; :message :active.clojure.validation/string,
;; :label nil})}
This namespace provides the utilities for applicative data validation. Chiefly, it provides the [[validation]] function that defines a data validation. It also provides predefined data validators for common use-cases (ints, strings, booleans, ...). Example: ``` (defn example-validation [id name] (validation (fn [id name] {:id id :name name}) (validate-pos-int id :id) (validate-string name))) (example-validation [42 "name"]) ;; => active.clojure.validation/ValidationSuccess{:candidate {:id 42, :name "name"}} (example-validation [42 23]) ;; => active.clojure.validation/ValidationFailure{:errors ;; [active.clojure.validation/ValidationError{:candidate 23, ;; :message :active.clojure.validation/string, ;; :label nil}]} (example-validation ["name" 42]) ;; => phoenix.common.validation/ValidationFailure{:errors ;; (active.clojure.validation/ValidationError{:candidate "name", ;; :message :active.clojure.validation/pos-int, ;; :label :id} ;; active.clojure.validation/ValidationError{:candidate 42, ;; :message :active.clojure.validation/string, ;; :label nil})} ```
(and-then e f)
Apply validations in sequence. Takes a validation e
and a function
f
and applies e
's candidate iff e
is a [[ValidationSuccess]].
Apply validations in sequence. Takes a validation `e` and a function `f` and applies `e`'s candidate iff `e` is a [[ValidationSuccess]].
(curry-n f n)
Curry a function f
of arity n
.
Curry a function `f` of arity `n`.
(fmap-failure f e)
fmap for validation errors: Apply f
to each of e
's errors iff e is a [[ValidationFailure]]
fmap for validation errors: Apply `f` to each of `e`'s errors iff e is a [[ValidationFailure]]
(fmap-result f-success f-failure result)
fmap over the result of a validation. Applies (fmap-success f-success result
) if result
is a [[ValidationSuccess]]
and (fmap-failure f-failure result)
if result
is
a [[ValidationFailure]]
fmap over the result of a validation. Applies `(fmap-success f-success result`) if `result` is a [[ValidationSuccess]] and `(fmap-failure f-failure result)` if `result` is a [[ValidationFailure]]
(fmap-success f e)
fmap for validation success: Apply f
to e
's candidate iff e is
a [[ValidationSuccess]].
fmap for validation success: Apply `f` to `e`'s candidate iff e is a [[ValidationSuccess]].
(make-validation-error candidate message label)
Construct a ValidationError
(Signifies a the error of a failed
validation. Holds the candidate value that was being validated, the
corresponding error message and an arbitrary label.) record.
candidate
(The candidate value that was being validated.): access via active.clojure.validation/validation-error-candidate
message
(A message signifying what kind of error occured. It should
be possible for the user to interpret the message as they please, so
usually a namespaced keyword representing the error works well.): access via active.clojure.validation/validation-error-message
label
(Arbitrary data that can be added to an error.): access via active.clojure.validation/validation-error-label
Construct a `ValidationError` (Signifies a the error of a failed validation. Holds the candidate value that was being validated, the corresponding error message and an arbitrary label.) record. `candidate` (The candidate value that was being validated.): access via [[active.clojure.validation/validation-error-candidate]] `message` (A message signifying what kind of error occured. It should be possible for the user to interpret the message as they please, so usually a namespaced keyword representing the error works well.): access via [[active.clojure.validation/validation-error-message]] `label` (Arbitrary data that can be added to an error.): access via [[active.clojure.validation/validation-error-label]]
(make-validation-failure errors)
Construct a ValidationFailure
(Signifies a failured validation. Holds
the [[ValidationError]].) record.
errors
(A sequence of [[ValidationError]] that lead to the failed
validation.): access via active.clojure.validation/validation-failure-errors
Construct a `ValidationFailure` (Signifies a failured validation. Holds the [[ValidationError]].) record. `errors` (A sequence of [[ValidationError]] that lead to the failed validation.): access via [[active.clojure.validation/validation-failure-errors]]
(make-validation-success candidate)
Construct a ValidationSuccess
(Signifies a successful validation. Holds
the candidate value that was being validated.) record.
candidate
(The candidate value that was beign validated.): access via active.clojure.validation/validation-success-candidate
Construct a `ValidationSuccess` (Signifies a successful validation. Holds the candidate value that was being validated.) record. `candidate` (The candidate value that was beign validated.): access via [[active.clojure.validation/validation-success-candidate]]
(make-validator candidate predicate error-message & [label])
Takes a candidate
value and a predicate
the candidate will be
applied to. If (predicate candidate)
returns false, returns
a [[ValidationFailure]] with error-message
as
the [[ValidationError]], using label
as the label if provided.
Takes a `candidate` value and a `predicate` the candidate will be applied to. If `(predicate candidate)` returns false, returns a [[ValidationFailure]] with `error-message` as the [[ValidationError]], using `label` as the label if provided.
(mappend-validation-failure vf1 vf2)
mappend the validation-failure-errors
of
two [[ValidationFailure]]s.
mappend the [[validation-failure-errors]] of two [[ValidationFailure]]s.
(optional validate)
Takes a validation function validate
and returns a validation
function that accepts what validate
accepts plus nil
.
Takes a validation function `validate` and returns a validation function that accepts what `validate` accepts plus `nil`.
(override-error-labels e new-label)
override validation error labels with new-label
in each of e
's
error iff e
is a [[ValidationFailure]]
override validation error labels with `new-label` in each of `e`'s error iff `e` is a [[ValidationFailure]]
(override-error-messages e new-message)
override validation error messages with new-message
in each of
e
's error iff e
is a [[ValidationFailure]]
override validation error messages with `new-message` in each of `e`'s error iff `e` is a [[ValidationFailure]]
Lift a value into the validation applicative.
Lift a value into the validation applicative.
(seq-validation v-1 v-2)
Apply two validations sequentially, from left to right. Analogous
to Either
where ValidationFailure
is Left
and
ValidationSuccess
is Right
.
Apply two validations sequentially, from left to right. Analogous to `Either` where `ValidationFailure` is `Left` and `ValidationSuccess` is `Right`.
(sequence validation-results)
Takes a vector of validation results ([[ValidationSuccess]] or [[ValidationFailure]]) and returns a [[ValidationSuccess]] of all candidates as a vector iff all are [[ValidationSuccess]]. Else it returns a [[ValidationFailure]] with all errors accumulated.
Takes a vector of validation results ([[ValidationSuccess]] or [[ValidationFailure]]) and returns a [[ValidationSuccess]] of all candidates as a vector iff all are [[ValidationSuccess]]. Else it returns a [[ValidationFailure]] with all errors accumulated.
(sequence-of validation candidates & [label])
Takes a validation function and a sequence of candidates and validates each candidate and returns the combined result.
If any one validation fails, returns a
ValidationFailure
, containing all failures.
All failures' validation-error-label
s are prepended with a tuple
of label
if present (otherwise, defaults to ::seq
) and the index
of the value that could not be validated.
Takes a validation function and a sequence of candidates and validates each candidate and returns the combined result. If any one validation fails, returns a `ValidationFailure`, containing _all_ failures. All failures' [[validation-error-label]]s are prepended with a tuple of `label` if present (otherwise, defaults to `::seq`) and the index of the value that could not be validated.
(succeed candidate & [label])
Validator that always succeeds.
Validator that always succeeds.
(validate-all validations candidate & [label])
Takes a sequence of validations
and a candidate
and applies all
validations
to candidate
sequentially. Collects either
all [[ValidationFailure]]s or returns a [[ValidationSuccess]] for
the candidate.
Takes a sequence of `validations` and a `candidate` and applies all `validations` to `candidate` sequentially. Collects either all [[ValidationFailure]]s or returns a [[ValidationSuccess]] for the candidate.
(validate-boolean b & [label])
Validates that a candidate is a boolean
Validates that a candidate is a boolean
(validate-choice validators candidate & [label])
Takes a sequence of validation
functions and a candidate
and applies each validation function to the candidate
.
If validations
is empty, the validation will always fail with the
[::choice ::no-validators]
message.
If exactly one validation succeeds, returns a [[ValidationSuccess]]. Otherwise, returns a [[ValidationFailure]] with all failed validations.
All failures' validation-error-label
s are prepended with a tuple
of label
if present (otherwise, defaults to ::choice
) and the
index of the value that could not be validated.
Takes a sequence of `validation` functions and a `candidate` and applies each validation function to the `candidate`. If `validations` is empty, the validation will always fail with the `[::choice ::no-validators]` message. If exactly one validation succeeds, returns a [[ValidationSuccess]]. Otherwise, returns a [[ValidationFailure]] with all failed validations. All failures' [[validation-error-label]]s are prepended with a tuple of `label` if present (otherwise, defaults to `::choice`) and the index of the value that could not be validated.
(validate-int i & [label])
Validates that a candidate is an integer.
Validates that a candidate is an integer.
(validate-keyword k & [label])
Validates that a candidate is a boolean
Validates that a candidate is a boolean
(validate-list xs & [label])
Validates that a candidate is a list.
Validates that a candidate is a list.
(validate-map m & [label])
Validates that a candidate is a map.
Validates that a candidate is a map.
(validate-non-empty-string s & [label])
Validates that a candidate is a non-empty String.
Validates that a candidate is a non-empty String.
(validate-none-of elems k & [label])
Validates that a candidate is anything except one of elems
.
Validates that a candidate is anything except one of `elems`.
(validate-one-of elems k & [label])
Validates that a candidate is exatly one of elems
.
Validates that a candidate is exatly one of `elems`.
(validate-pos-int i & [label])
Validates that a candidate is a positive integer.
Validates that a candidate is a positive integer.
(validate-sequential s & [label])
Validates that a candidate is sequential.
Validates that a candidate is sequential.
(validate-set s & [label])
Validates that a candidate is a set.
Validates that a candidate is a set.
(validate-string s & [label])
Validates that a candidate is a String.
Validates that a candidate is a String.
(validate-vector xs & [label])
Validates that a candidate is a vector.
Validates that a candidate is a vector.
(validation make-result & validations)
Takes a result construtor function and a sequence of validations.
If all validations success, constructs a result from the validated
values via make-result
, wrapped in a ValidationSuccess
. The
arguments will be supplied to make-result
in the order in which
they were validated.
If any one validation fails, returns a ValidationFailure
,
containing all failures.
The number of arguments make-result
expects must match the (count validations
). Supplying a wrong number of arguments considered
undefined behaviour.
Takes a result construtor function and a sequence of validations. If all validations success, constructs a result from the validated values via `make-result`, wrapped in a `ValidationSuccess`. The arguments will be supplied to `make-result` in the order in which they were validated. If any one validation fails, returns a `ValidationFailure`, containing _all_ failures. The number of arguments `make-result` expects must match the `(count validations`). Supplying a wrong number of arguments considered undefined behaviour.
(validation-error-candidate rec__3650__auto__)
(validation-error-candidate data__3651__auto__ v__3652__auto__)
(validation-error-candidate rec__3306__auto__)
(validation-error-candidate data__3307__auto__ v__3308__auto__)
Lens for the candidate
field (The candidate value that was being validated.) from a [[ValidationError]] record. See active.clojure.validation/make-validation-error
.
Lens for the `candidate` field (The candidate value that was being validated.) from a [[ValidationError]] record. See [[active.clojure.validation/make-validation-error]].
(validation-error-label rec__3650__auto__)
(validation-error-label data__3651__auto__ v__3652__auto__)
(validation-error-label rec__3306__auto__)
(validation-error-label data__3307__auto__ v__3308__auto__)
Lens for the label
field (Arbitrary data that can be added to an error.) from a [[ValidationError]] record. See active.clojure.validation/make-validation-error
.
Lens for the `label` field (Arbitrary data that can be added to an error.) from a [[ValidationError]] record. See [[active.clojure.validation/make-validation-error]].
(validation-error-message rec__3650__auto__)
(validation-error-message data__3651__auto__ v__3652__auto__)
(validation-error-message rec__3306__auto__)
(validation-error-message data__3307__auto__ v__3308__auto__)
Lens for the message
field (A message signifying what kind of error occured. It should
be possible for the user to interpret the message as they please, so
usually a namespaced keyword representing the error works well.) from a [[ValidationError]] record. See active.clojure.validation/make-validation-error
.
Lens for the `message` field (A message signifying what kind of error occured. It should be possible for the user to interpret the message as they please, so usually a namespaced keyword representing the error works well.) from a [[ValidationError]] record. See [[active.clojure.validation/make-validation-error]].
(validation-error? thing)
Is object a ValidationError
record? See active.clojure.validation/make-validation-error
.
Is object a `ValidationError` record? See [[active.clojure.validation/make-validation-error]].
(validation-failure-errors rec__3650__auto__)
(validation-failure-errors data__3651__auto__ v__3652__auto__)
(validation-failure-errors rec__3306__auto__)
(validation-failure-errors data__3307__auto__ v__3308__auto__)
Lens for the errors
field (A sequence of [[ValidationError]] that lead to the failed
validation.) from a [[ValidationFailure]] record. See active.clojure.validation/make-validation-failure
.
Lens for the `errors` field (A sequence of [[ValidationError]] that lead to the failed validation.) from a [[ValidationFailure]] record. See [[active.clojure.validation/make-validation-failure]].
(validation-failure? thing)
Is object a ValidationFailure
record? See active.clojure.validation/make-validation-failure
.
Is object a `ValidationFailure` record? See [[active.clojure.validation/make-validation-failure]].
(validation-result? thing)
Checks if thing
is a validation result.
Checks if `thing` is a validation result.
(validation-success-candidate rec__3650__auto__)
(validation-success-candidate data__3651__auto__ v__3652__auto__)
(validation-success-candidate rec__3306__auto__)
(validation-success-candidate data__3307__auto__ v__3308__auto__)
Lens for the candidate
field (The candidate value that was beign validated.) from a [[ValidationSuccess]] record. See active.clojure.validation/make-validation-success
.
Lens for the `candidate` field (The candidate value that was beign validated.) from a [[ValidationSuccess]] record. See [[active.clojure.validation/make-validation-success]].
(validation-success? thing)
Is object a ValidationSuccess
record? See active.clojure.validation/make-validation-success
.
Is object a `ValidationSuccess` record? See [[active.clojure.validation/make-validation-success]].
(with-validation-result f-success f-failure result)
Takes a validation result
and applies f-success
to the whole
result if it is a [[ValidationSuccess]], otherwise applies
f-failure
to the whole [[ValidationFailure]].
Takes a validation `result` and applies `f-success` to the whole result if it is a [[ValidationSuccess]], otherwise applies `f-failure` to the whole [[ValidationFailure]].
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close