Skip to content

Change standard validator behaviour w.r.t. nil/blank handling #37

@lorddoig

Description

@lorddoig

With regard to the following example on this page.

(ns my.app
  (:require [validateur.validation :refer [validation-set presence-of format-of]]))

(let [v (validation-set
         (presence-of :user-name)
         (format-of :user-name
                    :format #"^[^\s]*$"
                    :message "may not contain whitespace"))]
  (v {:user-name "99 bananas"}))
;= {:user-name #{"may not contain whitespace"}}

This gives the impression that presence and format are separate validations when, in reality, the format-of test is also going to check for presence meaning both validations are pushing "can't be blank." into the errors set. This isn't noticeable in this example because of the use of sets: it only shows up once.

However, if one wishes to make this change to the above example:

    (presence-of :user-name
                 :message "Your username can't be blank.")

then this happens:

(v {:user-name ""})
; => {:user-name  #{"Your username can't be blank." "can't be blank."}}

I honestly just spent the best part of 2 hours debugging this, before I realised I was debugging the wrong functions.

I'm scrolling through the source here and it seems like pretty much every -of function has an allow-blank or allow-nil option that defaults to false and has it's own, baked in default for blank-message of "can't be blank." So if you wish to combine them with a non-default blank-message you either have to a) set allow-blank to true on all but one (which both obscures intent and raises the probability of a refactoring disaster); or b) duplicate your desired blank message across them all.

The -of methods returning function closures limits the options for solving this a bit - the only thing I can think of is making validation-set a macro that looks for presence-of calls and rewrites the other functions as necessary, but this seems uber-kludgy. Sorry for being all problems and no solutions.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions