Class: Upfluence::HTTP::Endpoint::ValidationError

Inherits:
Object
  • Object
show all
Defined in:
lib/upfluence/http/endpoint/validation_error.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(validations) ⇒ ValidationError

Returns a new instance of ValidationError.



5
6
7
# File 'lib/upfluence/http/endpoint/validation_error.rb', line 5

def initialize(validations)
  @validations = validations
end

Class Method Details

.from_model(model) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/upfluence/http/endpoint/validation_error.rb', line 10

def from_model(model)
  validations = model.errors.details.map do |error_field, errors|
    errors.map do |error|
      OpenStruct.new(
        ressource: model.model_name.singular,
        field:     error_field.to_s,
        code:      error[:error].to_s
      )
    end
  end.flatten

  new(validations)
end

Instance Method Details

#to_jsonObject



25
26
27
# File 'lib/upfluence/http/endpoint/validation_error.rb', line 25

def to_json
  { errors: @validations }.to_json
end