Where will grape look for custom validators?

1,971 views
Skip to first unread message

Sebastian Spier

unread,
Aug 6, 2013, 10:00:33 AM8/6/13
to ruby-...@googlegroups.com
When writing custom validators for grape, like in this documentation example, where does grape expect these validators to be?

In my App I am using grape + Rails.
I want to separate my different grape resources and validators into different folders and files to keep things tidy.

The issue is that when I reference one of my custom validators in a grape resource, grape cannot find the validator.
Error messages are of the form:

... /ruby-1.9.3-p429/gems/grape-0.5.0/lib/grape/validations.rb:203:in `validate': unknown validator: is_status (Grape::Exceptions::UnknownValidator)

So my question is:
When specifying a customer validator for a parameter like is_status: true, where will grape look for these validators?

I am pretty sure it has to do with a combination of grape and the autoload_paths in rails but that is as far as I got.

Thanks for any hints!


Sebastian Spier

unread,
Aug 7, 2013, 7:22:40 AM8/7/13
to ruby-...@googlegroups.com
It turned out that the issue I was facing, was really a combination of how the autoloading in Rails works, and what grape expects.

So, if you write custom validators e.g. subclasses of Grape::Validations::Validator, you need to make sure that Rails loads them before your resources/endpoints etc are defined.
In my case I ended up adding explicit require statements into the resource definitions.

For example:

class API::V1::Beats < Grape::API
   require_relative 'validations/minimum_value'
   require_relative 'validations/some_other_validator'

   resource "beats" do
   ... 

Hope this helps somebody that has the same issue :)

haris rana

unread,
Sep 18, 2014, 1:36:11 AM9/18/14
to ruby-...@googlegroups.com
@sebastian will you please explain your directory structure a bit, I am facing a similar scenario and I want to know what is a suitable directory structure to place my custom validations in.

Thanks
Reply all
Reply to author
Forward
0 new messages