class Attr::Gather::Filters::Contract
Filters
values with a dry-validation contract
Attributes
dry_contract[R]
Public Class Methods
new(dry_contract)
click to toggle source
Creates a new instance of the filter
@param dry_contract
[Dry::Contract]
Calls superclass method
# File lib/attr/gather/filters/contract.rb, line 15 def initialize(dry_contract) validate_dry_contract!(dry_contract) @dry_contract = dry_contract super() end
Public Instance Methods
call(input)
click to toggle source
# File lib/attr/gather/filters/contract.rb, line 21 def call(input) value, filterings = filter_validation_errors input.dup Result.new(value, filterings) end
Private Instance Methods
filter_error_from_input(error, input)
click to toggle source
# File lib/attr/gather/filters/contract.rb, line 39 def filter_error_from_input(error, input) *path, key_to_delete = error.path target = path.empty? ? input : input.dig(*path) target.delete(key_to_delete) end
filter_validation_errors(unvalidated)
click to toggle source
# File lib/attr/gather/filters/contract.rb, line 29 def filter_validation_errors(unvalidated) contract_result = dry_contract.call(unvalidated) errors = contract_result.errors contract_hash = contract_result.to_h errors.each { |err| filter_error_from_input(err, contract_hash) } filterings = transform_errors_to_filtered_attributes(errors) [contract_hash, filterings] end
transform_errors_to_filtered_attributes(errors)
click to toggle source
# File lib/attr/gather/filters/contract.rb, line 45 def transform_errors_to_filtered_attributes(errors) errors.map do |err| Filtering.new(err.path, err.text, err.input) end end
validate_dry_contract!(con)
click to toggle source
# File lib/attr/gather/filters/contract.rb, line 51 def validate_dry_contract!(con) return if con.respond_to?(:call) && con.class.respond_to?(:schema) raise IncompatibleContractError, "contract is not compatible (#{con.inspect})" end