-
Notifications
You must be signed in to change notification settings - Fork 18
Validation
Arkenstone provides some basic model validations and allows you to create custom ones as well. To enable validations in your class, include the Arkenstone::Validation module and then add validation directives in the class definition. Arkenstone Validations can even be used on non-Arkenstone classes.
class Widget
include Arkenstone::Document
include Arkenstone::Validation
url 'http://example.com/widgets'
attributes :name, :size, :sku
validates :name, presence: true
end
class Ball
include Arkenstone::Validation
attr_accessor :radius
validates :radius, presence: true
endThe Arkenstone::Validation module adds a bunch of instance and class methods to the including class. This allows you to check if an object is valid or not.
widg = Widget.new
widg.valid? # false, since a name is required
widg.name = 'Framistat'
widg.valid? # trueArkenstone comes with a few common validators already built-in. All validators take an options hash as an optional parameter. This allows you to customize the error message it returns.
validates :name, presence: true, message: 'No really, you need a name'This validator will fail if the supplied property does not exist, if its value is nil, or if it is an empty string.
Similar to presence, but geard towards Strings. Will fail if the value is nil or empty.
This validator will fail if the supplied property does match the value specified by the directive.
validates :accept_t_and_c, acceptance: true
validates :is_assassin, acceptance: falseChecks if the property is a specific type. This uses is_a? to check, so subclasses will pass.
validates :name, type: StringChecks if the property's value matches the supplied regex. It is recommended that you provide a custom message here since Arkenstone can't read your mind about what this regex is testing.
validates :number_of_fleas, format: { with: /\d+/, message: "Must be a number." }This validator checks that the value of the property matches one that follows the supplied one with a _confirmation.
attr_accessor :password, :password_confirmation
validates :password, confirmation: true
widg.password = 'swordfish'
widg.password_confirmation = 'hunter2'
widg.valid? # false
widg.password = 'hunter2'
widg.valid? # trueIf those don't cover all of your bases, you can create custom validation methods for your classes.
class Widget
include Arkenstone::Document
include Arkenstone::Validation
url 'http://example.com/widgets'
attributes :number
validate :numberwang
def numberwang
errors.add(:number, "That's numberwang!") unless [3, 19, 333].include? @number
end
endWhen validations are run, the numberwang method will be called. If your custom check fails, call errors.add and supply a property name and error message.
An object that includes the Arkenstone::Validation module can check its validation by calling validate or valid?. The former just runs all the validation method, the latter does as well, and then returns a boolean if there were no errors.