No commit activity in last 3 years
No release in over 3 years
Instead of Rails translating validation errors automatically to current locale, validation errors are returned as error meta hashes easily embedded in API error responses and translated in the frontend.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.1
~> 1.3.9

Runtime

>= 4.0.0, ~> 4.0
 Project Readme

Gem Version Build Status Coverage Status Code Climate

Rails API validation errors

Untranslated validation errors with all meta data for APIs behind Javascript frontends

Installing

The easiest way to install Rails::API::ValidationErrors is to add it to your Gemfile:

gem "rails_api_validation_errors"

Then, install it on the command line:

$ bundle install

Usage

Include Rails::API::HashValidationErrors in your API's base controller. This makes sure that Rails will not translate error messages, but returns a hash per attribute and error including the error key and meta information.

class API::BaseController < ApplicationController
  include Rails::API::HashValidationErrors
end

To use the new error messages simply return the model's errors in JSON or XML in your controllers:

class API::PeopleController < API::BaseController

  def create
    @person = Person.new(person_params)

    if @person.valid?
      render :json => @person
    else
      render :json => { :errors => @person.errors }
    end
  end

end

This will result in the following JSON response in case of validation errors:

{
  "errors": {
    "name": [
      {
        "message": "blank",
        "meta":{}
      }
    ]
  }
}