class HaveAPI::ValidatorChain
A chain of validators for one input parameter.
Public Class Methods
new(args)
click to toggle source
# File lib/haveapi/validator_chain.rb, line 4 def initialize(args) @validators = [] @required = false find_validators(args) do |validator| obj = validator.use(args) next unless obj.useful? @required = true if obj.is_a?(Validators::Presence) @validators << obj end end
Public Instance Methods
add_or_replace(name, opt)
click to toggle source
Adds validator that takes option ‘name` with configuration in `opt`. If such validator already exists, it is reconfigured with newly provided `opt`.
If ‘opt` is `nil`, the validator is removed.
# File lib/haveapi/validator_chain.rb, line 22 def add_or_replace(name, opt) args = { name => opt } unless (v_class = find_validator(args)) raise "validator for '#{name}' not found" end exists = @validators.detect { |v| v.is_a?(v_class) } obj = exists if exists if opt.nil? @validators.delete(exists) else exists.reconfigure(name, opt) @validators.delete(exists) unless exists.useful? end else obj = v_class.use(args) @validators << obj if obj.useful? end return unless v_class == Validators::Presence @required = !opt.nil? && obj.useful? end
describe()
click to toggle source
# File lib/haveapi/validator_chain.rb, line 56 def describe ret = {} @validators.each do |v| ret[v.class.name] = v.describe end ret end
required?()
click to toggle source
Returns true if validator Validators::Presence
is used.
# File lib/haveapi/validator_chain.rb, line 52 def required? @required end
validate(value, params)
click to toggle source
Validate ‘value` using all configured validators. It returns either `true` if the value passed all validators or an array of errors.
# File lib/haveapi/validator_chain.rb, line 68 def validate(value, params) ret = [] @validators.each do |validator| next if validator.validate(value, params) ret << (format(validator.message, value:)) end ret.empty? ? true : ret end
Protected Instance Methods
find_validator(args)
click to toggle source
# File lib/haveapi/validator_chain.rb, line 82 def find_validator(args) HaveAPI::Validators.constants.select do |v| validator = HaveAPI::Validators.const_get(v) return validator if validator.use?(args) end nil end
find_validators(args) { |validator| ... }
click to toggle source
# File lib/haveapi/validator_chain.rb, line 92 def find_validators(args) HaveAPI::Validators.constants.select do |v| validator = HaveAPI::Validators.const_get(v) yield(validator) if validator.use?(args) end end