cypriss / mutations

Compose your business logic into commands that sanitize and validate input.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

is there a way to prefix errors before merging them ?

MathieuDerelle opened this issue · comments

Im nesting mutations like so :

class ParentMutation < Mutations::Command
  required do
    hash :a do
       model :b, class: Hash
    end
  end
  
  def validate
    validation_outcome = ChildMutation.validate(a[:b])
    return if validation_outcome.success?
    
    merge_errors(validation_outcome.errors)
  end
  
  def execute
    # ... some code
    ChildMutation.run!(a[:b])
    # ... some code
  end
end

class ChildMutation < Mutations::Command
  required do
    string :c
  end
end

(its an extremely simplified version)

when running ParentMutation.run(a: { b: {} }).errors, im getting errors as follow

{
    :c => #<Mutations::ErrorAtom:0x00007fd5a4540ca0 @key=:c, @symbol=:required, @message=nil, @index=nil>
}

but I'd like to prefix the errors with a.b during merge_errors to get something like this instead

{
    'a.b.c' => #<Mutations::ErrorAtom:0x00007fd5a4540ca0 @key=:c, @symbol=:required, @message=nil, @index=nil>
}

or

{
  :a => {
    :b => {
      :c => #<Mutations::ErrorAtom:0x00007fd5a4540ca0 @key=:c, @symbol=:required, @message=nil, @index=nil>
    }
  }
}

Is it possible ?