0

In my Rails 7 app I want to use dry-validation gem to validate incoming JSONs inside create method of my MandateController. Basically based on that JSON request:

  "sdd_setup_request": {
    "return_url": "https://example.com/return",
    "data": {
        (...)
    }
  }
}

I'm trying to validate it using that validator class:

  params do
    required(:sdd_setup_request).schema do
      required(:return_url).value(:string)
      required(:data).hash do
       (...)
      end
    end
  end
end

But still gets an error of sdd_setup_request is missing, when I change it to optional inside the validator class it will be passed. But then when I want to test if the other validations work and remove return_url form the JSON file I should get an error of missing return_url but the validation will pass instead.

Nothing fancy inside of the controller class:

class MandatesController < BaseController
  def create
    result = SddRequestValidator.call(params)
    if result.success?
     (...)
    else
      render_error(result.errors.to_h, status: :bad_request)
    end
  end

What did I missed here?

[Edit]

# Gemfile.lock
dry-validation (1.10.0)

I've got also the initializers/dry_validator.rb:

require 'dry/validation'

Dry::Validation.load_extensions(:monads)

parent class ApplicationService is here:

class ApplicationService < Dry::Validation::Contract
  def self.call(*args)
    new.call(*args)
  end
end
mr_muscle
  • 2,536
  • 18
  • 61
  • You might have to provide a bit more context because I cannot recreate your issue based on this post [Example](https://replit.com/@engineersmnky/CharmingFrugalTruetype#main.rb). Can you tell me what version of `dry-validation` you are using? – engineersmnky Dec 21 '22 at 14:33
  • @engineersmnky question updated. But you're facing the same issue - you've pass the param of `return_url` but the validator shows you an error of: `{:sdd_setup_request=>{:return_url=>["is missing"]}}` – mr_muscle Dec 21 '22 at 14:52
  • No I did not pass it in the second call and thus the error as expected. Everything is working as it should in the code. Updated using your additions. – engineersmnky Dec 21 '22 at 15:01
  • I've no idea, copy/paste all of your code and still getting `sdd_setup_request is missing` for a valid JSON. I'm using postman to make requests to my app – mr_muscle Dec 21 '22 at 15:17
  • What does your postman request look like specifically. Also might want to print out what `params` looks like in the controller. Chances are likely that your request is wrapped in some fashion and the `sdd_setup_request` key is not at the top level. – engineersmnky Dec 21 '22 at 15:19
  • It's a POST request with JSON body, No Auth, Headers with Content-Type: application/json, Accept */* - and that's it, super basic as you see. – mr_muscle Dec 21 '22 at 15:27
  • 1
    @engineersmnky Mother of perl I've got it! If I passed `request.params` instead of `params` to the validator call (`SddRequestValidator.call(request.params)` it will work like a charm. No idea why, I guess it's related with some key formatting. – mr_muscle Dec 22 '22 at 00:11

1 Answers1

0

I am not sure if this is an issue but if you are using the rails params method or if you are using strong parameters then call .to_h on it before passing params to validation.

For example if you have below method

def user_params
  params.require(:user).permit(:name, :email)
end

Pass above params like this: result = UserSchema::CreateParams.call(user_params.to_h)

Sanjay Prajapati
  • 789
  • 1
  • 8
  • 17