lib/crossbeams_responses.rb
# Create a response object with validation errors from more than one source.
# Returns:
#   - success: false.
#   - instance: a Hash of the objec(s) that failed validation.
#   - errors: the error messages.
#   - message: "Validation error".
#
# validation results must either be an array of Dry::Validation::Result or Hash.
# A Hash should have attributes for the object in error and a key `:messages`.
# `:messages` must be in the same format as Dry::Validation::Result.messages.
# i.e `messages: { field1: ['error', 'another error'], field2: ['an err'] }`
#
# @param validation_results [Array(Hash, Dry::Validation::Result)] the validation objects and messages.
# @return [OpenStruct] the response object.
Create a response object with validation errors from more than one source.
Returns:
success: false.
instance: a Hash of the objec(s) that failed validation.
errors: the error messages.
message: "Validation error".

validation results must either be an array of Dry::Validation::Result or Hash.
A Hash should have attributes for the object in error and a key `:messages`.
Create a response object with validation errors from more than one source . Returns : success : false . instance : a Hash of the objec ( s ) that failed validation . errors : the error messages . message : " Validation error " . validation results must either be an array of Dry :: Validation :: Result or Hash . A Hash should have attributes for the object in error and a key ` : messages ` .
def mixed_validation_failed_response(*validation_results)
      errs = {}
      instance = {}
      validation_results.each do |vr|
        errs.merge!(vr.respond_to?(:failure?) ? vr.errors.to_h : vr[:messages])
        instance.merge!(vr.to_h)
      end
      OpenStruct.new(success: false,
                     validation: true,
                     instance: instance.reject { |k, _| k == :messages },
                     errors: errs,
                     message: 'Validation error')
    end
def mixed_validation_failed_response ( * validation_results ) errs = { } instance = { } validation_results . each do | vr | errs . merge! ( vr . respond_to? ( :failure? ) ? vr . errors . to_h : vr [ :messages ] ) instance . merge! ( vr . to_h ) end OpenStruct . new ( success : false , validation : true , instance : instance . reject { | k , _ | k == :messages } , errors : errs , message : 'Validation error' ) end
Create a response object with validation errors from more than one source.
Create a response object with validation errors from more than one source .
# errs.merge!(vr.is_a?(Hash) ? vr[:messages] : vr.messages) # errs.merge!(vr.is_a?(Hash) ? vr[:messages] : vr.errors.to_h)
the response object.
the response object .
OpenStruct
validation_results
the validation objects and messages.
the validation objects and messages .