Module: HasValidations
- Included in:
- GeoEngineer::Environment, GeoEngineer::Project, GeoEngineer::Resource
- Defined in:
- lib/geoengineer/utils/has_validations.rb
Overview
HasValidations provides methods to enable validations
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- MAX_POLICY_LENGTH =
5120
Class Method Summary collapse
Instance Method Summary collapse
-
#errors ⇒ Object
This method will return a list of errors if not valid, or nil.
-
#validate_at_least_one_present(attributes) ⇒ Object
Validates that at least one of the specified attributes is present.
-
#validate_cidr_block(cidr_block) ⇒ Object
Validates CIDR block format Returns error when argument fails validation.
-
#validate_only_one_present(attributes) ⇒ Object
Validates that ONLY one of the specified attributes is present.
- #validate_policy_length(policy) ⇒ Object
-
#validate_required_attributes(keys) ⇒ Object
Validation Helper Methods.
Class Method Details
.included(base) ⇒ Object
9 10 11 |
# File 'lib/geoengineer/utils/has_validations.rb', line 9 def self.included(base) base.extend(ClassMethods) end |
Instance Method Details
#errors ⇒ Object
This method will return a list of errors if not valid, or nil
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/geoengineer/utils/has_validations.rb', line 31 def errors execute_lifecycle(:before, :validation) if self.respond_to? :execute_lifecycle errs = [] self.class.validations.each do |validation| errs << (validation.is_a?(Proc) ? self.instance_exec(&validation) : self.send(validation)) end # remove nils errs = errs.flatten.select { |x| !x.nil? } errs end |
#validate_at_least_one_present(attributes) ⇒ Object
Validates that at least one of the specified attributes is present
66 67 68 69 70 71 |
# File 'lib/geoengineer/utils/has_validations.rb', line 66 def validate_at_least_one_present(attributes) errs = [] present = attributes.select { |attribute| !self[attribute].nil? }.count errs << "At least one of #{attributes.join(', ')} must be defined" unless present.positive? errs end |
#validate_cidr_block(cidr_block) ⇒ Object
Validates CIDR block format Returns error when argument fails validation
53 54 55 56 57 58 |
# File 'lib/geoengineer/utils/has_validations.rb', line 53 def validate_cidr_block(cidr_block) return "Empty cidr block" if cidr_block.nil? || cidr_block.empty? return if NetAddr::CIDR.create(cidr_block) rescue NetAddr::ValidationError return "Bad cidr block \"#{cidr_block}\" #{for_resource}" end |
#validate_only_one_present(attributes) ⇒ Object
Validates that ONLY one of the specified attributes is present
74 75 76 77 78 79 |
# File 'lib/geoengineer/utils/has_validations.rb', line 74 def validate_only_one_present(attributes) errs = [] present = attributes.select { |attribute| !self[attribute].nil? }.count errs << "Only one of #{attributes.join(', ')} can be defined" unless present == 1 errs end |
#validate_policy_length(policy) ⇒ Object
60 61 62 63 |
# File 'lib/geoengineer/utils/has_validations.rb', line 60 def validate_policy_length(policy) return unless policy.to_s.length >= MAX_POLICY_LENGTH "Policy is too large - must be less than #{MAX_POLICY_LENGTH} characters" end |
#validate_required_attributes(keys) ⇒ Object
Validation Helper Methods
43 44 45 46 47 48 49 |
# File 'lib/geoengineer/utils/has_validations.rb', line 43 def validate_required_attributes(keys) errs = [] keys.each do |key| errs << "#{key} attribute nil for #{self}" if self[key].nil? end errs end |