Skip to content

Make SimpleCommand::Errors compatible with ActiveModel::Errors #24

@Hirurg103

Description

@Hirurg103

After update to v0.1.0 from v0.0.9 tests started to fail with

expect(
  get_users(page: '-2')
).to eq(errors: { page: ["must be a positive number"] })
expected: {:errors=>{:page=>["must be a positive number"]}}
     got: {:errors=>{:page=>"must be a positive number"}}

In the controller errors are rendered the following way:

load_users = LoadUsers.(params)
if load_users.success?
  render json: load_users.result
else
  render json: { errors: load_users.errors }
end

The problem is that SimpleCommand::Errors#to_json format differs from ActiveModel::Errors#to_json

# SimpleCommand::Errors
load_users = LoadUsers.new
load_users.errors.add(:page, 'must be a positive number')
load_users.errors.add(:page, 'cannot be blank')
load_users.errors.as_json
=> {"page"=>"cannot be blank"}

# ActiveModel::Errors
user = User.new
user.errors.add(:email, 'is invalid')
user.errors.add(:email, 'cannot be blank')
user.errors.as_json
=> {:email=>["is invalid", "cannot be blank"]}

It would be good to have SimpleCommand::Errors#as_json to be the same format as ActiveModel::Errors#as_json

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions