Tools for validating forms and their fields with https://github.com/logaan/vlad.
If you wish to use this namespace, you must provide vlad
in your own
dependencies. If you want to validate with another library, do not load this
namespace.
Tools for validating forms and their fields with https://github.com/logaan/vlad. If you wish to use this namespace, you must provide `vlad` in your own dependencies. If you want to validate with another library, do not load this namespace.
(assign-names errors field-labels)
Helper: augments errors
with field labels. These labels will be used to
generate the error messages.
The field-labels
work in coordination with f-form.validation.vlad/field
.
Suppose you have the following:
(def form
(form/init [(field/init [:address/street])]))
(def validation
(f-form.validation.vlad/field [:address/street]
(f-form.validation.vlad/non-nil)))
(def field-labels
{[:address/street] "Street"})
Then:
(f-form.validation.vlad/validate form validation field-labels)
;; => {:form/fields {:address/street {:field/errors ["Street is required."] ,,,}
;; :form/fields-valid? false}
This function has special handling for complex fields. Suppose you are collecting a birthday, like so:
(def form
(form/init [(field/init [:person/birthday]
{:date/month nil
:date/day nil})]))
(def validation
(f-form.validation.vlad/field [:person/birthday]
(vlad/chain
(vlad/attr [:date/month] (vlad/present))
(vlad/attr [:date/year] (vlad/present)))))
The first option is to label the field as a whole:
(def field-labels
{[:person/birthday] "Birthday"})
Then validation will be translated like so:
(f-form.validation.vlad/validate form validation field-labels)
;; => {:form/fields {:person/birthday {:field/errors ["Birthday is required."] ,,,}
;; :form/fields-valid? false}
Alternatively, if the individual components of a complex-valued field need their own labels, they can be provided like so:
(def field-labels
{[:person/birthday] {[:date/month] "Birth month"
[:date/year] "Birth year"}})
(f-form.validation.vlad/validate form validation field-labels)
;; => {:form/fields {:person/birthday {:field/errors ["Birth month is required."]}
;; :form/fields-valid? false}
_Helper:_ augments `errors` with field labels. These labels will be used to generate the error messages. The `field-labels` work in coordination with [[f-form.validation.vlad/field]]. Suppose you have the following: ``` clojure (def form (form/init [(field/init [:address/street])])) (def validation (f-form.validation.vlad/field [:address/street] (f-form.validation.vlad/non-nil))) (def field-labels {[:address/street] "Street"}) ``` Then: ``` clojure (f-form.validation.vlad/validate form validation field-labels) ;; => {:form/fields {:address/street {:field/errors ["Street is required."] ,,,} ;; :form/fields-valid? false} ``` This function has special handling for complex fields. Suppose you are collecting a birthday, like so: ``` clojure (def form (form/init [(field/init [:person/birthday] {:date/month nil :date/day nil})])) (def validation (f-form.validation.vlad/field [:person/birthday] (vlad/chain (vlad/attr [:date/month] (vlad/present)) (vlad/attr [:date/year] (vlad/present))))) ``` The first option is to label the field as a whole: ``` clojure (def field-labels {[:person/birthday] "Birthday"}) ``` Then validation will be translated like so: ``` clojure (f-form.validation.vlad/validate form validation field-labels) ;; => {:form/fields {:person/birthday {:field/errors ["Birthday is required."] ,,,} ;; :form/fields-valid? false} ``` Alternatively, if the individual components of a complex-valued field need their own labels, they can be provided like so: ```clojure (def field-labels {[:person/birthday] {[:date/month] "Birth month" [:date/year] "Birth year"}}) (f-form.validation.vlad/validate form validation field-labels) ;; => {:form/fields {:person/birthday {:field/errors ["Birth month is required."]} ;; :form/fields-valid? false} ```
(english-translation error)
Helper: Takes an error and returns a human readable version of it.
Adapted from vlad.core/english-translation to account for f-form specific errors and urgency levels.
_Helper:_ Takes an error and returns a human readable version of it. Adapted from [vlad.core/english-translation](https://github.com/logaan/vlad/blob/1dd8d427578655148e5333b3e40f0fe0223ee5eb/src/vlad/core.cljc#L224) to account for f-form specific errors and urgency levels.
(field field-path)
(field field-path validation)
Validation wrapper: Runs a validation
on the :field/value
of a field
whose :field/path
matches the given field-path
.
_Validation wrapper:_ Runs a `validation` on the `:field/value` of a field whose `:field/path` matches the given `field-path`.
(is-inst)
(is-inst error-data)
Validator: Checks that the value is an instant.
_Validator:_ Checks that the value is an instant.
(is-uuid)
(is-uuid error-data)
Validator: Checks that the value is a uuid.
_Validator:_ Checks that the value is a uuid.
(non-nil)
(non-nil error-data)
Validator: Checks that the value is not nil.
_Validator:_ Checks that the value is not nil.
(not-pristine)
(not-pristine error-data)
Validator: Checks that the field is not pristine. Should be used on a field, not a field's value, so:
;; do this
(vlad/attr [:postal-code] (form.validation/not-pristine))
;; not this
(form.validation/field [:postal-code] (form.validation/not-pristine))
_Validator:_ Checks that the field is not pristine. Should be used on a field, not a field's value, so: ```clojure ;; do this (vlad/attr [:postal-code] (form.validation/not-pristine)) ;; not this (form.validation/field [:postal-code] (form.validation/not-pristine)) ```
(pos-number)
(pos-number error-data)
Validator: Checks that the value is a positive number.
_Validator:_ Checks that the value is a positive number.
(urgency level validation)
Validation wrapper: Sets the urgency for any errors generated by validation
to level
.
_Validation wrapper:_ Sets the urgency for any errors generated by `validation` to `level`.
(validate form validation field-labels)
(validate form validation field-labels translation)
Core: Applies a validation
to a form
. Adds :field/errors
and
:field/warnings
to invalid fields. Updates the whole form's
:form/fields-valid?
depending on whether there are any :field/errors
.
Similar to vlad.core/field-errors, but customized for dealing with forms.
Errors and warnings are vectors of English strings, translated using the
field-labels
(according to assign-names
) and
vlad.core/translate-errors
using the translation
(by default english-translation
).
See vlad.core/translate-errors for additional detail about error message generation.
_Core:_ Applies a `validation` to a `form`. Adds `:field/errors` and `:field/warnings` to invalid fields. Updates the whole form's `:form/fields-valid?` depending on whether there are any `:field/errors`. Similar to [vlad.core/field-errors](https://github.com/logaan/vlad/blob/1dd8d427578655148e5333b3e40f0fe0223ee5eb/src/vlad/core.cljc#L282), but customized for dealing with forms. Errors and warnings are vectors of English strings, translated using the `field-labels` (according to [[assign-names]]) and [vlad.core/translate-errors](https://github.com/logaan/vlad/blob/1dd8d427578655148e5333b3e40f0fe0223ee5eb/src/vlad/core.cljc#L260) using the `translation` (by default [[english-translation]]). See [vlad.core/translate-errors](https://github.com/logaan/vlad/blob/1dd8d427578655148e5333b3e40f0fe0223ee5eb/src/vlad/core.cljc#L260) for additional detail about error message generation.
(value)
(value validation)
Validation wrapper: Runs a validation
on the :field/value
of the field
specified in the surrounding attr. In most cases you should prefer field
.
_Validation wrapper:_ Runs a `validation` on the `:field/value` of the field specified in the surrounding attr. In most cases you should prefer [[field]].
(value-in low high)
(value-in low high error-data)
Validator: Checks that the value is over low
and under high
, inclusive of both. No
checking is done that low
is lower than high
.
_Validator:_ Checks that the value is over `low` and under `high`, inclusive of both. No checking is done that `low` is lower than `high`.
(warning validation)
Validation wrapper: Sets the urgency for any errors generated by validation
to ::warning
. This controls whether validate
puts the errors on
:field/errors
or :field/warnings
and influences the translation of the
error.
_Validation wrapper:_ Sets the urgency for any errors generated by `validation` to `::warning`. This controls whether [[validate]] puts the errors on `:field/errors` or `:field/warnings` and influences the translation of the error.
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close