Skip to content

clojure.core/set-validator!

View this page on ClojureDocs

Type: function Added: Clojure 1.0 Examples: 2 Runnable: 0
([iref validator-fn])

Sets the validator-fn for a var/ref/agent/atom. validator-fn must be nil or a side-effect-free fn of one argument, which will be passed the intended new state on any state change. If the new state is unacceptable, the validator-fn should return false or throw an exception. If the current state (root value if var) is not acceptable to the new validator, an exception will be thrown and the validator will not be changed.

Examples

by nickbauman on . May have evaluation errors.
by beoliver on . May have evaluation errors.

Note by TimMc

If you want your validator to throw an exception with a useful message, make sure it is a RuntimeException (or subclass), otherwise ARef#validate will throw an IllegalStateException with a generic message.

See also


Content from the matching ClojureDocs page, with authors credited on each contribution.