module Quartz::Verifiable
Overview
Provides a runtime verification framework for your models.
Example:
class WeightModel
include Quartz::Verifiable
property weight : Float64 = 0.0 # in kg
check :weight, numericality: {greater_than: 40, lesser_than: 160}
end
model = WeightModel.new
model.weight = 75.0
model.valid? # => true
model.invalid? # => false
model.weight = 200.0
model.valid? # => false
model.invalid? # => true
model.errors.messages # => { :weight => ["must be lesser than 160"] }
Direct including types
Defined in:
quartz/verifiable.crInstance Method Summary
-
#clear_errors
Clears attribute error messages.
-
#errors
Returns the
VerificationErrors
object that holds all information about attribute error messages. -
#invalid?(context : Symbol? = nil) : Bool
Performs the opposite of
#valid?
. -
#valid?(context : Symbol? = nil) : Bool
Runs all the specified verifications and returns true if no errors were added otherwise false.
Instance Method Detail
def errors
#
Returns the VerificationErrors
object that holds all information about
attribute error messages.
def invalid?(context : Symbol? = nil) : Bool
#
Performs the opposite of #valid?
. Returns true if errors were added,
false otherwise.
Usage:
class MyModel
include Quartz::Verifiable
property :phase : String?
check :phase, presence: true
end
model = MyModel.new
model.phase = ""
model.invalid? # => true
model.phase = "idle"
model.invalid? # => false
Context can optionally be supplied to define which verifiers to test against (the context is defined on the verifiers using on: option).
def valid?(context : Symbol? = nil) : Bool
#
Runs all the specified verifications and returns true if no errors were added otherwise false.