Module: AttrValidator::Validator
- Extended by:
- Concern
- Defined in:
- lib/attr_validator/validator.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
Methods included from Concern
append_features, extended, included
Instance Method Details
#validate(entity) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/attr_validator/validator.rb', line 56 def validate(entity) errors = AttrValidator::ValidationErrors.new self.validations ||= {} self.custom_validations ||= [] self.associated_validations ||= {} self.validations.each do |attr_name, validators| = validate_attr(attr_name, entity, validators) errors.add_all(attr_name, ) unless .empty? end self.associated_validations.each do |association_name, | next if skip_validation?() validator = [:validator].is_a?(Class) ? [:validator].new : self.send([:validator]) children = entity.send(association_name) if children.is_a?(Array) validate_children(association_name, validator, children, errors) elsif children validate_child(association_name, validator, children, errors) end end self.custom_validations.each do |custom_validation| if custom_validation.is_a?(Symbol) self.send(custom_validation, entity, errors) else # it's Proc custom_validation.call(entity, errors) end end errors.to_hash end |
#validate!(entity) ⇒ Object
86 87 88 89 90 91 |
# File 'lib/attr_validator/validator.rb', line 86 def validate!(entity) errors = validate(entity) unless errors.empty? raise AttrValidator::Errors::ValidationError.new("Validation Error", errors) end end |