Liking cljdoc? Tell your friends :D

active.clojure.validation

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})}
```
raw docstring

and-thenclj/s

(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]].
sourceraw docstring

curry-nclj/s

(curry-n f n)

Curry a function f of arity n.

Curry a function `f` of arity `n`.
sourceraw docstring

fmap-failureclj/s

(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]]
sourceraw docstring

fmap-resultclj/s

(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]]
sourceraw docstring

fmap-successclj/s

(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]].
sourceraw docstring

make-validation-errorclj/s

(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]]
sourceraw docstring

make-validation-failureclj/s

(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]]
sourceraw docstring

make-validation-successclj/s

(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]]
sourceraw docstring

make-validatorclj/s

(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.
sourceraw docstring

mappend-validation-failureclj/s

(mappend-validation-failure vf1 vf2)

mappend the validation-failure-errors of two [[ValidationFailure]]s.

mappend the [[validation-failure-errors]] of
two [[ValidationFailure]]s.
sourceraw docstring

optionalclj/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`.
sourceraw docstring

override-error-labelsclj/s

(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]]
sourceraw docstring

override-error-messagesclj/s

(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]]
sourceraw docstring

pure-validationclj/s

Lift a value into the validation applicative.

Lift a value into the validation applicative.
sourceraw docstring

seq-validationclj/s

(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`.
sourceraw docstring

sequenceclj/s

(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. 
sourceraw docstring

sequence-ofclj/s

(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-labels 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.
sourceraw docstring

succeedclj/s

(succeed candidate & [label])

Validator that always succeeds.

Validator that always succeeds.
sourceraw docstring

validate-allclj/s

(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.
sourceraw docstring

validate-booleanclj/s

(validate-boolean b & [label])

Validates that a candidate is a boolean

Validates that a candidate is a boolean
sourceraw docstring

validate-choiceclj/s

(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-labels 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.
sourceraw docstring

validate-intclj/s

(validate-int i & [label])

Validates that a candidate is an integer.

Validates that a candidate is an integer.
sourceraw docstring

validate-keywordclj/s

(validate-keyword k & [label])

Validates that a candidate is a boolean

Validates that a candidate is a boolean
sourceraw docstring

validate-listclj/s

(validate-list xs & [label])

Validates that a candidate is a list.

Validates that a candidate is a list.
sourceraw docstring

validate-mapclj/s

(validate-map m & [label])

Validates that a candidate is a map.

Validates that a candidate is a map.
sourceraw docstring

validate-non-empty-stringclj/s

(validate-non-empty-string s & [label])

Validates that a candidate is a non-empty String.

Validates that a candidate is a non-empty String.
sourceraw docstring

validate-none-ofclj/s

(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`.
sourceraw docstring

validate-one-ofclj/s

(validate-one-of elems k & [label])

Validates that a candidate is exatly one of elems.

Validates that a candidate is exatly one of `elems`.
sourceraw docstring

validate-pos-intclj/s

(validate-pos-int i & [label])

Validates that a candidate is a positive integer.

Validates that a candidate is a positive integer.
sourceraw docstring

validate-sequentialclj/s

(validate-sequential s & [label])

Validates that a candidate is sequential.

Validates that a candidate is sequential.
sourceraw docstring

validate-setclj/s

(validate-set s & [label])

Validates that a candidate is a set.

Validates that a candidate is a set.
sourceraw docstring

validate-stringclj/s

(validate-string s & [label])

Validates that a candidate is a String.

Validates that a candidate is a String.
sourceraw docstring

validate-vectorclj/s

(validate-vector xs & [label])

Validates that a candidate is a vector.

Validates that a candidate is a vector.
sourceraw docstring

validationclj/s

(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.
sourceraw docstring

validation-error-candidateclj/s≠

clj
(validation-error-candidate rec__3650__auto__)
(validation-error-candidate data__3651__auto__ v__3652__auto__)
cljs
(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]].
sourceraw docstring

validation-error-labelclj/s≠

clj
(validation-error-label rec__3650__auto__)
(validation-error-label data__3651__auto__ v__3652__auto__)
cljs
(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]].
sourceraw docstring

validation-error-messageclj/s≠

clj
(validation-error-message rec__3650__auto__)
(validation-error-message data__3651__auto__ v__3652__auto__)
cljs
(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]].
sourceraw docstring

validation-error?clj/s

(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]].
sourceraw docstring

validation-failure-errorsclj/s≠

clj
(validation-failure-errors rec__3650__auto__)
(validation-failure-errors data__3651__auto__ v__3652__auto__)
cljs
(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]].
sourceraw docstring

validation-failure?clj/s

(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]].
sourceraw docstring

validation-result?clj/s

(validation-result? thing)

Checks if thing is a validation result.

Checks if `thing` is a validation result.
sourceraw docstring

validation-success-candidateclj/s≠

clj
(validation-success-candidate rec__3650__auto__)
(validation-success-candidate data__3651__auto__ v__3652__auto__)
cljs
(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]].
sourceraw docstring

validation-success?clj/s

(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]].
sourceraw docstring

with-validation-resultclj/s

(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]].
sourceraw docstring

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close