Liking cljdoc? Tell your friends :D

params-helper

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 ..."}]}
  • mop-fields opts ^map


keyvalue / defaultdescription
:ignore-uuidboolean / falseignores string to uuid conversion, making the algorithm a bit faster
  • Examples:
  {:ignore-uuid true}

Symbols

  • uuid-pattern

    UUID string regex

Functions

  • extract-field-value [field body]

    gets value from the body using field ks, converting uuid's from string to UUID if needed

    - field ^ks : field to be extracted from a map
    - body ^map : map where the field will be extracted
    - returns ? : any value from the map
    (extract-field-value :name {:name "Rosa"})
    ;=> "Rosa"
    (extract-field-value :id {:id "53bd29d3-9b41-4550-83cc-f970d49da04d"}) 
    ;=> #uuid "53bd29d3-9b41-4550-83cc-f970d49da04d"
  • is-uuid [id]

    if id param is a string, checks if it matches uuid regex, otherwise returns false

    - id ^string : string to be checked against [uuid pattern](#uuid-pattern)
    - returns ^boolean : if is a string and an uuid or not
    (is-uuid "53bd29d3-9b41-4550-83cc-f970d49da04d") ;=> true
  • mop-fields [body fields]

    Clean the body removing values not present in fields param

    - body ^map : map to be cleaned
    - fields [^string]: string collection with the name of the allowed fields in the map
    - opts [^opts-def?](#mop-fields-opts-def): optional options - **returns** *^map* : cleaned map
    (mop-fields {:name "Rosa" :age 41} ["name"]) 
    ;=> {:name "Rosa"} 
  • uuid

    returns a new random UUID

    - returns ^uuid : a random uuid
    (uuid) ;=> #uuid "53bd29d3-9b41-4550-83cc-f970d49da04d"
  • uuid-as-string [uuid]

    converts uuid into a string

    - uuid ^uuid : uuid to be converted to string
    - returns ^string : uuid as a string
    (uuid-as-string (uuid)) ;=> "53bd29d3-9b41-4550-83cc-f970d49da04d"
  • validate-and-mop!! [body mandatory accepted & field-message = "Field %s is not present"]

    Validates and clean body by executing [validate-mandatory](#validate-mandatory) and `mop-fields`

    - body ^uuid : uuid to be converted to string
    - mandatory [^string] | [^validation-def](#validation-def) : either coll of strings or map following [^validation-def](#validation-def) specs. For coll of strings, mandatory validation is triggered by default, other validations require the map
    - accepted [^string] : collection of strings having accepted keys on body, the others will be removed
    - field-message ^string? : optional argument to customize message, used only when mandatory argument is coll of strings
    - returns ^map : filtered and validated body
    - throws ^ExceptionInfo : exception info with data having bad format type and validation-messages for each field
   (validate-and-mop!! {:name "Rosa" :extra 6} ["name"] ["name"])
   ;=> {:name "Rosa"}
   (validate-and-mop!! {:name "Rosa"} ["age"] ["name" "age"])
   ;=> ExceptionInfo thrown => ExceptionInfo{data {:type :bad-format
   ;                                               :validation-messages [{:field "age"
   ;                                                                      :message "Field :age is not present"}]}}
   (validate-and-mop!! 
        {:name "Rosa" :age 17}
        {"age" [{:validate/type :validate/min, :validate/value 18}]}
        ["name" "age"])
   ;=> ExceptionInfo thrown => ExceptionInfo{data {:type :bad-format
   ;                                               :validation-messages [{:field "age"
   ;                                                                      :message "Field age must have a minimum size of 18"}]}}
  • validate-mandatory [body fields & message-untranslated = "Field %s is not present"]

    checks if body map has mandatory keys, if not, throws an exception containing all missing fields in `ExceptionInfo` `.getData :validation-messages`

    - body ^uuid : uuid to be converted to string
    - fields [^string] : coll of strings being the mandatory keys for the body
    - message-untranslated* ^string? : optional argument to customize message, used only when mandatory argument is coll of strings
    - returns ^boolean : true when validation succeeds
    - throws ^ExceptionInfo : exception info with data having bad format type and validation-messages for each field
    (validate-mandatory {:name "Rosa"} ["name"]) 
    ;=> true
    (validate-mandatory {} ["name"]) 
    ;=> ExceptionInfo thrown => ExceptionInfo{data {:type :bad-format
    ;                                               :validation-messages [{:field "name"
    ;                                                                      :message "Field :name is not present"}]}}
    (validate-mandatory {} ["name"] "Field %s has a custom message") 
    ;=> ExceptionInfo thrown => ExceptionInfo{data {:type :bad-format
    ;                                               :validation-messages [{:field "name"
    ;                                                                      :message "Field :name has a custom message"}]}}

Can you improve this documentation?Edit on GitHub

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

× close