Cory O'Daniel – These are just words Software development, thoughts, and randomness

30Dec/090

DataMapper and Merb, sharing your errors via the merb display API.

This is a quick little snippet. At Vokle all of our products are built using our core API, which thanks to merb, was a piece of cake. The thing that sucks is sometimes we return objects as JSON via the display API and the "errors" are missing in the event that validation failed. How to fix that?

Throw this somewhere:

1
2
3
4
5
6
7
8
9
10
module DataMapper
  module Validate
 
    class ValidationErrors
      def to_json
        @errors.to_hash.to_json
      end
    end
  end
end

Now in your merb controllers, when you are displaying an object that may have errors:

1
2
3
4
5
6
7
8
class People < Merb::Controller
 
  def create(person)
    #... *SNIP* ...
    display @person, nil, {:methods => [:errors]}
    #... *SNIP* ...
  end
end

And whatever is getting your data back in XML or JSON (yeah or YAML, right) will get the errors on your object as well. Cool.

Yay, users give you invalid data. Congrats.

Post to Twitter Post to Digg Post to Facebook Post to Reddit Post to StumbleUpon

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


No trackbacks yet.