Module: Tram::Validators
- Defined in:
- lib/tram/validators.rb
Constant Summary collapse
- CONDITIONS =
Standard conditions to check
{ equal_to: ->(value, limit) { value == limit }, other_than: ->(value, limit) { value != limit }, less_than: ->(value, limit) { value < limit }, less_than_or_equal_to: ->(value, limit) { value <= limit }, greater_than: ->(value, limit) { value > limit }, greater_than_or_equal_to: ->(value, limit) { value >= limit } }.freeze
Class Method Summary collapse
-
.chained_value(record, chain) ⇒ Object
Gets value of chained attribute.
-
.error_key(source, target, nested_keys: nil, original_keys: nil) ⇒ Object
Provides standard key for error collected by standalone validators.
-
.validators(attr, options, *blacklist) ⇒ Object
Extracts nested validators from options.
Class Method Details
.chained_value(record, chain) ⇒ Object
Gets value of chained attribute
18 19 20 |
# File 'lib/tram/validators.rb', line 18 def chained_value(record, chain) chain.to_s.split(".").inject(record) { |obj, name| obj&.send(name) } end |
.error_key(source, target, nested_keys: nil, original_keys: nil) ⇒ Object
Provides standard key for error collected by standalone validators
23 24 25 26 27 28 29 30 31 |
# File 'lib/tram/validators.rb', line 23 def error_key(source, target, nested_keys: nil, original_keys: nil, **) return source if original_keys return target unless nested_keys source.to_s .split(/\[|\]/) .compact .inject(target) { |obj, key| "#{obj}[#{key}]" } .to_sym end |
.validators(attr, options, *blacklist) ⇒ Object
Extracts nested validators from options
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/tram/validators.rb', line 34 def validators(attr, , *blacklist) .map.with_object({}) do |(key, opts), obj| name = key.to_s next if blacklist.map(&:to_s).include? name next if %w(allow_nil if unless on message).include? name opts = {} unless opts.is_a? Hash obj[key] = find_validator_by_name(name).new(attributes: attr, **opts) end end |