postrank-labs/goliath

Validation Errors for Required Params are not a valid format

markalanevans opened this issue · 4 comments

Using a simple validation rule like

use Goliath::Rack::Validation::RequiredParam, :key => 'cid'

Goliath returns:

[:error, "cid identifier missing"]

That's not valid JSON.

A better response would be

{
   "errors":{
       "cid": "cid identifier missing",
       ...
    }
}

The method being called appears to be here:
https://github.com/postrank-labs/goliath/blob/master/lib/goliath/rack/validator.rb#L12

def validation_error(status_code, msg, headers={})
        headers.delete('Content-Length')
        [status_code, headers, {:error => msg}]
end

Perhaps the response from validation_error() should be sent to a the post_process method and format the response.
https://github.com/postrank-labs/goliath/blob/master/lib/goliath/rack/render.rb#L21

 def post_process(env, status, headers, body)
        ::Rack::RespondTo.env = env

        # the respond_to block is what actually triggers the
        # setting of selected_media_type, so it's required

        respond_to do |format|
          ::Rack::RespondTo.media_types.each do |type|
            format.send(type, Proc.new { body })
          end
        end

        extra = { 'Content-Type' => get_content_type(env),
                  'Vary' => [headers.delete('Vary'), 'Accept'].compact.join(',') }

        [status, extra.merge(headers), body]
      end

Am i missing something?

@markalanevans Are you including the JSON formatter?

@dankozlowski this was an issue that came up in 2015, at the time was there a JSON formatter? Perhaps just post an example here for anyone that finds this issue.

@markalanevans Gotcha. @igrigorik Do we want to leave issues like this open for visibility, or should we close them since they're accessible/searchable closed anyway?

dj2 commented

In order to get JSON formatted output you'd need to include the JSON formatter so this sounds like it's fixed.