class Trailblazer::Macro::Contract::Validate

Public Class Methods

new(name: "default", representer: false, params_path: nil, constant: nil) click to toggle source
# File lib/trailblazer/macro/contract/validate.rb, line 44
def initialize(name: "default", representer: false, params_path: nil, constant: nil)
  @name, @representer, @params_path, @constant = name, representer, params_path, constant
end

Public Instance Methods

call(ctx, **) click to toggle source

Task: Validates contract `:name`.

# File lib/trailblazer/macro/contract/validate.rb, line 49
def call(ctx, **)
  validate!(
    ctx,
    representer: ctx[:"representer.#{@name}.class"] ||= @representer, # FIXME: maybe @representer should use DI.
    params_path: @params_path
  )
end
validate!(options, representer: false, from: :document, params_path: nil) click to toggle source
# File lib/trailblazer/macro/contract/validate.rb, line 57
def validate!(options, representer: false, from: :document, params_path: nil)
  path     = :"contract.#{@name}"
  contract = @constant || options[path]

  # this is for 1.1-style compatibility and should be removed once we have Deserializer in place:
  options[:"result.#{path}"] = result =
    if representer
      # use :document as the body and let the representer deserialize to the contract.
      # this will be simplified once we have Deserializer.
      # translates to contract.("{document: bla}") { MyRepresenter.new(contract).from_json .. }
      contract.(options[from]) { |document| representer.new(contract).parse(document) }
    else
      # let Reform handle the deserialization.
      contract.(options[params_path])
    end

  result.success?
end