4

I am using the Hyperstack.org framework, so working with Opal compiled Ruby code. Hyperstack's integration with Rails creates a representation of some of the Models on the client and I have a question about error validation in the response.

When saving a Model with validators, when one of the validators is triggered I am unable to get the full error message in the promise response.

In this code:

@User.save(validate: true).then do |result|
      if result[:success]
        puts 'successs'
        mutate @open = false
      else
        result[:models].each do |response_model|
          puts "response_model.errors.class = #{response_model.errors.class}" #ActiveModel::Errors
          puts "response_model.errors.full_messages = #{response_model.errors.full_messages}" #nothing puts
        end
      end

The first puts returns #ActiveModel::Errors but I seem unable to use the methods of that model.

I can see the tests for this: https://github.com/hyperstack-org/hyperstack/blob/a09bc601b3ad289c9f75156416ed39dff88059c9/ruby/hyper-model/spec/batch1/misc/errors_spec.rb#L340 so I would expect this to be working and it must be me!

Also, I have noticed that that JSON response to the promise does actually include the error message:

{
  "success" => false, "saved_models" => [
    [227154, "User", {
      "id" => 48,
      "first_name" => "ds",
      "last_name" => nil,
      "email" => nil,
      "image_src" => nil,
      "date_of_birth" => nil,
      "is_admin" => false,
      "is_female" => false,
      "is_enabled" => true,
      "created_at" => "2019-03-23T12:29:05.728Z",
      "updated_at" => "2019-03-23T12:29:05.728Z"
    }, {
      "last_name" => ["can't be blank"]
    }]
  ], "message" => "HyperModel saving records failed!", "models" => [ < User: 0x37752(0x37750)[errors {
    "last_name" => ["can't be blank"]
  }] > ]
}

Any help appreciated!

Mitch VanDuyn
  • 2,838
  • 1
  • 22
  • 29
BarrieH
  • 373
  • 3
  • 11
  • I don't understand the `result` object you are getting as a parameter of the block. Shouldn't it be the `self` object that is being saved? Does it responds to `[]`? – André Guimarães Sakata Mar 23 '19 at 19:15
  • Note `@User` should be `@user` as Ruby is case-sensitive and method and variable names should be `lower_case` only. `@User` invites confusion with `User`, the class. – tadman Mar 23 '19 at 20:16
  • Thank you both for the replies, I should have said, but this is client side Opal compiled code through the Hyperstack framework. The @User is a convention used to demonstrate that this is an immutable param passed into this Component (which in this case is a Model, which is actually mutable - its the exception that proves the rule). – BarrieH Mar 23 '19 at 20:34

1 Answers1

2

Looks like you hit a bug in HyperModel with the full_messages method. AFAIK all the other methods would work fine in your example.

If you look at https://github.com/hyperstack-org/hyperstack/issues/143 there is a work around patch to apply if you really need full_messages

Mitch VanDuyn
  • 2,838
  • 1
  • 22
  • 29