Where to place Grape custom validations in Rails 4

androidharry

I have a Rails 4 project which uses Grape for API stuff, I want to do some custom validations as described in the grape documentation. I want to know where should I place my custom validations code (like in lib file) and will I need to include or require something to use it inside my API file?

The documentation tells you to make a class but I am confused about the file structure if I have to write many custom validations.

Tom Kadwill

The last time I used Grape I added custom validations to lib, I then required them into any API classes that used them. Eg:

lib/api/validations/minimum_value.rb

class AlphaNumeric < Grape::Validations::Validator
  def validate_param!(attr_name, params)
    unless params[attr_name] =~ /^[[:alnum:]]+$/
      raise Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: "must consist of alpha-numeric characters"
    end
  end
end

app/api/twitter.rb

class Twitter::API < Grape::API
  require_relative '../../lib/api/validations/minimum_value'

Of course you may want to add lib/api/validations to the auto-loader to prevent having to manually require.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Rails 4 Custom Validations

From Dev

Rails 4: where to place Struct?

From Dev

Rails 4: where to place Struct?

From Dev

Rails validations and custom callbacks ordering

From Dev

Rails: Custom validations using ActiveModel

From Dev

Evaluating Rails 4 validations on conditions

From Dev

Rails 4 Tableless Model with Validations

From Dev

Evaluating Rails 4 validations on conditions

From Dev

Rails, Grape create custom JSON from collection

From Dev

Rails 4 Grape API ActionController::RoutingError

From Dev

Rails running validations on custom model manually

From Dev

Rails 5: Adding conditional custom validations

From Dev

Where to place my environment dependent gem config in Rails 4?

From Dev

Share validations across models with rails 4

From Dev

Unable to add ActiveModel::Validations::HelperMethods in Rails 4?

From Dev

add validations to inherited model in rails 4

From Dev

Share validations across models with rails 4

From Dev

Ruby on Rails 4 testunit skips validations

From Dev

Model validations causing error Rails 4

From Dev

Unable to add ActiveModel::Validations::HelperMethods in Rails 4?

From Dev

Rails 4: Separate model and controller functions with validations

From Dev

Rails tests skipping custom validations or custom validation not working?

From Dev

Where to place generic methods in Rails?

From Dev

Rails 4 and grape - undefined method `call' when splitting API file

From Dev

Uninitialized constant in rails 4 w/ grape occuring in deployment only

From Dev

Rails 4 using validations on DateTime to get shoulda test passing

From Dev

rails 4 - update user object and bypass model validations

From Dev

Correct usage of applying character limit validations in Rails 4

From Dev

repeated issue, could be validations in my models? [Rails 4]

Related Related

  1. 1

    Rails 4 Custom Validations

  2. 2

    Rails 4: where to place Struct?

  3. 3

    Rails 4: where to place Struct?

  4. 4

    Rails validations and custom callbacks ordering

  5. 5

    Rails: Custom validations using ActiveModel

  6. 6

    Evaluating Rails 4 validations on conditions

  7. 7

    Rails 4 Tableless Model with Validations

  8. 8

    Evaluating Rails 4 validations on conditions

  9. 9

    Rails, Grape create custom JSON from collection

  10. 10

    Rails 4 Grape API ActionController::RoutingError

  11. 11

    Rails running validations on custom model manually

  12. 12

    Rails 5: Adding conditional custom validations

  13. 13

    Where to place my environment dependent gem config in Rails 4?

  14. 14

    Share validations across models with rails 4

  15. 15

    Unable to add ActiveModel::Validations::HelperMethods in Rails 4?

  16. 16

    add validations to inherited model in rails 4

  17. 17

    Share validations across models with rails 4

  18. 18

    Ruby on Rails 4 testunit skips validations

  19. 19

    Model validations causing error Rails 4

  20. 20

    Unable to add ActiveModel::Validations::HelperMethods in Rails 4?

  21. 21

    Rails 4: Separate model and controller functions with validations

  22. 22

    Rails tests skipping custom validations or custom validation not working?

  23. 23

    Where to place generic methods in Rails?

  24. 24

    Rails 4 and grape - undefined method `call' when splitting API file

  25. 25

    Uninitialized constant in rails 4 w/ grape occuring in deployment only

  26. 26

    Rails 4 using validations on DateTime to get shoulda test passing

  27. 27

    rails 4 - update user object and bypass model validations

  28. 28

    Correct usage of applying character limit validations in Rails 4

  29. 29

    repeated issue, could be validations in my models? [Rails 4]

HotTag

Archive