trailblazer

Can an inherited Trailblazer operation's contract alter validations defined by its superclass?


When a Trailblazer operation is defined by inheritance, it inherits its superclass's contract:

class Create < Trailblazer::Operation
  contract do 
    ... 
  end
  ...
end

class Update < Create
  ...
end

Can an inherited Trailblazer operation's contract alter validations defined by its superclass ?

This question arose because a create operation's contract defined a mandatory property that needed to be optional in the update operation:

validates :foo, presence: true

The initial thought was to somehow reverse this definition in the inherited class but there didn't appear to be a way to do this (it is possible to ignore a property in the subclass (writeable:false - book p61) but there appears to be no way to change its validity criteria).


Solution

  • One solution is to use an external form in each operation's contract. By extracting the form to an external class, the create operation would include and augment it like so:

    contract Form do
      validates :upload, presence: true
    end 
    

    and the update uperation would include it simply as:

    contract Form
    

    Now the validations added in Create don't apply in Update.