theleoborges / bouncer

A validation DSL for Clojure & Clojurescript applications
364 stars 38 forks source link

v/matches implies v/required? #40

Closed jonase closed 8 years ago

jonase commented 9 years ago

I'm not sure if this is intended behaviour but

(bouncer/validate {}
                  :id [v/string [v/matches #"a*"]])

evaluates to

[{:id ("id must be a string")} {:bouncer.core/errors {:id ("id must be a string")}}]

I would have expected the validation to pass since :id is not v/required.

If this is expected how do I create a validator where an optional string value must match a regex only if the key exists?

Edit After some digging it's actually v/string that makes the key-value non-optional. This was a surprise for me

theleoborges commented 8 years ago

This has been changed on #41 so bouncer will behave like this:

user=> (b/validate {} :id [v/string [v/matches #"a*"]])
[nil {}]
user=> (b/validate {} :id [v/required v/string [v/matches #"a*"]])
[{:id ("id must be present")} {:bouncer.core/errors {:id ("id must be present")}}]

I've just released a new version containing this change. Give that a go and let me know if you run into issues.