Module: Ardm::Property::Validation
- Defined in:
- lib/ardm/property/validation.rb
Class Method Summary collapse
-
.rules_for_property(property) ⇒ Object
private
Infer validations for a given property.
Class Method Details
.rules_for_property(property) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Infer validations for a given property. This will only occur if the option :auto_validation is either true or left undefined.
Triggers that generate validator creation
:required => true
Setting the option :required to true causes a Rule::Presence
to be created for the property
:length => 20
Setting the option :length causes a Rule::Length to be created
for the property.
If the value is a Integer the Rule will have :maximum => value.
If the value is a Range the Rule will have :within => value.
:format => :predefined / lambda / Proc
Setting the :format option causes a Rule::Format to be created
for the property
:set => ["foo", "bar", "baz"]
Setting the :set option causes a Rule::Within to be created
for the property
Integer type
Using a Integer type causes a Rule::Numericalness to be created
for the property. The Rule's :integer_only option is set to true
BigDecimal or Float type
Using a Integer type causes a Rule::Numericalness to be created
for the property. The Rule's :integer_only option will be set
to false, and precision/scale will be set to match the Property
Messages
:messages => {..}
Setting :messages hash replaces standard error messages
with custom ones. For instance:
:messages => {:presence => "Field is required",
:format => "Field has invalid format"}
Hash keys are: :presence, :format, :length, :is_unique,
:is_number, :is_primitive
:message => "Some message"
It is just shortcut if only one validation option is set
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/ardm/property/validation.rb', line 62 def self.rules_for_property(property) rule_definitions = [] # all inferred rules should not be skipped when the value is nil # (aside from Rule::Presence/Rule::Absence) opts = { :allow_nil => true } if property..key?(:validates) opts[:context] = property.[:validates] end rule_definitions << infer_presence( property, opts.dup) rule_definitions << infer_length( property, opts.dup) rule_definitions << infer_format( property, opts.dup) rule_definitions << infer_uniqueness(property, opts.dup) rule_definitions << infer_within( property, opts.dup) rule_definitions << infer_type( property, opts.dup) rule_definitions.compact end |