How can I serialize DataMapper::Validations::ValidationErrors to_json in Sinatra?

rest, ruby, ruby-datamapper, sinatra, validation

Solution

So, as I was typing this up, the answer came to me (of course!). I've burned several hours trying to figure this out, and I hope this will save others the pain and frustration I've experienced.

To get the JSON I'm looking for, I just had to create a hash like this:

{ :errors => person.errors.to_h }.to_json

So, now my Sinatra route looks like this:

get '/person' do
    person = Person.new :first_name => 'Dave'
    if person.save
        person.to_json
    else
        { :errors => person.errors.to_h }.to_json
    end
end

Hope this helps others looking to solve this problem.

Problem

I'm developing a RESTful API using Sinatra and DataMapper. When my models fail validation, I want to return JSON to indicate what fields were in error. DataMapper adds an 'errors' attribute to my model of type `DataMapper::Validations::ValidationErrors`. I want to return a JSON representation of this attribute. Here's a single file example (gotta love Ruby/Sinatra/DataMapper!): ``` require 'sinatra' require 'data_mapper' require 'json' class Person include DataMapper::Resource property :id, Serial property :first_name, String, :required => true property :middle_name, String property :last_name, String, :required => true end DataMapper.setup :default, 'sqlite::memory:' DataMapper.auto_migrate! get '/person' do person = Person.new :first_name => 'Dave' if person.save person.to_json else # person.errors - what to do with this? { :errors => [:last_name => ['Last name must not be blank']] }.to_json end end Sinatra::Application.run! ``` In my actual app, I'm handling a POST or PUT, but to make the problem easy to reproduce, I'm using GET so you can use `curl http://example.com:4567/person` or your browser. So, what I have is `person.errors` and the JSON output I'm looking for is like what's produced by the hash: ``` {"errors":{"last_name":["Last name must not be blank"]}} ``` What do I have to do to get the `DataMapper::Validations::ValidationErrors` into the JSON format I want?

Original source