Class: Trailblazer::Macro::Contract::Validate
- Inherits:
-
Object
- Object
- Trailblazer::Macro::Contract::Validate
- Defined in:
- lib/trailblazer/macro/contract/validate.rb
Defined Under Namespace
Classes: Extract
Instance Method Summary collapse
-
#call(ctx) ⇒ Object
Task: Validates contract ‘:name`.
-
#initialize(name: "default", representer: false, params_path: nil, constant: nil) ⇒ Validate
constructor
A new instance of Validate.
- #validate!(options, representer: false, from: :document, params_path: nil) ⇒ Object
Constructor Details
#initialize(name: "default", representer: false, params_path: nil, constant: nil) ⇒ Validate
Returns a new instance of Validate.
44 45 46 |
# 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 |
Instance Method Details
#call(ctx) ⇒ Object
Task: Validates contract ‘:name`.
49 50 51 52 53 54 55 |
# 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) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/trailblazer/macro/contract/validate.rb', line 57 def validate!(, representer: false, from: :document, params_path: nil) path = :"contract.#{@name}" contract = @constant || [path] # this is for 1.1-style compatibility and should be removed once we have Deserializer in place: [:"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.([from]) { |document| representer.new(contract).parse(document) } else # let Reform handle the deserialization. contract.([params_path]) end result.success? end |