Coder Social home page Coder Social logo

apiable-model-errors's Introduction

APIable Model Errors

ActiveModel provides a great framework for handling validations for models. It easily allows you to add any validation message to any attribute however these arbitary strings aren't very helpful if you want to deliver validation errors to APIs. API developers don't want to see Number is too long (maximum is 5 characters), they would much rather see something they can handle programatically like too_long or blank.

This gem extends the ActiveModel::Errors class to allow for a more API-friendly hash of errors to be returned.

Installation

gem "apiable_model_errors"

Usage

Once you've installed the gems, you can call the to_api_hash method on any ActiveModel::Errors object.

user = User.new(params)
unless user.valid?
  user.errors.to_api_hash
end

Example Output

When using this you'll receive a ruby hash however you can easily convert this into JSON by calling to_json on the hash.

{
  "name":[
    {
      "code":"too_long",
      "message":"is too long (maximum is 4 characters)",
      "options":{
        "count":4
      }
    }
  ]
}

As you can see you'll receive the code, the actual translated message from ActiveModel and any options.

has_message?

In addition to provide the to_api_hash method, we also provide a method called has_message? which allows you to determine if a message exists for a given attribute. This is similar to the added? however does not require that you provide all options to see if a message has been added.

# See if there are any :too_long messages on the name attribute
user.errors.has_message?(:name, :too_long)
# See if there are any :too_long messages on the name attribute
# with the options provided.
user.errors.has_message?(:name, :too_long, :count => 4)
# See if there are any :blank messages on the name attribute
user.errors.has_message?(:name, :blank)

apiable-model-errors's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.