ActiveRecord validation errors - nomtek/JsonErrors GitHub Wiki

First, uncomment or add new label to the custom_codes

  validation_failed: { code: 1020, http_status: 422, validation_errors: :active_record }

Notice validation_errors option.

Uncomment or add the ActiveRecord::RecordInvalid to the error_dictionary:

ActiveRecord::RecordInvalid => :validation_failed,

Then use ActiveModel::Model validations:

# Model
class User < ApplicationRecord
  validates :username, presence: true
end


# Anywhere you create or update the model instance
user = User.create!

Be sure to use error throwing methods, like save! or create! etc.

The errors will be rendered like this:

{
  "code":2001,
  "message":"Authentication needed",
  "payload":{
    "User":[
      { "username":"is required" }
    ]
  }
}