From a6dc150291f27ae2b8e4bd7775b0bca96f0f15c5 Mon Sep 17 00:00:00 2001 From: Lance Clark Date: Tue, 10 May 2016 21:00:03 -0500 Subject: [PATCH] Add new validator 'alphanumeric' and its test case --- src/bouncer/validators.cljx | 9 +++++++++ test/bouncer/validators_test.cljx | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/src/bouncer/validators.cljx b/src/bouncer/validators.cljx index 41fbd8f..a8e42ee 100644 --- a/src/bouncer/validators.cljx +++ b/src/bouncer/validators.cljx @@ -199,3 +199,12 @@ {:default-message-format "%s is less than the minimum"} [value minimum] (>= (count (seq value)) minimum)) + + +(defvalidator alphanumeric + "Validates that value is alphanumeric + + For use with validation functions such as `validate` or `valid?`" + {:default-message-format "%s is not alphanumeric"} + [value] + (matches value #"^[a-zA-Z0-9]+$")) diff --git a/test/bouncer/validators_test.cljx b/test/bouncer/validators_test.cljx index 607fbd7..f1f30b9 100644 --- a/test/bouncer/validators_test.cljx +++ b/test/bouncer/validators_test.cljx @@ -212,3 +212,12 @@ (is (core/valid? {:a-vector [1 2 3]} :a-vector [[v/min-count 3]])) (is (core/valid? {:a-list '(1 2 3)} :a-list [[v/min-count 3]])) (is (not (core/valid? {:a-map {:city "Atlanta" :state "Georgia"}} :a-map [[v/min-count 3]])))))) + +(deftest alphanumeric-validator + (testing "enforcing alphanumeric values" + (testing "with strings" + (is (core/valid? {:string "a3lcien209sta2un48"} :string v/alphanumeric)) + (is (not (core/valid? {:string "a193r8l~!@#`"} :string v/alphanumeric))) + (is (not (core/valid? {:string "?>