Liking cljdoc? Tell your friends :D

validation

Index

Definitions

Patterns for some maps passed to the functions as arguments:

  • validation-def ^map


keyvaluedescriptionmandatory extra keyoptional extra key
:validate/type:validate/mandatoryvalidates if field is present-:validate/message ^string with %s being field name
:validate/type:validate/maxvalidates if max is not reached:validate/value ˆint:validate/message ^string with 1st %s being field name, 2nd being max value
:validate/type:validate/minvalidates if at least min value is reached:validate/value ˆint:validate/message ^string with 1st %s being field name, 2nd being min value
:validate/type:validate/regexvalidates if string matches regex pattern:validate/value ˆregex:validate/message ^string with %s being field name
:validate/type:validate/customvalidates if custom fn receiving the value returns true:validate/value ^fn:validate/message ^string with %s being field name

- Examples:

      {"field-name"
        [{:validate/type :validate/mandatory, :validate/message "%s is ..."},
         {:validate/type :validate/min, :validate/value 12, :validate/message "%s is mandatory"},
         {:validate/type :validate/max, :validate/value 40, :validate/message "%s is ..."},
         {:validate/type :validate/regex, :validate/value #"^[\d]{1,2}$", :validate/message "%s is ..."},
         {:validate/type :validate/custom, :validate/value fn, :validate/message "% is ..."}]}

Symbols

Functions

  • validate [body fields]

    validates the body based on a map following [validation-def](#validation-def)

    - body ^map : map where the field will be extracted
    - fields ^map : map following [validation-def](#validation-def) to perform validations over the body
    - returns boolean : true if validations succeed
    - throws ^ExceptionInfo : exception info with data having bad format type and validation-messages for each field
   (validate 
        {:name "Rosa"} 
        {"age" [{:validate/type :validate/mandatory}]})
   ;=> ExceptionInfo thrown => ExceptionInfo{data {:type :bad-format
   ;                                               :validation-messages [{:field "age"
   ;                                                                      :message "Field age is not present"}]}}
   (validate 
        {:name "Rosa" :age 17}
        {"age" [{:validate/type :validate/min, :validate/value 18}]})
   ;=> ExceptionInfo thrown => ExceptionInfo{data {:type :bad-format
   ;                                               :validation-messages [{:field "age"
   ;                                                                      :message "Field age must have a minimum size of 18"}]}}

Can you improve this documentation?Edit on GitHub

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

× close