clova.core

The core namespace contains all of the built in validators and the defvalidator macro.

The only other public functions are

  • validation-set
  • validate
  • valid?
  • results
  • as-validator

You can view more information in the readme. There are also example validation scenarios here.

The API documentation is available here.

You can also view blog posts about clova.

=?

added in 0.22.0

(=? value v)

Check an input value to see if it is equal to v using the regular = function.

It is worthwhile using =date? for validating date equality, since it has support for parsing string dates and comparing DateTime objects.

=date?

added in 0.21.0

(=date? value d & [opt])

Check an input value to see if it is chronoligically equal to d. Where d is either the string representation of a date or one of [java.util.Date org.joda.time.DateTime] or [js/Date goog.date.Date goog.date.DateTime]

Optionally, takes a map argument and makes use of the following keys:

  • :formatter You can use one of the built in ISO8601 formatters from clj-time or cljs-time. You can also define your own custom format string.

after?

added in 0.20.0

(after? value d & [opt])

Check an input value to see if it is chronoligically after d. Where d is either the string representation of a date or one of [java.util.Date org.joda.time.DateTime] or [js/Date goog.date.Date goog.date.DateTime]

Optionally, takes a map argument and makes use of the following keys:

  • :formatter You can use one of the built in ISO8601 formatters from clj-time or cljs-time. You can also define your own custom format string.

all?

added in 0.9.0

(all? value col)

Check an input value to see if it matches a given collection of predicates. Predicates can be concrete values or they can be functions, they can be single items or collections.

(all? true [true (fn [v] (= true v))]) (all? true (fn [v] (= true v)))

alphanumeric?

added in 0.28.0

(alphanumeric? value)

Checks an input value to see if it is alphanumeric. Alphanumeric is defined as [a-zA-Z0-9*$], if you need to customise this you can use the matches? validator with a custom regex.

as-validator

(as-validator f)(as-validator f {:keys [default-message allow-missing-key?], :or {default-message "%s is %s but this is not a valid value.", allow-missing-key? true}, :as m})

Takes a function f and applies optional m as meta data around it. f should be accept a first argument as the value to validate.

When m is present the following keys are taken and used as meta data to declare a validator:

  • :default-message The default message template to be used when validation fails.
  • :allow-missing-key? Should validation fail if the :target key is not present.

When m is not specified suitable defaults are used.

before?

added in 0.19.0

(before? value d & [opt])

Check an input value to see if it is chronoligically before d. Where d is either the string representation of a date or one of [java.util.Date org.joda.time.DateTime] or [js/Date goog.date.Date goog.date.DateTime]

Optionally, takes a map argument and makes use of the following keys:

  • :formatter You can use one of the built in ISO8601 formatters from clj-time or cljs-time. You can also define your own custom format string.

between?

added in 0.2.0

(between? value lower upper)

Checks an input value to see if it is between lower and upper.

credit-card?

added in 0.11.0

(credit-card? value)

Chacks an input value to see if it is a “valid” credit card number based on the Luhn algorithm.

date?

added in 0.18.0

(date? value & [opt])

Checks an input value to see if it is a date.

If value is a string then it is parsed using clj-time or cljs-time.

If value is not a string but is one of [java.util.Date org.joda.time.DateTime] or [js/Date goog.date.Date goog.date.DateTime] then it will be considered a date?.

Optionally, takes a map argument and makes use of the following keys:

  • :formatter You can use one of the built in ISO8601 formatters from clj-time or cljs-time. You can also define your own custom format string.

defvalidator

macro

(defvalidator doc-string fname validator-meta-data args & body)

Wraps body in a function and defines it with meta data used to support the validation process. Works using a similar pattern to defn.

email?

added in 0.2.0

(email? value)

Checks an input value to see if it is a valid email address

exists?

added in 0.32.0

(exists? value col)

Checks for the presence of an item in a collection.

greater?

added in 0.2.0

(greater? value lower)

Checks an input value to see if it is greater than lower.

length?

added in 0.5.0

(length? value l)

Check an input value to see if it has a length equal to l. Work on sequences and strings.

lesser?

added in 0.2.0

(lesser? value lower)

Checks an input value to see if it is less than lower.

longer?

added in 0.6.0

(longer? value l)

Check an input value to see if it has a length longer than l. Work on sequences and strings.

matches?

added in 0.2.0

(matches? value regex)

Checks a string representation of value against regex and returns true if value matches the regex. If value is not a match then returns nil.

negative?

added in 0.4.0

(negative? value)

Checks an input value to see if it is a negative number.

not-exists?

added in 0.31.0

(not-exists? value col)

Checks for the non presence of an item in a collection.

not-nil?

added in 0.2.0

(not-nil? value)

Checks for the presence of a non nil value.

numeric?

added in 0.14.0

(numeric? value)

Checks an input value to see if it is numeric.

one-of?

added in 0.2.0

(one-of? value col)

Checks an input value to see if it is one of the items in a col

positive?

added in 0.4.0

(positive? value)

Checks an input value to see if it is a positive number.

post-code?

added in 0.2.0

(post-code? value)

Checks an input value to see if it is a valid uk post code.

required?

added in 0.8.0

(required? value)

Checks for the presence of a key based on the default value of ::key-not-found? for a missing key.

results

(results v-set m)

Takes a validation set and applies it to m. This is just a shorthand method over the validate function and returns only the validation results.

shorter?

added in 0.6.0

(shorter? value l)

Check an input value to see if it has a length shorter than l. Work on sequences and strings.

stringy?

added in 0.15.0

(stringy? value)

Checks an input value to see if it is a string

url?

added in 0.2.0

(url? value)

Checks an input value to see if it is a valid url.

valid?

(valid? v-set m)

Takes a validation set and applies it to m. This is just a shorthand method over the validate function and returns only a truthy or falsy value indicating the validation status.

validate

(validate v-set m)(validate v-set m {:keys [default-message-fn short-circuit?], :or {default-message-fn (fn [v-type value args] nil), short-circuit? false}})

Takes a validation set and applies it to m. Returns a map containing :valid? with either a truthy or falsy value and :results which is a sequence of validation failure messages, if applicable.

Optionally takes a map of options:

:default-message-fn can be specified to override the default validation messages. If specified the function will be called and receive the validator type as an argument. If the result of calling the function is anything but nil it will be used as the default validation message.

:short-circuit? when true no further validators for the validation set will be processed. The default is false and therefore to process all validators.

validation-set

(validation-set col)

Takes a sequence (col) that represents keys to validate and the functions used to validate them.

e.g. [:email email? :post-code post-code?]

It is also possible to specify keys to traverse nested maps.

e.g. using [[:user :credentials :name] [matches? #"someregex"]] we can define a validation function to target the :name key in a map like {:user {:credentials {:name "username" }}}

Returns a sequence of functions merged with meta data used by the validation.

zip-code?

added in 0.2.0

(zip-code? value)

Checks an input value to see if it is a valid zip code.